Skip to content

Commit

Permalink
fix(builtin): fix for symlinked node_modules issue #802
Browse files Browse the repository at this point in the history
This will fix the #802 case of deleting and manually re-creating node_modules by no longer writing any files under node_modules.

A few keys caveats:

1. The downside of this is that the root @npm BUILD file grows huge again as all files under node_modules need to be added to exports_files([...]). If Bazel had a method to export all files without explicitly listing them such as exports_files_all() that would solve this problem.

2. npm package that ships with BUILD files would now cause issues with labels used by yarn_install & npm_install generated targets. With this change, packages such as @bazel/typescript which install bazel workspaces now ship with _BUILD.bazel files. The bazel workspaces installer is already looking for files of this name since the current repo rule renames BUILD files for the same reason.

To solve (2), I propose publishing a tool that could be added as a postinstall step to rename all BUILD files under node_modules and prefix them with _. This should not be a common case as most npm packages don't ship with BUILD files but there are a few that do. For example, rxjs < 6.5.0 had build files for a few releases to support building it from source.
  • Loading branch information
gregmagolan authored and alexeagle committed Jun 18, 2019
1 parent 08b231a commit 43cebe7
Show file tree
Hide file tree
Showing 65 changed files with 10,556 additions and 6,044 deletions.
3 changes: 3 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ npm_package(
"defs.bzl",
"package.bzl",
],
# Don't rename BUILD files as this package is not published to npm
# but is compressed in "release" below and published as a .tar.gz to GitHub
rename_build_files = False,
# Don't replace the default 0.0.0-PLACEHOLDER for this npm_package since
# we are packaging up the packager itself and this replacement will break it
replace_with_version = "",
Expand Down
9 changes: 0 additions & 9 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ filegroup(
"//unidiff/bin:BUILD.bazel",
"//unidiff:BUILD.bazel",
"//zone.js:BUILD.bazel",
"//node_modules/@angular/core:BUILD.bazel",
"//node_modules/@gregmagolan:BUILD.bazel",
"//node_modules/@gregmagolan/test-a:BUILD.bazel",
"//node_modules/@gregmagolan/test-b:BUILD.bazel",
"//node_modules/ajv:BUILD.bazel",
"//node_modules/jasmine:BUILD.bazel",
"//node_modules/rxjs:BUILD.bazel",
"//node_modules/unidiff:BUILD.bazel",
"//node_modules/zone.js:BUILD.bazel",
],
)""",
package_json = "//internal/npm_install/test:package.json",
Expand Down
4 changes: 2 additions & 2 deletions docs/node/node.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h3 id="nodejs_binary_args">Attributes</h3>
```
nodejs_binary(
name = "history-server",
entry_point = "@npm//node_modules/history-server:modules/cli.js",
entry_point = "@npm//:node_modules/history-server/modules/cli.js",
data = ["@npm//history-server"],
)
```
Expand Down Expand Up @@ -591,7 +591,7 @@ <h3 id="nodejs_test_args">Attributes</h3>
```
nodejs_binary(
name = "history-server",
entry_point = "@npm//node_modules/history-server:modules/cli.js",
entry_point = "@npm//:node_modules/history-server/modules/cli.js",
data = ["@npm//history-server"],
)
```
Expand Down
2 changes: 1 addition & 1 deletion e2e/bazel_bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ else
fi
# --- end runfiles.bash initialization ---

readonly OUT=$($(rlocation "npm/node_modules/testy/testy__bin"))
readonly OUT=$($(rlocation "npm/testy/bin/testy"))

if [ "$OUT" != "Hello world" ]; then
echo "Expected output 'Hello world' but was $OUT"
Expand Down
2 changes: 1 addition & 1 deletion e2e/karma_typescript/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ filegroup(
name = "rxjs_umd_modules",
srcs = [
# do not sort
"@npm//node_modules/rxjs:bundles/rxjs.umd.js",
"@npm//:node_modules/rxjs/bundles/rxjs.umd.js",
":rxjs_shims.js",
],
)
Expand Down
2 changes: 1 addition & 1 deletion e2e/karma_typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"@bazel/typescript": "file:../../dist/npm_bazel_typescript",
"@types/jasmine": "^3.3.12",
"jasmine": "^3.4.0",
"rxjs": "^6.4.0",
"rxjs": "6.5.0",
"typescript": "2.9.2"
},
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion e2e/ts_devserver/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ts_devserver(
entry_module = "e2e_ts_devserver/app",
port = 8080,
scripts = [
"@npm//node_modules/date-fns:date-fns.umd.js",
"@npm//date-fns:date-fns.umd.js",
],
# We'll collect all the devmode JS sources from these TypeScript libraries
deps = [":app"],
Expand Down
6 changes: 3 additions & 3 deletions e2e/ts_library/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ yarn_install(
filegroup(
name = "node_modules_filegroup",
srcs = [
"//node_modules/@types/hammerjs:hammerjs__files",
"//node_modules/@types/jasmine:jasmine__files",
"//node_modules/typescript:typescript__files",
"//@types/hammerjs:hammerjs__files",
"//@types/jasmine:jasmine__files",
"//typescript:typescript__files",
],
)""",
package_json = "//:package.json",
Expand Down
2 changes: 1 addition & 1 deletion e2e/ts_library/googmodule/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ nodejs_binary(
"@npm//@bazel/typescript",
"@npm//tsickle",
],
entry_point = "@npm//node_modules/@bazel/typescript:internal/tsc_wrapped/tsc_wrapped.js",
entry_point = "@npm//:node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js",
install_source_map_support = False,
)

