diff --git a/docs/generated/cli/format-check.md b/docs/generated/cli/format-check.md index 20bc7ebf91..1ae7c08fa3 100644 --- a/docs/generated/cli/format-check.md +++ b/docs/generated/cli/format-check.md @@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options -| Option | Type | Description | -| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | -| `--all` | boolean | Format all projects. | -| `--base` | string | Base of the current branch (usually main). | -| `--exclude` | string | Exclude certain projects from being processed. | -| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | -| `--head` | string | Latest commit of the current branch (usually HEAD). | -| `--help` | boolean | Show help. | -| `--libs-and-apps` | boolean | Format only libraries and applications files. | -| `--projects` | string | Projects to format (comma/space delimited). | -| `--uncommitted` | boolean | Uncommitted changes. | -| `--untracked` | boolean | Untracked changes. | -| `--version` | boolean | Show version number. | +| Option | Type | Description | +| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| `--all` | boolean | Format all projects. | +| `--base` | string | Base of the current branch (usually main). | +| `--exclude` | string | Exclude certain projects from being processed. | +| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | +| `--head` | string | Latest commit of the current branch (usually HEAD). | +| `--help` | boolean | Show help. | +| `--libs-and-apps` | boolean | Format only libraries and applications files. | +| `--projects` | string | Projects to format (comma/space delimited). | +| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) | +| `--uncommitted` | boolean | Uncommitted changes. | +| `--untracked` | boolean | Untracked changes. | +| `--version` | boolean | Show version number. | diff --git a/docs/generated/cli/format-write.md b/docs/generated/cli/format-write.md index 0ebd35431e..9e95119af8 100644 --- a/docs/generated/cli/format-write.md +++ b/docs/generated/cli/format-write.md @@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options -| Option | Type | Description | -| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | -| `--all` | boolean | Format all projects. | -| `--base` | string | Base of the current branch (usually main). | -| `--exclude` | string | Exclude certain projects from being processed. | -| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | -| `--head` | string | Latest commit of the current branch (usually HEAD). | -| `--help` | boolean | Show help. | -| `--libs-and-apps` | boolean | Format only libraries and applications files. | -| `--projects` | string | Projects to format (comma/space delimited). | -| `--uncommitted` | boolean | Uncommitted changes. | -| `--untracked` | boolean | Untracked changes. | -| `--version` | boolean | Show version number. | +| Option | Type | Description | +| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| `--all` | boolean | Format all projects. | +| `--base` | string | Base of the current branch (usually main). | +| `--exclude` | string | Exclude certain projects from being processed. | +| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | +| `--head` | string | Latest commit of the current branch (usually HEAD). | +| `--help` | boolean | Show help. | +| `--libs-and-apps` | boolean | Format only libraries and applications files. | +| `--projects` | string | Projects to format (comma/space delimited). | +| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) | +| `--uncommitted` | boolean | Uncommitted changes. | +| `--untracked` | boolean | Untracked changes. | +| `--version` | boolean | Show version number. | diff --git a/docs/generated/devkit/formatFiles.md b/docs/generated/devkit/formatFiles.md index 5f1b2c8db1..54d7371458 100644 --- a/docs/generated/devkit/formatFiles.md +++ b/docs/generated/devkit/formatFiles.md @@ -1,14 +1,16 @@ # Function: formatFiles -▸ **formatFiles**(`tree`): `Promise`\<`void`\> +▸ **formatFiles**(`tree`, `options?`): `Promise`\<`void`\> Formats all the created or updated files using Prettier #### Parameters -| Name | Type | Description | -| :----- | :------------------------------------ | :------------------- | -| `tree` | [`Tree`](../../devkit/documents/Tree) | the file system tree | +| Name | Type | Description | +| :------------------------------ | :------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `tree` | [`Tree`](../../devkit/documents/Tree) | the file system tree | +| `options?` | `Object` | - | +| `options.sortRootTsconfigPaths` | `boolean` | TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important in Nx workspace setups, and the sorting causes comments to be lost. | #### Returns diff --git a/docs/generated/packages/nx/documents/format-check.md b/docs/generated/packages/nx/documents/format-check.md index 20bc7ebf91..1ae7c08fa3 100644 --- a/docs/generated/packages/nx/documents/format-check.md +++ b/docs/generated/packages/nx/documents/format-check.md @@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options -| Option | Type | Description | -| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | -| `--all` | boolean | Format all projects. | -| `--base` | string | Base of the current branch (usually main). | -| `--exclude` | string | Exclude certain projects from being processed. | -| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | -| `--head` | string | Latest commit of the current branch (usually HEAD). | -| `--help` | boolean | Show help. | -| `--libs-and-apps` | boolean | Format only libraries and applications files. | -| `--projects` | string | Projects to format (comma/space delimited). | -| `--uncommitted` | boolean | Uncommitted changes. | -| `--untracked` | boolean | Untracked changes. | -| `--version` | boolean | Show version number. | +| Option | Type | Description | +| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| `--all` | boolean | Format all projects. | +| `--base` | string | Base of the current branch (usually main). | +| `--exclude` | string | Exclude certain projects from being processed. | +| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | +| `--head` | string | Latest commit of the current branch (usually HEAD). | +| `--help` | boolean | Show help. | +| `--libs-and-apps` | boolean | Format only libraries and applications files. | +| `--projects` | string | Projects to format (comma/space delimited). | +| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) | +| `--uncommitted` | boolean | Uncommitted changes. | +| `--untracked` | boolean | Untracked changes. | +| `--version` | boolean | Show version number. | diff --git a/docs/generated/packages/nx/documents/format-write.md b/docs/generated/packages/nx/documents/format-write.md index 0ebd35431e..9e95119af8 100644 --- a/docs/generated/packages/nx/documents/format-write.md +++ b/docs/generated/packages/nx/documents/format-write.md @@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx` ## Options -| Option | Type | Description | -| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | -| `--all` | boolean | Format all projects. | -| `--base` | string | Base of the current branch (usually main). | -| `--exclude` | string | Exclude certain projects from being processed. | -| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | -| `--head` | string | Latest commit of the current branch (usually HEAD). | -| `--help` | boolean | Show help. | -| `--libs-and-apps` | boolean | Format only libraries and applications files. | -| `--projects` | string | Projects to format (comma/space delimited). | -| `--uncommitted` | boolean | Uncommitted changes. | -| `--untracked` | boolean | Untracked changes. | -| `--version` | boolean | Show version number. | +| Option | Type | Description | +| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | +| `--all` | boolean | Format all projects. | +| `--base` | string | Base of the current branch (usually main). | +| `--exclude` | string | Exclude certain projects from being processed. | +| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. | +| `--head` | string | Latest commit of the current branch (usually HEAD). | +| `--help` | boolean | Show help. | +| `--libs-and-apps` | boolean | Format only libraries and applications files. | +| `--projects` | string | Projects to format (comma/space delimited). | +| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) | +| `--uncommitted` | boolean | Uncommitted changes. | +| `--untracked` | boolean | Untracked changes. | +| `--version` | boolean | Show version number. | diff --git a/packages/devkit/src/generators/format-files.ts b/packages/devkit/src/generators/format-files.ts index 63a786c7c0..29b29a67f5 100644 --- a/packages/devkit/src/generators/format-files.ts +++ b/packages/devkit/src/generators/format-files.ts @@ -8,13 +8,24 @@ import { sortObjectByKeys } from 'nx/src/devkit-internals'; * Formats all the created or updated files using Prettier * @param tree - the file system tree */ -export async function formatFiles(tree: Tree): Promise { +export async function formatFiles( + tree: Tree, + options = { + /** + * TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important + * in Nx workspace setups, and the sorting causes comments to be lost. + */ + sortRootTsconfigPaths: true, + } +): Promise { let prettier: typeof Prettier; try { prettier = await import('prettier'); } catch {} - sortTsConfig(tree); + if (options.sortRootTsconfigPaths) { + sortTsConfig(tree); + } if (!prettier) return; diff --git a/packages/nx/src/command-line/format/command-object.ts b/packages/nx/src/command-line/format/command-object.ts index 60328163f6..7234290554 100644 --- a/packages/nx/src/command-line/format/command-object.ts +++ b/packages/nx/src/command-line/format/command-object.ts @@ -39,6 +39,15 @@ function withFormatOptions(yargs: Argv): Argv { type: 'string', coerce: parseCSV, }) + .option('sort-root-tsconfig-paths', { + describe: `Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost.`, + type: 'boolean', + /** + * TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important + * in Nx workspace setups, and the sorting causes comments to be lost. + */ + default: true, + }) .option('all', { describe: 'Format all projects.', type: 'boolean', diff --git a/packages/nx/src/command-line/format/format.ts b/packages/nx/src/command-line/format/format.ts index fb23a34da2..c5a9c028a7 100644 --- a/packages/nx/src/command-line/format/format.ts +++ b/packages/nx/src/command-line/format/format.ts @@ -60,7 +60,9 @@ export async function format( switch (command) { case 'write': - sortTsConfig(); + if (nxArgs.sortRootTsconfigPaths) { + sortTsConfig(); + } addRootConfigFiles(chunkList, nxArgs); chunkList.forEach((chunk) => write(chunk)); break; diff --git a/packages/nx/src/utils/command-line-utils.ts b/packages/nx/src/utils/command-line-utils.ts index 985caa7188..629ccb08aa 100644 --- a/packages/nx/src/utils/command-line-utils.ts +++ b/packages/nx/src/utils/command-line-utils.ts @@ -41,6 +41,7 @@ export interface NxArgs { batch?: boolean; excludeTaskDependencies?: boolean; skipSync?: boolean; + sortRootTsconfigPaths?: boolean; } export function createOverrides(__overrides_unparsed__: string[] = []) {