From dd9d6ea50b580b90e48f54732346cf4360ab92a5 Mon Sep 17 00:00:00 2001 From: Kerick Howlett <88661181+KerickHowlett@users.noreply.github.com> Date: Mon, 7 Oct 2024 19:30:23 -0400 Subject: [PATCH 1/2] fix(core): allow for non-js libs to be moved without errors or the creation of any unneeded tsconfig files closed #28349 --- .../src/generators/move/lib/update-imports.ts | 50 ++++++++++--------- packages/workspace/src/utilities/ts-config.ts | 22 +++++--- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index 0a027347d336b..d162f5586544a 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -1,13 +1,13 @@ import { - ChangeType, - ProjectConfiguration, - StringChange, - Tree, applyChangesToString, + ChangeType, getProjects, getWorkspaceLayout, joinPathFragments, + ProjectConfiguration, readJson, + StringChange, + Tree, visitNotIgnoredFiles, writeJson, } from '@nx/devkit'; @@ -17,11 +17,13 @@ import { getImportPath } from '../../../utilities/get-import-path'; import { findNodes, getRootTsConfigPathInTree, + TSConfig, + type TSConfigFileName, } from '../../../utilities/ts-config'; import { ensureTypescript } from '../../../utilities/typescript'; +import { isUsingTsSolutionSetup } from '../../../utilities/typescript/ts-solution-setup'; import { NormalizedSchema } from '../schema'; import { normalizePathSlashes } from './utils'; -import { isUsingTsSolutionSetup } from '../../../utilities/typescript/ts-solution-setup'; let tsModule: typeof import('typescript'); @@ -46,31 +48,32 @@ export function updateImports( // use the source root to find the from location // this attempts to account for libs that have been created with --importPath - const tsConfigPath = isUsingTsSolution - ? 'tsconfig.json' - : getRootTsConfigPathInTree(tree); // If we are using a ts solution setup, we need to use tsconfig.json instead of tsconfig.base.json - let tsConfig: any; - let mainEntryPointImportPath: string; - let secondaryEntryPointImportPaths: string[]; - let serverEntryPointImportPath: string; + const tsConfigPath: TSConfigFileName | null = isUsingTsSolution + ? ('tsconfig.json' as const) + : getRootTsConfigPathInTree(tree); + let tsConfig: TSConfig | undefined; + let paths: string[] = []; + + let mainEntryPointImportPath: string | undefined; + let secondaryEntryPointImportPaths: string[] = []; + let serverEntryPointImportPath: string | undefined; + if (tree.exists(tsConfigPath)) { tsConfig = readJson(tree, tsConfigPath); const sourceRoot = project.sourceRoot ?? joinPathFragments(project.root, 'src'); - mainEntryPointImportPath = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).find((path) => + paths = Object.keys(tsConfig?.compilerOptions.paths ?? {}); + + mainEntryPointImportPath = paths.find((path) => tsConfig.compilerOptions.paths[path].some((x) => x.startsWith(ensureTrailingSlash(sourceRoot)) ) ); - secondaryEntryPointImportPaths = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).filter((path) => + secondaryEntryPointImportPaths = paths.filter((path) => tsConfig.compilerOptions.paths[path].some( - (x) => + (x: string) => x.startsWith(ensureTrailingSlash(project.root)) && !x.startsWith(ensureTrailingSlash(sourceRoot)) ) @@ -78,9 +81,7 @@ export function updateImports( // Next.js libs have a custom path for the server we need to update that as well // example "paths": { @acme/lib/server : ['libs/lib/src/server.ts'] } - serverEntryPointImportPath = Object.keys( - tsConfig.compilerOptions?.paths ?? {} - ).find((path) => + serverEntryPointImportPath = paths.find((path) => tsConfig.compilerOptions.paths[path].some( (x) => x.startsWith(ensureTrailingSlash(sourceRoot)) && @@ -166,13 +167,14 @@ export function updateImports( } else { updateTsConfigReferences(tsConfig, projectRoot, tsConfigPath, schema); } + writeJson(tree, tsConfigPath, tsConfig); } } } function updateTsConfigReferences( - tsConfig: any, + tsConfig: TSConfig, projectRoot: { from: string; to: string }, tsConfigPath: string, schema: NormalizedSchema @@ -208,7 +210,7 @@ function updateTsConfigReferences( } function updateTsConfigPaths( - tsConfig: any, + tsConfig: TSConfig, projectRef: { from: string; to: string }, tsConfigPath: string, projectRoot: { from: string; to: string }, diff --git a/packages/workspace/src/utilities/ts-config.ts b/packages/workspace/src/utilities/ts-config.ts index 927741226fc3c..166990fb88fc0 100644 --- a/packages/workspace/src/utilities/ts-config.ts +++ b/packages/workspace/src/utilities/ts-config.ts @@ -1,11 +1,20 @@ import { offsetFromRoot, Tree, workspaceRoot } from '@nx/devkit'; import { existsSync } from 'fs'; import { dirname, join } from 'path'; -import type { Node, SyntaxKind } from 'typescript'; +import type { CompilerOptions, Node, SyntaxKind } from 'typescript'; import { ensureTypescript } from './typescript'; let tsModule: typeof import('typescript'); +const TSCONFIG_FILE_NAMES = ['tsconfig.base.json', 'tsconfig.json'] as const; +export type TSConfigFileName = (typeof TSCONFIG_FILE_NAMES)[number]; + +export type TSConfig = { + compilerOptions: CompilerOptions; + references: { path: string }[]; + [string: string]: unknown; +}; + export function readTsConfig(tsConfigPath: string) { if (!tsModule) { tsModule = ensureTypescript(); @@ -20,15 +29,14 @@ export function readTsConfig(tsConfigPath: string) { dirname(tsConfigPath) ); } - -export function getRootTsConfigPathInTree(tree: Tree): string | null { - for (const path of ['tsconfig.base.json', 'tsconfig.json']) { +export function getRootTsConfigPathInTree(tree: Tree): TSConfigFileName | null { + for (const path of TSCONFIG_FILE_NAMES) { if (tree.exists(path)) { return path; } } - return 'tsconfig.base.json'; + return null; } export function getRelativePathToRootTsConfig( @@ -38,8 +46,8 @@ export function getRelativePathToRootTsConfig( return offsetFromRoot(targetPath) + getRootTsConfigPathInTree(tree); } -export function getRootTsConfigFileName(): string | null { - for (const tsConfigName of ['tsconfig.base.json', 'tsconfig.json']) { +export function getRootTsConfigFileName(): TSConfigFileName | null { + for (const tsConfigName of TSCONFIG_FILE_NAMES) { const tsConfigPath = join(workspaceRoot, tsConfigName); if (existsSync(tsConfigPath)) { return tsConfigName; From 30903defcf0d32fd4371ca8faa9b2d18698f92d1 Mon Sep 17 00:00:00 2001 From: Kerick Howlett <8661181+KerickHowlett@users.noreply.github.com> Date: Wed, 27 Nov 2024 21:46:15 -0500 Subject: [PATCH 2/2] feat(core): add null check for tsconfigpath closed #28349 --- packages/workspace/src/generators/move/lib/update-imports.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index d162f5586544a..e5ed9a2479e1a 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -59,7 +59,7 @@ export function updateImports( let secondaryEntryPointImportPaths: string[] = []; let serverEntryPointImportPath: string | undefined; - if (tree.exists(tsConfigPath)) { + if (tsConfigPath !== null && tree.exists(tsConfigPath)) { tsConfig = readJson(tree, tsConfigPath); const sourceRoot = project.sourceRoot ?? joinPathFragments(project.root, 'src');