Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34.2k
module: resolve self-references#29327
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
hybrist commented Aug 26, 2019 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
MylesBorins commented Aug 26, 2019
I have to dig up the PR for introducing a scope / namespace... but |
hybrist commented Aug 26, 2019
@MylesBorins Unfortunately basically anything but |
MylesBorins commented Aug 26, 2019 via email
It isn't a valid path on Windows though. With it being a tier 1 platform do we have to worry about breaking it? …On Mon, Aug 26, 2019, 6:11 PM Jan Olaf Krems ***@***.***> wrote: @MylesBorins <https://github.com/MylesBorins> Unfortunately basically anything but \0 is a valid Unix directory name. So namespaces etc. won't remove the need for making it resolve last for backwards compat. But it could help make it more obvious that it's "special"! — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#29327?email_source=notifications&email_token=AADZYV32XQHZW7LTXBBAYO3QGRIHLA5CNFSM4IPWVHE2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD5F22IQ#issuecomment-525053218>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AADZYV3IZODLSGGRX5UJKF3QGRIHLANCNFSM4IPWVHEQ> . |
GeoffreyBooth commented Aug 26, 2019
This dovetails with the concept of package scope we’re introducing in ESM, as Of course I assume you plan on making this work in ESM too; we should make sure it doesn’t conflict with potentially eventually supporting browser import maps, if they have any plans for |
GeoffreyBooth commented Aug 26, 2019
Isn’t it the opposite? On Windows we know that |
hybrist commented Aug 26, 2019
We may have to check that trying to stat on Windows won’t throw (so we actually get to our logic). The specifier not working as-is across platforms would be a good thing because it reduces the risk that somebody used it already for other purposes. |
ljharb commented Aug 27, 2019
cc @nodejs/modules |
bmeck commented Aug 27, 2019
On *nix: date >'~'; cat <'~'Or on win32: date> '~' ; type< '~'However, the fact that it is valid doesn't necessarily mean it is portable. Lack of portability in particular on the myriad of package registries means that it is probably safe I would think, a quick search of require on gzemnid naive findingsThat comes out to 92 modules in the public registry using this pattern.From github it is harder to perform the search since |
devsnek left a comment
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 this isn't pointing to os.homedir() i don't think tilde should be used. Just too surprising/confusing.
Fishrock123 commented Aug 27, 2019
Would it be more reasonable to add a separate api for this rather than relying on figuring out a new specifier? |
ljharb commented Aug 27, 2019
@Fishrock123 that would be much more confusing and complex for the massive ecosystem that handles resolution; and it wouldn’t work as nicely with ESM. It’s far cleaner to do it via specifier imo. |
ljharb commented Aug 27, 2019
@devsnek tilde is the most commonly used alias in my experience (babel/webpack/TS) for this exact feature, package root. Do you have an alternative suggestion? |
devsnek commented Aug 27, 2019
|
hybrist commented Aug 27, 2019
Since |
MylesBorins commented Aug 27, 2019
Wouldn't the namespace approach make it different enough from home directory? |
hybrist commented Aug 27, 2019
@MylesBorins Ah, gotcha. Right, if the issue is "~/ could be confused with the home directory", then we may be able to compromise on |
devsnek commented Aug 27, 2019
My issue with the tilde is more of the fact that it's the first character in the string, which for the last 30 years has meant "expand to $HOME" in unix, and however long powershell has existed. If we're trying to make paths less confusing, I don't think using tilde at the start is the best approach. Perhaps |
Fishrock123 commented Aug 27, 2019 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
My 2c is over at nodejs/modules#306 (comment)
|
ljharb commented Aug 27, 2019
Requiring the package name defeats one of the benefits of having the alias, which is “not repeating the package name”. |
ljharb commented Aug 27, 2019
@Fishrock123 TC39 is irrelevant here; there’s no concept of a “package” and the spec intentionally avoids imposing any restrictions on specifier contents. |
MylesBorins commented Aug 27, 2019
my idea would be that
|
ljharb commented Aug 27, 2019
by "named-export" you mean, any specifier that's reachable from the package root, filtered through |
devongovett commented Aug 27, 2019 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
FWIW, Parcel and other tools already implement tilde resolution as it is being proposed here. https://parceljs.org/module_resolution.html I don't think anyone would expect |
GeoffreyBooth commented Aug 27, 2019
I agree with this. Non-portable code is uncommon, and we shouldn’t assume that that’s the default or the user expectation. The only use case I can think of is the Node equivalent of shell scripts, but even those would probably usually use relative references. I think there’s an advantage to reusing the |
devsnek commented Aug 27, 2019
Userland code can do whatever it wants, people opt into it. We don't plan to let this be overridden and I know at least three people I've talked to who would want avoid using tilde as an expansion. |
hybrist commented Aug 27, 2019
@devsnek Asking explicitly: Would Myles' proposal change things (using |
devsnek commented Aug 27, 2019 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
@jkrems my critera above was "what happens if i put it in bash". For example on my computer However, thinking about this further, I'm curious about whether this feature in general goes against our "browser compatibility" goals. |
nodejs-github-bot commented Oct 24, 2019
hybrist commented Oct 24, 2019
Landed in 71bcd05 |
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
MylesBorins left a comment
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.
Post land RSLGTM
| 4. LOAD_NODE_MODULES(X, dirname(Y)) | ||
| 5. THROW "not found" | ||
| 5. LOAD_NODE_MODULES(X, dirname(Y)) | ||
| 4. LOAD_SELF_REFERENCE(X, dirname(Y)) |
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.
Are these entries misnumbered?
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.
They are! Let me create a follow-up to fix that.
hybristOct 25, 2019 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
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.
Fix in #30117
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
Adds the ability to `import` or `require` a package from within its own source code. This allows tests and examples to be written using the package name, making them easier to reuse by consumers of the package. Assuming the `name` field in `package.json` is set to `my-pkg`, its test could use `require('my-pkg')` or `import 'my-pkg'` even if there's no `node_modules/my-pkg` while testing the package itself. An important difference between this and relative specifiers like `require('../')` is that self-references use the public interface of the package as defined in the `exports` field while relative specifiers don't. This behavior is guarded by a new experimental flag (`--experimental-resolve-self`). PR-URL: #29327 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>Refs: #29327 PR-URL: #30117 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
See nodejs/modules#306
requestisn't a relative path and wasn't resolved successfully using the existing algorithm.package.json(the "package scope"), bail if none can be found.requestmatches thenamefield ofpackage.json.requestagainst the package as if requiring from the outside.package.json#name) to be used.package.json#name, checknode_modulesfirst.The unflagging issue should cover:
Checklist
make -j4 test(UNIX), orvcbuild test(Windows) passes