-
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
Add a --preserve-symlinks-main option #19911
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { spawn } = require('child_process'); | ||
const assert = require('assert'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
const tmpdir = require('../common/tmpdir'); | ||
tmpdir.refresh(); | ||
const tmpDir = tmpdir.path; | ||
|
||
fs.mkdirSync(path.join(tmpDir, 'nested')); | ||
fs.mkdirSync(path.join(tmpDir, 'nested2')); | ||
|
||
const entry = path.join(tmpDir, 'nested', 'entry.js'); | ||
const entry_link_absolute_path = path.join(tmpDir, 'link.js'); | ||
const submodule = path.join(tmpDir, 'nested2', 'submodule.js'); | ||
const submodule_link_absolute_path = path.join(tmpDir, 'submodule_link.js'); | ||
|
||
fs.writeFileSync(entry, ` | ||
const assert = require('assert'); | ||
|
||
// this import only resolves with --preserve-symlinks-main set | ||
require('./submodule_link.js'); | ||
`); | ||
fs.writeFileSync(submodule, ''); | ||
|
||
try { | ||
fs.symlinkSync(entry, entry_link_absolute_path); | ||
fs.symlinkSync(submodule, submodule_link_absolute_path); | ||
} catch (err) { | ||
if (err.code !== 'EPERM') throw err; | ||
common.skip('insufficient privileges for symlinks'); | ||
} | ||
|
||
function doTest(flags, done) { | ||
// invoke the main file via a symlink. In this case --preserve-symlinks-main | ||
// dictates that it'll resolve relative imports in the main file relative to | ||
// the symlink, and not relative to the symlink target; the file structure set | ||
// up above requires this to not crash when loading ./submodule_link.js | ||
spawn(process.execPath, | ||
flags.concat([ | ||
'--preserve-symlinks', | ||
'--preserve-symlinks-main', entry_link_absolute_path | ||
]), | ||
{ stdio: 'inherit' }).on('exit', (code) => { | ||
assert.strictEqual(code, 0); | ||
done(); | ||
}); | ||
} | ||
|
||
// first test the commonjs module loader | ||
doTest([], () => { | ||
// now test the new loader | ||
doTest(['--experimental-modules'], () => {}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed a bit lower in
lib/internal/modules/cjs/loader.js
thattryFile
has a similarpreserveSymlinks && !isMain
check. Should it also add thepreserveSymlinksMain
juggle there?node/lib/internal/modules/cjs/loader.js
Lines 193 to 199 in 452d73d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, maybe? I'm really not familiar with this code. --preserve-symlinks-main did pass the test cases I wrote for it, though maybe those aren't exhaustive enough? I wonder if there's something unique about the way the main file is loaded that it doesn't hit the tryFile code path? Just a guess.
If we can find a case where the code misbehaves as written then it's definitely something that we should fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reading a little closer... I think that node is supposed to be able to figure out that if
asdf.js
exists, thennode asdf
supposed to meannode asdf.js
. It looks like what you've noticed is that the code for that doesn't handle --preserve-symlinks-main like it would handle --preserve-symlinks, which does seem like a bug, though at least an easy to work around one (just remember to specify the extension on the command line)