Skip to content

Commit 0104be7

Browse files
gregmagolanalexeagle
authored andcommitted
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.
1 parent 2a63ed6 commit 0104be7

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

internal/npm_install/generate_build_file.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,21 @@ function flattenDependencies(pkgs) {
127127
* Handles Bazel files in npm distributions.
128128
*/
129129
function hideBazelFiles(pkg) {
130+
const hasHideBazelFiles = isDirectory('node_modules/@bazel/hide-bazel-files');
130131
pkg._files = pkg._files.map(file => {
131132
const basename = path.basename(file);
132133
const basenameUc = basename.toUpperCase();
133134
if (basenameUc === 'BUILD' || basenameUc === 'BUILD.BAZEL') {
134-
if (ERROR_ON_BAZEL_FILES) {
135+
// If bazel files are detected and there is no @bazel/hide-bazel-files npm
136+
// package then error out and suggest adding the package. It is possible to
137+
// have bazel BUILD files with the package installed as it's postinstall
138+
// step, which hides bazel BUILD files, only runs when the @bazel/hide-bazel-files
139+
// is installed and not when new packages are added (via `yarn add`
140+
// for example) after the initial install. In this case, however, the repo rule
141+
// will re-run as the package.json && lock file has changed so we just
142+
// hide the added BUILD files during the repo rule run here since @bazel/hide-bazel-files
143+
// was not run.
144+
if (!hasHideBazelFiles && ERROR_ON_BAZEL_FILES) {
135145
console.error(`npm package '${pkg._dir}' from @${WORKSPACE} ${RULE_TYPE} rule
136146
has a Bazel BUILD file '${file}'. Use the @bazel/hide-bazel-files utility to hide these files.
137147
See https://github.com/bazelbuild/rules_nodejs/blob/master/packages/hide-bazel-files/README.md
@@ -446,6 +456,7 @@ function findPackages(p = 'node_modules') {
446456
.filter(f => !f.startsWith('.'))
447457
.map(f => path.posix.join(p, f))
448458
.filter(f => isDirectory(f));
459+
449460
packages.forEach(
450461
f => pkgs.push(parsePackage(f), ...findPackages(path.posix.join(f, 'node_modules'))));
451462

0 commit comments

Comments
 (0)