Skip to content

WIP/RFC: Node loader resolve fix - #240

Closed
gregmagolan wants to merge 3 commits into
bazel-contrib:masterfrom
gregmagolan:node-loader-resolve-fix
Closed

gregmagolan wants to merge 3 commits into
bazel-contrib:masterfrom
gregmagolan:node-loader-resolve-fix

Conversation

@gregmagolan

@gregmagolan gregmagolan commented Jun 25, 2018 •

Copy link
Copy Markdown
Collaborator

Hermetic resolve in node_loader.js for nodejs_binary & nodejs_test.

Resolves now in the following order:

  1. build-in modules (such as fs, path, etc...)

  2. relative and absolute imports (node can resolve these with request directly from the parent.filename for relative imports without needing to use the runfiles path)

  3. npm dependencies within the parent.filename node_modules (this is the case when an npm package N1/node_modules/A imports another dependent npm package within the same node_modules such as N1/node_modules/B or N1/node_modules/A/node_modules/B). Protection against unintentionally importing from another node_modules further down the path tree such as N2/node_modules/B is in place. This can happen if the workspace has a node_modules folder in it but the workspace node_modules is not intended to be in the runfiles (it is not the node_modules filegroup specified for the operation).

  4. runfiles import

  5. node_modules filegroup import

  6. module root (unchanged)

This PR also adds an optional node_modules_list attribute to nodejs_binary and nodejs_test. This allows for ordered node_modules in 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_modules filegroup which does not include @angular/compiler-cli. When building in angular/angular, it has access to @angular/compiler-cli via deps:

ts_library(
    name = "ngc_lib",
    srcs = [
        "extract_i18n.ts",
        "index.ts",
    ],
    module_name = "@angular/bazel",
    tsconfig = ":tsconfig.json",
    visibility = ["//packages/bazel/test/ngc-wrapped:__subpackages__"],
    deps = [
        # BEGIN-INTERNAL
        # Only needed when compiling within the Angular repo.
        # Users will get this dependency from node_modules.
        "//packages/compiler-cli",
        # END-INTERNAL
        "@build_bazel_rules_typescript//internal:tsc_wrapped",
    ],
)

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_modules like so:

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_list = ["@build_bazel_rules_typescript_tsc_wrapped_deps//:node_modules", "@//:node_modules"],
    visibility = ["//visibility:public"],
)

@gregmagolan
gregmagolan requested a review from alexeagle June 25, 2018 16:48
@gregmagolan

Copy link
Copy Markdown
Collaborator Author

Tests still need to be added for these changes but thought I would put them up for comment at this point.

@gregmagolan

Copy link
Copy Markdown
Collaborator Author

@alexeagle The change in resolve order was necessary to have angular/angular build from bazel managed node_modules @angular_deps//:node_modules. I verified manually that there are no longer any resolves to angular/node_modules (the workspace folder). Some tests to verify this would probably be good as well. Turning on preserve-symlinks and building within a docker container for angular/angular would do the trick.

@johnbendi

Copy link
Copy Markdown

@gregmagolan if i need to use this commit immediately in my repo how can I request it with git_repository?

@gregmagolan

gregmagolan commented Jun 28, 2018 •

Copy link
Copy Markdown
Collaborator Author

@johnbendi You can use http_archive in your WORKSPACE to use this commit.

http_archive(
    name = "build_bazel_rules_nodejs",
    url = "https://github.com/gregmagolan/rules_nodejs/archive/d450dbff75a1a6a0833a643c6ffb9731293258dd.zip",
    strip_prefix = "rules_nodejs-d450dbff75a1a6a0833a643c6ffb9731293258dd",
    sha256 = "9f230129c813b55289051f25e09a763a40994cb95c4bbc36484a98d8adf156b3",
)

sha256 is optional.

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.

@johnbendi

Copy link
Copy Markdown

@gregmagolan I'm getting issue similar to angular/angular#24521

@johnbendi

Copy link
Copy Markdown

@gregmagolan Now I'm getting build failure with the following message:

INFO: Build options have changed, discarding analysis cache.
INFO: Analysed target //src/ts/ng/client/app:main_spa (27 packages loaded).
INFO: Found 1 target...
INFO: From SassCompiler src/ts/ng/client/app/components/app-header/app-header.component.css:
WARNING: source-map-support module not installed.
   Stack traces from languages like TypeScript will point to generated .js files.
   Error: Cannot find module 'source-map-support' (target sass:sass_bin)
  looked in:
   source-map-support (built-in, relative or absolute) - Error: Not a built-in module, relative or absolute import,
   source-map-support (in runfiles) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/5/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/source-map-support',
   source-map-support (in node_modules build_bazel_rules_sass_compiletime_deps/node_modules) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/5/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/build_bazel_rules_sass_compiletime_deps/node_modules/source-map-support'
