Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): unregister in-process ts transpilers when projectGraph is created #19187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/nx/src/plugins/js/utils/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ export function getTsNodeTranspiler(
warnTsNodeUsage();
}

return () => {};
return () => {
service.enabled(false);
};
}

export function getTranspiler(compilerOptions: CompilerOptions) {
Expand Down
7 changes: 6 additions & 1 deletion packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -86,6 +87,10 @@ export async function buildProjectGraphWithoutDaemon() {
cacheEnabled
)
).projectGraph;

unregisterPluginTSTranspiler();

return projectGraph;
}

function handleProjectGraphError(opts: { exitOnError: boolean }, e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
loadNxPlugins,
loadNxPluginsSync,
NxPluginV2,
unregisterPluginTSTranspiler,
} from '../../utils/nx-plugin';
import { CreateProjectJsonProjectsPlugin } from '../../plugins/project-json/build-nodes/project-json';
import {
Expand Down
28 changes: 21 additions & 7 deletions packages/nx/src/utils/nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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');

Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -412,7 +425,7 @@ function lookupLocalPlugin(importPath: string, root = workspaceRoot) {
return null;
}

if (!tsNodeAndPathsRegistered) {
if (!tsNodeAndPathsUnregisterCallback) {
registerPluginTSTranspiler();
}

Expand Down Expand Up @@ -452,6 +465,7 @@ function findNxProjectForImportPath(
}

let tsconfigPaths: Record<string, string[]>;

function readTsConfigPaths(root: string = workspaceRoot) {
if (!tsconfigPaths) {
const tsconfigPath: string | null = ['tsconfig.base.json', 'tsconfig.json']
Expand Down