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

fix require.resolve not considering paths . and .. relative #56735

Conversation

dario-piotrowicz
Copy link
Contributor

Fixes: #47000

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Jan 23, 2025
@dario-piotrowicz dario-piotrowicz force-pushed the dario/require-resolve-opts-paths-dot-dot-fix branch from 9ffc34a to 7e988e5 Compare January 23, 2025 23:31
@dario-piotrowicz dario-piotrowicz changed the title src: fix require.resolve paths option not treating .. as relative fix require.resolve not considering paths . and .. relative Jan 23, 2025
@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. labels Jan 23, 2025
@ljharb
Copy link
Member

ljharb commented Jan 23, 2025

do all of these added tests fail on main, or only a subset of them?

@aduh95
Copy link
Contributor

aduh95 commented Jan 24, 2025

The src: subsystem is reserved for changes in src/. Let's use module: instead

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 24, 2025
@nodejs-github-bot
Copy link
Collaborator

@dario-piotrowicz
Copy link
Contributor Author

dario-piotrowicz commented Jan 24, 2025

do all of these added tests fail on main, or only a subset of them?

only a subset, but I think that the paths option is actually never tested anywhere else (or at least I think?) so I figured that adding some would be beneficial (without going overboard here)

Screenshot 2025-01-24 at 00 07 00

@dario-piotrowicz dario-piotrowicz force-pushed the dario/require-resolve-opts-paths-dot-dot-fix branch from c8424cc to 02cbc98 Compare January 24, 2025 00:09
@ljharb
Copy link
Member

ljharb commented Jan 24, 2025

ah ok, so this is specifically a bug with paths only?

@dario-piotrowicz
Copy link
Contributor Author

The src: subsystem is reserved for changes in src/. Let's use module: instead

sorry, my bad, updated 🫡

@dario-piotrowicz
Copy link
Contributor Author

ah ok, so this is specifically a bug with paths only?

Yes, as far as I can tell that's the case, this for example:

// require(".") should resolve like require("./")
assert.strictEqual(a, b);

does indicate that . is correctly treated as a relative path in standard require calls

@aduh95
Copy link
Contributor

aduh95 commented Jan 24, 2025

May I suggest to not include add early return for perf gain and fix linting in the commit message – I would also suggest to not bother with the Fixes: which will be added by the bot upon landing as long as it's in the PR description.

The commit message needs to be shorter, e.g. module: fix relative path handling in `require.resolve` .

@dario-piotrowicz dario-piotrowicz force-pushed the dario/require-resolve-opts-paths-dot-dot-fix branch from 02cbc98 to 7a97c0b Compare January 24, 2025 00:20
@dario-piotrowicz
Copy link
Contributor Author

May I suggest to not include add early return for perf gain and fix linting in the commit message

Sorry I added them by mistake when I squashed fixup commits, I've already removed them

I would also suggest to not bother with the Fixes: which will be added by the bot upon landing as long as it's in the PR description.

Sorry the contributing MD made me thing I was supposed to add that 😓

The commit message needs to be shorter, e.g. module: fix relative path handling in `require.resolve`.

Yes I did notice the linting error, hopefully my newer title is ok?

@aduh95
Copy link
Contributor

aduh95 commented Jan 24, 2025

Sorry the contributing MD made me thing I was supposed to add that 😓

To be clear, you can 100% do that if you prefer to do it – what's important is for the commit landing on main to contain it, and what I'm saying is you can let the bot add it for you

this change fixes `require.resolve` used with the `paths` option
not considering `.` and `..` as relative

Fixes: nodejs#47000
@dario-piotrowicz dario-piotrowicz force-pushed the dario/require-resolve-opts-paths-dot-dot-fix branch from 7a97c0b to 562e1c6 Compare January 24, 2025 00:29
@dario-piotrowicz
Copy link
Contributor Author

dario-piotrowicz commented Jan 24, 2025

Sorry the contributing MD made me thing I was supposed to add that 😓

To be clear, you can 100% do that if you prefer to do it – what's important is for the commit landing on main to contain it, and what I'm saying is you can let the bot add it for you

Got it 🙂

Maybe I can open a quick PR to add a note to clarify that here?

4. If your patch fixes an open issue, you can add a reference to it at the end
of the log. Use the `Fixes:` prefix and the full issue URL. For other
references use `Refs:`.
Examples:
* `Fixes: https://github.com/nodejs/node/issues/1337`
* `Refs: https://eslint.org/docs/rules/space-in-parens.html`
* `Refs: https://github.com/nodejs/node/pull/3615`

WDYT? (I'm guessing that Fixes is autopopulated by bots but Refs is still on the developer?)

Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com>

This comment was marked as outdated.

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Jan 24, 2025
@jasnell jasnell removed the request-ci Add this label to start a Jenkins CI on a PR. label Jan 24, 2025
@nodejs-github-bot
Copy link
Collaborator

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

Comment on lines +1966 to +1979
/**
* Checks if a path is relative
* @param {string} path the target path
* @returns {boolean} true if the path is relative, false otherwise
*/
function isRelative(path) {
if (StringPrototypeCharCodeAt(path, 0) !== CHAR_DOT) { return false; }

return path.length === 1 || path === '..' ||
StringPrototypeStartsWith(path, './') ||
StringPrototypeStartsWith(path, '../') ||
((isWindows && StringPrototypeStartsWith(path, '.\\')) ||
StringPrototypeStartsWith(path, '..\\'));
}

Choose a reason for hiding this comment

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

🎉

@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue Add this label to land a pull request using GitHub Actions. labels Jan 24, 2025
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 25, 2025
@nodejs-github-bot nodejs-github-bot merged commit 7119303 into nodejs:main Jan 25, 2025
65 checks passed
@nodejs-github-bot
Copy link
Collaborator

Landed in 7119303

@dario-piotrowicz dario-piotrowicz deleted the dario/require-resolve-opts-paths-dot-dot-fix branch January 26, 2025 16:38
aduh95 pushed a commit that referenced this pull request Jan 27, 2025
this change fixes `require.resolve` used with the `paths` option
not considering `.` and `..` as relative

Fixes: #47000
PR-URL: #56735
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
aduh95 pushed a commit that referenced this pull request Jan 30, 2025
this change fixes `require.resolve` used with the `paths` option
not considering `.` and `..` as relative

Fixes: #47000
PR-URL: #56735
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
hvanness pushed a commit to hvanness/node that referenced this pull request Jan 30, 2025
this change fixes `require.resolve` used with the `paths` option
not considering `.` and `..` as relative

Fixes: nodejs#47000
PR-URL: nodejs#56735
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
aduh95 pushed a commit that referenced this pull request Jan 31, 2025
this change fixes `require.resolve` used with the `paths` option
not considering `.` and `..` as relative

Fixes: #47000
PR-URL: #56735
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jordan Harband <ljharb@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
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. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: require.resolve(".", { paths }) behaves differently from require.resolve("./", { paths })
9 participants