Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(builtin): linker no longer makes node_modules symlink to the root of the workspace output tree #1797

Merged
merged 8 commits into from
Jun 16, 2020

Conversation

gregmagolan
Copy link
Collaborator

@gregmagolan gregmagolan commented Apr 6, 2020

For 2.0.

Thanks @mrmeku for updating ts_proto_library for this PR.

BREAKING CHANGES:

  1. Any nodejs_binary/nodejs_test processes with the linker enabled (--nobazel_patch_module_resolver is set) that were relying on standard node_module resolution to resolve manifest file paths such as my_workspace/path/to/output/file.js must now use the runfiles helper such as.

Previously:

const absPath = require.resolve('my_workspace/path/to/output/file.js');

With runfiles helper:

const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
const absPath = runfiles.resolve('my_workspace/path/to/output/file.js');
  1. Angular ViewEngine + Bazel is no longer supported in rules_nodejs 2.0 as the generated ngfactory & ngsummary files depend on absolute imports. Users upgrading to rules_nodejs 2.0 will also need to upgrade to Ivy if they are on ViewEngine.

@gregmagolan gregmagolan added this to the 2.0 milestone Apr 6, 2020
@gregmagolan gregmagolan force-pushed the linker_no_root_symlink branch 2 times, most recently from 2ad92b2 to b3b9f0e Compare April 8, 2020 01:36
@gregmagolan gregmagolan changed the title fix(builtin): linker no longer makes node_modules symlink to the rootof the workspace output tree WIP fix(builtin): linker no longer makes node_modules symlink to the rootof the workspace output tree Apr 8, 2020
@gregmagolan gregmagolan force-pushed the linker_no_root_symlink branch 2 times, most recently from b8e4def to f83daca Compare April 10, 2020 14:20
@gregmagolan
Copy link
Collaborator Author

Pretty much done except for issues with the ts_proto_library labs rule which has examples & tests that depends on absolute imports. @mrmeku PTAL.

@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@gregmagolan gregmagolan changed the title WIP fix(builtin): linker no longer makes node_modules symlink to the rootof the workspace output tree fix(builtin): linker no longer makes node_modules symlink to the root of the workspace output tree Apr 17, 2020
@alexeagle
Copy link
Collaborator

What do we do about the recommendation in a monorepo that you can avoid crazy import {} from '../../../../../../path/to/thing' and instead always import {} from my_wksp/path/to/thing
do we have any revised guidance? is there a README somewhere that needs to be updated?

Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

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

I think this is deleting the sources for bazel.angular.io such that we can never deploy it again. Not sure if that's a problem but we should coordinate with @IgorMinar about tearing that down

Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

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

I think ts_library pathmapping is still going to allow code to type-check with these workspace-absolute paths, is that a problem that it type-checks but can't run?

</li>
<li>
<a
href="https://medium.com/@Jakeherringbone/running-tools-under-bazel-8aa416e7090c"
Copy link
Collaborator

Choose a reason for hiding this comment

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

all this content we didn't really mean to delete...


