Skip to content

Commit

Permalink
fix(@angular/build): ensure full rebuild after initial error build in…
Browse files Browse the repository at this point in the history
… watch mode

If an initial build of an application results in an error during watch mode
(including `ng serve`), the following non-error rebuild will now always be
a full build result. This ensures that all new files are available for later
incremental build result updates.

(cherry picked from commit b24089e)
  • Loading branch information
clydin authored and alan-agius4 committed Feb 5, 2025
1 parent 27f8331 commit 8f6ee7e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ export async function* runEsBuildBuildAction(
return;
}

// Used to force a full result on next rebuild if there were initial errors.
// This ensures at least one full result is emitted.
let hasInitialErrors = result.errors.length > 0;

// Wait for changes and rebuild as needed
const currentWatchFiles = new Set(result.watchFiles);
try {
Expand Down Expand Up @@ -201,10 +205,13 @@ export async function* runEsBuildBuildAction(
result,
outputOptions,
changes,
incrementalResults ? rebuildState : undefined,
incrementalResults && !hasInitialErrors ? rebuildState : undefined,
)) {
yield outputResult;
}

// Clear initial build errors flag if no errors are now present
hasInitialErrors &&= result.errors.length > 0;
}
} finally {
// Stop the watcher and cleanup incremental rebuild state
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { concatMap, count, take, timeout } from 'rxjs';
import { executeDevServer } from '../../index';
import { describeServeBuilder } from '../jasmine-helpers';
import { BASE_OPTIONS, BUILD_TIMEOUT, DEV_SERVER_BUILDER_INFO } from '../setup';
import { logging } from '@angular-devkit/core';

describeServeBuilder(executeDevServer, DEV_SERVER_BUILDER_INFO, (harness, setupTarget) => {
describe('Behavior: "Rebuild Error Detection"', () => {
beforeEach(() => {
setupTarget(harness);
});

it('Emits full build result with incremental enabled and initial build has errors', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
watch: true,
});

// Missing ending `>` on the div will cause an error
await harness.appendToFile('src/app/app.component.html', '<div>Hello, world!</div');

const buildCount = await harness
.execute({ outputLogsOnFailure: false })
.pipe(
timeout(BUILD_TIMEOUT),
concatMap(async ({ result, logs }, index) => {
switch (index) {
case 0:
expect(result?.success).toBeFalse();
debugger;
expect(logs).toContain(
jasmine.objectContaining<logging.LogEntry>({
message: jasmine.stringMatching('Unexpected character "EOF"'),
}),
);

await harness.appendToFile('src/app/app.component.html', '>');

break;
case 1:
expect(result?.success).toBeTrue();
expect(logs).not.toContain(
jasmine.objectContaining<logging.LogEntry>({
message: jasmine.stringMatching('Unexpected character "EOF"'),
}),
);
break;
}
}),
take(2),
count(),
)
.toPromise();

expect(buildCount).toBe(2);
});
});
});
2 changes: 2 additions & 0 deletions packages/angular/build/src/builders/dev-server/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ export async function* serveWithVite(
},
});
}

yield { baseUrl: '', success: false };
continue;
}
// Clear existing error overlay on successful result
Expand Down
6 changes: 6 additions & 0 deletions tests/legacy-cli/e2e/tests/build/jit-ngmodule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export default async function () {
};
}

// Remove bundle budgets due to the increased size from JIT
build.configurations.production = {
...build.configurations.production,
budgets: undefined,
};

build.options.aot = false;
});
// Test it works
Expand Down

0 comments on commit 8f6ee7e

Please sign in to comment.