Skip to content

Commit 1a8175d

Browse files
gregmagolanalexeagle
authored andcommitted
feat: implicit hide-bazel-files
@bazel/hide-bazel-files updated to automatically run its postinstall step and @bazel/bazel now has a dependency on the tool so that users get the fix automatically and when a better fix is provided by Bazel it can be removed seamlessly.
1 parent 28d7254 commit 1a8175d

10 files changed

Lines changed: 23 additions & 47 deletions

File tree

README.md

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -271,31 +271,6 @@ As of Bazel 0.26 this feature is still experimental, so also add this line to th
271271
common --experimental_allow_incremental_repository_updates
272272
```
273273

274-
#### @bazel/hide-bazel-files
275-
276-
We recommend adding the `@bazel/hide-bazel-files` utility as a postinstall step to any `package.json` files that
277-
are being used by `yarn_install` or `npm_install`. This utility hides Bazel files that may be shipped with npm
278-
packages you are using by renaming them with a `_` prefix.
279-
280-
Bazel files such as `BUILD` or `BUILD.bazel` in node_modules will cause build failures when using Bazel-managed dependencies. If you see an error such as
281-
282-
```
283-
ERROR: /private/var/tmp/_bazel_greg/37b273501bbecefcf5ce4f3afcd7c47a/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/rxjs/src/AsyncSubject.ts' crosses boundary of subpackage '@npm//node_modules/rxjs/src' (perhaps you meant to put the colon here: '@npm//node_modules/rxjs/src:AsyncSubject.ts'?)
284-
```
285-
286-
then chances are there is an npm package in your dependencies that contains a `BUILD` file. To resolve this, add `@bazel/hide-bazel-files` to your `devDependencies` and `hide-bazel-files` to your `postinstall` script like so:
287-
288-
```
289-
"devDependencies": {
290-
"@bazel/hide-bazel-files": "0.0.0-PLACEHOLDER"
291-
},
292-
"scripts": {
293-
"postinstall": "hide-bazel-files"
294-
}
295-
```
296-
297-
Note: The commonly used npm package rxjs contains `BUILD` files from version 5.5.5 to 6.4.0 inclusive. These have now been removed in version 6.5.0. If you are using an rxjs version in that range and that is the only npm package in your dependencies that contains `BUILD` files then you can try upgrading to rxjs 6.4.0 instead of using `hide-bazel-files`.
298-
299274
#### yarn_install vs. npm_install
300275

301276
`yarn_install` is the preferred rule for setting up Bazel-managed dependencies for a number of reasons:

e2e/symlinked_node_modules_yarn/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@bazel/hide-bazel-files": "file:../../dist/npm_bazel_hide-bazel-files"
88
},
99
"scripts": {
10-
"postinstall": "hide-bazel-files",
1110
"test": "bazel test ... && rm -rf node_modules && bazel test ... && rm -rf node_modules && yarn install && bazel test ..."
1211
}
1312
}

internal/npm_install/generate_build_file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ function hideBazelFiles(pkg) {
134134
if (ERROR_ON_BAZEL_FILES) {
135135
console.error(`npm package '${pkg._dir}' from @${WORKSPACE} ${RULE_TYPE} rule
136136
has a Bazel BUILD file '${file}'. Use the @bazel/hide-bazel-files utility to hide these files.
137-
See https://github.com/gregmagolan/rules_nodejs/blob/master/packages/hide-bazel-files/README.md
137+
See https://github.com/bazelbuild/rules_nodejs/blob/master/packages/hide-bazel-files/README.md
138138
for installation instructions.`);
139139
process.exit(1);
140140
} else {

internal/npm_install/npm_install.bzl

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,9 @@ COMMON_ATTRIBUTES = dict(dict(), **{
4848
4949
See https://github.com/bazelbuild/rules_nodejs/issues/802 for more details.
5050
51-
The recommended solution to the above is to add `@bazel/hide-bazel-files` to your `devDependencies`
52-
and `hide-bazel-files` to your `postinstall` script like so:
53-
54-
```
55-
"devDependencies": {
56-
"@bazel/hide-bazel-files": "latest"
57-
},
58-
"scripts": {
59-
"postinstall": "hide-bazel-files"
60-
}
61-
```
51+
The recommended solution is to use the @bazel/hide-bazel-files utility to hide these files.
52+
See https://github.com/bazelbuild/rules_nodejs/blob/master/packages/hide-bazel-files/README.md
53+
for installation instructions.
6254
6355
The alternate solution is to set `always_hide_bazel_files` to True which tell
6456
this rule to hide Bazel files even when `symlink_node_modules` is True. This means

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
"zone.js": "0.8.29"
4646
},
4747
"scripts": {
48-
"postinstall": "hide-bazel-files",
4948
"build_packages_all": "./scripts/build_packages_all.sh",
5049
"build_packages": "./scripts/build_packages.sh",
5150
"build_release": "./scripts/build_release.sh",

packages/bazel/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"bugs": {
1616
"url": "https://github.com/bazelbuild/bazel/issues"
1717
},
18+
"//": "@bazel/hide-bazel-files is required to fix https://github.com/bazelbuild/rules_nodejs/issues/802. See https://github.com/bazelbuild/rules_nodejs/blob/master/packages/hide-bazel-files/README.md for more information.",
19+
"dependencies": {
20+
"@bazel/hide-bazel-files": "latest"
21+
},
1822
"optionalDependencies": {
1923
"@bazel/bazel-linux_x64": "0.26.0-rc10",
2024
"@bazel/bazel-darwin_x64": "0.26.0-rc10",

packages/create/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ function main(argv, error = console.error, log = console.log) {
9999
'@bazel/bazel': 'latest',
100100
'@bazel/ibazel': 'latest',
101101
'@bazel/buildifier': 'latest',
102-
'@bazel/hide-bazel-files': 'latest',
103102
};
104103
let rootBuildContent = '# Add rules here to build your software\n' +
105104
'# See https://docs.bazel.build/versions/master/build-ref.html#BUILD_files\n\n';
@@ -183,7 +182,6 @@ ts_setup_workspace()`;
183182
private: true,
184183
devDependencies,
185184
scripts: {
186-
'postinstall': 'hide-bazel-files',
187185
'build': 'bazel build //...',
188186
'test': 'bazel test //...',
189187
}

