Skip to content

Commit

Permalink
fix(builtin): always hide bazel files in yarn_install & npm install---
Browse files Browse the repository at this point in the history
---if @bazel/hide-bazel-files is detected.

Fixes a @bazel/hide-bazel-files bug when adding an npm package with a bazel BUILD files after the initial node_modules install. In this case @bazel/hide-bazel-files postinstall does not run so it does not hide the newly added BUILD files. The repo rules, however, will run before the next build as the package.json & lock files will have changed so we can hide the bazel BUILD files at that point and avoid any build failures.
  • Loading branch information
gregmagolan authored and alexeagle committed Jun 21, 2019
1 parent 2a63ed6 commit 0104be7
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/npm_install/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,21 @@ function flattenDependencies(pkgs) {
* Handles Bazel files in npm distributions.
*/
function hideBazelFiles(pkg) {
const hasHideBazelFiles = isDirectory('node_modules/@bazel/hide-bazel-files');
pkg._files = pkg._files.map(file => {
const basename = path.basename(file);
const basenameUc = basename.toUpperCase();
if (basenameUc === 'BUILD' || basenameUc === 'BUILD.BAZEL') {
if (ERROR_ON_BAZEL_FILES) {
// If bazel files are detected and there is no @bazel/hide-bazel-files npm
// package then error out and suggest adding the package. It is possible to
// have bazel BUILD files with the package installed as it's postinstall
// step, which hides bazel BUILD files, only runs when the @bazel/hide-bazel-files
// is installed and not when new packages are added (via `yarn add`
// for example) after the initial install. In this case, however, the repo rule
// will re-run as the package.json && lock file has changed so we just
// hide the added BUILD files during the repo rule run here since @bazel/hide-bazel-files
// was not run.
if (!hasHideBazelFiles && ERROR_ON_BAZEL_FILES) {
console.error(`npm package '${pkg._dir}' from @${WORKSPACE} ${RULE_TYPE} rule
has a Bazel BUILD file '${file}'. Use the @bazel/hide-bazel-files utility to hide these files.
See https://github.com/bazelbuild/rules_nodejs/blob/master/packages/hide-bazel-files/README.md
Expand Down Expand Up @@ -446,6 +456,7 @@ function findPackages(p = 'node_modules') {
.filter(f => !f.startsWith('.'))
.map(f => path.posix.join(p, f))
.filter(f => isDirectory(f));

packages.forEach(
f => pkgs.push(parsePackage(f), ...findPackages(path.posix.join(f, 'node_modules'))));

Expand Down

0 comments on commit 0104be7

Please sign in to comment.