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: revert checking for package.json before extensions #15015

Closed
wants to merge 3 commits into from

Conversation

bmeck
Copy link
Member

@bmeck bmeck commented Aug 24, 2017

This PR reverts a regression from a change in behavior that deviated from the documented behavior of the module resolution algorithm. The deviation caused the module resolution algorithm to check for package.json files when traversing up to a parent directory before following the documented module resolution algorithm.

Link discussing the regression in greater depth

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
Affected core subsystem(s)

module

@nodejs-github-bot nodejs-github-bot added the module Issues and PRs related to the module subsystem. label Aug 24, 2017
@bmeck bmeck changed the title [module] Revert checking for package.json before extensions DO NOT MERGE: [module] Revert checking for package.json before extensions Aug 24, 2017
Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

would love this + a backport

@jasnell jasnell added the wip Issues and PRs that are still a work in progress. label Aug 24, 2017
@mscdex mscdex added the semver-major PRs that contain breaking changes and should be released in the next major version. label Aug 24, 2017
@mscdex
Copy link
Contributor

mscdex commented Aug 24, 2017

I'm not sure we need to use the word "revert" if the current behavior was added a very long time ago.

@bmeck
Copy link
Member Author

bmeck commented Aug 24, 2017

@mscdex it has accidental side effects, I would prefer to keep that word.

@ljharb
Copy link
Member

ljharb commented Aug 24, 2017

If it was unintentional and undocumented, it's a bug - I think the term isn't bound by recency.

@targos
Copy link
Member

targos commented Sep 13, 2017

/cc @nodejs/tsc

I am in favor of this change

@BridgeAR
Copy link
Member

BridgeAR commented Dec 5, 2017

I am also in favor of this. It seems like a more correct approach than the way it is currently working.

As there was no progress for a long time, I would like to have the @nodejs/tsc to have a look at this and to decide on how to move forward. There are already two TSC votes in favor.

@BridgeAR BridgeAR added tsc-agenda Issues and PRs to discuss during the meetings of the TSC. tsc-review labels Dec 5, 2017
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.

This needs a rebase. LGTM otherwise

fixturesRequire,
require(fixtures.path('module-extension-over-directory', 'inner.js')),
'test-require-extension-over-directory failed to import fixture' +
' requirements'
Copy link
Member

Choose a reason for hiding this comment

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

There is a new eslint rule in place that prevents messages without dynamic content (or it is at least planned). I do see that it makes sense to have a individual message here, so we should either explicitly accept the message here or maybe just change the inner.js export content to contain a property with a string, remove the message and use deepStrictEqual. With the latter the error message should be explicit enough.

Copy link
Member

Choose a reason for hiding this comment

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

@BridgeAR If that rule ever comes to pass, we can always disable it with a comment for the instances where a string literal makes sense. So this can stay as is, if the proposed lint rule is the only motivation for changing it.

@BridgeAR
Copy link
Member

@bmeck I guess you actually want this to be looked at and it to get merged, right?

In that case it will need a rebase and the title should be changed accordingly.

@fhinkel
Copy link
Member

fhinkel commented Dec 13, 2017

@BridgeAR I don't see any -1. With the approvals, I think we can go ahead and merge it (after a rebase). Can we take this off the TSC agenda? Or am I missing something?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM with a better PR description and commit description

@BridgeAR BridgeAR removed tsc-agenda Issues and PRs to discuss during the meetings of the TSC. tsc-review labels Dec 15, 2017
@BridgeAR
Copy link
Member

@fhinkel sure, I just removed the labels. I just wanted to make sure it is properly looked at as it was originally controversial if that should be changed or not.

@BridgeAR BridgeAR changed the title DO NOT MERGE: [module] Revert checking for package.json before extensions module: revert checking for package.json before extensions Dec 15, 2017
@BridgeAR
Copy link
Member

Ping @bmeck

@bmeck
Copy link
Member Author

bmeck commented Dec 29, 2017

I can make a better description tomorrow and I think we are approved beyond that.

@jasnell
Copy link
Member

jasnell commented Jan 22, 2018

just double checking... this will land in v10

Should be plenty of time for that assuming it lands by end of March... :-)

@jdalton
Copy link
Member

jdalton commented Jan 22, 2018

Anyone know about the treatment of the trailing /. In some places I see logic to handle \ (for Windows). I'm wondering if it's needed here too.

Update:

Okay confirmed. Windows supports paths ending in \. and \... I think the check should be done to support both or branch per platform as is common elsewhere in Node.

@bmeck
Copy link
Member Author

bmeck commented Jan 22, 2018

@jdalton

I think the check should be done to support both or branch per platform as is common elsewhere in Node.

The surrounding code is only looking at / characters outside of this PR, it looks like \\ has been swapped by the point it gets to that otherwise there would be lots of bugs in module.js.

@jdalton
Copy link
Member

jdalton commented Jan 22, 2018

It looks like it goes

