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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading from multiple node_modules with version > 4.4.1 #3067

Closed
duarten opened this issue Nov 6, 2021 · 13 comments
Closed

Loading from multiple node_modules with version > 4.4.1 #3067

duarten opened this issue Nov 6, 2021 · 13 comments
Assignees
Labels

Comments

@duarten
Copy link
Contributor

duarten commented Nov 6, 2021

馃悶 bug report

Affected Rule

nodejs_binary / nodejs_test

Is this a regression?

Yes, 4.4.1 works fine, but 4.4.2 and 4.4.3 are broken. This is likely related to the linker refactor.

Description

When a package is symlinked under node_modules due to, for example, the packge_name attribute of js_library, then node modules will be loaded from a different directory.

I noticed this with react, which complains when multiple instances are loaded.

We can see the problem from the following (edited) stack trace:

      <snip>
      at useMemo (node_modules/repro/node_modules/react/cjs/react.development.js:1531:20)
      at Component (../component/component.tsx:4:12)
      <snip>
      at act (node_modules/react-dom/cjs/react-dom-test-utils.development.js:1042:14)
      <snip>
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)

Notice how we first load react from node_modules/react-dom, and later from node_modules/repro/node_modules.

The component package contains the following rule:

js_library(
    name = "component",
    package_name = "repro/component",
    visibility = ["//visibility:public"],
    deps = ["component-ts"],
)

Omitting the workspace name (repro) from the package_name seems to solve the problem (i.e., changing it to "component" or "blah/component").

馃敩 Minimal Reproduction

I committed a reproduction here.

馃敟 Exception or Error

In this case, react complains and the test fails.

馃實 Your Environment

Operating System:

  
  macOS
  

Output of bazel version:

  
  4.2.1
  

Rules_nodejs version:

  
  4.4.3
  
duarten added a commit to duarten/rules_nodejs that referenced this issue Nov 6, 2021
@duarten
Copy link
Contributor Author

duarten commented Jan 13, 2022

This is still happening with version 4.6.0.

@alexeagle, @gregmagolan any tips on how to fix this would be greatly appreciated :)

@alexeagle
Copy link
Collaborator

hi sorry, we've been 100% heads-down on the 5.x breaking changes and getting that release out. I doubt this is fixed there but we should at least verify it's still reproducible on 5.0.0-rc.0 which I'm going to cut todayish.

@alexeagle alexeagle added the bug label Jan 13, 2022
duarten added a commit to duarten/rules_nodejs that referenced this issue Jan 14, 2022
@duarten
Copy link
Contributor Author

duarten commented Jan 14, 2022

It still reproduces with 5.0.0-rc.1. I've updated the reproducer here.

@m0ar
Copy link

m0ar commented Feb 25, 2022

Could this possibly be related to my issue here? 馃 #3340

@duarten
Copy link
Contributor Author

duarten commented Feb 25, 2022

It could be! If you are on 5.x.x it may also be related to #3264, so you should try to set exports_directories_only to False in your yarn_install or npm_install rule.

@m0ar
Copy link

m0ar commented Feb 28, 2022

@duarten Thanks for the clue! 馃檹 Indeed solved the problem. Spend most of Friday trying to hunt this one out.

If you understand the mechanics behind the problem: will it be solved when #3264 is taken care of? I have a hard time figuring out if this is a bug or a side-effect of an optimisation. Anyway if it's the latter, it shouldn't be the default behaviour at least... :)

@duarten
Copy link
Contributor Author

duarten commented Feb 28, 2022

I think it's a bug and will be fixed in #3264, but I don't understand what exactly is causing the issue, I just had recently spent some time trying to figure a problem that was caused by it :)

@thesayyn
Copy link
Collaborator

thesayyn commented Apr 1, 2022

Fixed by #3380

@thesayyn thesayyn closed this as completed Apr 1, 2022
duarten added a commit to duarten/rules_nodejs that referenced this issue Apr 1, 2022
@duarten
Copy link
Contributor Author

duarten commented Apr 1, 2022

@thesayyn Can you reopen this issue? #3380 doesn't fix it (this is unrelated to exports_directories_only). You can run the reproducer here to check the test is still failing.

@thesayyn
Copy link
Collaborator

thesayyn commented Apr 1, 2022

Ah sorry...

@gregmagolan
Copy link
Collaborator

I checked the repo rebased to stable HEAD. This looks like a side-effect of link_workspace_root = True, in the js_test.

In the call stack you can see it resolving into a deeply nested node_modules via the node_modules/repro symlink that the linker creates:

test.sh.runfiles/repro/node_modules/repro/node_modules/react/cjs/react.development.js

If I set that to false then the test passes,

        _jest_test(
            name = test_name,
            data = [jest_setup, jest_config, src] + deps + data,
            # link_workspace_root = True,
            args = args + ["--runTestsByPath", "$(rootpath %s)" % src],
            **kwargs
        )

Is the link_workspace_root intentional here?

@duarten
Copy link
Contributor Author

duarten commented May 4, 2022

It was intentional at some point, but it is indeed no longer necessary. Happy to close the issue unless there's something to attach to it like docs update or something.

@gregmagolan
Copy link
Collaborator

No longer in scope for rules_nodejs which only supplies the Node.js toolchain as of v6.0.0.

Downstream canonical JavaScript + Node.js ruleset is now https://github.com/aspect-build/rules_js.

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

No branches or pull requests

5 participants