INFO: From SassCompiler src/ts/ng/client/app/components/app-footer/app-footer.component.css:
WARNING: source-map-support module not installed.
   Stack traces from languages like TypeScript will point to generated .js files.
   Error: Cannot find module 'source-map-support' (target sass:sass_bin)
  looked in:
   source-map-support (built-in, relative or absolute) - Error: Not a built-in module, relative or absolute import,
   source-map-support (in runfiles) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/6/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/source-map-support',
   source-map-support (in node_modules build_bazel_rules_sass_compiletime_deps/node_modules) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/6/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/build_bazel_rules_sass_compiletime_deps/node_modules/source-map-support'
INFO: From SassCompiler src/ts/ng/client/app/components/app-menu-tree/app-menu-tree.component.css:
WARNING: source-map-support module not installed.
   Stack traces from languages like TypeScript will point to generated .js files.
   Error: Cannot find module 'source-map-support' (target sass:sass_bin)
  looked in:
   source-map-support (built-in, relative or absolute) - Error: Not a built-in module, relative or absolute import,
   source-map-support (in runfiles) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/7/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/source-map-support',
   source-map-support (in node_modules build_bazel_rules_sass_compiletime_deps/node_modules) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/7/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/build_bazel_rules_sass_compiletime_deps/node_modules/source-map-support'
INFO: From SassCompiler src/ts/ng/client/app/app.component.css:
WARNING: source-map-support module not installed.
   Stack traces from languages like TypeScript will point to generated .js files.
   Error: Cannot find module 'source-map-support' (target sass:sass_bin)
  looked in:
   source-map-support (built-in, relative or absolute) - Error: Not a built-in module, relative or absolute import,
   source-map-support (in runfiles) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/8/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/source-map-support',
   source-map-support (in node_modules build_bazel_rules_sass_compiletime_deps/node_modules) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/8/execroot/betadeals_frontend/bazel-out/host/bin/external/io_bazel_rules_sass/sass/sass.runfiles/build_bazel_rules_sass_compiletime_deps/node_modules/source-map-support'
ERROR: /home/johnbendi/projects/betab2b/frontend/responsive/material/src/ts/ng/client/app/components/app-header/BUILD.bazel:12:1: Compiling Angu
lar templates (ngc) //src/ts/ng/client/app/components/app-header:app_header failed (Exit 1)
failed to load main  Error: Cannot find module '@angular/compiler-cli' (target src/ngc-wrapped:ngc-wrapped_bin)
  looked in:
   @angular/compiler-cli (built-in, relative or absolute) - Error: Not a built-in module, relative or absolute import,
   @angular/compiler-cli (in runfiles) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e
4a/sandbox/linux-sandbox/18/execroot/betadeals_frontend/bazel-out/host/bin/external/angular/src/ngc-wrapped/ngc-wrapped.runfiles/@angular/compiler-cli',
   @angular/compiler-cli (in node_modules build_bazel_rules_typescript_tsc_wrapped_deps/node_modules) - Error: Cannot find module '/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/18/execroot/betadeals_frontend/bazel-out/host/bin/external/angular/src/ngc-wrapped/ngc-wrapped.runfiles/build_bazel_rules_typescript_tsc_wrapped_deps/node_modules/@angular/compiler-cli'
    at Function.module.constructor._resolveFilename (/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/execroot/betadeals_frontend/bazel-out/host/bin/external/angular/src/ngc-wrapped/ngc-wrapped_bin_loader.js:316:17)
    at Function.Module._load (module.js:466:25)
    at Module.require (module.js:579:17)
    at require (internal/module.js:11:18)
    at /home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/execroot/betadeals_frontend/external/angular/src/ngc-wrapped/index.ts:9:1
    at Object.defineProperty.value (/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/execroot/betadeals_frontend/bazel-out/host/bin/external/angular/src/ngc-wrapped/index.js:40:17)
    at Object.<anonymous> (/home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/execroot/betadeals_frontend/bazel-out/host/bin/external/angular/src/ngc-wrapped/index.js:46:3)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
Target //src/ts/ng/client/app:main_spa failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 48.980s, Critical Path: 35.92s
INFO: 16 processes: 12 linux-sandbox, 4 local.
FAILED: Build did NOT complete successfully

Before this I was getting build failure with the following message:

