Skip to content

Commit 7d5fd90

Browse files
authored
fix: incorrect loading of ESM based configurations (#32515)
* add regression test * fix: make sure esm configs are indeed loaded as ESM
1 parent a4b03b4 commit 7d5fd90

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ _Released 9/23/2025 (PENDING)_
66
**Bugfixes:**
77

88
- In development mode, Electron `stderr` is piped directly to Cypress' `stderr` to make it clear why Electron failed to start, if it fails to start. Fixes [#32358](https://github.com/cypress-io/cypress/issues/32358). Addressed in [32468](https://github.com/cypress-io/cypress/pull/32468).
9+
- Fixed an issue where ESM Cypress configurations were not being interpreted correctly. Fixes [#32493](https://github.com/cypress-io/cypress/issues/32493). Fixed in [#32515](https://github.com/cypress-io/cypress/pull/32515).
910

1011
**Misc:**
1112

packages/server/lib/plugins/child/run_require_async_child.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,29 +83,37 @@ function run (ipc, file, projectRoot) {
8383
// Config file loading of modules is tested within
8484
// system-tests/projects/config-cjs-and-esm/*
8585
const loadFile = async (file) => {
86+
let errorLoadingCJSConfig = null
87+
8688
try {
8789
debug('Loading file %s', file)
8890

8991
return require(file)
9092
} catch (err) {
9193
if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
92-
throw err
94+
errorLoadingCJSConfig = err
9395
}
9496
}
9597

96-
debug('User is loading an ESM config file')
98+
debug('User may be trying to load an ESM config file')
9799

98100
try {
99101
// We cannot replace the initial `require` with `await import` because
100102
// Certain modules cannot be dynamically imported.
101103
// pathToFileURL for windows interop: https://github.com/nodejs/node/issues/31710
102104
const fileURL = pathToFileURL(file).href
103105

104-
debug(`importing esm file %s`, fileURL)
106+
debug(`importing config as esm file %s`, fileURL)
107+
const config = await import(fileURL)
105108

106-
return await import(fileURL)
109+
return config
107110
} catch (err) {
108111
debug('error loading file via native Node.js module loader %s', err.message)
112+
if (errorLoadingCJSConfig) {
113+
debug('CJS loading initially failed. Rethrowing %s', errorLoadingCJSConfig.message)
114+
throw errorLoadingCJSConfig
115+
}
116+
109117
throw err
110118
}
111119
}

system-tests/projects/config-cjs-and-esm/config-with-mjs/cypress.config.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import { defineConfig } from 'cypress'
2+
import { resolve } from 'node:path'
3+
4+
const root = import.meta.dirname
5+
6+
// should fail if not loading ESM correctly
7+
resolve(root, 'dist')
28

39
export default defineConfig({
410
e2e: { supportFile: false },
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import systemTests from '../lib/system-tests'
2+
3+
describe('e2e issue 32493: ESM config loading should not fail', () => {
4+
systemTests.setup()
5+
6+
systemTests.it('loads the config file', {
7+
spec: 'app.cy.js',
8+
browser: 'chrome',
9+
project: 'config-cjs-and-esm/config-with-mjs',
10+
expectedExitCode: 0,
11+
})
12+
})

0 commit comments

Comments
 (0)