Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: fix ESM path resolution on Windows #29574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,12 @@ enum DescriptorType {
// Nothing for the "null" cache entries.
inline Maybe<uv_file> OpenDescriptor(const std::string& path) {
uv_fs_t fs_req;
#ifdef _WIN32
std::string pth = "\\\\.\\" + path;
uv_file fd = uv_fs_open(nullptr, &fs_req, pth.c_str(), O_RDONLY, 0, nullptr);
#else
uv_file fd = uv_fs_open(nullptr, &fs_req, path.c_str(), O_RDONLY, 0, nullptr);
#endif
uv_fs_req_cleanup(&fs_req);
if (fd < 0) return Nothing<uv_file>();
return Just(fd);
Expand Down
49 changes: 49 additions & 0 deletions test/es-module/test-esm-windows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

// Flags: --experimental-modules
// This test ensures that JavaScript file that includes
// a reserved Windows word can be loaded as ESM module

const common = require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const fs = require('fs').promises;
const path = require('path');

const imp = (file) => {
return import(path.relative(__dirname, file).replace(/\\/g, '/'));
};

(async () => {
const tmp = tmpdir.path;
await fs.mkdir(tmp).catch(() => {});
const rel = (file) => path.join(tmp, file);

{ // Load a single script
const file = rel('con.mjs');
await fs.writeFile(file, 'export default "ok"');
assert.strictEqual((await imp(file)).default, 'ok');
await fs.unlink(file);
}

{ // Load a module
const entry = rel('entry.mjs');
const nmDir = rel('node_modules');
const mDir = rel('node_modules/con');
const pkg = rel('node_modules/con/package.json');
const script = rel('node_modules/con/index.mjs');

await fs.writeFile(entry, 'export {default} from "con"');
await fs.mkdir(nmDir);
await fs.mkdir(mDir);
await fs.writeFile(pkg, '{"main":"index.mjs"}');
await fs.writeFile(script, 'export default "ok"');

assert.strictEqual((await imp(entry)).default, 'ok');
await fs.unlink(script);
await fs.unlink(pkg);
await fs.rmdir(mDir);
await fs.rmdir(nmDir);
await fs.unlink(entry);
}
})().then(common.mustCall());