Skip to content

Commit

Permalink
fix(js): improve typescript-sync generator messaging (#28162)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez authored Sep 27, 2024
1 parent 49c5a73 commit 153451f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
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

0 comments on commit 153451f

Please sign in to comment.