-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module: fix bad
require.resolve
with option paths for .
and ..
this change fixes `require.resolve` used with the `paths` option not considering `.` and `..` as relative Fixes: #47000 PR-URL: #56735 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
- Loading branch information
1 parent
59b3a8b
commit 7119303
Showing
3 changed files
with
61 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/fixtures/module-require/relative/subdir/relative-subdir.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports.value = 'relative subdir'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fixtures = require('../common/fixtures'); | ||
|
||
if (!common.isMainThread) | ||
Check failure on line 7 in test/parallel/test-require-resolve-opts-paths-relative.js GitHub Actions / test-macOS (macos-14)
Check failure on line 7 in test/parallel/test-require-resolve-opts-paths-relative.js GitHub Actions / test-linux
Check failure on line 7 in test/parallel/test-require-resolve-opts-paths-relative.js GitHub Actions / test-macOS (macos-13)
|
||
common.skip('process.chdir is not available in Workers'); | ||
|
||
const subdir = fixtures.path('module-require', 'relative', 'subdir'); | ||
|
||
process.chdir(subdir); | ||
|
||
// Parent directory paths (`..`) work as intended | ||
{ | ||
assert(require.resolve('.', { paths: ['../'] }).endsWith('index.js')); | ||
assert(require.resolve('./index.js', { paths: ['../'] }).endsWith('index.js')); | ||
|
||
// paths: [".."] should resolve like paths: ["../"] | ||
assert(require.resolve('.', { paths: ['..'] }).endsWith('index.js')); | ||
assert(require.resolve('./index.js', { paths: ['..'] }).endsWith('index.js')); | ||
} | ||
|
||
process.chdir('..'); | ||
|
||
// Current directory paths (`.`) work as intended | ||
{ | ||
assert(require.resolve('.', { paths: ['.'] }).endsWith('index.js')); | ||
assert(require.resolve('./index.js', { paths: ['./'] }).endsWith('index.js')); | ||
|
||
// paths: ["."] should resolve like paths: ["../"] | ||
assert(require.resolve('.', { paths: ['.'] }).endsWith('index.js')); | ||
assert(require.resolve('./index.js', { paths: ['.'] }).endsWith('index.js')); | ||
} | ||
|
||
// Sub directory paths work as intended | ||
{ | ||
// assert.deepStrictEqual(fs.readdirSync('./subdir'), [5]); | ||
assert(require.resolve('./relative-subdir.js', { paths: ['./subdir'] }).endsWith('relative-subdir.js')); | ||
|
||
// paths: ["subdir"] should resolve like paths: ["./subdir"] | ||
assert(require.resolve('./relative-subdir.js', { paths: ['subdir'] }).endsWith('relative-subdir.js')); | ||
} |