From 0104be7ce8455a4a36a6f420def5252440ea3ab0 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Fri, 21 Jun 2019 02:28:24 -0700 Subject: [PATCH] fix(builtin): always hide bazel files in yarn_install & npm install--- ---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. --- internal/npm_install/generate_build_file.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/internal/npm_install/generate_build_file.js b/internal/npm_install/generate_build_file.js index 14fe2079cd..ebca39f839 100644 --- a/internal/npm_install/generate_build_file.js +++ b/internal/npm_install/generate_build_file.js @@ -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 @@ -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'))));