INFO: Build options have changed, discarding analysis cache.
INFO: Analysed target //src/ts/ng/client/app:main_spa (26 packages loaded).
INFO: Found 1 target...
ERROR: /home/johnbendi/projects/betab2b/frontend/responsive/material/src/ts/ng/client/app/components/app-footer/BUILD.bazel:12:1: Compiling Angu
lar templates (ngc) //src/ts/ng/client/app/components/app-footer:app_footer failed (Exit 1)

WARNING: your tsconfig.json file specifies options which are overridden by Bazel:
 - compilerOptions.target and compilerOptions.module are controlled by downstream dependencies, such as ts_devserver
 - compilerOptions.paths is determined by the module_name attribute in transitive deps[]
 - compilerOptions.typeRoots is always set to the @types subdirectory of the node_modules attribute
 - compilerOptions.rootDir and compilerOptions.baseUrl are always the workspace root directory


: Error: Could not resolve ./router.ngfactory from /home/johnbendi/.cache/bazel/_bazel_johnbendi/a78f499bf50e18fa3ff9c74610609e4a/sandbox/linux-sandbox/19/execroot/betadeals_frontend/node_modules/@angular/router/router.d.ts
    at TsCompilerAotCompilerTypeCheckHostAdapter.fromSummaryFileName (/home/johnbendi/projects/betab2b/frontend/packages/compiler-cli/src/transformers/compiler_host.ts:274:13)
    at AotSummaryResolver.fromSummaryFileName (/home/execroot/angular/packages/compiler/src/aot/summary_resolver.ts:64:22)
    at /home/execroot/angular/packages/compiler/src/aot/summary_serializer.ts:441:34
    at Array.map (<anonymous>)
    at FromJsonDeserializer.deserialize (/home/execroot/angular/packages/compiler/src/aot/summary_serializer.ts:439:33)
    at deserializeSummaries (/home/execroot/angular/packages/compiler/src/aot/summary_serializer.ts:61:23)
    at AotSummaryResolver._loadSummaryFile (/home/execroot/angular/packages/compiler/src/aot/summary_resolver.ts:118:13)
    at AotSummaryResolver.resolveSummary (/home/execroot/angular/packages/compiler/src/aot/summary_resolver.ts:73:12)
    at StaticSymbolResolver._resolveSymbolFromSummary (/home/execroot/angular/packages/compiler/src/aot/static_symbol_resolver.ts:234:42)
    at StaticSymbolResolver.resolveSymbol (/home/execroot/angular/packages/compiler/src/aot/static_symbol_resolver.ts:85:36)

Target //src/ts/ng/client/app:main_spa failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 47.993s, Critical Path: 37.05s
INFO: 16 processes: 12 linux-sandbox, 4 local.
FAILED: Build did NOT complete successfully

@johnbendi

johnbendi commented Jul 1, 2018 •

Copy link
Copy Markdown

@gregmagolan am I doing something wrong with regard to my previous post? This issue is keeping me from moving forward with Bazel.

@gregmagolan

gregmagolan commented Jul 2, 2018 •

Copy link
Copy Markdown
Collaborator Author

@johnbendi The Could not resolve ./router.ngfactory error you were seeing will happen with Angular 6.0.5 with Bazel unless you build Angular from source with Bazel which isn't ready yet. If you stick to 6.0.4 for the time being then you can avoid that issue.

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.

Comment thread internal/npm_install/package.json Outdated
"dependencies": {
"removeNPMAbsolutePaths": "1.0.4"
"removeNPMAbsolutePaths": "1.0.4",
"source-map": "0.5.7",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"],
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread internal/rollup/package.json Outdated
"rollup": "0.60.1",
"rollup-plugin-node-resolve": "3.3.0",
"rollup-plugin-sourcemaps": "^0.4.2",
"source-map": "0.5.7",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, is this really require'd somewhere in our code?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this has lots of conditional logic, maybe we could write a unit test for this file?

Comment thread internal/node/node_loader.js Outdated
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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opt: would be easier to review if the style changes were a separate commit / PR

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good

Comment thread internal/node/node.bzl
# 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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"],
)

Comment thread internal/node/node.bzl
escaped = mn.replace("/", r"\/").replace(".", r"\.")
mapping = r"{module_name: /^%s\b/, module_root: '%s'}" % (escaped, mr)
module_mappings.append(mapping)
node_modules_roots = []

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

@gregmagolan gregmagolan Jul 3, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@gregmagolan

Copy link
Copy Markdown
Collaborator Author

Replaced by #253

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants