◐ Shell
clean mode source ↗

module,win: fix long subpath import · nodejs/node@6b53efb

Original file line numberDiff line numberDiff line change

@@ -426,8 +426,16 @@ void BindingData::GetPackageScopeConfig(

426426

url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt);

427427

return;

428428

}

429+

BufferValue file_path_buf(realm->isolate(),

430+

String::NewFromUtf8(realm->isolate(),

431+

file_url->c_str(),

432+

NewStringType::kInternalized,

433+

file_url->size())

434+

.ToLocalChecked());

435+

ToNamespacedPath(realm->env(), &file_path_buf);

429436

error_context.specifier = resolved.ToString();

430-

auto package_json = GetPackageJSON(realm, *file_url, &error_context);

437+

auto package_json =

438+

GetPackageJSON(realm, file_path_buf.ToStringView(), &error_context);

431439

if (package_json != nullptr) {

432440

if constexpr (return_only_type) {

433441

Local<Value> value;

Original file line numberDiff line numberDiff line change

@@ -60,7 +60,7 @@ describe('long path on Windows', () => {

6060

tmpdir.refresh();

6161
6262

fs.mkdirSync(packageDirPath);

63-

fs.writeFileSync(packageJSPath, '');

63+

fs.writeFileSync(packageJSPath, '{}');

6464

fs.writeFileSync(indexJSPath, '');

6565
6666

const packageJsonUrl = pathToFileURL(

@@ -83,7 +83,7 @@ describe('long path on Windows', () => {

8383

tmpdir.refresh();

8484
8585

fs.mkdirSync(packageDirPath);

86-

fs.writeFileSync(packageJSPath, '');

86+

fs.writeFileSync(packageJSPath, '{}');

8787

fs.writeFileSync(indexJSPath, '');

8888
8989

const packageJsonUrl = pathToFileURL(

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');