Skip to content
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
21 changes: 16 additions & 5 deletions packages/angular/build/src/builders/karma/application_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { Config, ConfigOptions, FilePattern, InlinePluginDef, Server } from
import { randomUUID } from 'node:crypto';
import { rmSync } from 'node:fs';
import * as fs from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import { ReadableStream } from 'node:stream/web';
import { createVirtualModulePlugin } from '../../tools/esbuild/virtual-module-plugin';
Expand Down Expand Up @@ -66,8 +65,14 @@ export function execute(
init = await initializeApplication(normalizedOptions, context, karmaOptions, transforms);
} catch (err) {
if (err instanceof ApplicationBuildError) {
controller.enqueue({ success: false, message: err.message });
controller.close();
if (controller.desiredSize !== null) {
try {
controller.enqueue({ success: false, message: err.message });
controller.close();
} catch {
// Stream controller may already be closed or cancelled
}
}

return;
}
Expand All @@ -85,8 +90,14 @@ export function execute(

// Close the stream once the Karma server returns.
karmaServer = new karma.Server(karmaConfig as Config, (exitCode) => {
controller.enqueue({ success: exitCode === 0 });
controller.close();
if (controller.desiredSize !== null) {
try {
controller.enqueue({ success: exitCode === 0 });
controller.close();
} catch {
// Stream controller may already be closed or cancelled
}
}
Comment thread
clydin marked this conversation as resolved.
});

await karmaServer.start();
Expand Down
22 changes: 17 additions & 5 deletions packages/angular/build/src/builders/karma/progress-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,18 @@ export function injectKarmaReporter(
break;
}

if (controller.desiredSize === null) {
break;
}

if (buildOutput.kind === ResultKind.Failure) {
controller.enqueue({ success: false, message: 'Build failed' });
if (controller.desiredSize !== null) {
try {
controller.enqueue({ success: false, message: 'Build failed' });
} catch {
// Stream controller may already be closed or cancelled
}
}
} else if (
buildOutput.kind === ResultKind.Incremental ||
buildOutput.kind === ResultKind.Full
Expand All @@ -81,10 +91,12 @@ export function injectKarmaReporter(
}

onRunComplete = function (_browsers: unknown, results: RunCompleteInfo): void {
if (results.exitCode === 0) {
controller.enqueue({ success: true });
} else {
controller.enqueue({ success: false });
if (controller.desiredSize !== null) {
try {
controller.enqueue({ success: results.exitCode === 0 });
} catch {
// Stream controller may already be closed or cancelled
}
}
Comment thread
clydin marked this conversation as resolved.
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,18 @@ describeKarmaBuilder(execute, KARMA_BUILDER_INFO, (harness, setupTarget) => {

expect(result?.success).toBeFalse();
});

it('handles stream cancellation gracefully in watch mode', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
watch: true,
});

const { result } = await harness.executeOnce({
outputLogsOnFailure: false,
});

expect(result?.success).toBeTrue();
});
});
});
Loading