From ca85d2683eb2b4cabb53f7059655b6dae7f2d8a5 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Fri, 22 Sep 2023 09:47:27 -0400 Subject: [PATCH] fix(core): unregister in-process ts transpilers when projectGraph is created (#19187) Co-authored-by: Jason Jean --- packages/nx/src/plugins/js/utils/register.ts | 4 ++- .../nx/src/project-graph/project-graph.ts | 7 ++++- .../utils/retrieve-workspace-files.ts | 1 + packages/nx/src/utils/nx-plugin.ts | 28 ++++++++++++++----- 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/packages/nx/src/plugins/js/utils/register.ts b/packages/nx/src/plugins/js/utils/register.ts index f60d7bbde2bf1..60f57e67f86c5 100644 --- a/packages/nx/src/plugins/js/utils/register.ts +++ b/packages/nx/src/plugins/js/utils/register.ts @@ -73,7 +73,9 @@ export function getTsNodeTranspiler( warnTsNodeUsage(); } - return () => {}; + return () => { + service.enabled(false); + }; } export function getTranspiler(compilerOptions: CompilerOptions) { diff --git a/packages/nx/src/project-graph/project-graph.ts b/packages/nx/src/project-graph/project-graph.ts index a6b2c25a2cb30..f8dcd65a00370 100644 --- a/packages/nx/src/project-graph/project-graph.ts +++ b/packages/nx/src/project-graph/project-graph.ts @@ -14,6 +14,7 @@ import { workspaceRoot } from '../utils/workspace-root'; import { performance } from 'perf_hooks'; import { retrieveWorkspaceFiles } from './utils/retrieve-workspace-files'; import { readNxJson } from '../config/nx-json'; +import { unregisterPluginTSTranspiler } from '../utils/nx-plugin'; /** * Synchronously reads the latest cached copy of the workspace's ProjectGraph. @@ -76,7 +77,7 @@ export async function buildProjectGraphWithoutDaemon() { await retrieveWorkspaceFiles(workspaceRoot, nxJson); const cacheEnabled = process.env.NX_CACHE_PROJECT_GRAPH !== 'false'; - return ( + const projectGraph = ( await buildProjectGraphUsingProjectFileMap( projectConfigurations.projects, externalNodes, @@ -86,6 +87,10 @@ export async function buildProjectGraphWithoutDaemon() { cacheEnabled ) ).projectGraph; + + unregisterPluginTSTranspiler(); + + return projectGraph; } function handleProjectGraphError(opts: { exitOnError: boolean }, e) { diff --git a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts index 9ab713def47d8..7461fb70180ed 100644 --- a/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts +++ b/packages/nx/src/project-graph/utils/retrieve-workspace-files.ts @@ -25,6 +25,7 @@ import { loadNxPlugins, loadNxPluginsSync, NxPluginV2, + unregisterPluginTSTranspiler, } from '../../utils/nx-plugin'; import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json'; import { diff --git a/packages/nx/src/utils/nx-plugin.ts b/packages/nx/src/utils/nx-plugin.ts index 7c93cc91c9ae4..4fde168d9874d 100644 --- a/packages/nx/src/utils/nx-plugin.ts +++ b/packages/nx/src/utils/nx-plugin.ts @@ -178,7 +178,7 @@ function getPluginPathAndName( // Register the ts-transpiler if we are pointing to a // plain ts file that's not part of a plugin project - if (extension === '.ts' && !tsNodeAndPathsRegistered) { + if (extension === '.ts' && !tsNodeAndPathsUnregisterCallback) { registerPluginTSTranspiler(); } @@ -369,14 +369,14 @@ export function resolveLocalNxPlugin( return localPluginCache[importPath]; } -let tsNodeAndPathsRegistered = false; +let tsNodeAndPathsUnregisterCallback = undefined; /** * Register swc-node or ts-node if they are not currently registered * with some default settings which work well for Nx plugins. */ export function registerPluginTSTranspiler() { - if (!tsNodeAndPathsRegistered) { + if (!tsNodeAndPathsUnregisterCallback) { // nx-ignore-next-line const ts: typeof import('typescript') = require('typescript'); @@ -390,8 +390,8 @@ export function registerPluginTSTranspiler() { ? readTsConfig(tsConfigName) : {}; - registerTsConfigPaths(tsConfigName); - registerTranspiler({ + const unregisterTsConfigPaths = registerTsConfigPaths(tsConfigName); + const unregisterTranspiler = registerTranspiler({ experimentalDecorators: true, emitDecoratorMetadata: true, ...tsConfig.options, @@ -401,8 +401,21 @@ export function registerPluginTSTranspiler() { inlineSourceMap: true, skipLibCheck: true, }); + tsNodeAndPathsUnregisterCallback = () => { + unregisterTsConfigPaths(); + unregisterTranspiler(); + }; + } +} + +/** + * Unregister the ts-node transpiler if it is registered + */ +export function unregisterPluginTSTranspiler() { + if (tsNodeAndPathsUnregisterCallback) { + tsNodeAndPathsUnregisterCallback(); + tsNodeAndPathsUnregisterCallback = undefined; } - tsNodeAndPathsRegistered = true; } function lookupLocalPlugin(importPath: string, root = workspaceRoot) { @@ -412,7 +425,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) { return null; } - if (!tsNodeAndPathsRegistered) { + if (!tsNodeAndPathsUnregisterCallback) { registerPluginTSTranspiler(); } @@ -452,6 +465,7 @@ function findNxProjectForImportPath( } let tsconfigPaths: Record; + function readTsConfigPaths(root: string = workspaceRoot) { if (!tsconfigPaths) { const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json']