`require` then
`Module.prototype.require` then
`Module._resolveFilename` then
`Module._findPath`, with array from `Module._resolveLookupPaths`, then
// this code

The request isn't normalized through those passes yet.
The bug is likely there with the initial trailingSlash check in Node today too.

Update:

Filed the bug: #18299.

} else if (rc === 1) { // Directory.
if (exts === undefined)
exts = Object.keys(Module._extensions);
filename = tryPackage(basePath, exts, isMain);
}
Copy link
Member

@jdalton jdalton Jan 22, 2018

Choose a reason for hiding this comment

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

👆 at the moment because the trailing slash check is for forward-slash-only, on Windows paths ending in a backslash hit that code path (which is removed in this PR). This means it will now fall through to the tryExtensions call on line 234, instead of the tryPackage call on line 243.

Note: Assumes a fixed #18299

@BridgeAR
Copy link
Member

BridgeAR commented Feb 7, 2018

@BridgeAR
Copy link
Member

BridgeAR commented Feb 9, 2018

@bmeck it seems like there is a lint error and a tests constantly fails:

not ok 1376 parallel/test-require-extensions-same-filename-as-dir
  ---
  duration_ms: 0.171
  severity: fail
  stack: |-
    assert.js:74
      throw new AssertionError(obj);
      ^
    
    AssertionError [ERR_ASSERTION]: undefined strictEqual 'artischocko'
        at Object.<anonymous> (/Users/iojs/build/workspace/node-test-commit-osx/nodes/osx1010/test/parallel/test-require-extensions-same-filename-as-dir.js:31:8)
        at Module._compile (module.js:668:30)
        at Object.Module._extensions..js (module.js:679:10)
        at Module.load (module.js:580:32)
        at tryModuleLoad (module.js:520:12)
        at Function.Module._load (module.js:512:3)
        at Function.Module.runMain (module.js:709:10)
        at startup (bootstrap_node.js:189:16)
        at bootstrap_node.js:699:3

Would you be so kind and take a look? :-)

bmeck and others added 2 commits February 17, 2018 05:54
The documented resolution algorithm was regressed and started to
search for package.json files prior to searching for file extensions
when searching for a specifier. Oddly, it did not search for index
files at same time it searched for package.json. This reverts that
regression and restores the documented behavior of searching for
file extensions prior to searching directories.

Fixes: nodejs#14990
PR-URL: nodejs#15015
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@BridgeAR
Copy link
Member

I just rebased this, fixed the linter error and fixed the test issue. I am a bit confused that the old test is now working as before.

@bmeck can you please have a look at this?

@BridgeAR
Copy link
Member

@bmeck
Copy link
Member Author

bmeck commented Feb 17, 2018

@BridgeAR I'm dealing w/ family, won't get to this in next few days.

@BridgeAR
Copy link
Member

BridgeAR commented Mar 2, 2018

Ping @bmeck. I hope everything is fine on your side :-)

@bmeck
Copy link
Member Author

bmeck commented Mar 2, 2018

@BridgeAR I'm still pretty under water with workload due to things, nothing seems really apparent on why it would fail. I could try to revert and then git bisect if desired but won't get to it for a while.

@BridgeAR
Copy link
Member

BridgeAR commented Mar 2, 2018

@bmeck I fixed the failure in 39f51aa but I wanted you to confirm that this is the right thing. To me it seems like a independent change made it possible to have this change without having a impact on any existing code.

@BridgeAR
Copy link
Member

BridgeAR commented Mar 2, 2018

And I rebased and pushed the changes here. So this is good to go if you give your LG.

@bmeck
Copy link
Member Author

bmeck commented Mar 2, 2018

@BridgeAR that test change looks correct, this PR should be adding trailing / effectively when going up a directory. So it should preserve upwards traversal looking at package.json with that test which uses ../.. for the require specifier. It would have had to have been ../../../module-stub for it to grab module-stub.json instead.

@BridgeAR BridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Mar 2, 2018
BridgeAR pushed a commit to BridgeAR/node that referenced this pull request Mar 11, 2018
The documented resolution algorithm started to search for package.json
files prior to searching for file extensions when searching for a
specifier. Oddly, it did not search for index files at same time it
searched for package.json. This restores the documented behavior of
searching for file extensions prior to searching directories.

PR-URL: nodejs#15015
Fixes: nodejs#14990
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
@BridgeAR
Copy link
Member

Landed in 1ed36ae 🎉

@BridgeAR BridgeAR closed this Mar 11, 2018
MayaLekova pushed a commit to MayaLekova/node that referenced this pull request May 8, 2018
The documented resolution algorithm started to search for package.json
files prior to searching for file extensions when searching for a
specifier. Oddly, it did not search for index files at same time it
searched for package.json. This restores the documented behavior of
searching for file extensions prior to searching directories.

PR-URL: nodejs#15015
Fixes: nodejs#14990
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Khaidi Chu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. module Issues and PRs related to the module subsystem. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.