Files
cypress/packages/server/lib/plugins/util.js
Bill Glesias 9b3b023aa0 feat: replace ts-node with tsx for parsing user configuration (#31520)
* feat: replace tsnode with tsx for parsing user configuration in all cases

* bump ubuntu images from node 18 to 20 as node 18 is not supported in cypress 15 and allows us to not include the 18.19.0 workaround to use --import with tsx inside the ProjectConfigIpc

* fix: issues finding tsx on windows as it needs the file:// protocol as absolute drive paths are not recognized as a file protocol in the node context

* make sure to filter out stack code correctly for windows

* fix: fix flake from windows on reporter menu not expanding (unrelated to this PR and should be merged into develop)

* chore: update changelog with all issues tsx cutover closes

* fix merge conflicts

* chore: add regression tests for cypress projects that previously did not work but now do with tsx

* build all binaries

* chore: address issues from code review

* update changelog

* remove todo comment on testing legacy migration with tsx

* refactor codeFrame calculation into a util function and add a unit test

* updated node versions in project config ipc tests to remove 18 and test threshold / latest values. Removed redundant comments on ts-node.
2025-04-29 13:31:28 -04:00

92 lines
2.7 KiB
JavaScript

const _ = require('lodash')
const EE = require('events')
const Promise = require('bluebird')
const path = require('path')
const UNDEFINED_SERIALIZED = '__cypress_undefined__'
const buildErrorLocationFromTransformError = (err, projectRoot) => {
const cleanMessage = err.message
// replace the first line with better text (remove potentially misleading word TypeScript for example)
.replace(/^.*\n/g, 'Error compiling file\n')
// Regex to pull out the error from the message body of a tsx TransformError. It displays the relative path to a file
const transformErrorRegex = /\n(.*?):(\d+):(\d+):/g
const failurePath = transformErrorRegex.exec(cleanMessage)
return {
compilerErrorLocation: failurePath ? { filePath: path.relative(projectRoot, failurePath[1]), line: Number(failurePath[2]), column: Number(failurePath[3]) } : null,
originalMessage: err.message,
message: cleanMessage,
}
}
const serializeError = (err) => {
const obj = _.pick(err,
'name', 'message', 'stack', 'code', 'annotated', 'type',
'details', 'isCypressErr', 'messageMarkdown',
'originalError',
// Location of the error when a TransformError or a esbuild error occurs (parse error from ts-node or esbuild)
'compilerErrorLocation')
if (obj.originalError) {
obj.originalError = serializeError(obj.originalError)
}
return obj
}
module.exports = {
buildErrorLocationFromTransformError,
serializeError,
nonNodeRequires () {
return Object.keys(require.cache).filter((c) => !c.includes('/node_modules/'))
},
wrapIpc (aProcess) {
const emitter = new EE()
aProcess.on('message', (message) => {
return emitter.emit(message.event, ...message.args)
})
// prevent max listeners warning on ipc
// @see https://github.com/cypress-io/cypress/issues/1305#issuecomment-780895569
emitter.setMaxListeners(Infinity)
return {
send (event, ...args) {
if (aProcess.killed || !aProcess.connected) {
return
}
return aProcess.send({
event,
args,
})
},
on: emitter.on.bind(emitter),
removeListener: emitter.removeListener.bind(emitter),
}
},
wrapChildPromise (ipc, invoke, ids, args = []) {
return Promise.try(() => {
return invoke(ids.eventId, args)
})
.then((value) => {
// undefined is coerced into null when sent over ipc, but we need
// to differentiate between them for 'task' event
if (value === undefined) {
value = UNDEFINED_SERIALIZED
}
return ipc.send(`promise:fulfilled:${ids.invocationId}`, null, value)
}).catch((err) => {
return ipc.send(`promise:fulfilled:${ids.invocationId}`, serializeError(err))
})
},
}