-
Notifications
You must be signed in to change notification settings - Fork 30k
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
--preserve-symlinks-main doesn't work #41000
Comments
hm, the one thing that comes to mind is that I've only ever used --preserve-symlinks-main with full file paths. Does it repro with
Also it was implemented before ESM was supported by default in node, so could be a commonjs vs ESM module loading issue. |
Fixed resolving relative imports when symlink to main file called without file extension Fixies: nodejs#41000
I can easily reproduce this with
|
This was previously attempted to fix in #4324 While the above fix resolves issues with dependencies, it causes failures when the binary is shelling out to other node processes. This is due to the intrusive and indiscriminate overloading of NODE_OPTIONS, which will forcibly apply to any child processes as well. While in theory adding the symlink flags should not be an issue, this seems to trigger a bug in node: nodejs/node#41000 tl;dr this all sucks very much and we are now just disabling the runtime cache for binaries.
This was previously attempted to fix in #4324 While the above fix resolves issues with dependencies, it causes failures when the binary is shelling out to other node processes. This is due to the intrusive and indiscriminate overloading of NODE_OPTIONS, which will forcibly apply to any child processes as well. While in theory adding the symlink flags should not be an issue, this seems to trigger a bug in node: nodejs/node#41000 tl;dr this all sucks very much and we are now just disabling the runtime cache for binaries. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
not sure I entirely understand your npm example but in general I would not expect --preserve-symlinks-main to be usable with a node_modules/.bin/thing main argument - thing there would be a symlink into an entirely different folder, probably node_modules/thing/cli.js or similar, and then require()s or imports in cli.js should be resolved relative to node_modules/thing/cli.js, not the node_modules/.bin/thing. So I don't think your npm example is a valid reproduction. |
Fixes resolving main module when the `argv[1]` was pointing to a symlink without its file extension. PR-URL: nodejs#51312 Fixes: nodejs#41000 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fixes resolving main module when the `argv[1]` was pointing to a symlink without its file extension. PR-URL: nodejs#51312 Fixes: nodejs#41000 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Version
v17.0.1
Platform
Subsystem
No response
What steps will reproduce the bug?
Create a package with a file and a symlink to a file in a different directory.
Run Node.js with the package as the entrypoint.
How often does it reproduce? Is there a required condition?
The feature
--preserve-symlinks-main
works with some file structures and doesn't work with others (like the example). I haven't yet figured out the general rule.What is the expected behavior?
Node.js runs without error.
Namely, the entry point
/home/paul/example/package/index.js
resolves./lib.js
to/home/paul/example/package/lib.js
What do you see instead?
Node.js errs:
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module './lib.js' Require stack: - /home/paul/example/file.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:999:19) at require (node:internal/modules/cjs/helpers:102:18) at Object.<anonymous> (/home/paul/dev/example/file.js:1:1) at Module._compile (node:internal/modules/cjs/loader:1095:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/paul/example/file.js' ] }
Notice that Node.js has resolved
/home/paul/example/package/index.js
to its realpath (/home/paul/example/file.js
), causing module resolution to fail.Additional information
The validity of the example can be confirmed by replacing the symlink with a hardlink (
ln file.js package/index.js
), after which module resolution works.The text was updated successfully, but these errors were encountered: