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(js): improve typescript-sync generator messaging #28162

Merged
merged 1 commit into from
Sep 27, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ describe('syncGenerator()', () => {
writeJson(tree, 'nx.json', nxJson);

await expect(syncGenerator(tree)).rejects.toMatchInlineSnapshot(
`[Error: The @nx/js/typescript plugin must be added to the "plugins" array in nx.json before syncing tsconfigs]`
`[SyncError: The "@nx/js/typescript" plugin is not registered]`
);
});

it('should error if there is no root tsconfig.json', async () => {
tree.delete('tsconfig.json');

await expect(syncGenerator(tree)).rejects.toMatchInlineSnapshot(
`[Error: A "tsconfig.json" file must exist in the workspace root in order to use this sync generator.]`
`[SyncError: Missing root "tsconfig.json"]`
);
});

Expand Down
19 changes: 11 additions & 8 deletions packages/js/src/generators/typescript-sync/typescript-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import {
import ignore from 'ignore';
import { applyEdits, modify } from 'jsonc-parser';
import { dirname, normalize, relative } from 'node:path/posix';
import type { SyncGeneratorResult } from 'nx/src/utils/sync-generators';
import {
SyncError,
type SyncGeneratorResult,
} from 'nx/src/utils/sync-generators';
import * as ts from 'typescript';
import {
PLUGIN_NAME,
Expand Down Expand Up @@ -63,17 +66,17 @@ export async function syncGenerator(tree: Tree): Promise<SyncGeneratorResult> {
}
);
if (!tscPluginConfig) {
throw new Error(
`The ${PLUGIN_NAME} plugin must be added to the "plugins" array in nx.json before syncing tsconfigs`
);
throw new SyncError(`The "${PLUGIN_NAME}" plugin is not registered`, [
`The "${PLUGIN_NAME}" plugin must be added to the "plugins" array in "nx.json" in order to sync the project graph information to the TypeScript configuration files.`,
]);
}

// Root tsconfig containing project references for the whole workspace
const rootTsconfigPath = 'tsconfig.json';
if (!tree.exists(rootTsconfigPath)) {
throw new Error(
`A "tsconfig.json" file must exist in the workspace root in order to use this sync generator.`
);
throw new SyncError('Missing root "tsconfig.json"', [
`A "tsconfig.json" file must exist in the workspace root in order to sync the project graph information to the TypeScript configuration files.`,
]);
}

const rawTsconfigContentsCache = new Map<string, string>();
Expand Down Expand Up @@ -254,7 +257,7 @@ export async function syncGenerator(tree: Tree): Promise<SyncGeneratorResult> {

return {
outOfSyncMessage:
'Based on the workspace project graph, some TypeScript configuration files are missing project references to the projects they depend on.',
'Based on the workspace project graph, some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.',
};
}
}
Expand Down