Skip to content

Commit b83bdc2

Browse files
authored
fix: #22038 support esm import for windows (#22042)
* fix: #22038 better windows ESM interop * updates from @flotwig feedback
1 parent a0a64cc commit b83bdc2

File tree

4 files changed

+12
-22
lines changed

4 files changed

+12
-22
lines changed

packages/launchpad/cypress/e2e/scaffold-project.cy.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ function scaffoldAndOpenE2EProject (opts: {
4444
cy.contains('E2E Testing').click()
4545
cy.contains('We added the following files to your project:')
4646
cy.contains('Continue').click()
47+
// Going through the loading of config
48+
cy.get('[data-cy="loading-spinner"]')
49+
cy.get('[data-cy="loading-spinner"]').should('not.exist')
50+
// No errrors were encountered
51+
cy.get('[data-testid="error-header"]').should('not.exist')
52+
// Asserts that we've made it through the flow
53+
cy.contains('Choose a Browser')
4754
}
4855

4956
function scaffoldAndOpenCTProject (opts: {

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require('graceful-fs').gracefulify(require('fs'))
22
const stripAnsi = require('strip-ansi')
33
const debug = require('debug')(`cypress:lifecycle:child:run_require_async_child:${process.pid}`)
4+
const { pathToFileURL } = require('url')
45
const tsNodeUtil = require('./ts_node')
56
const util = require('../util')
67
const { RunPlugins } = require('./run_plugins')
@@ -97,12 +98,10 @@ function run (ipc, file, projectRoot) {
9798
// 3a. Yes: Use bundleRequire
9899
// 3b. No: Continue through to `await import(configFile)`
99100
// 4. Use node's dynamic import to import the configFile
100-
let originalError
101101

102102
try {
103103
return require(file)
104104
} catch (err) {
105-
originalError = err
106105
if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
107106
throw err
108107
}
@@ -124,16 +123,10 @@ function run (ipc, file, projectRoot) {
124123
debug(`User doesn't have esbuild. Going to use native node imports.`)
125124

126125
// We cannot replace the initial `require` with `await import` because
127-
// Certain modules cannot be dynamically imported. If this throws, however, we want
128-
// to show the original error that was thrown, because that's ultimately the source of the problem
129-
try {
130-
return await import(file)
131-
} catch (e) {
132-
// If we aren't able to import the file at all, throw the original error, since that has more accurate information
133-
// of what failed to begin with
134-
debug('esbuild fallback for loading config failed, throwing original error. node import error: %o', e)
135-
throw originalError
136-
}
126+
// Certain modules cannot be dynamically imported.
127+
128+
// pathToFileURL for windows interop: https://github.com/nodejs/node/issues/31710
129+
return await import(pathToFileURL(file).href)
137130
}
138131

139132
throw err

system-tests/__snapshots__/config_modules_spec.ts.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

system-tests/test/config_modules_spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ describe('cypress config with esm and cjs', function () {
3232
spec: 'app.cy.js',
3333
browser: 'chrome',
3434
expectedExitCode: 1,
35-
snapshot: true,
36-
onStdout (stdout) {
37-
expect(stdout).to.include('nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules')
38-
39-
// Need to make this stable b/c of filepaths, and snapshot: true is needed to invoke onStdout
40-
return 'STDOUT_ERROR_VALIDATED'
41-
},
4235
})
4336
})
4437

0 commit comments

Comments
 (0)