|
| 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 | +const { describe, it } = require('node:test'); |
| 14 | +const { legacyMainResolve } = require('node:internal/modules/esm/resolve'); |
| 15 | +const { pathToFileURL } = require('node:url'); |
| 16 | +const { spawnPromisified } = require('../common'); |
| 17 | +const assert = require('assert'); |
| 18 | +const { execPath } = require('node:process'); |
| 19 | + |
| 20 | +describe('long path on Windows', () => { |
| 21 | + it('check long path in ReadPackageJSON', () => { |
| 22 | + // Module layout will be the following: |
| 23 | + // package.json |
| 24 | + // main |
| 25 | + // main.js |
| 26 | + const packageDirNameLen = Math.max(260 - tmpdir.path.length, 1); |
| 27 | + const packageDirName = tmpdir.resolve('x'.repeat(packageDirNameLen)); |
| 28 | + const packageDirPath = path.resolve(packageDirName); |
| 29 | + const packageJsonFilePath = path.join(packageDirPath, 'package.json'); |
| 30 | + const mainDirName = 'main'; |
| 31 | + const mainDirPath = path.resolve(packageDirPath, mainDirName); |
| 32 | + const mainJsFile = 'main.js'; |
| 33 | + const mainJsFilePath = path.resolve(mainDirPath, mainJsFile); |
| 34 | + |
| 35 | + tmpdir.refresh(); |
| 36 | + |
| 37 | + fs.mkdirSync(packageDirPath); |
| 38 | + fs.writeFileSync( |
| 39 | + packageJsonFilePath, |
| 40 | + `{"main":"${mainDirName}/${mainJsFile}"}` |
| 41 | + ); |
| 42 | + fs.mkdirSync(mainDirPath); |
| 43 | + fs.writeFileSync(mainJsFilePath, 'console.log("hello world");'); |
| 44 | + |
| 45 | + require('internal/modules/run_main').executeUserEntryPoint(packageDirPath); |
| 46 | + |
| 47 | + tmpdir.refresh(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('check long path in LegacyMainResolve - 1', () => { |
| 51 | + // Module layout will be the following: |
| 52 | + // package.json |
| 53 | + // index.js |
| 54 | + const packageDirNameLen = Math.max(260 - tmpdir.path.length, 1); |
| 55 | + const packageDirPath = tmpdir.resolve('y'.repeat(packageDirNameLen)); |
| 56 | + const packageJSPath = path.join(packageDirPath, 'package.json'); |
| 57 | + const indexJSPath = path.join(packageDirPath, 'index.js'); |
| 58 | + const packageConfig = { }; |
| 59 | + |
| 60 | + tmpdir.refresh(); |
| 61 | + |
| 62 | + fs.mkdirSync(packageDirPath); |
| 63 | + fs.writeFileSync(packageJSPath, ''); |
| 64 | + fs.writeFileSync(indexJSPath, ''); |
| 65 | + |
| 66 | + const packageJsonUrl = pathToFileURL( |
| 67 | + path.resolve(packageJSPath) |
| 68 | + ); |
| 69 | + |
| 70 | + console.log(legacyMainResolve(packageJsonUrl, packageConfig, '')); |
| 71 | + }); |
| 72 | + |
| 73 | + it('check long path in LegacyMainResolve - 2', () => { |
| 74 | + // Module layout will be the following: |
| 75 | + // package.json |
| 76 | + // main.js |
| 77 | + const packageDirNameLen = Math.max(260 - tmpdir.path.length, 1); |
| 78 | + const packageDirPath = tmpdir.resolve('z'.repeat(packageDirNameLen)); |
| 79 | + const packageJSPath = path.join(packageDirPath, 'package.json'); |
| 80 | + const indexJSPath = path.join(packageDirPath, 'main.js'); |
| 81 | + const packageConfig = { main: 'main.js' }; |
| 82 | + |
| 83 | + tmpdir.refresh(); |
| 84 | + |
| 85 | + fs.mkdirSync(packageDirPath); |
| 86 | + fs.writeFileSync(packageJSPath, ''); |
| 87 | + fs.writeFileSync(indexJSPath, ''); |
| 88 | + |
| 89 | + const packageJsonUrl = pathToFileURL( |
| 90 | + path.resolve(packageJSPath) |
| 91 | + ); |
| 92 | + |
| 93 | + console.log(legacyMainResolve(packageJsonUrl, packageConfig, '')); |
| 94 | + }); |
| 95 | + |
| 96 | + it('check long path in GetNearestParentPackageJSON', async () => { |
| 97 | + // Module layout will be the following: |
| 98 | + // node_modules |
| 99 | + // test-module |
| 100 | + // package.json (path is less than 260 chars long) |
| 101 | + // cjs |
| 102 | + // package.json (path is more than 260 chars long) |
| 103 | + // index.js |
| 104 | + const testModuleDirPath = 'node_modules/test-module'; |
| 105 | + let cjsDirPath = path.join(testModuleDirPath, 'cjs'); |
| 106 | + let modulePackageJSPath = path.join(testModuleDirPath, 'package.json'); |
| 107 | + let cjsPackageJSPath = path.join(cjsDirPath, 'package.json'); |
| 108 | + let cjsIndexJSPath = path.join(cjsDirPath, 'index.js'); |
| 109 | + |
| 110 | + const tmpDirNameLen = Math.max( |
| 111 | + 260 - tmpdir.path.length - cjsPackageJSPath.length, 1); |
| 112 | + const tmpDirPath = tmpdir.resolve('k'.repeat(tmpDirNameLen)); |
| 113 | + |
| 114 | + cjsDirPath = path.join(tmpDirPath, cjsDirPath); |
| 115 | + modulePackageJSPath = path.join(tmpDirPath, modulePackageJSPath); |
| 116 | + cjsPackageJSPath = path.join(tmpDirPath, cjsPackageJSPath); |
| 117 | + cjsIndexJSPath = path.join(tmpDirPath, cjsIndexJSPath); |
| 118 | + |
| 119 | + tmpdir.refresh(); |
| 120 | + |
| 121 | + fs.mkdirSync(cjsDirPath, { recursive: true }); |
| 122 | + fs.writeFileSync( |
| 123 | + modulePackageJSPath, |
| 124 | + `{ |
| 125 | + "type": "module" |
| 126 | + }` |
| 127 | + ); |
| 128 | + fs.writeFileSync( |
| 129 | + cjsPackageJSPath, |
| 130 | + `{ |
| 131 | + "type": "commonjs" |
| 132 | + }` |
| 133 | + ); |
| 134 | + fs.writeFileSync( |
| 135 | + cjsIndexJSPath, |
| 136 | + 'const fs = require("fs");' |
| 137 | + ); |
| 138 | + const { code, signal, stderr } = await spawnPromisified(execPath, [cjsIndexJSPath]); |
| 139 | + assert.strictEqual(stderr.trim(), ''); |
| 140 | + assert.strictEqual(code, 0); |
| 141 | + assert.strictEqual(signal, null); |
| 142 | + }); |
| 143 | + |
| 144 | + it('check long path in GetNearestParentPackageJSONType', async () => { |
| 145 | + // Module layout will be the following: |
| 146 | + // node_modules |
| 147 | + // test-module |
| 148 | + // package.json (path is less than 260 chars long) |
| 149 | + // cjs |
| 150 | + // package.json (path is more than 260 chars long) |
| 151 | + // index.js |
| 152 | + const testModuleDirPath = 'node_modules/test-module'; |
| 153 | + let cjsDirPath = path.join(testModuleDirPath, 'cjs'); |
| 154 | + let modulePackageJSPath = path.join(testModuleDirPath, 'package.json'); |
| 155 | + let cjsPackageJSPath = path.join(cjsDirPath, 'package.json'); |
| 156 | + let cjsIndexJSPath = path.join(cjsDirPath, 'index.js'); |
| 157 | + |
| 158 | + const tmpDirNameLen = Math.max(260 - tmpdir.path.length - cjsPackageJSPath.length, 1); |
| 159 | + const tmpDirPath = tmpdir.resolve('l'.repeat(tmpDirNameLen)); |
| 160 | + |
| 161 | + cjsDirPath = path.join(tmpDirPath, cjsDirPath); |
| 162 | + modulePackageJSPath = path.join(tmpDirPath, modulePackageJSPath); |
| 163 | + cjsPackageJSPath = path.join(tmpDirPath, cjsPackageJSPath); |
| 164 | + cjsIndexJSPath = path.join(tmpDirPath, cjsIndexJSPath); |
| 165 | + |
| 166 | + tmpdir.refresh(); |
| 167 | + |
| 168 | + fs.mkdirSync(cjsDirPath, { recursive: true }); |
| 169 | + fs.writeFileSync( |
| 170 | + modulePackageJSPath, |
| 171 | + `{ |
| 172 | + "type": "module" |
| 173 | + }` |
| 174 | + ); |
| 175 | + fs.writeFileSync( |
| 176 | + cjsPackageJSPath, |
| 177 | + `{ |
| 178 | + "type": "commonjs" |
| 179 | + }` |
| 180 | + ); |
| 181 | + |
| 182 | + fs.writeFileSync(cjsIndexJSPath, 'import fs from "node:fs/promises";'); |
| 183 | + const { code, signal, stderr } = await spawnPromisified(execPath, [cjsIndexJSPath]); |
| 184 | + |
| 185 | + assert.ok(stderr.includes('Warning: To load an ES module')); |
| 186 | + assert.strictEqual(code, 1); |
| 187 | + assert.strictEqual(signal, null); |
| 188 | + }); |
| 189 | +}); |
0 commit comments