packages/hide-bazel-files/README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,12 @@ If you see an error such as
1010
ERROR: /private/var/tmp/_bazel_greg/37b273501bbecefcf5ce4f3afcd7c47a/external/npm/BUILD.bazel:9:1: Label '@npm//:node_modules/rxjs/src/AsyncSubject.ts' crosses boundary of subpackage '@npm//node_modules/rxjs/src' (perhaps you meant to put the colon here: '@npm//node_modules/rxjs/src:AsyncSubject.ts'?)
1111
```
1212

13-
then chances are there is an npm package in your dependencies that contains a `BUILD` file. To resolve this, add `@bazel/hide-bazel-files` to your `devDependencies` and `hide-bazel-files` to your `postinstall` script like so:
13+
then chances are there is an npm package in your dependencies that contains a `BUILD` file. To resolve this, add `@bazel/hide-bazel-files` to your `devDependencies`. The `@bazel/hide-bazel-files` npm package automatically runs a postinstall step that renames all Bazel build files in your node_modules.
1414

1515
```
1616
"devDependencies": {
1717
"@bazel/hide-bazel-files": "0.0.0-PLACEHOLDER"
1818
},
19-
"scripts": {
20-
"postinstall": "hide-bazel-files"
21-
}
2219
```
2320

2421
Note: The commonly used npm package rxjs contains `BUILD` files from version 5.5.5 to 6.4.0 inclusive. These have now been removed in version 6.5.0. If you are using an rxjs version in that range and that is the only npm package in your dependencies that contains `BUILD` files then you can try upgrading to rxjs 6.4.0 instead of using `hide-bazel-files`.

packages/hide-bazel-files/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const fs = require('fs');
44
const path = require('path');
55

66
function findBazelFiles(dir) {
7+
if (!fs.existsSync(dir)) {
8+
// Fail-safe
9+
return [];
10+
}
711
return fs.readdirSync(dir).reduce((files, file) => {
812
const fullPath = path.posix.join(dir, file);
913
const isSymbolicLink = fs.lstatSync(fullPath).isSymbolicLink();
@@ -42,7 +46,12 @@ function findBazelFiles(dir) {
4246

4347
function main() {
4448
// Rename all bazel files found by prefixing them with `_`
45-
for (f of findBazelFiles('node_modules')) {
49+
const cwd = process.cwd();
50+
const rootNodeModules =
51+
/\/node_modules\/@bazel\/hide-bazel-files$/.test(cwd.replace(/\\/g, '/')) ?
52+
path.dirname(path.dirname(cwd)) :
53+
path.posix.join(cwd, 'node_modules');
54+
for (f of findBazelFiles(rootNodeModules)) {
4655
const d = path.posix.join(path.dirname(f), `_${path.basename(f)}`);
4756
fs.renameSync(f, d);
4857
}

packages/hide-bazel-files/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,8 @@
99
"bazel",
1010
"javascript"
1111
],
12-
"description": "A tool that hides all Bazel files in node_modules by prefixing them with an underscore"
12+
"description": "A tool that hides all Bazel files in node_modules by prefixing them with an underscore",
13+
"scripts": {
14+
"postinstall": "node ./index.js"
15+
}
1316
}

0 commit comments

Comments
 (0)