Skip to content

Commit 4c3bc3d

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

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-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 =
437+
GetPackageJSON(realm, file_path_buf.ToStringView(), &error_context);
430438
if (package_json != nullptr) {
431439
if constexpr (return_only_type) {
432440
Local<Value> value;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 fs = require('fs');
10+
const { createRequire } = require('module');
11+
const path = require('path');
12+
const tmpdir = require('../common/tmpdir');
13+
14+
tmpdir.refresh();
15+
16+
const TARGET = 260; // Shortest length that used to trigger the bug
17+
const fixedLen = tmpdir.path.length + 2 + 'package.json'.length;
18+
const dirNameLen = Math.max(TARGET - fixedLen, 1);
19+
20+
const dir = path.join(tmpdir.path, 'a'.repeat(dirNameLen));
21+
const depDir = path.join(dir, 'node_modules', 'dep');
22+
const packageJsonPath = path.join(dir, 'package.json');
23+
24+
fs.mkdirSync(depDir, { recursive: true });
25+
fs.writeFileSync(
26+
packageJsonPath,
27+
JSON.stringify({ imports: { '#foo': './foo.mjs' } }),
28+
);
29+
fs.writeFileSync(path.join(dir, 'foo.mjs'), 'export default 1;\n');
30+
fs.writeFileSync(
31+
path.join(depDir, 'package.json'),
32+
JSON.stringify({ name: 'dep', exports: { '.': './index.mjs' } }),
33+
);
34+
fs.writeFileSync(path.join(depDir, 'index.mjs'), 'export default 1;\n');
35+
36+
const req = createRequire(path.join(dir, '_.mjs'));
37+
38+
// Both resolves should succeed without throwing
39+
req.resolve('dep');
40+
req.resolve('#foo');

0 commit comments

Comments
 (0)