Skip to content

Commit

Permalink
feat(core): improve sync messages (#29149)
Browse files Browse the repository at this point in the history
## `nx sync:check` (out of sync)

**Before**:

![image](https://github.com/user-attachments/assets/3fd737be-9c08-4cb4-b4e2-956bfd325ec7)

**After**:

![image](https://github.com/user-attachments/assets/c96c8f6c-b5e9-4566-88e4-cbe66037e411)

> The `Custom out of sync message` in these examples is a message
returned from the sync generator, while the other one is the fallback
message displayed if the sync generator doesn't return one.

## `nx sync:check` (up to date)

**Before**:

![image](https://github.com/user-attachments/assets/88f46c72-7222-4407-8b4a-90b4b6a7c9de)

**After**:

![image](https://github.com/user-attachments/assets/f4b9e181-67cd-4042-b1ac-be61fb400711)

## Running tasks

**Before**:

![image](https://github.com/user-attachments/assets/0bdc160c-c893-421a-8407-4de05e4d31b4)

**After**:

![image](https://github.com/user-attachments/assets/a7f2d471-1ad8-450f-8438-d1c3d4f8a3f9)

## Sync generator failures

**Before**:

![image](https://github.com/user-attachments/assets/805941cb-cef8-4346-9572-4eea142e6b40)

**After**:

![image](https://github.com/user-attachments/assets/621a3217-efe1-4f92-932f-80509d205dbc)

## Sync generator failures (`--verbose`)

**Before**:

![image](https://github.com/user-attachments/assets/070c22a6-39d0-4c6d-9f9f-38d77903eb6b)

**After**:

![image](https://github.com/user-attachments/assets/b007ecf7-100e-4e04-a7b1-d8bb713dbb37)

<!-- 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 Dec 3, 2024
1 parent 5448046 commit e9be7d9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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 or contain outdated project references.',
'Some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.',
};
}
}
Expand Down
9 changes: 3 additions & 6 deletions packages/nx/src/command-line/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ export function syncHandler(options: SyncOptions): Promise<number> {
? 'The workspace is up to date'
: 'The workspace is already up to date',
bodyLines: syncGenerators.map(
(generator) =>
`The ${chalk.bold(
generator
)} sync generator didn't identify any files in the workspace that are out of sync.`
(generator) => `[${chalk.bold(generator)}]: All files are up to date.`
),
});
return 0;
Expand Down Expand Up @@ -133,9 +130,9 @@ export function syncHandler(options: SyncOptions): Promise<number> {
'Syncing the workspace failed with the following error:',
'',
e.message,
...(options.verbose && !!e.stack ? [`\n${e.stack}`] : []),
...(!!e.stack ? [`\n${e.stack}`] : []),
'',
'Please rerun with `--verbose` and report the error at: https://github.com/nrwl/nx/issues/new/choose',
'Please report the error at: https://github.com/nrwl/nx/issues/new/choose',
],
});

Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ async function ensureWorkspaceIsInSyncAndGetGraphs(
const outOfSyncTitle = 'The workspace is out of sync';
const resultBodyLines = getSyncGeneratorSuccessResultsMessageLines(results);
const fixMessage =
'You can manually run `nx sync` to update your workspace with the identified changes or you can set `sync.applyChanges` to `true` in your `nx.json` to apply the changes automatically when running tasks in interactive environments.';
const willErrorOnCiMessage =
'Please note that having the workspace out of sync will result in an error in CI.';
'Make sure to run `nx sync` to apply the identified changes or set `sync.applyChanges` to `true` in your `nx.json` to apply them automatically when running tasks in interactive environments.';
const willErrorOnCiMessage = 'This will result in an error in CI.';

if (isCI() || !process.stdout.isTTY) {
// If the user is running in CI or is running in a non-TTY environment we
Expand Down
45 changes: 17 additions & 28 deletions packages/nx/src/utils/sync-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,10 @@ export function getSyncGeneratorSuccessResultsMessageLines(
}

messageLines.push(
`The ${chalk.bold(
result.generatorName
)} sync generator identified ${chalk.bold(result.changes.length)} file${
result.changes.length === 1 ? '' : 's'
} in the workspace that ${
result.changes.length === 1 ? 'is' : 'are'
} out of sync${result.outOfSyncMessage ? ':' : '.'}`
`[${chalk.bold(result.generatorName)}]: ${
result.outOfSyncMessage ?? `Some files are out of sync.`
}`
);
if (result.outOfSyncMessage) {
messageLines.push(result.outOfSyncMessage);
}
}

return messageLines;
Expand All @@ -295,16 +288,15 @@ export function getFailedSyncGeneratorsFixMessageLines(
let isFirst = true;
for (const result of results) {
if ('error' in result) {
if (!isFirst) {
if (!isFirst && verbose) {
messageLines.push('');
}
isFirst = false;
messageLines.push(
`The ${chalk.bold(
result.generatorName
)} sync generator reported the following error:`,
'',
errorToString(result.error, verbose)
`[${chalk.bold(result.generatorName)}]: ${errorToString(
result.error,
verbose
)}`
);

if (globalGeneratorSet.has(result.generatorName)) {
Expand Down Expand Up @@ -336,16 +328,15 @@ export function getFlushFailureMessageLines(
const taskGenerators: string[] = [];
let isFirst = true;
for (const failure of result.generatorFailures) {
if (!isFirst) {
if (!isFirst && verbose) {
messageLines.push('');
}
isFirst = false;
messageLines.push(
`The ${chalk.bold(
failure.generator
)} sync generator failed to apply its changes with the following error:`,
'',
errorToString(failure.error, verbose)
`[${chalk.bold(failure.generator)}]: ${errorToString(
failure.error,
verbose
)}`
);

if (globalGeneratorSet.has(failure.generator)) {
Expand Down Expand Up @@ -375,13 +366,11 @@ export function getFlushFailureMessageLines(
...[
'',
result.generalFailure.message,
...(verbose && !!result.generalFailure.stack
...(!!result.generalFailure.stack
? [`\n${result.generalFailure.stack}`]
: []),
'',
verbose
? 'Please report the error at: https://github.com/nrwl/nx/issues/new/choose'
: 'Please run with `--verbose` and report the error at: https://github.com/nrwl/nx/issues/new/choose',
'Please report the error at: https://github.com/nrwl/nx/issues/new/choose',
]
);
}
Expand Down Expand Up @@ -545,7 +534,7 @@ function getFailedSyncGeneratorsMessageLines(

function errorToString(error: SerializableSimpleError, verbose: boolean) {
if (error.title) {
let message = ` ${chalk.red(error.title)}`;
let message = `${chalk.red(error.title)}`;
if (error.bodyLines?.length) {
message += `
Expand All @@ -557,7 +546,7 @@ function errorToString(error: SerializableSimpleError, verbose: boolean) {
}
}

return ` ${chalk.red(error.message)}${
return `${chalk.red(error.message)}${
verbose && error.stack ? '\n ' + error.stack : ''
}`;
}
Expand Down

0 comments on commit e9be7d9

Please sign in to comment.