-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
module: allow passing a directory to createRequireFromPath #23818
Conversation
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.
CI failure seems to be unrelated as far as I can tell, but perhaps someone can double check.
Seems like a flaky streams test, from the CI output:
|
Resume Build CI: https://ci.nodejs.org/job/node-test-pull-request/18906/ |
This affects CJS modules so I want to make sure it gets sufficient review, so.... @nodejs/collaborators (Maybe we should have a CJS modules team?) |
Note it is important to merge this or the docs change as effectively the documentation is incorrect until then! |
assert.strictEqual(req('./baz'), 'perhaps I work'); | ||
assert.throws(() => { | ||
reqFromNotDir('./baz'); | ||
}, { code: 'MODULE_NOT_FOUND' }); |
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'd expect both of this to pass. fixtures
is a directory, so they should both work IMHO. This probably requires adding a round-trip to OS, but I don't think it could be avoided.
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.
That would be a very different approach to this function, but it could work. //cc @devsnek
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'm happy to change the implementation to call stat.isDirectory()
if that's the way we want to move this forward :)
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.
If we want to get this merged, I'd suggest we stick with the current behaviour which matches the behaviour of path resolution.
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.
+1 to sticking to behavior that matches path... we should change this behavior in the path module if we want to fix it imho
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 personally think calling stat
is a good idea in this case. createRequireFromPath
is IMO special enough to work differently from the rest of Module
.
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 would like to land this to improve the behavior and introduce the stat separately in a follow up. Is that reasonable for you @BridgeAR ?
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.
This has three different open comments at the moment (actually four but two are about using stat
). I personally would not land this but I won't block it either.
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.
@BridgeAR afaict all open comments are about stat, which I think should be an incremental improvement discussed separately.
Did I miss any other open comments?
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.
LGTM
83f4c9b
to
4c6c0d6
Compare
This comment has been minimized.
This comment has been minimized.
went ahead and rebased against master and added a very minor doc fix to at the very least get the docs working. @Trott I've dropped the semver-minor label on this as I believe it should be considered a bug fix. The docs at the very least imply that being able to pass a directory to The function is not used anywhere in core so AFAIK I don't think we need to consider it a change to the way CJS modules work. @mcollina do you have any objections to this landing? |
lib/internal/modules/cjs/loader.js
Outdated
// Allow a directory to be passed as the filename | ||
const normalized = path.win32.normalize(filename); | ||
const trailingSlash = normalized.charCodeAt(normalized.length - 1) === | ||
CHAR_BACKWARD_SLASH; |
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.
Should we maybe just call stat
to verify if it's a file or directory? That way it's even safer and works without a trailing slash.
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 think the issue isn't being different from module it is being different from how Path works... I'm not 100% we should be adding the stat here
assert.strictEqual(req('./baz'), 'perhaps I work'); | ||
assert.throws(() => { | ||
reqFromNotDir('./baz'); | ||
}, { code: 'MODULE_NOT_FOUND' }); |
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 personally think calling stat
is a good idea in this case. createRequireFromPath
is IMO special enough to work differently from the rest of Module
.
@targos why did you add back the semver-minor label? I don't believe this is a minor as lack of directory support is a bug, original docs implied feature was intended |
Sorry, I was just passing through PRs without any label. I didn't see it was added and removed. |
d797960
to
9ec4cb8
Compare
rebased PR and added changes based on review. PTAL |
only failure is infra failure on AIX... will land in 24 hours if there are no objections |
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.
LGTM. It would be nice to get a follow-up PR that implements the suggested stat call.
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.
LGTM – I don’t think a stat call necessarily makes sense, or at least I’d like to see an opt-out switch of that, because it’s only a help to avoid accidentally passing in non-directories, not something that actually influences how the feature works.
I would not use an opt-out but up to two stat calls: one for the filename as it is and if that fails one for |
@BridgeAR I think it’s a bad idea because I think the whole idea of tying the module system to the actual filesystem breaks orthogonality and is consequentially a design flaw; but that’s a bigger discussion. |
landed in e5c8be2 |
Fixes: #23710
I still need to update the documentation for this change, I'm not quite sure if we should update the documentation to encourage users to pass a directory or do we prefer a file path?
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes