Skip to content

Commit ec2ea14

Browse files
module,win: fix long path resolve
Fixes: #50753
1 parent 7a2a4d0 commit ec2ea14

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/node_file.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2905,6 +2905,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29052905

29062906
for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
29072907
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
2908+
#ifdef _WIN32
2909+
file_path = "\\\\?\\" + file_path;
2910+
#endif
29082911

29092912
switch (FilePathIsFile(env, file_path)) {
29102913
case BindingData::FilePathIsFileReturnType::kIsFile:
@@ -2942,6 +2945,9 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
29422945
i < legacy_main_extensions_package_fallback_end;
29432946
i++) {
29442947
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
2948+
#ifdef _WIN32
2949+
file_path = "\\\\?\\" + file_path;
2950+
#endif
29452951

29462952
switch (FilePathIsFile(env, file_path)) {
29472953
case BindingData::FilePathIsFileReturnType::kIsFile:

src/node_modules.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,13 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
256256
permission::PermissionScope::kFileSystemRead,
257257
path.ToStringView());
258258

259+
#ifdef _WIN32
260+
auto package_json = GetPackageJSON(
261+
realm, "\\\\?\\" + path.ToString(), is_esm ? &error_context : nullptr);
262+
#else
259263
auto package_json =
260264
GetPackageJSON(realm, path.ToString(), is_esm ? &error_context : nullptr);
265+
#endif
261266
if (package_json == nullptr) {
262267
return;
263268
}

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

Lines changed: 42 additions & 0 deletions
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();

0 commit comments

Comments
 (0)