Expand Down
4 changes: 2 additions & 2 deletions examples/protocol_buffers/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ rollup_bundle(
genrule(
name = "protobufjs",
srcs = [
"@build_bazel_rules_typescript_protobufs_compiletime_deps//node_modules/protobufjs:dist/minimal/protobuf.min.js",
"@build_bazel_rules_typescript_protobufs_compiletime_deps//node_modules/long:dist/long.js",
"@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/protobufjs/dist/minimal/protobuf.min.js",
"@build_bazel_rules_typescript_protobufs_compiletime_deps//:node_modules/long/dist/long.js",
],
outs = [
"protobuf.min.js",
Expand Down
12 changes: 6 additions & 6 deletions internal/e2e/fine_grained_deps/npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/e2e/fine_grained_deps/npm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"chokidar": "2.0.4",
"http-server": "github:alexeagle/http-server#97205e945b69091606ed83aa0c8489e9ce65d282",
"klaw": "1.3.1",
"rxjs": "6.3.3"
"rxjs": "6.5.0"
}
}
2 changes: 1 addition & 1 deletion internal/e2e/fine_grained_deps/yarn/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
"chokidar": "2.0.4",
"http-server": "github:alexeagle/http-server#97205e945b69091606ed83aa0c8489e9ce65d282",
"klaw": "1.3.1",
"rxjs": "6.3.3"
"rxjs": "6.5.0"
}
}
8 changes: 4 additions & 4 deletions internal/e2e/fine_grained_deps/yarn/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1121,10 +1121,10 @@ rimraf@^2.6.1:
dependencies:
glob "^7.0.5"

rxjs@6.3.3:
version "6.3.3"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.3.3.tgz#3c6a7fa420e844a81390fb1158a9ec614f4bad55"
integrity sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==
rxjs@6.5.0:
version "6.5.0"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.0.tgz#1e4bc933f14a0c5c2ce7bb71ddd4244d42f4b04b"
integrity sha512-FOTfP3LK5VN3dDtr+wjFAeKVe5nTPPTC2+NUFJ8kyuO+YbIl/aME0eQDiX2MCVgnhKyuUYaEjgZEx8iL/4AV6A==
dependencies:
tslib "^1.9.0"

Expand Down
4 changes: 2 additions & 2 deletions internal/e2e/packages/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ VARIANTS = [
jasmine_node_test(
name = "npm_determinism_test",
srcs = ["npm_determinism.spec.js"],
data = ["@internal_e2e_packages_npm_install//node_modules/jsesc:package.json"],
data = ["@internal_e2e_packages_npm_install//:node_modules/jsesc/package.json"],
node_modules = "@internal_e2e_packages_npm_install_duplicate_for_determinism_testing//:node_modules",
)

jasmine_node_test(
name = "yarn_determinism_test",
srcs = ["yarn_determinism.spec.js"],
data = ["@internal_e2e_packages_yarn_install//node_modules/jsesc:package.json"],
data = ["@internal_e2e_packages_yarn_install//:node_modules/jsesc/package.json"],
node_modules = "@internal_e2e_packages_yarn_install_duplicate_for_determinism_testing//:node_modules",
)
2 changes: 1 addition & 1 deletion internal/history-server/history_server.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def history_server(templated_args = [], **kwargs):

nodejs_binary_macro(
node_modules = "@history-server_runtime_deps//:node_modules",
entry_point = "@history-server_runtime_deps//node_modules/history-server:modules/cli.js",
entry_point = "@history-server_runtime_deps//:node_modules/history-server/modules/cli.js",
install_source_map_support = False,
templated_args = templated_args,
**kwargs
Expand Down
2 changes: 1 addition & 1 deletion internal/http-server/http_server.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def http_server(templated_args = [], **kwargs):

nodejs_binary_macro(
node_modules = "@http-server_runtime_deps//:node_modules",
entry_point = "@http-server_runtime_deps//node_modules/http-server:bin/http-server",
entry_point = "@http-server_runtime_deps//:node_modules/http-server/bin/http-server",
install_source_map_support = False,
templated_args = templated_args,
**kwargs
Expand Down
21 changes: 12 additions & 9 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ def _compute_node_modules_root(ctx):
The node_modules root as a string
"""
node_modules_root = None
if ctx.files.node_modules:
# ctx.files.node_modules is not an empty list
workspace = ctx.attr.node_modules.label.workspace_root.split("/")[1] if ctx.attr.node_modules.label.workspace_root else ctx.workspace_name
node_modules_root = "/".join([f for f in [
workspace,
_trim_package_node_modules(ctx.attr.node_modules.label.package),
"node_modules",
] if f])
if ctx.attr.node_modules:
if NodeModuleSources in ctx.attr.node_modules:
node_modules_root = "/".join([ctx.attr.node_modules[NodeModuleSources].workspace, "node_modules"])
elif ctx.files.node_modules:
# ctx.files.node_modules is not an empty list
workspace = ctx.attr.node_modules.label.workspace_root.split("/")[1] if ctx.attr.node_modules.label.workspace_root else ctx.workspace_name
node_modules_root = "/".join([f for f in [
workspace,
_trim_package_node_modules(ctx.attr.node_modules.label.package),
"node_modules",
] if f])
for d in ctx.attr.data:
if NodeModuleSources in d:
possible_root = "/".join([d[NodeModuleSources].workspace, "node_modules"])
Expand Down Expand Up @@ -275,7 +278,7 @@ _NODEJS_EXECUTABLE_ATTRS = {
```
nodejs_binary(
name = "history-server",
entry_point = "@npm//node_modules/history-server:modules/cli.js",
entry_point = "@npm//:node_modules/history-server/modules/cli.js",
data = ["@npm//history-server"],
)
```
Expand Down
Loading

0 comments on commit 43cebe7

Please sign in to comment.