Skip to content

Commit df7de0b

Browse files
ckerrGitHub Copilot
authored andcommitted
test: hide output package.json in node spec runner
Virtual CommonJS files rooted at process.execPath inherit the output directory's type=module package unless the runner hides it alongside Chromium's root package. Ref: nodejs/node#44713 Co-Authored-By: GitHub Copilot <copilot@github.com>
1 parent e4078f4 commit df7de0b

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

‎script/node-spec-runner.js‎

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const args = minimist(process.argv.slice(2), {
1515
const BASE = path.resolve(__dirname, '../..');
1616

1717
const ROOT_PACKAGE_JSON = path.resolve(BASE, 'package.json');
18+
const OUT_PACKAGE_JSON = path.resolve(path.dirname(utils.getAbsoluteElectronExec()), 'package.json');
1819
const NODE_DIR = path.resolve(BASE, 'third_party', 'electron_node');
1920
const JUNIT_DIR = args.jUnitDir ? path.resolve(args.jUnitDir) : null;
2021
const TAP_FILE_NAME = 'test.tap';
@@ -39,36 +40,41 @@ const defaultOptions = [
3940
'-J'
4041
];
4142

42-
// The upstream Node.js test suite assumes there is no package.json above the
43-
// test tree. In Electron, third_party/electron_node lives under Chromium's
44-
// src/, whose package.json ("type": "module") is always an ancestor. That
45-
// changes how Node resolves the module type of test files and fixtures: it
46-
// disables module-syntax detection (breaking e.g.
47-
// test-compile-cache-typescript-esm) and emits MODULE_TYPELESS_PACKAGE_JSON
43+
// The upstream Node.js test suite assumes there is no package.json above its
44+
// test files or virtual files rooted at process.execPath. Electron has
45+
// "type": "module" package files at both Chromium's src/ root and in the
46+
// selected output directory. They change how Node resolves the module type of
47+
// test files and fixtures: they disable module-syntax detection (breaking e.g.
48+
// test-compile-cache-typescript-esm) and emit MODULE_TYPELESS_PACKAGE_JSON
4849
// warnings that break tests asserting clean stderr (e.g. test-esm-detect-
49-
// ambiguous, test-esm-import-meta-main-eval, test-output-coverage-with-mock).
50+
// ambiguous, test-esm-import-meta-main-eval, test-output-coverage-with-mock),
51+
// and make virtual CommonJS files under process.execPath load as ESM.
5052
//
51-
// While the suite runs we move src/package.json aside so the environment
52-
// matches upstream exactly, then restore it when done. The original contents
53-
// are kept in a sibling backup file so an interrupted/killed run self-heals on
54-
// the next invocation rather than leaving src/package.json missing.
55-
const ROOT_PACKAGE_JSON_BACKUP = `${ROOT_PACKAGE_JSON}.spec-runner-backup`;
53+
// While the suite runs we move both package files aside so the environment
54+
// matches upstream exactly, then restore them when done. Original contents are
55+
// kept in sibling backup files so an interrupted run self-heals next time.
56+
const PACKAGE_JSON_PATHS = [ROOT_PACKAGE_JSON, OUT_PACKAGE_JSON];
5657

5758
const stashPackageJson = () => {
58-
// This won't always exist in CI.
59-
if (!fs.existsSync(ROOT_PACKAGE_JSON)) {
60-
return;
59+
for (const packageJson of PACKAGE_JSON_PATHS) {
60+
// These won't always exist in CI.
61+
if (!fs.existsSync(packageJson)) {
62+
continue;
63+
}
64+
fs.copyFileSync(packageJson, `${packageJson}.spec-runner-backup`);
65+
fs.rmSync(packageJson);
6166
}
62-
fs.copyFileSync(ROOT_PACKAGE_JSON, ROOT_PACKAGE_JSON_BACKUP);
63-
fs.rmSync(ROOT_PACKAGE_JSON);
6467
};
6568

6669
const restorePackageJson = () => {
67-
if (!fs.existsSync(ROOT_PACKAGE_JSON_BACKUP)) {
68-
return;
70+
for (const packageJson of PACKAGE_JSON_PATHS) {
71+
const backup = `${packageJson}.spec-runner-backup`;
72+
if (!fs.existsSync(backup)) {
73+
continue;
74+
}
75+
fs.copyFileSync(backup, packageJson);
76+
fs.rmSync(backup);
6977
}
70-
fs.copyFileSync(ROOT_PACKAGE_JSON_BACKUP, ROOT_PACKAGE_JSON);
71-
fs.rmSync(ROOT_PACKAGE_JSON_BACKUP);
7278
};
7379

7480
const getCustomOptions = () => {

0 commit comments

Comments
 (0)