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

module: allow passing a directory to createRequireFromPath #23818

Closed

Conversation

gillesdemey
Copy link
Contributor

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), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@Trott
Copy link
Member

Trott commented Nov 19, 2018

Copy link
Contributor

@guybedford guybedford left a 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.

@gillesdemey
Copy link
Contributor Author

Seems like a flaky streams test, from the CI output:

02:54:38 not ok 410 parallel/test-stream-pipeline
02:54:38   ---
02:54:38   duration_ms: 120.94
02:54:38   severity: fail
02:54:38   exitcode: 1
02:54:38   stack: |-
02:54:38     timeout
02:54:38   ...

@Trott
Copy link
Member

Trott commented Nov 23, 2018

@Trott Trott added the semver-minor PRs that contain new features and should be released in the next minor version. label Nov 28, 2018
@Trott
Copy link
Member

Trott commented Nov 28, 2018

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?)

@guybedford
Copy link
Contributor

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' });
Copy link
Member

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.

Copy link
Contributor

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

Copy link
Contributor Author

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 :)

Copy link
Contributor

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.

Copy link
Contributor

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

Copy link
Member

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.

Copy link
Contributor

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 ?

Copy link
Member

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.

Copy link
Contributor

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?

Copy link
Contributor

@MylesBorins MylesBorins left a comment

Choose a reason for hiding this comment

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

LGTM

@MylesBorins MylesBorins self-assigned this Apr 25, 2019
@nodejs-github-bot

This comment has been minimized.

@MylesBorins
Copy link
Contributor

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 createRequireFromPath was expected behavior.

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?

@nodejs-github-bot
Copy link
Collaborator

lib/internal/modules/cjs/loader.js Outdated Show resolved Hide resolved
// 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;
Copy link
Member

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.

Copy link
Contributor

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' });
Copy link
Member

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 targos added module Issues and PRs related to the module subsystem. semver-minor PRs that contain new features and should be released in the next minor version. labels Apr 25, 2019
@MylesBorins
Copy link
Contributor

@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

@targos targos removed the semver-minor PRs that contain new features and should be released in the next minor version. label Apr 25, 2019
@targos
Copy link
Member

targos commented Apr 25, 2019

Sorry, I was just passing through PRs without any label. I didn't see it was added and removed.

@nodejs-github-bot
Copy link
Collaborator

@MylesBorins
Copy link
Contributor

rebased PR and added changes based on review. PTAL

@MylesBorins
Copy link
Contributor

only failure is infra failure on AIX... will land in 24 hours if there are no objections

Copy link
Member

@BridgeAR BridgeAR left a 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.

Copy link
Member

@addaleax addaleax left a 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.

@nodejs-github-bot
Copy link
Collaborator

@BridgeAR
Copy link
Member

BridgeAR commented May 1, 2019

@addaleax

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 path.dirname(filename).

@addaleax
Copy link
Member

addaleax commented May 1, 2019

@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.

@MylesBorins
Copy link
Contributor

landed in e5c8be2

@MylesBorins MylesBorins closed this May 2, 2019
MylesBorins pushed a commit that referenced this pull request May 2, 2019
Fixes: #23710

PR-URL: #23818
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@gillesdemey gillesdemey deleted the fix/createRequireFromPath branch May 2, 2019 08:23
targos pushed a commit that referenced this pull request May 4, 2019
Fixes: #23710

PR-URL: #23818
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
@targos targos mentioned this pull request May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

module: createRequireFromPath documentation is incorrect
10 participants