forked from bazel-contrib/rules_nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(builtin): add @bazel/hide-bazel-files utility
- Loading branch information
1 parent
43cebe7
commit e7d2fbd
Showing
8 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
load("@build_bazel_rules_nodejs//:defs.bzl", "npm_package") | ||
|
||
npm_package( | ||
name = "npm_package", | ||
srcs = [ | ||
"README.md", | ||
"index.js", | ||
"package.json", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# @bazel/hide-bazel-files | ||
|
||
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`. | ||
|
||
This tool renames all `BUILD` and `BUILD.bazel` files under node_modules to `_BUILD` and `_BUILD.bazel` respectively. | ||
|
||
If you see an error such as | ||
|
||
``` | ||
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'?) | ||
``` | ||
|
||
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: | ||
|
||
``` | ||
"devDependencies": { | ||
"@bazel/hide-bazel-files": "0.0.0-PLACEHOLDER" | ||
}, | ||
"scripts": { | ||
"postinstall": "hide-bazel-files" | ||
} | ||
``` | ||
|
||
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`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env node | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
function findBazelFiles(dir) { | ||
return fs.readdirSync(dir).reduce((files, file) => { | ||
const fullPath = path.posix.join(dir, file); | ||
const isSymbolicLink = fs.lstatSync(fullPath).isSymbolicLink(); | ||
let stat; | ||
try { | ||
stat = fs.statSync(fullPath); | ||
} catch (e) { | ||
if (isSymbolicLink) { | ||
// Filter out broken symbolic links. These cause fs.statSync(fullPath) | ||
// to fail with `ENOENT: no such file or directory ...` | ||
return files; | ||
} | ||
throw e; | ||
} | ||
const isDirectory = stat.isDirectory(); | ||
if (isDirectory && isSymbolicLink) { | ||
// Filter out symbolic links to directories. An issue in yarn versions | ||
// older than 1.12.1 creates symbolic links to folders in the .bin folder | ||
// which leads to Bazel targets that cross package boundaries. | ||
// See https://github.com/bazelbuild/rules_nodejs/issues/428 and | ||
// https://github.com/bazelbuild/rules_nodejs/issues/438. | ||
// This is tested in internal/e2e/fine_grained_symlinks. | ||
return files; | ||
} | ||
if (isDirectory) { | ||
return files.concat(findBazelFiles(fullPath)); | ||
} else { | ||
const fileUc = file.toUpperCase(); | ||
if (fileUc == 'BUILD' || fileUc == 'BUILD.BAZEL') { | ||
return files.concat(fullPath); | ||
} | ||
return files; | ||
} | ||
}, []); | ||
} | ||
|
||
function main() { | ||
// Rename all bazel files found by prefixing them with `_` | ||
for (f of findBazelFiles('node_modules')) { | ||
const d = path.posix.join(path.dirname(f), `_${path.basename(f)}`); | ||
fs.renameSync(f, d); | ||
} | ||
return 0; | ||
} | ||
|
||
module.exports = {main}; | ||
|
||
if (require.main === module) { | ||
process.exitCode = main(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "@bazel/hide-bazel-files", | ||
"bin": { | ||
"hide-bazel-files": "index.js" | ||
}, | ||
"license": "Apache-2.0", | ||
"version": "0.0.0-PLACEHOLDER", | ||
"keywords": [ | ||
"bazel", | ||
"javascript" | ||
], | ||
"description": "A tool that hides all Bazel files in node_modules by prefixing them with an underscore" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,9 @@ | |
"@bazel/buildifier-linux_x64" "0.25.1" | ||
"@bazel/buildifier-win32_x64" "0.25.1" | ||
|
||
"@bazel/hide-bazel-files@file:./packages/hide-bazel-files": | ||
version "0.0.0-PLACEHOLDER" | ||
|
||
"@bazel/[email protected]": | ||
version "0.10.1" | ||
resolved "https://registry.yarnpkg.com/@bazel/ibazel/-/ibazel-0.10.1.tgz#cebcc5cf045fd0e7957e2c491b60094fce28667a" | ||
|