|
| 1 | +// Regression test for https://github.com/nodejs/node/issues/62043 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +if (!common.isWindows) { |
| 6 | + common.skip('this test is Windows-specific.'); |
| 7 | +} |
| 8 | + |
| 9 | +const fs = require('fs'); |
| 10 | +const { createRequire } = require('module'); |
| 11 | +const path = require('path'); |
| 12 | +const tmpdir = require('../common/tmpdir'); |
| 13 | + |
| 14 | +tmpdir.refresh(); |
| 15 | + |
| 16 | +const TARGET = 260; // Shortest length that used to trigger the bug |
| 17 | +const fixedLen = tmpdir.path.length + 2 + 'package.json'.length; |
| 18 | +const dirNameLen = Math.max(TARGET - fixedLen, 1); |
| 19 | + |
| 20 | +const dir = path.join(tmpdir.path, 'a'.repeat(dirNameLen)); |
| 21 | +const depDir = path.join(dir, 'node_modules', 'dep'); |
| 22 | +const packageJsonPath = path.join(dir, 'package.json'); |
| 23 | + |
| 24 | +fs.mkdirSync(depDir, { recursive: true }); |
| 25 | +fs.writeFileSync( |
| 26 | + packageJsonPath, |
| 27 | + JSON.stringify({ imports: { '#foo': './foo.mjs' } }), |
| 28 | +); |
| 29 | +fs.writeFileSync(path.join(dir, 'foo.mjs'), 'export default 1;\n'); |
| 30 | +fs.writeFileSync( |
| 31 | + path.join(depDir, 'package.json'), |
| 32 | + JSON.stringify({ name: 'dep', exports: { '.': './index.mjs' } }), |
| 33 | +); |
| 34 | +fs.writeFileSync(path.join(depDir, 'index.mjs'), 'export default 1;\n'); |
| 35 | + |
| 36 | +const req = createRequire(path.join(dir, '_.mjs')); |
| 37 | + |
| 38 | +// Both resolves should succeed without throwing |
| 39 | +req.resolve('dep'); |
| 40 | +req.resolve('#foo'); |
0 commit comments