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

module: unify package exports test for CJS and ESM #28831

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/module_wrap.cc
Expand Up @@ -847,8 +847,11 @@ Maybe<URL> PackageExportsResolve(Environment* env,
std::string target(*target_utf8, target_utf8.length());
if (target.back() == '/' && target.substr(0, 2) == "./") {
std::string subpath = pkg_subpath.substr(best_match_str.length());
URL target_url(target + subpath, pjson_url);
return FinalizeResolution(env, target_url, base);
URL url_prefix(target, pjson_url);
URL target_url(subpath, url_prefix);
if (target_url.path().find(url_prefix.path()) == 0) {
guybedford marked this conversation as resolved.
Show resolved Hide resolved
return FinalizeResolution(env, target_url, base);
}
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions test/common/index.mjs
@@ -1,12 +1,7 @@
// Flags: --experimental-modules
/* eslint-disable node-core/require-common-first, node-core/required-modules */

import { createRequireFromPath } from 'module';
import { fileURLToPath as toPath } from 'url';

function createRequire(metaUrl) {
return createRequireFromPath(toPath(metaUrl));
}
import { createRequire } from 'module';
guybedford marked this conversation as resolved.
Show resolved Hide resolved

const require = createRequire(import.meta.url);
const common = require('./index.js');
Expand Down
111 changes: 85 additions & 26 deletions test/es-module/test-esm-exports.mjs
@@ -1,29 +1,88 @@
// Flags: --experimental-modules

import { mustCall } from '../common/index.mjs';
import { ok, strictEqual } from 'assert';

import { asdf, asdf2, space } from '../fixtures/pkgexports.mjs';
import {
loadMissing,
loadFromNumber,
loadDot,
} from '../fixtures/pkgexports-missing.mjs';

strictEqual(asdf, 'asdf');
strictEqual(asdf2, 'asdf');
strictEqual(space, 'encoded path');

loadMissing().catch(mustCall((err) => {
ok(err.message.toString().startsWith('Package exports'));
ok(err.message.toString().indexOf('do not define a \'./missing\' subpath'));
}));

loadFromNumber().catch(mustCall((err) => {
ok(err.message.toString().startsWith('Package exports'));
ok(err.message.toString().indexOf('do not define a \'./missing\' subpath'));
}));

loadDot().catch(mustCall((err) => {
ok(err.message.toString().startsWith('Cannot find main entry point'));
}));
import { ok, deepStrictEqual, strictEqual } from 'assert';

import { requireFixture, importFixture } from '../fixtures/pkgexports.mjs';

[requireFixture, importFixture].forEach((loadFixture) => {
const isRequire = loadFixture === requireFixture;

const validSpecifiers = new Map([
// A simple mapping of a path.
['pkgexports/valid-cjs', { default: 'asdf' }],
// A directory mapping, pointing to the package root.
['pkgexports/sub/asdf.js', { default: 'asdf' }],
// A mapping pointing to a file that needs special encoding (%20) in URLs.
['pkgexports/space', { default: 'encoded path' }],
// Verifying that normal packages still work with exports turned on.
isRequire ? ['baz/index', { default: 'eye catcher' }] : [null],
]);
for (const [validSpecifier, expected] of validSpecifiers) {
if (validSpecifier === null) continue;

loadFixture(validSpecifier)
.then(mustCall((actual) => {
deepStrictEqual({ ...actual }, expected);
}));
}

// There's no such export - so there's nothing to do.
loadFixture('pkgexports/missing').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, 'do not define a \'./missing\' subpath');
}));

// The file exists but isn't exported. The exports is a number which counts
// as a non-null value without any properties, just like `{}`.
loadFixture('pkgexports-number/hidden.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message, 'do not define a \'./hidden.js\' subpath');
}));

// There's no main field so we won't find anything when importing the name.
// The fact that "." is mapped is ignored, it's not a valid main config.
loadFixture('pkgexports').catch(mustCall((err) => {
if (isRequire) {
strictEqual(err.code, 'MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Cannot find module \'pkgexports\'');
} else {
strictEqual(err.code, 'ERR_MODULE_NOT_FOUND');
assertStartsWith(err.message, 'Cannot find main entry point');
}
}));

// Even though 'pkgexports/sub/asdf.js' works, alternate "path-like" variants
// do not to prevent confusion and accidental loopholes.
loadFixture('pkgexports/sub/./../asdf.js').catch(mustCall((err) => {
strictEqual(err.code, 'ERR_PATH_NOT_EXPORTED');
assertStartsWith(err.message, 'Package exports');
assertIncludes(err.message,
'do not define a \'./sub/./../asdf.js\' subpath');
}));

// Covering out bases - not a file is still not a file after dir mapping.
loadFixture('pkgexports/sub/not-a-file.js').catch(mustCall((err) => {
if (isRequire) {
strictEqual(err.code, 'MODULE_NOT_FOUND');
assertStartsWith(err.message,
'Cannot find module \'pkgexports/sub/not-a-file.js\'');
} else {
strictEqual(err.code, 'ERR_MODULE_NOT_FOUND');
// ESM currently returns a full file path
assertStartsWith(err.message, 'Cannot find module');
}
}));
});

function assertStartsWith(actual, expected) {
const start = actual.toString().substr(0, expected.length);
strictEqual(start, expected);
}

function assertIncludes(actual, expected) {
ok(actual.toString().indexOf(expected),
`${JSON.stringify(actual)} includes ${JSON.stringify(expected)}`);
}
1 change: 0 additions & 1 deletion test/fixtures/node_modules/pkgexports/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions test/fixtures/pkgexports-missing.mjs

This file was deleted.

15 changes: 12 additions & 3 deletions test/fixtures/pkgexports.mjs
@@ -1,3 +1,12 @@
export { default as asdf } from 'pkgexports/asdf';
export { default as asdf2 } from 'pkgexports/sub/asdf.js';
export { default as space } from 'pkgexports/space';
import { fileURLToPath } from 'url';
import { createRequire } from 'module';

const rawRequire = createRequire(fileURLToPath(import.meta.url));

export async function requireFixture(specifier) {
return { default: rawRequire(specifier ) };
}

export function importFixture(specifier) {
return import(specifier);
}
1 change: 0 additions & 1 deletion test/message/async_error_sync_esm.out
Expand Up @@ -5,4 +5,3 @@ Error: test
at async three (*fixtures*async-error.js:20:3)
at async four (*fixtures*async-error.js:24:3)
at async main (*message*async_error_sync_esm.mjs:7:5)
(node:*) [DEP0130] DeprecationWarning: Module.createRequireFromPath() is deprecated. Use Module.createRequire() instead.
gengjiawen marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 0 additions & 47 deletions test/parallel/test-module-package-exports.js

This file was deleted.