Skip to content

Commit

Permalink
module,win: fix long path resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanStojanovic committed Feb 9, 2024
1 parent 8a41d9b commit e6da803
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/node_file.cc
Expand Up @@ -2922,6 +2922,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {

for (int i = 0; i < legacy_main_extensions_with_main_end; i++) {
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
// TODO(StefanStojanovic): Remove ifdef after path.toNamespacedPath logic is ported to C++
#ifdef _WIN32
file_path = "\\\\?\\" + file_path;
#endif

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down Expand Up @@ -2959,6 +2963,10 @@ void BindingData::LegacyMainResolve(const FunctionCallbackInfo<Value>& args) {
i < legacy_main_extensions_package_fallback_end;
i++) {
file_path = *initial_file_path + std::string(legacy_main_extensions[i]);
// TODO(StefanStojanovic): Remove ifdef after path.toNamespacedPath logic is ported to C++
#ifdef _WIN32
file_path = "\\\\?\\" + file_path;
#endif

switch (FilePathIsFile(env, file_path)) {
case BindingData::FilePathIsFileReturnType::kIsFile:
Expand Down
6 changes: 6 additions & 0 deletions src/node_modules.cc
Expand Up @@ -256,8 +256,14 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo<Value>& args) {
permission::PermissionScope::kFileSystemRead,
path.ToStringView());

// TODO(StefanStojanovic): Remove ifdef after path.toNamespacedPath logic is ported to C++
#ifdef _WIN32
auto package_json = GetPackageJSON(
realm, "\\\\?\\" + path.ToString(), is_esm ? &error_context : nullptr);
#else
auto package_json =
GetPackageJSON(realm, path.ToString(), is_esm ? &error_context : nullptr);
#endif
if (package_json == nullptr) {
return;
}
Expand Down
42 changes: 42 additions & 0 deletions test/es-module/test-GH-50753.js
@@ -0,0 +1,42 @@
'use strict';

// Flags: --expose-internals

const common = require('../common');
if (!common.isWindows) {
common.skip('this test is Windows-specific.');
}

const fs = require('fs');
const path = require('path');
const tmpdir = require('../common/tmpdir');

// https://github.com/nodejs/node/issues/50753
// Make a path that is more than 260 chars long.
// Module layout will be the following:
// package.json
// main
// main.js

const packageDirNameLen = Math.max(260 - tmpdir.path.length, 1);
const packageDirName = tmpdir.resolve('x'.repeat(packageDirNameLen));
const packageDirPath = path.resolve(packageDirName);
const packageJsonFilePath = path.join(packageDirPath, 'package.json');
const mainDirName = 'main';
const mainDirPath = path.resolve(packageDirPath, mainDirName);
const mainJsFile = 'main.js';
const mainJsFilePath = path.resolve(mainDirPath, mainJsFile);

tmpdir.refresh();

fs.mkdirSync(packageDirPath);
fs.writeFileSync(
packageJsonFilePath,
`{"main":"${mainDirName}/${mainJsFile}"}`
);
fs.mkdirSync(mainDirPath);
fs.writeFileSync(mainJsFilePath, 'console.log("hello world");');

require('internal/modules/run_main').executeUserEntryPoint(packageDirPath);

tmpdir.refresh();

0 comments on commit e6da803

Please sign in to comment.