let c;
try {
// As of 2.0, we no longer support `require('my_workspace/path/to/output/file.js')` for absolute
Copy link
Collaborator

Choose a reason for hiding this comment

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

would be nice to link to this spot in the BREAKING CHANGE

@IgorMinar
Copy link
Contributor

Let's coordinate. I have a meeting with Greg later this week so we can cover this as well.

gregmagolan added a commit to gregmagolan/rules_nodejs that referenced this pull request Apr 29, 2020
…when under runfiles

Resolves bazel-contrib#1823 which had a repro https://github.com/purkhusid/ts-jest-repro.

NB: The repo uses ts_library devmode UMD output so the relative require in the source code will be transformed to an absolute require which will include the workspace name.

In 1.5.0:

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/lib.test.js

const lib_1 = require("jest_repro/lib");
```

works as it finds

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro/lib.js
```

where `test.sh.runfiles/jest_repro/node_modules/jest_repro` is a symlink to the root of the output tree setup by the linker:

```
lrwxr-xr-x  1 greg  wheel  165 29 Apr 05:56 .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro -> .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro
```

In 1.6.0:

Bug introduced in bazel-contrib#1850 (thanks for tracking down the culprit @alexeagle) where the root symlink is not created properly when under runfiles (such as a jest_test context).

Fix is:

```
          let runfilesPath = modulePath;
          if (runfilesPath.startsWith(`${bin}/`)) {
            runfilesPath = runfilesPath.slice(bin.length + 1);
+          } else if (runfilesPath === bin) {
+            runfilesPath = '';
          }
```

in `link_node_modules.ts`.

NB: This root workspace symlink will be removed in 2.0 as it causes other unrelated issues. PR for that change is in progress bazel-contrib#1797. In 2.0, we recommend either using the prodmode esm output of ts_library for jest_test or switching to ts_project where the output flavor is controlled by your tsconfig. An example of using ts_library esm output of ts_library (output_group 'es6_sources') is `under /internal/linker/test/issue_1823_use_ts_library_esm`. It require babel-jest to transform the esm to commonjs. Alternately, you can control the 'es5_sources' module format of ts_library with the `devmode_module` but this can lead to other issues such as the output no longer being compatible with ts_devserver and karma_web_test which require named-UMD or named-AMD.
gregmagolan added a commit that referenced this pull request Apr 29, 2020
…when under runfiles

Resolves #1823 which had a repro https://github.com/purkhusid/ts-jest-repro.

NB: The repo uses ts_library devmode UMD output so the relative require in the source code will be transformed to an absolute require which will include the workspace name.

In 1.5.0:

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/lib.test.js

const lib_1 = require("jest_repro/lib");
```

works as it finds

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro/lib.js
```

where `test.sh.runfiles/jest_repro/node_modules/jest_repro` is a symlink to the root of the output tree setup by the linker:

```
lrwxr-xr-x  1 greg  wheel  165 29 Apr 05:56 .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro -> .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro
```

In 1.6.0:

Bug introduced in #1850 (thanks for tracking down the culprit @alexeagle) where the root symlink is not created properly when under runfiles (such as a jest_test context).

Fix is:

```
          let runfilesPath = modulePath;
          if (runfilesPath.startsWith(`${bin}/`)) {
            runfilesPath = runfilesPath.slice(bin.length + 1);
+          } else if (runfilesPath === bin) {
+            runfilesPath = '';
          }
```

in `link_node_modules.ts`.

NB: This root workspace symlink will be removed in 2.0 as it causes other unrelated issues. PR for that change is in progress #1797. In 2.0, we recommend either using the prodmode esm output of ts_library for jest_test or switching to ts_project where the output flavor is controlled by your tsconfig. An example of using ts_library esm output of ts_library (output_group 'es6_sources') is `under /internal/linker/test/issue_1823_use_ts_library_esm`. It require babel-jest to transform the esm to commonjs. Alternately, you can control the 'es5_sources' module format of ts_library with the `devmode_module` but this can lead to other issues such as the output no longer being compatible with ts_devserver and karma_web_test which require named-UMD or named-AMD.
gregmagolan added a commit to gregmagolan/rules_nodejs that referenced this pull request Apr 30, 2020
…when under runfiles

Resolves bazel-contrib#1823 which had a repro https://github.com/purkhusid/ts-jest-repro.

NB: The repo uses ts_library devmode UMD output so the relative require in the source code will be transformed to an absolute require which will include the workspace name.

In 1.5.0:

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/lib.test.js

const lib_1 = require("jest_repro/lib");
```

works as it finds

```
.../sandbox/darwin-sandbox/23/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro/lib.js
```

where `test.sh.runfiles/jest_repro/node_modules/jest_repro` is a symlink to the root of the output tree setup by the linker:

```
lrwxr-xr-x  1 greg  wheel  165 29 Apr 05:56 .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro/node_modules/jest_repro -> .../sandbox/darwin-sandbox/6/execroot/jest_repro/bazel-out/darwin-fastbuild/bin/test.sh.runfiles/jest_repro
```

In 1.6.0:

Bug introduced in bazel-contrib#1850 (thanks for tracking down the culprit @alexeagle) where the root symlink is not created properly when under runfiles (such as a jest_test context).

Fix is:

```
          let runfilesPath = modulePath;
          if (runfilesPath.startsWith(`${bin}/`)) {
            runfilesPath = runfilesPath.slice(bin.length + 1);
+          } else if (runfilesPath === bin) {
+            runfilesPath = '';
          }
```

in `link_node_modules.ts`.

NB: This root workspace symlink will be removed in 2.0 as it causes other unrelated issues. PR for that change is in progress bazel-contrib#1797. In 2.0, we recommend either using the prodmode esm output of ts_library for jest_test or switching to ts_project where the output flavor is controlled by your tsconfig. An example of using ts_library esm output of ts_library (output_group 'es6_sources') is `under /internal/linker/test/issue_1823_use_ts_library_esm`. It require babel-jest to transform the esm to commonjs. Alternately, you can control the 'es5_sources' module format of ts_library with the `devmode_module` but this can lead to other issues such as the output no longer being compatible with ts_devserver and karma_web_test which require named-UMD or named-AMD.
@alexeagle
Copy link
Collaborator

@gregmagolan if you don't have time to re-base and land, we could hand to someone else?

@alexeagle
Copy link
Collaborator

Need to check that it's still possible for users to link their project root by putting a pkg_npm or something in the /BUILD.bazel

… of the workspace output tree

BREAKING CHANGE:

Any nodejs_binary/nodejs_test processes with the linker enabled (--nobazel_patch_module_resolver is set) that were relying on standard node_module resolution to resolve manfest file paths such as `my_workspace/path/to/output/file.js` must now use the runfiles helper such as.

Previously:
```
const absPath = require.resolve('my_workspace/path/to/output/file.js');
```
With runfiles helper:
```
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
const absPath = runfiles.resolve('my_workspace/path/to/output/file.js');
```
…kspace symlink

Absolute imports no longer supported via require. You must use the runfiles helper.
@gregmagolan gregmagolan force-pushed the linker_no_root_symlink branch 3 times, most recently from c5a749b to 3742a9f Compare June 13, 2020 01:31
@alexeagle
Copy link
Collaborator

Windows failure FYI


Test output for //packages/typescript/test/ts_library_esm_with_jest:test:
--
  | FAIL ../../lib.test.js
  | ● Test suite failed to run
  |  
  | Cannot find module 'build_bazel_rules_nodejs/packages/typescript/test/ts_library_esm_with_jest/lib' from 'lib.test.js'
  |  
  | > 1 \| import {doStuff} from './lib';
  | \| ^
  | 2 \|
  | 3 \| describe('doStuff', () => {
  | 4 \|   it('should do some stuff', () => {
  |  
  | at Resolver.resolveModule (../../../../../../../node_modules/jest-resolve/build/index.js:296:11)
  | at ../../../../../../../packages/typescript/test/ts_library_esm_with_jest/lib.test.ts:1:1
  | at lib.test.js:3:17
  | at Object.<anonymous> (lib.test.js:9:3)

Copy link
Collaborator

@alexeagle alexeagle left a comment

Choose a reason for hiding this comment

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

looks like angular_view_engine is the only example that shows how to put a pkg_npm in the root to get workspace-absolute imports? Maybe we should do this in some other example or a new one, so if we delete angular_view_engine at some point, we aren't missing one

@@ -5,6 +5,7 @@ load("@npm//@bazel/rollup:index.bzl", "rollup_bundle")
load("@npm//@bazel/terser:index.bzl", "terser_minified")
load("@npm//@bazel/typescript:index.bzl", "ts_config", "ts_devserver", "ts_library")
load("@npm//http-server:index.bzl", "http_server")
load("@rules_proto//proto:defs.bzl", "proto_library")
Copy link
Collaborator

Choose a reason for hiding this comment

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

was this just a warning you're fixing or is it related to the change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

🤔 where did that symbol come from before I added that load?

@alexeagle alexeagle force-pushed the linker_no_root_symlink branch 4 times, most recently from 557d7fc to 9faf03f Compare June 16, 2020 05:55
@alexeagle
Copy link
Collaborator

Note, #2175 added back the ability to link the workspace root into the node_modules tree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants