Skip to content

Commit f6f2533

Browse files
module,win: fix long subpath import
Fixes: #62043
1 parent 330e3ee commit f6f2533

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/node_modules.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,16 @@ void BindingData::GetPackageScopeConfig(
425425
url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);
426426
return;
427427
}
428+
BufferValue file_path_buf(realm->isolate(),
429+
String::NewFromUtf8(realm->isolate(),
430+
file_url->c_str(),
431+
NewStringType::kNormal,
432+
file_url->size())
433+
.ToLocalChecked());
434+
ToNamespacedPath(realm->env(), &file_path_buf);
428435
error_context.specifier = resolved.ToString();
429-
auto package_json = GetPackageJSON(realm, *file_url, &error_context);
436+
auto package_json = GetPackageJSON(
437+
realm, file_path_buf.ToStringView(), &error_context);
430438
if (package_json != nullptr) {
431439
if constexpr (return_only_type) {
432440
Local<Value> value;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Regression test for https://github.com/nodejs/node/issues/62043
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.isWindows) {
6+
common.skip('this test is Windows-specific.');
7+
}
8+
9+
const assert = require('assert');
10+
const fs = require('fs');
11+
const { createRequire } = require('module');
12+
const path = require('path');
13+
const tmpdir = require('../common/tmpdir');
14+
15+
tmpdir.refresh();
16+
17+
const TARGET = 260; // shortest length that used to trigger the bug
18+
const fixedLen = tmpdir.path.length + 2 + 'package.json'.length;
19+
const dirNameLen = Math.max(TARGET - fixedLen, 1);
20+
21+
const dir = path.join(tmpdir.path, 'a'.repeat(dirNameLen));
22+
const depDir = path.join(dir, 'node_modules', 'dep');
23+
const packageJsonPath = path.join(dir, 'package.json');
24+
25+
fs.mkdirSync(depDir, { recursive: true });
26+
fs.writeFileSync(
27+
packageJsonPath,
28+
JSON.stringify({ imports: { '#foo': './foo.mjs' } }),
29+
);
30+
fs.writeFileSync(path.join(dir, 'foo.mjs'), 'export default 1;\n');
31+
fs.writeFileSync(
32+
path.join(depDir, 'package.json'),
33+
JSON.stringify({ name: 'dep', exports: { '.': './index.mjs' } }),
34+
);
35+
fs.writeFileSync(path.join(depDir, 'index.mjs'), 'export default 1;\n');
36+
37+
const req = createRequire(path.join(dir, '_.mjs'));
38+
39+
assert.doesNotThrow(
40+
() => req.resolve('dep'),
41+
'resolve("dep") should succeed with a long package.json path',
42+
);
43+
44+
assert.doesNotThrow(
45+
() => req.resolve('#foo'),
46+
'resolve("#foo") should succeed with a long package.json path',
47+
);

0 commit comments

Comments
 (0)