From 93dc8cbaf00c4b3b232f41271f506e1a40743760 Mon Sep 17 00:00:00 2001 From: StefanStojanovic Date: Thu, 29 Feb 2024 21:37:42 +0100 Subject: [PATCH] module,win: fix long path resolve Fixes: https://github.com/nodejs/node/issues/50753 --- src/node_modules.cc | 7 ++++ test/es-module/test-GH-50753.js | 42 ++++++++++++++++++++++++ test/es-module/test-esm-invalid-pjson.js | 3 +- test/sequential/test-module-loading.js | 2 +- 4 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/es-module/test-GH-50753.js diff --git a/src/node_modules.cc b/src/node_modules.cc index 9dca1857f96e43..7f75be9a469e5b 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -256,8 +256,15 @@ void BindingData::ReadPackageJSON(const FunctionCallbackInfo& 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; } diff --git a/test/es-module/test-GH-50753.js b/test/es-module/test-GH-50753.js new file mode 100644 index 00000000000000..1cecfa5bebf6a5 --- /dev/null +++ b/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(); diff --git a/test/es-module/test-esm-invalid-pjson.js b/test/es-module/test-esm-invalid-pjson.js index 52cef9ea98b40a..d7e815746645ff 100644 --- a/test/es-module/test-esm-invalid-pjson.js +++ b/test/es-module/test-esm-invalid-pjson.js @@ -3,6 +3,7 @@ const { spawnPromisified } = require('../common'); const fixtures = require('../common/fixtures.js'); const assert = require('node:assert'); +const path = require('node:path'); const { execPath } = require('node:process'); const { describe, it } = require('node:test'); @@ -17,7 +18,7 @@ describe('ESM: Package.json', { concurrency: true }, () => { assert.ok(stderr.includes('code: \'ERR_INVALID_PACKAGE_CONFIG\''), stderr); assert.ok( stderr.includes( - `Invalid package config ${invalidJson} while importing "invalid-pjson" from ${entry}.` + `Invalid package config ${path.toNamespacedPath(invalidJson)} while importing "invalid-pjson" from ${entry}.` ), stderr ); diff --git a/test/sequential/test-module-loading.js b/test/sequential/test-module-loading.js index 1020539827b913..1d30a2ba736eb8 100644 --- a/test/sequential/test-module-loading.js +++ b/test/sequential/test-module-loading.js @@ -110,7 +110,7 @@ assert.strictEqual(require('../fixtures/packages/main-index').ok, 'ok'); common.expectWarning( 'DeprecationWarning', "Invalid 'main' field in '" + - require.resolve('../fixtures/packages/missing-main/package.json') + + path.toNamespacedPath(require.resolve('../fixtures/packages/missing-main/package.json')) + "' of 'doesnotexist.js'. Please either fix that or report it to the" + ' module author', 'DEP0128');