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 <guybedford@gmail.com>
1 parent a7b56a5 commit 3f6ce39

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

‎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
returnJust(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+
constcommon=require('../common');
8+
consttmpdir=require('../common/tmpdir');
9+
constassert=require('assert');
10+
constfs=require('fs').promises;
11+
constpath=require('path');
12+
13+
constimp=(file)=>{
14+
returnimport(path.relative(__dirname,file).replace(/\\/g,'/'));
15+
};
16+
17+
(async()=>{
18+
consttmp=tmpdir.path;
19+
awaitfs.mkdir(tmp).catch(()=>{});
20+
constrel=(file)=>path.join(tmp,file);
21+
22+
{// Load a single script
23+
constfile=rel('con.mjs');
24+
awaitfs.writeFile(file,'export default "ok"');
25+
assert.strictEqual((awaitimp(file)).default,'ok');
26+
awaitfs.unlink(file);
27+
}
28+
29+
{// Load a module
30+
constentry=rel('entry.mjs');
31+
constnmDir=rel('node_modules');
32+
constmDir=rel('node_modules/con');
33+
constpkg=rel('node_modules/con/package.json');
34+
constscript=rel('node_modules/con/index.mjs');
35+
36+
awaitfs.writeFile(entry,'export {default} from "con"');
37+
awaitfs.mkdir(nmDir);
38+
awaitfs.mkdir(mDir);
39+
awaitfs.writeFile(pkg,'{"main":"index.mjs"}');
40+
awaitfs.writeFile(script,'export default "ok"');
41+
42+
assert.strictEqual((awaitimp(entry)).default,'ok');
43+
awaitfs.unlink(script);
44+
awaitfs.unlink(pkg);
45+
awaitfs.rmdir(mDir);
46+
awaitfs.rmdir(nmDir);
47+
awaitfs.unlink(entry);
48+
}
49+
})().then(common.mustCall());

0 commit comments

Comments
 (0)