From 8224e35c85f20a97ef6c24860dcaa37f7f980d2e Mon Sep 17 00:00:00 2001 From: StefanStojanovic Date: Wed, 4 Mar 2026 14:55:31 +0100 Subject: [PATCH 1/3] module,win: fix long subpath import Fixes: https://github.com/nodejs/node/issues/62043 --- src/node_modules.cc | 10 ++++- .../test-module-subpath-import-long-path.js | 40 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-module-subpath-import-long-path.js diff --git a/src/node_modules.cc b/src/node_modules.cc index 7d8e24f915be95..7633c3616e1ba8 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -425,8 +425,16 @@ void BindingData::GetPackageScopeConfig( url::ThrowInvalidURL(realm->env(), resolved.ToStringView(), std::nullopt); return; } + BufferValue file_path_buf(realm->isolate(), + String::NewFromUtf8(realm->isolate(), + file_url->c_str(), + NewStringType::kNormal, + file_url->size()) + .ToLocalChecked()); + ToNamespacedPath(realm->env(), &file_path_buf); error_context.specifier = resolved.ToString(); - auto package_json = GetPackageJSON(realm, *file_url, &error_context); + auto package_json = + GetPackageJSON(realm, file_path_buf.ToStringView(), &error_context); if (package_json != nullptr) { if constexpr (return_only_type) { Local value; diff --git a/test/parallel/test-module-subpath-import-long-path.js b/test/parallel/test-module-subpath-import-long-path.js new file mode 100644 index 00000000000000..69207746f77062 --- /dev/null +++ b/test/parallel/test-module-subpath-import-long-path.js @@ -0,0 +1,40 @@ +// Regression test for https://github.com/nodejs/node/issues/62043 +'use strict'; + +const common = require('../common'); +if (!common.isWindows) { + common.skip('this test is Windows-specific.'); +} + +const fs = require('fs'); +const { createRequire } = require('module'); +const path = require('path'); +const tmpdir = require('../common/tmpdir'); + +tmpdir.refresh(); + +const TARGET = 260; // Shortest length that used to trigger the bug +const fixedLen = tmpdir.path.length + 2 + 'package.json'.length; +const dirNameLen = Math.max(TARGET - fixedLen, 1); + +const dir = path.join(tmpdir.path, 'a'.repeat(dirNameLen)); +const depDir = path.join(dir, 'node_modules', 'dep'); +const packageJsonPath = path.join(dir, 'package.json'); + +fs.mkdirSync(depDir, { recursive: true }); +fs.writeFileSync( + packageJsonPath, + JSON.stringify({ imports: { '#foo': './foo.mjs' } }), +); +fs.writeFileSync(path.join(dir, 'foo.mjs'), 'export default 1;\n'); +fs.writeFileSync( + path.join(depDir, 'package.json'), + JSON.stringify({ name: 'dep', exports: { '.': './index.mjs' } }), +); +fs.writeFileSync(path.join(depDir, 'index.mjs'), 'export default 1;\n'); + +const req = createRequire(path.join(dir, '_.mjs')); + +// Both resolves should succeed without throwing +req.resolve('dep'); +req.resolve('#foo'); From dad3c5a3ec39481184be4e1e1308d5d6ce5e5ac6 Mon Sep 17 00:00:00 2001 From: Stefan Stojanovic Date: Tue, 10 Mar 2026 14:44:10 +0100 Subject: [PATCH 2/3] Update src/node_modules.cc Co-authored-by: Yagiz Nizipli --- src/node_modules.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node_modules.cc b/src/node_modules.cc index 7633c3616e1ba8..ad1e001ae0b68c 100644 --- a/src/node_modules.cc +++ b/src/node_modules.cc @@ -428,7 +428,7 @@ void BindingData::GetPackageScopeConfig( BufferValue file_path_buf(realm->isolate(), String::NewFromUtf8(realm->isolate(), file_url->c_str(), - NewStringType::kNormal, + NewStringType::kInternalized, file_url->size()) .ToLocalChecked()); ToNamespacedPath(realm->env(), &file_path_buf); From 8bc3929fc96ced2ccc702589360e14a82180c308 Mon Sep 17 00:00:00 2001 From: StefanStojanovic Date: Thu, 2 Apr 2026 10:58:17 +0200 Subject: [PATCH 3/3] test: fix failing esm long path test --- test/es-module/test-esm-long-path-win.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/es-module/test-esm-long-path-win.js b/test/es-module/test-esm-long-path-win.js index fca22172a85995..d125d341f09202 100644 --- a/test/es-module/test-esm-long-path-win.js +++ b/test/es-module/test-esm-long-path-win.js @@ -60,7 +60,7 @@ describe('long path on Windows', () => { tmpdir.refresh(); fs.mkdirSync(packageDirPath); - fs.writeFileSync(packageJSPath, ''); + fs.writeFileSync(packageJSPath, '{}'); fs.writeFileSync(indexJSPath, ''); const packageJsonUrl = pathToFileURL( @@ -83,7 +83,7 @@ describe('long path on Windows', () => { tmpdir.refresh(); fs.mkdirSync(packageDirPath); - fs.writeFileSync(packageJSPath, ''); + fs.writeFileSync(packageJSPath, '{}'); fs.writeFileSync(indexJSPath, ''); const packageJsonUrl = pathToFileURL(