Skip to content

Commit

Permalink
fix: partial fix for #21997 & #22004, throw the originalError (#22009)
Browse files Browse the repository at this point in the history
* fix: partial fix for #21997 & #22004, throw the originalError

* throw originalError test

Co-authored-by: Zach Bloomquist <git@chary.us>
  • Loading branch information
tgriesser and flotwig authored Jun 1, 2022
1 parent f33eca8 commit 369a865
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/server/lib/plugins/child/run_require_async_child.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ function run (ipc, file, projectRoot) {
// 3a. Yes: Use bundleRequire
// 3b. No: Continue through to `await import(configFile)`
// 4. Use node's dynamic import to import the configFile
let originalError

try {
return require(file)
} catch (err) {
originalError = err
if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
throw err
}
Expand All @@ -122,8 +124,16 @@ function run (ipc, file, projectRoot) {
debug(`User doesn't have esbuild. Going to use native node imports.`)

// We cannot replace the initial `require` with `await import` because
// Certain modules cannot be dynamically imported
return await import(file)
// Certain modules cannot be dynamically imported. If this throws, however, we want
// to show the original error that was thrown, because that's ultimately the source of the problem
try {
return await import(file)
} catch (e) {
// If we aren't able to import the file at all, throw the original error, since that has more accurate information
// of what failed to begin with
debug('esbuild fallback for loading config failed, throwing original error. node import error: %o', e)
throw originalError
}
}

throw err
Expand Down
3 changes: 3 additions & 0 deletions system-tests/__snapshots__/config_modules_spec.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports['cypress config with esm and cjs / does not support modules and ts without esbuild in config-cjs-and-esm/config-with-ts-module'] = `
STDOUT_ERROR_VALIDATED
`
7 changes: 7 additions & 0 deletions system-tests/test/config_modules_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe('cypress config with esm and cjs', function () {
spec: 'app.cy.js',
browser: 'chrome',
expectedExitCode: 1,
snapshot: true,
onStdout (stdout) {
expect(stdout).to.include('nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules')

// Need to make this stable b/c of filepaths, and snapshot: true is needed to invoke onStdout
return 'STDOUT_ERROR_VALIDATED'
},
})
})

Expand Down

2 comments on commit 369a865

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 369a865 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.1/linux-x64/develop-369a865237df4388c6421d5eae7ed948dd606606/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 369a865 Jun 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/10.0.1/darwin-x64/develop-369a865237df4388c6421d5eae7ed948dd606606/cypress.tgz

Please sign in to comment.