fix: handle errors on plugins migration child process (#21442)

* fix: handle errors on plugins migration child process

* Update error handler

* Remove console.log
This commit is contained in:
Alejandro Estrada
2022-05-13 16:04:35 -05:00
committed by GitHub
parent b4aa3d9513
commit fbe645224e
12 changed files with 67 additions and 11 deletions

View File

@@ -88,7 +88,16 @@ export async function processConfigViaLegacyPlugins (projectRoot: string, legacy
const configProcessArgs = ['--projectRoot', projectRoot, '--file', cwd]
const CHILD_PROCESS_FILE_PATH = require.resolve('@packages/server/lib/plugins/child/require_async_child')
const ipc = new LegacyPluginsIpc(fork(CHILD_PROCESS_FILE_PATH, configProcessArgs, childOptions))
const childProcess = fork(CHILD_PROCESS_FILE_PATH, configProcessArgs, childOptions)
const ipc = new LegacyPluginsIpc(childProcess)
childProcess.on('error', (error) => {
error = getError('LEGACY_CONFIG_ERROR_DURING_MIGRATION', cwd, error)
reject(error)
ipc.killChildProcess()
})
const legacyConfigWithDefaults = getConfigWithDefaults(legacyConfig)
@@ -107,17 +116,19 @@ export async function processConfigViaLegacyPlugins (projectRoot: string, legacy
const legacyConfigWithChanges = _.merge(legacyConfig, diff)
resolve(legacyConfigWithChanges)
ipc.childProcess.kill()
ipc.killChildProcess()
})
ipc.on('loadLegacyPlugins:error', (error) => {
error = getError('LEGACY_CONFIG_ERROR_DURING_MIGRATION', cwd, error)
reject(error)
ipc.childProcess.kill()
ipc.killChildProcess()
})
ipc.on('childProcess:unhandledError', (error) => {
reject(error)
ipc.childProcess.kill()
ipc.killChildProcess()
})
})
}

View File

@@ -32,4 +32,13 @@ export class LegacyPluginsIpc extends EventEmitter {
on (evt: string, listener: (...args: any[]) => void) {
return super.on(evt, listener)
}
killChildProcess () {
this.childProcess.kill()
this.childProcess.stdout?.removeAllListeners()
this.childProcess.stderr?.removeAllListeners()
this.childProcess.removeAllListeners()
this.removeAllListeners()
}
}

View File

@@ -34,12 +34,12 @@
</style>
</head>
<body><pre><span style="color:#e05561">Your <span style="color:#e5e510">cypress/plugins/index.js<span style="color:#e05561"> at <span style="color:#4ec4ff">cypress/plugins/index.js<span style="color:#e05561"> threw an error. <span style="color:#e6e6e6">
<body><pre><span style="color:#e05561">Your <span style="color:#e5e510">cypress/plugins/index.js<span style="color:#e05561"> file threw an error. <span style="color:#e6e6e6">
<span style="color:#e05561"><span style="color:#e6e6e6">
<span style="color:#e05561">Please ensure your pluginsFile is valid and relaunch the migration tool to migrate to Cypress version 10.0.0.<span style="color:#e6e6e6">
<span style="color:#e05561"><span style="color:#e6e6e6">
<span style="color:#c062de"><span style="color:#e6e6e6">
<span style="color:#c062de">Error: fail whale<span style="color:#e6e6e6">
<span style="color:#c062de"> at makeErr (cypress/packages/errors/test/unit/visualSnapshotErrors_spec.ts)<span style="color:#e6e6e6">
<span style="color:#c062de"> at LEGACY_CONFIG_ERROR_DURING_MIGRATION (cypress/packages/errors/test/unit/visualSnapshotErrors_spec.ts)<span style="color:#e6e6e6"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span style="color:#c062de"> at LEGACY_CONFIG_ERROR_DURING_MIGRATION (cypress/packages/errors/test/unit/visualSnapshotErrors_spec.ts)<span style="color:#e6e6e6"></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
</pre></body></html>

View File

@@ -1191,7 +1191,7 @@ export const AllCypressErrors = {
LEGACY_CONFIG_ERROR_DURING_MIGRATION: (file: string, error: Error) => {
return errTemplate`
Your ${fmt.highlight(file)} at ${fmt.path(`${file}`)} threw an error. ${fmt.stackTrace(error)}
Your ${fmt.highlight(file)} file threw an error. ${fmt.stackTrace(error)}
Please ensure your pluginsFile is valid and relaunch the migration tool to migrate to ${fmt.cypressVersion('10.0.0')}.
`

View File

@@ -1339,6 +1339,14 @@ describe('Migration', { viewportWidth: 1200, retries: { openMode: 2, runMode: 2
cy.get('button').contains('Cancel').click()
cy.get('h2').should('not.contain', 'Change the existing spec file extension')
})
it('shows error if plugins file throws an error', () => {
scaffoldAndVisitLaunchpad('migration-e2e-plugins-throw-error')
cy.contains('cypress/plugins/index.js file threw an error.')
cy.contains('Please ensure your pluginsFile is valid and relaunch the migration tool to migrate to Cypress version 10.0.0.')
cy.contains('throw new Error(\'New error from plugin\')')
})
})
describe('Migrate custom config files', () => {
@@ -1509,6 +1517,13 @@ describe('Migrate custom config files', () => {
scaffoldAndVisitLaunchpad('migration-custom-config-file-with-existing-v10-config-file', ['--config-file', 'customConfig.json'])
cy.contains('There is both a customConfig.config.js and a customConfig.json file at the location below:')
cy.contains('ypress no longer supports customConfig.json, please remove it from your project.')
cy.contains('Cypress no longer supports customConfig.json, please remove it from your project.')
})
it('shows error if plugins file do not exist', () => {
scaffoldAndVisitLaunchpad('migration', ['--config-file', 'erroredConfigFiles/incorrectPluginsFile.json'])
cy.contains('foo/bar file threw an error.')
cy.contains('Please ensure your pluginsFile is valid and relaunch the migration tool to migrate to Cypress version 10.0.0.')
})
})

View File

@@ -287,9 +287,7 @@ function run (ipc, file, projectRoot) {
ipc.send('loadLegacyPlugins:reply', mergedLegacyConfig)
} catch (e) {
ipc.send('loadLegacyPlugins:error', util.serializeError(
require('@packages/errors').getError('LEGACY_CONFIG_ERROR_DURING_MIGRATION', file, e),
))
ipc.send('loadLegacyPlugins:error', util.serializeError(e))
}
})

View File

@@ -0,0 +1,3 @@
## Migration E2E Plugins Throw Error
An e2e project where `cypress/plugins/index.js` modifies throws an error is handled correctly

View File

@@ -0,0 +1,6 @@
/* eslint-disable */
module.exports = (on, config) => {
throw new Error('New error from plugin')
return config
}

View File

@@ -0,0 +1,13 @@
{
"baseUrl": "http://localhost:3000",
"retries": 2,
"defaultCommandTimeout": 5000,
"fixturesFolder": false,
"componentFolder": "src",
"testFiles": "**/*.spec.{tsx,js}",
"pluginsFile": "foo/bar",
"e2e": {
"defaultCommandTimeout": 10000,
"slowTestThreshold": 5000
}
}