Skip to content

Commit 93dc8cb

Browse files
module,win: fix long path resolve
Fixes: #50753
1 parent f63e8b7 commit 93dc8cb

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/node_modules.cc

+7
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,15 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
256256
permission::PermissionScope::kFileSystemRead,
257257
path.ToStringView());
258258

259+
// TODO(StefanStojanovic): Remove ifdef after
260+
// path.toNamespacedPath logic is ported to C++
261+
#ifdef _WIN32
262+
auto package_json = GetPackageJSON(
263+
realm, "\\\\?\\" + path.ToString(), is_esm ? &error_context : nullptr);
264+
#else
259265
auto package_json =
260266
GetPackageJSON(realm, path.ToString(), is_esm ? &error_context : nullptr);
267+
#endif
261268
if (package_json == nullptr) {
262269
return;
263270
}

test/es-module/test-GH-50753.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
// Flags: --expose-internals
4+
5+
const common = require('../common');
6+
if (!common.isWindows) {
7+
common.skip('this test is Windows-specific.');
8+
}
9+
10+
const fs = require('fs');
11+
const path = require('path');
12+
const tmpdir = require('../common/tmpdir');
13+
14+
// https://github.com/nodejs/node/issues/50753
15+
// Make a path that is more than 260 chars long.
16+
// Module layout will be the following:
17+
// package.json
18+
// main
19+
// main.js
20+
21+
const packageDirNameLen = Math.max(260 - tmpdir.path.length, 1);
22+
const packageDirName = tmpdir.resolve('x'.repeat(packageDirNameLen));
23+
const packageDirPath = path.resolve(packageDirName);
24+
const packageJsonFilePath = path.join(packageDirPath, 'package.json');
25+
const mainDirName = 'main';
26+
const mainDirPath = path.resolve(packageDirPath, mainDirName);
27+
const mainJsFile = 'main.js';
28+
const mainJsFilePath = path.resolve(mainDirPath, mainJsFile);
29+
30+
tmpdir.refresh();
31+
32+
fs.mkdirSync(packageDirPath);
33+
fs.writeFileSync(
34+
packageJsonFilePath,
35+
`{"main":"${mainDirName}/${mainJsFile}"}`
36+
);
37+
fs.mkdirSync(mainDirPath);
38+
fs.writeFileSync(mainJsFilePath, 'console.log("hello world");');
39+
40+
require('internal/modules/run_main').executeUserEntryPoint(packageDirPath);
41+
42+
tmpdir.refresh();

test/es-module/test-esm-invalid-pjson.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const { spawnPromisified } = require('../common');
44
const fixtures = require('../common/fixtures.js');
55
const assert = require('node:assert');
6+
const path = require('node:path');
67
const { execPath } = require('node:process');
78
const { describe, it } = require('node:test');
89

@@ -17,7 +18,7 @@ describe('ESM: Package.json', { concurrency: true }, () => {
1718
assert.ok(stderr.includes('code: \'ERR_INVALID_PACKAGE_CONFIG\''), stderr);
1819
assert.ok(
1920
stderr.includes(
20-
`Invalid package config ${invalidJson} while importing "invalid-pjson" from ${entry}.`
21+
`Invalid package config ${path.toNamespacedPath(invalidJson)} while importing "invalid-pjson" from ${entry}.`
2122
),
2223
stderr
2324
);

test/sequential/test-module-loading.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok');
110110
common.expectWarning(
111111
'DeprecationWarning',
112112
"Invalid 'main' field in '" +
113-
require.resolve('../fixtures/packages/missing-main/package.json') +
113+
path.toNamespacedPath(require.resolve('../fixtures/packages/missing-main/package.json')) +
114114
"' of 'doesnotexist.js'. Please either fix that or report it to the" +
115115
' module author',
116116
'DEP0128');

0 commit comments

Comments
 (0)