Skip to content

Commit e7d2fbd

Browse files
gregmagolanalexeagle
authored andcommitted
fix(builtin): add @bazel/hide-bazel-files utility
1 parent 43cebe7 commit e7d2fbd

8 files changed

Lines changed: 136 additions & 0 deletions

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,31 @@ 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+
274299
#### yarn_install vs. npm_install
275300

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

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"devDependencies": {
1212
"@bazel/bazel": "0.27.0",
1313
"@bazel/buildifier": "^0.25.1",
14+
"@bazel/hide-bazel-files": "file:./packages/hide-bazel-files",
1415
"@bazel/ibazel": "0.10.1",
1516
"@commitlint/cli": "^8.0.0",
1617
"@commitlint/config-conventional": "^8.0.0",
@@ -44,6 +45,7 @@
4445
"zone.js": "0.8.29"
4546
},
4647
"scripts": {
48+
"postinstall": "hide-bazel-files",
4749
"build_packages_all": "./scripts/build_packages_all.sh",
4850
"build_packages": "./scripts/build_packages.sh",
4951
"build_release": "./scripts/build_release.sh",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package")
2+
3+
npm_package(
4+
name = "npm_package",
5+
srcs = [
6+
"README.md",
7+
"index.js",
8+
"package.json",
9+
],
10+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# @bazel/hide-bazel-files
2+
3+
A tool to hide Bazel files that may be shipped with some npm packages. Packages with these files cause build failures when used with `npm_install` or `yarn_install`.
4+
5+
This tool renames all `BUILD` and `BUILD.bazel` files under node_modules to `_BUILD` and `_BUILD.bazel` respectively.
6+
7+
If you see an error such as
8+
9+
```
10+
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'?)
11+
```
12+
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:
14+
15+
```
16+
"devDependencies": {
17+
"@bazel/hide-bazel-files": "0.0.0-PLACEHOLDER"
18+
},
19+
"scripts": {
20+
"postinstall": "hide-bazel-files"
21+
}
22+
```
23+
24+
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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
6+
function findBazelFiles(dir) {
7+
return fs.readdirSync(dir).reduce((files, file) => {
8+
const fullPath = path.posix.join(dir, file);
9+
const isSymbolicLink = fs.lstatSync(fullPath).isSymbolicLink();
10+
let stat;
11+
try {
12+
stat = fs.statSync(fullPath);
13+
} catch (e) {
14+
if (isSymbolicLink) {
15+
// Filter out broken symbolic links. These cause fs.statSync(fullPath)
16+
// to fail with `ENOENT: no such file or directory ...`
17+
return files;
18+
}
19+
throw e;
20+
}
21+
const isDirectory = stat.isDirectory();
22+
if (isDirectory && isSymbolicLink) {
23+
// Filter out symbolic links to directories. An issue in yarn versions
24+
// older than 1.12.1 creates symbolic links to folders in the .bin folder
25+
// which leads to Bazel targets that cross package boundaries.
26+
// See https://github.com/bazelbuild/rules_nodejs/issues/428 and
27+
// https://github.com/bazelbuild/rules_nodejs/issues/438.
28+
// This is tested in internal/e2e/fine_grained_symlinks.
29+
return files;
30+
}
31+
if (isDirectory) {
32+
return files.concat(findBazelFiles(fullPath));
33+
} else {
34+
const fileUc = file.toUpperCase();
35+
if (fileUc == 'BUILD' || fileUc == 'BUILD.BAZEL') {
36+
return files.concat(fullPath);
37+
}
38+
return files;
39+
}
40+
}, []);
41+
}
42+
43+
function main() {
44+
// Rename all bazel files found by prefixing them with `_`
45+
for (f of findBazelFiles('node_modules')) {
46+
const d = path.posix.join(path.dirname(f), `_${path.basename(f)}`);
47+
fs.renameSync(f, d);
48+
}
49+
return 0;
50+
}
51+
52+
module.exports = {main};
53+
54+
if (require.main === module) {
55+
process.exitCode = main();
56+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "@bazel/hide-bazel-files",
3+
"bin": {
4+
"hide-bazel-files": "index.js"
5+
},
6+
"license": "Apache-2.0",
7+
"version": "0.0.0-PLACEHOLDER",
8+
"keywords": [
9+
"bazel",
10+
"javascript"
11+
],
12+
"description": "A tool that hides all Bazel files in node_modules by prefixing them with an underscore"
13+
}

scripts/publish_release.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,6 @@ for pkg in ${PACKAGES[@]} ; do (
2929

3030
# packages/create is not a nested workspace and has no deps
3131
echo_and_run node_modules/.bin/bazel --output_base=$TMP run --workspace_status_command=scripts/current_version.sh //packages/create:npm_package.${NPM_COMMAND}
32+
33+
# packages/hide-bazel-files is not a nested workspace and has no deps
34+
echo_and_run node_modules/.bin/bazel --output_base=$TMP run --workspace_status_command=scripts/current_version.sh //packages/hide-bazel-files:npm_package.${NPM_COMMAND}

yarn.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
"@bazel/buildifier-linux_x64" "0.25.1"
5151
"@bazel/buildifier-win32_x64" "0.25.1"
5252

53+
"@bazel/hide-bazel-files@file:./packages/hide-bazel-files":
54+
version "0.0.0-PLACEHOLDER"
55+
5356
"@bazel/ibazel@0.10.1":
5457
version "0.10.1"
5558
resolved "https://registry.yarnpkg.com/@bazel/ibazel/-/ibazel-0.10.1.tgz#cebcc5cf045fd0e7957e2c491b60094fce28667a"

0 commit comments

Comments
 (0)