Skip to content

Commit 3f6ce39

Browse files
Hakerh400BridgeAR
authored andcommitted
src: fix ESM path resolution on Windows
Windows has some reserved file names such as "con", "prn", "nul", etc. Such files can be accessed only if the path is prefixed with "\\.\" PR-URL: #29574 Reviewed-By: Guy Bedford <[email protected]>
1 parent a7b56a5 commit 3f6ce39

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/module_wrap.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,12 @@ enum DescriptorType {
488488
// Nothing for the "null" cache entries.
489489
inline Maybe<uv_file> OpenDescriptor(const std::string& path) {
490490
uv_fs_t fs_req;
491+
#ifdef _WIN32
492+
std::string pth = "\\\\.\\" + path;
493+
uv_file fd = uv_fs_open(nullptr, &fs_req, pth.c_str(), O_RDONLY, 0, nullptr);
494+
#else
491495
uv_file fd = uv_fs_open(nullptr, &fs_req, path.c_str(), O_RDONLY, 0, nullptr);
496+
#endif
492497
uv_fs_req_cleanup(&fs_req);
493498
if (fd < 0) return Nothing<uv_file>();
494499
return Just(fd);

test/es-module/test-esm-windows.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
// Flags: --experimental-modules
4+
// This test ensures that JavaScript file that includes
5+
// a reserved Windows word can be loaded as ESM module
6+
7+
const common = require('../common');
8+
const tmpdir = require('../common/tmpdir');
9+
const assert = require('assert');
10+
const fs = require('fs').promises;
11+
const path = require('path');
12+
13+
const imp = (file) => {
14+
return import(path.relative(__dirname, file).replace(/\\/g, '/'));
15+
};
16+
17+
(async () => {
18+
const tmp = tmpdir.path;
19+
await fs.mkdir(tmp).catch(() => {});
20+
const rel = (file) => path.join(tmp, file);
21+
22+
{ // Load a single script
23+
const file = rel('con.mjs');
24+
await fs.writeFile(file, 'export default "ok"');
25+
assert.strictEqual((await imp(file)).default, 'ok');
26+
await fs.unlink(file);
27+
}
28+
29+
{ // Load a module
30+
const entry = rel('entry.mjs');
31+
const nmDir = rel('node_modules');
32+
const mDir = rel('node_modules/con');
33+
const pkg = rel('node_modules/con/package.json');
34+
const script = rel('node_modules/con/index.mjs');
35+
36+
await fs.writeFile(entry, 'export {default} from "con"');
37+
await fs.mkdir(nmDir);
38+
await fs.mkdir(mDir);
39+
await fs.writeFile(pkg, '{"main":"index.mjs"}');
40+
await fs.writeFile(script, 'export default "ok"');
41+
42+
assert.strictEqual((await imp(entry)).default, 'ok');
43+
await fs.unlink(script);
44+
await fs.unlink(pkg);
45+
await fs.rmdir(mDir);
46+
await fs.rmdir(nmDir);
47+
await fs.unlink(entry);
48+
}
49+
})().then(common.mustCall());

0 commit comments

Comments
 (0)