Repository navigation
WIP/RFC: Node loader resolve fix - #240
gregmagolan wants to merge 3 commits into
Conversation
|
Tests still need to be added for these changes but thought I would put them up for comment at this point. |
|
@alexeagle The change in resolve order was necessary to have angular/angular build from bazel managed node_modules |
|
@gregmagolan if i need to use this commit immediately in my repo how can I request it with |
|
@johnbendi You can use http_archive in your WORKSPACE to use this commit.
I'd be interested to know what issue you're seeing that this commit may solve. Also, this may not be the final solution. Needs some review and consensus from stakeholders. |
|
@gregmagolan I'm getting issue similar to angular/angular#24521 |
|
@gregmagolan Now I'm getting build failure with the following message: Before this I was getting build failure with the following message: |
|
@gregmagolan am I doing something wrong with regard to my previous post? This issue is keeping me from moving forward with Bazel. |
|
@johnbendi The The new error you're seeing after updating your rules_nodejs is because a change is needed to the ngc_wrapped in @angular/bazel rules to support the changes in this PR. I'd recommend using angular 6.0.4 and the latest released rules_nodejs until we have a released solution for angular/angular#24521. |
| "dependencies": { | ||
| "removeNPMAbsolutePaths": "1.0.4" | ||
| "removeNPMAbsolutePaths": "1.0.4", | ||
| "source-map": "0.5.7", |
There was a problem hiding this comment.
why are these added? it feels like a workaround for some issue - I don't see why npm_install cares about source maps. Can we change the place where this is require'd to make it non-fatal instead?
There was a problem hiding this comment.
the packager nodejs_binary uses the npm_install_deps which is why that got added here to prevent the warning if source-map-support is not available for node_loader.js
There was a problem hiding this comment.
nodejs_binary(
name = "packager",
data = [
"packager.js",
"@nodejs//:run_npm.sh.template",
],
entry_point = "build_bazel_rules_nodejs/internal/npm_package/packager.js",
node_modules = "@build_bazel_rules_nodejs_npm_install_deps//:node_modules",
visibility = ["//visibility:public"],
)
There was a problem hiding this comment.
Removed these in favor of using @build_bazel_rules_nodejs_node_loader_deps//:node_modules for the source-map-support require in node_loader.js
| "rollup": "0.60.1", | ||
| "rollup-plugin-node-resolve": "3.3.0", | ||
| "rollup-plugin-sourcemaps": "^0.4.2", | ||
| "source-map": "0.5.7", |
There was a problem hiding this comment.
again, is this really require'd somewhere in our code?
There was a problem hiding this comment.
node_loader.js does a require on source-map-support:
try {
require('source-map-support').install();
} catch (e) {
console.error(`WARNING: source-map-support module not installed.
Stack traces from languages like TypeScript will point to generated .js files.
`);
}
which means that any nodejs_binary needs to have that available or you see the warning above
There was a problem hiding this comment.
Removed these in favor of using @build_bazel_rules_nodejs_node_loader_deps//:node_modules for the source-map-support require in node_loader.js
|
|
||
| // Built-in modules, relative, absolute imports and npm dependencies | ||
| // can be resolved using request | ||
| try { |
There was a problem hiding this comment.
this has lots of conditional logic, maybe we could write a unit test for this file?
| for (var i = 0; i < MODULE_ROOTS.length; i++) { | ||
| var m = MODULE_ROOTS[i]; | ||
| var p = path.replace(m.module_name, m.module_root); | ||
| let match; |
There was a problem hiding this comment.
opt: would be easier to review if the style changes were a separate commit / PR
| # dependency on a package like @bazel/typescript. | ||
| # See discussion: https://github.com/bazelbuild/rules_typescript/issues/13 | ||
| default = Label("@//:node_modules")), | ||
| "node_modules_list": attr.label_list( |
There was a problem hiding this comment.
could you add more docs here explaining why we need this additonal attribute? feels like it's making the surface area more complex for users.
There was a problem hiding this comment.
Sounds good. Most users should not need to use this as a single node_modules works in most cases. The place where it was required for building angular with bazel managed deps was here:
nodejs_binary(
name = "ngc-wrapped",
data = [
":ngc_lib",
"@build_bazel_rules_typescript//internal:worker_protocol.proto",
],
entry_point = "angular/packages/bazel/src/ngc-wrapped/index.js",
# @//:node_modules should be first so that typescript is always picked up
# from @//:node_modules to prevent version skew in the build that can
# lead to failures. This also gives ngc-wrapped hermetic access to a user's
# @angular/compiler-cli package when building angular apps with Bazel.
node_modules_list = [
"@//:node_modules",
"@build_bazel_rules_typescript_tsc_wrapped_deps//:node_modules",
],
visibility = ["//visibility:public"],
)
| escaped = mn.replace("/", r"\/").replace(".", r"\.") | ||
| mapping = r"{module_name: /^%s\b/, module_root: '%s'}" % (escaped, mr) | ||
| module_mappings.append(mapping) | ||
| node_modules_roots = [] |
There was a problem hiding this comment.
does it really work to take two top-level directories and give a unioned view of them to node resolve?
I'm thinking things like
top_level_A/
mod1/
mod2v1/
top_level_B/
mod2v2/
now if you require(mod2) will you get v1 or v2?
There was a problem hiding this comment.
Should write a test to verify it but that should that depends on where you require(mod2) from. If its done from within mod1 then node module resolution will start there (parent.filename in node_loader.js) and it will get v1 since it depends on it. If its from the user code then it will get v2 in this case if it checks top_level_A first and then top_level_B node_modules.
But that changes if the structure looks like this.
top_level_A/
mod1/
mod2v1/
top_level_B/
mod2v2/
Then it will depends on which top level is checked first if the require is from the user's code.
|
Replaced by #253 |
Hermetic resolve in node_loader.js for nodejs_binary & nodejs_test.
Resolves now in the following order:
build-in modules (such as fs, path, etc...)
relative and absolute imports (node can resolve these with
requestdirectly from the parent.filename for relative imports without needing to use the runfiles path)npm dependencies within the parent.filename
node_modules(this is the case when an npm packageN1/node_modules/Aimports another dependent npm package within the samenode_modulessuch asN1/node_modules/BorN1/node_modules/A/node_modules/B). Protection against unintentionally importing from another node_modules further down the path tree such asN2/node_modules/Bis in place. This can happen if the workspace has anode_modulesfolder in it but the workspacenode_modulesis not intended to be in therunfiles(it is not thenode_modulesfilegroup specified for the operation).runfiles import
node_modules filegroup import
module root (unchanged)
This PR also adds an optional
node_modules_listattribute tonodejs_binaryandnodejs_test. This allows for orderednode_modulesin runfiles resolves at (5). This was needed for downstream projects to build angular from source since ngc-wrapped uses the@build_bazel_rules_typescript_tsc_wrapped_deps//:node_modulesfilegroup which does not include@angular/compiler-cli. When building in angular/angular, it has access to@angular/compiler-clivia deps:but when building angular from source downstream this dep does not exist so node_modules_list allows for ngc-wrapped to have both tsc_wrapped_deps and
@//:node_moduleslike so: