From fdcca1f2feaebd64569546e71789c7aaad0980a7 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Tue, 8 Aug 2023 14:31:56 +1000 Subject: [PATCH 01/11] try to only add commonjs when running Cypress config --- packages/server/lib/plugins/child/ts_node.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/server/lib/plugins/child/ts_node.js b/packages/server/lib/plugins/child/ts_node.js index 7fcaa431e677..8e1941b8e5cb 100644 --- a/packages/server/lib/plugins/child/ts_node.js +++ b/packages/server/lib/plugins/child/ts_node.js @@ -22,7 +22,6 @@ const getTsNodeOptions = (tsPath, registeredFile) => { * we need to only set options that are supported in this version of TypeScript. */ const compilerOptions = { - module: 'commonjs', ...(semver.satisfies(version, '>=4.5.0') // Only adding this option for TS >= 4.5.0 ? { preserveValueImports: false } @@ -34,6 +33,9 @@ const getTsNodeOptions = (tsPath, registeredFile) => { if (process.env.TS_NODE_COMPILER) { try { + // @ts-ignore - compilerOptions is an object we can assign properties on. + // It's the 'tsconfig.compilerOptions'. + compilerOptions.module = 'commonjs' compiler = require.resolve(process.env.TS_NODE_COMPILER, { paths: [path.dirname(registeredFile)] }) } catch { // ts-node compiler not installed in project directory From bf11620c9d4b8f0a317f6b476c2ff6aefc5b5da1 Mon Sep 17 00:00:00 2001 From: jordanpowell88 Date: Wed, 9 Aug 2023 09:48:40 -0400 Subject: [PATCH 02/11] chore: add moduleResolution node --- packages/server/lib/plugins/child/ts_node.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/server/lib/plugins/child/ts_node.js b/packages/server/lib/plugins/child/ts_node.js index 8e1941b8e5cb..0835acef5b3e 100644 --- a/packages/server/lib/plugins/child/ts_node.js +++ b/packages/server/lib/plugins/child/ts_node.js @@ -36,6 +36,9 @@ const getTsNodeOptions = (tsPath, registeredFile) => { // @ts-ignore - compilerOptions is an object we can assign properties on. // It's the 'tsconfig.compilerOptions'. compilerOptions.module = 'commonjs' + // @ts-ignore - moduleResolution is an object we can assign properties on. + // It's the 'tsconfig.compilerOptions'. + compilerOptions.moduleResolution = 'node' compiler = require.resolve(process.env.TS_NODE_COMPILER, { paths: [path.dirname(registeredFile)] }) } catch { // ts-node compiler not installed in project directory From 88d8322a8956d5c77cedebd14248cea2a9488227 Mon Sep 17 00:00:00 2001 From: jordanpowell88 Date: Wed, 9 Aug 2023 10:48:24 -0400 Subject: [PATCH 03/11] test: update tests to follow new patch --- .../test/unit/plugins/child/ts_node_spec.js | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/server/test/unit/plugins/child/ts_node_spec.js b/packages/server/test/unit/plugins/child/ts_node_spec.js index d6945aa08fcd..095461aaf430 100644 --- a/packages/server/test/unit/plugins/child/ts_node_spec.js +++ b/packages/server/test/unit/plugins/child/ts_node_spec.js @@ -22,9 +22,7 @@ describe('lib/plugins/child/ts_node', () => { transpileOnly: true, compiler: 'typescript/lib/typescript.js', dir: '/path/to/plugins', - compilerOptions: { - module: 'commonjs', - }, + compilerOptions: {}, ignore: [ '(?:^|/)node_modules/', '/packages/telemetry/dist/span-exporters/ipc-span-exporter', @@ -43,7 +41,6 @@ describe('lib/plugins/child/ts_node', () => { compiler: 'typescript/lib/typescript.js', dir: '/path/to/plugins', compilerOptions: { - module: 'commonjs', preserveValueImports: false, }, ignore: [ @@ -55,6 +52,30 @@ describe('lib/plugins/child/ts_node', () => { }) }) + it('registers ts-node with commonjs and node moduleResolution when process.env.TS_NODE_COMPILER is set', () => { + process.env.TS_NODE_COMPILER = true + sinon.stub(typescriptObject, 'version').value('1.1.1') + tsNodeUtil.register('proj-root', '/path/to/plugins/file.js') + + expect(tsnode.register).to.be.calledWith({ + transpileOnly: true, + compiler: 'typescript/lib/typescript.js', + dir: '/path/to/plugins', + compilerOptions: { + module: 'commonjs', + moduleResolution: 'node', + }, + ignore: [ + '(?:^|/)node_modules/', + '/packages/telemetry/dist/span-exporters/ipc-span-exporter', + '/packages/telemetry/dist/span-exporters/console-trace-link-exporter', + '/packages/telemetry/dist/processors/on-start-span-processor', + ], + }) + + delete process.env.TS_NODE_COMPILER + }) + it('does not register ts-node if typescript is not installed', () => { resolve.typescript.returns(null) From 0bc6c0aa07b7cf464a323f1e3ad1f0fe0b05d306 Mon Sep 17 00:00:00 2001 From: jordanpowell88 Date: Wed, 9 Aug 2023 13:25:18 -0400 Subject: [PATCH 04/11] move module outside of TS_NODE_COMPILER process env --- packages/server/lib/plugins/child/ts_node.js | 4 +--- packages/server/test/unit/plugins/child/ts_node_spec.js | 5 ++++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/server/lib/plugins/child/ts_node.js b/packages/server/lib/plugins/child/ts_node.js index 0835acef5b3e..49e5be0cc6eb 100644 --- a/packages/server/lib/plugins/child/ts_node.js +++ b/packages/server/lib/plugins/child/ts_node.js @@ -22,6 +22,7 @@ const getTsNodeOptions = (tsPath, registeredFile) => { * we need to only set options that are supported in this version of TypeScript. */ const compilerOptions = { + module: 'commonjs', ...(semver.satisfies(version, '>=4.5.0') // Only adding this option for TS >= 4.5.0 ? { preserveValueImports: false } @@ -35,9 +36,6 @@ const getTsNodeOptions = (tsPath, registeredFile) => { try { // @ts-ignore - compilerOptions is an object we can assign properties on. // It's the 'tsconfig.compilerOptions'. - compilerOptions.module = 'commonjs' - // @ts-ignore - moduleResolution is an object we can assign properties on. - // It's the 'tsconfig.compilerOptions'. compilerOptions.moduleResolution = 'node' compiler = require.resolve(process.env.TS_NODE_COMPILER, { paths: [path.dirname(registeredFile)] }) } catch { diff --git a/packages/server/test/unit/plugins/child/ts_node_spec.js b/packages/server/test/unit/plugins/child/ts_node_spec.js index 095461aaf430..ecaed2b3a6f6 100644 --- a/packages/server/test/unit/plugins/child/ts_node_spec.js +++ b/packages/server/test/unit/plugins/child/ts_node_spec.js @@ -22,7 +22,9 @@ describe('lib/plugins/child/ts_node', () => { transpileOnly: true, compiler: 'typescript/lib/typescript.js', dir: '/path/to/plugins', - compilerOptions: {}, + compilerOptions: { + module: 'commonjs', + }, ignore: [ '(?:^|/)node_modules/', '/packages/telemetry/dist/span-exporters/ipc-span-exporter', @@ -41,6 +43,7 @@ describe('lib/plugins/child/ts_node', () => { compiler: 'typescript/lib/typescript.js', dir: '/path/to/plugins', compilerOptions: { + module: 'commonjs', preserveValueImports: false, }, ignore: [ From d5cc8cb7fe3f0b56188e1b61f23f9016c204dc22 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 10:14:26 +1000 Subject: [PATCH 05/11] changelog --- cli/CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 8e0c76580b35..07d715225155 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,4 +1,12 @@ + +## 12.17.5 + +_Pending Release 08/29/2023_ + +**Bugfixes:** +- Only force CommonJS when running `ts-node` with a `TS_NODE_COMPILER` environment variable, such as when Cypress uses `ts-node` internally. This solves an issue where Cypress' internal `tsconfig` conflicts with properties set in the user's `tsconfig.json` such as `module` and `moduleResolution`. Fixes [#26308](https://github.com/cypress-io/cypress/issues/26308) and [#27448](https://github.com/cypress-io/cypress/issues/27448). + ## 12.17.4 _Released 08/15/2023_ From 9947d2fc33986691ad3e6d92cb31033439c4c1e6 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 10:15:35 +1000 Subject: [PATCH 06/11] changelog --- cli/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 07d715225155..f45240093116 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,5 +1,4 @@ - ## 12.17.5 _Pending Release 08/29/2023_ From 91fee336a06968b0e700c1ff2cff148d1d308249 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 10:18:05 +1000 Subject: [PATCH 07/11] changelog --- cli/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index f45240093116..beac2f1afe3e 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,7 +1,7 @@ ## 12.17.5 -_Pending Release 08/29/2023_ +_Release 08/29/2023 (PENDING)_ **Bugfixes:** - Only force CommonJS when running `ts-node` with a `TS_NODE_COMPILER` environment variable, such as when Cypress uses `ts-node` internally. This solves an issue where Cypress' internal `tsconfig` conflicts with properties set in the user's `tsconfig.json` such as `module` and `moduleResolution`. Fixes [#26308](https://github.com/cypress-io/cypress/issues/26308) and [#27448](https://github.com/cypress-io/cypress/issues/27448). From f52ffa251f2f1c1d0a1054f2490c2197c576d809 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 10:18:14 +1000 Subject: [PATCH 08/11] changelog --- cli/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index beac2f1afe3e..00874ebdee0a 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -1,7 +1,7 @@ ## 12.17.5 -_Release 08/29/2023 (PENDING)_ +_Released 08/29/2023 (PENDING)_ **Bugfixes:** - Only force CommonJS when running `ts-node` with a `TS_NODE_COMPILER` environment variable, such as when Cypress uses `ts-node` internally. This solves an issue where Cypress' internal `tsconfig` conflicts with properties set in the user's `tsconfig.json` such as `module` and `moduleResolution`. Fixes [#26308](https://github.com/cypress-io/cypress/issues/26308) and [#27448](https://github.com/cypress-io/cypress/issues/27448). From 7b8e7743a14074b517ec3d443b36d501e22c36e2 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 10:19:09 +1000 Subject: [PATCH 09/11] changelog --- cli/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 00874ebdee0a..d6d3ed7bc235 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -4,6 +4,7 @@ _Released 08/29/2023 (PENDING)_ **Bugfixes:** + - Only force CommonJS when running `ts-node` with a `TS_NODE_COMPILER` environment variable, such as when Cypress uses `ts-node` internally. This solves an issue where Cypress' internal `tsconfig` conflicts with properties set in the user's `tsconfig.json` such as `module` and `moduleResolution`. Fixes [#26308](https://github.com/cypress-io/cypress/issues/26308) and [#27448](https://github.com/cypress-io/cypress/issues/27448). ## 12.17.4 From 8f56da55bea186264d9727320fea1cd886505748 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Thu, 17 Aug 2023 15:05:55 +1000 Subject: [PATCH 10/11] regression test --- .../cypress.config.js | 7 +++++++ .../package.json | 6 ++++++ .../tsconfig.json | 12 ++++++++++++ .../config-with-module-resolution-bundler/yarn.lock | 8 ++++++++ system-tests/test/config_modules_spec.ts | 11 ++++++----- 5 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/cypress.config.js create mode 100644 system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/package.json create mode 100644 system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/tsconfig.json create mode 100644 system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/yarn.lock diff --git a/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/cypress.config.js b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/cypress.config.js new file mode 100644 index 000000000000..42ec879cc9e6 --- /dev/null +++ b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/cypress.config.js @@ -0,0 +1,7 @@ +const { defineConfig } = require('cypress') + +module.exports = defineConfig({ + e2e: { + supportFile: false, + }, +}) diff --git a/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/package.json b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/package.json new file mode 100644 index 000000000000..2e4ad67943ca --- /dev/null +++ b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/package.json @@ -0,0 +1,6 @@ +{ + "devDependencies": { + "typescript": "4.7.3" + }, + "projectFixtureDirectory": "simple_passing" +} diff --git a/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/tsconfig.json b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/tsconfig.json new file mode 100644 index 000000000000..603f393a19bc --- /dev/null +++ b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "module": "es2015", + "moduleResolution": "bundler", + "types": [ + "cypress" + ], + }, + "include": [ + "src" + ] +} diff --git a/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/yarn.lock b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/yarn.lock new file mode 100644 index 000000000000..6774dcd69294 --- /dev/null +++ b/system-tests/projects/config-cjs-and-esm/config-with-module-resolution-bundler/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +typescript@4.7.3: + version "4.7.3" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.7.3.tgz#8364b502d5257b540f9de4c40be84c98e23a129d" + integrity sha512-WOkT3XYvrpXx4vMMqlD+8R8R37fZkjyLGlxavMc4iB8lrl8L0DeTcHbYgw/v0N/z9wAFsgBhcsF0ruoySS22mA== diff --git a/system-tests/test/config_modules_spec.ts b/system-tests/test/config_modules_spec.ts index a1b617a68210..2c0be671e542 100644 --- a/system-tests/test/config_modules_spec.ts +++ b/system-tests/test/config_modules_spec.ts @@ -39,11 +39,12 @@ describe('compiles config files using the native node import', () => { ;[ // esbuild chokes on these kinds of projects (JS Config File + TSConfig that's out of range) // so this makes sure we're using the native node import - 'config-cjs-and-esm/config-with-mjs-tsconfig-es5', - 'config-cjs-and-esm/config-with-cjs-tsconfig-es5', - 'config-cjs-and-esm/config-with-js-tsconfig-es5', - 'config-cjs-and-esm/config-with-js-tsconfig-es3', - 'config-cjs-and-esm/config-with-js-tsconfig-es2015', + // 'config-cjs-and-esm/config-with-mjs-tsconfig-es5', + // 'config-cjs-and-esm/config-with-cjs-tsconfig-es5', + // 'config-cjs-and-esm/config-with-js-tsconfig-es5', + // 'config-cjs-and-esm/config-with-js-tsconfig-es3', + // 'config-cjs-and-esm/config-with-js-tsconfig-es2015', + 'config-cjs-and-esm/config-with-module-resolution-bundler', ].forEach((project) => { systemTests.it(`${project}`, { project, From 17627f974a3ac1baeea9a89f7e73fb467cff5499 Mon Sep 17 00:00:00 2001 From: Lachlan Miller Date: Fri, 18 Aug 2023 06:41:27 +1000 Subject: [PATCH 11/11] add tests bck in --- packages/server/lib/plugins/child/ts_node.js | 2 +- system-tests/test/config_modules_spec.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/server/lib/plugins/child/ts_node.js b/packages/server/lib/plugins/child/ts_node.js index 49e5be0cc6eb..f5e75701379d 100644 --- a/packages/server/lib/plugins/child/ts_node.js +++ b/packages/server/lib/plugins/child/ts_node.js @@ -34,7 +34,7 @@ const getTsNodeOptions = (tsPath, registeredFile) => { if (process.env.TS_NODE_COMPILER) { try { - // @ts-ignore - compilerOptions is an object we can assign properties on. + // @ts-expect-error - compilerOptions is an object we can assign properties on. // It's the 'tsconfig.compilerOptions'. compilerOptions.moduleResolution = 'node' compiler = require.resolve(process.env.TS_NODE_COMPILER, { paths: [path.dirname(registeredFile)] }) diff --git a/system-tests/test/config_modules_spec.ts b/system-tests/test/config_modules_spec.ts index 2c0be671e542..eee01dfbbf4a 100644 --- a/system-tests/test/config_modules_spec.ts +++ b/system-tests/test/config_modules_spec.ts @@ -39,11 +39,11 @@ describe('compiles config files using the native node import', () => { ;[ // esbuild chokes on these kinds of projects (JS Config File + TSConfig that's out of range) // so this makes sure we're using the native node import - // 'config-cjs-and-esm/config-with-mjs-tsconfig-es5', - // 'config-cjs-and-esm/config-with-cjs-tsconfig-es5', - // 'config-cjs-and-esm/config-with-js-tsconfig-es5', - // 'config-cjs-and-esm/config-with-js-tsconfig-es3', - // 'config-cjs-and-esm/config-with-js-tsconfig-es2015', + 'config-cjs-and-esm/config-with-mjs-tsconfig-es5', + 'config-cjs-and-esm/config-with-cjs-tsconfig-es5', + 'config-cjs-and-esm/config-with-js-tsconfig-es5', + 'config-cjs-and-esm/config-with-js-tsconfig-es3', + 'config-cjs-and-esm/config-with-js-tsconfig-es2015', 'config-cjs-and-esm/config-with-module-resolution-bundler', ].forEach((project) => { systemTests.it(`${project}`, {