From 93fef8b29767a77e025be5be8cf1cfd61bbfa107 Mon Sep 17 00:00:00 2001 From: Alan Agius <17563226+alan-agius4@users.noreply.github.com> Date: Mon, 12 Oct 2026 13:15:29 +0000 Subject: [PATCH] perf(@angular/build): enable TypeScript incremental build cache in library builder Pass the normalized `cacheOptions.path` to `compilationInstance.initialize` when persistent disk caching is enabled so TypeScript writes and reuses `.tsbuildinfo` across builds. --- .../builders/library/pipeline/compilation.ts | 4 +- .../library/tests/behavior/build_spec.ts | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/angular/build/src/builders/library/pipeline/compilation.ts b/packages/angular/build/src/builders/library/pipeline/compilation.ts index 51a8fb0d231e..a4d26f3457da 100644 --- a/packages/angular/build/src/builders/library/pipeline/compilation.ts +++ b/packages/angular/build/src/builders/library/pipeline/compilation.ts @@ -69,7 +69,8 @@ export async function compileLibrary( cached?: SingleProgramCache, modifiedFiles?: Set, ): Promise { - const { tsConfigPath, compilationMode, preserveSymlinks, colors, declarationMap } = options; + const { tsConfigPath, compilationMode, preserveSymlinks, colors, declarationMap, cacheOptions } = + options; const entryPathsMap: Record = {}; const rootFiles: string[] = []; @@ -141,6 +142,7 @@ export async function compileLibrary( declarationMap, compilationMode, paths: entryPathsMap, + cachePath: cacheOptions.enabled ? cacheOptions.path : undefined, }, 'library', ); diff --git a/packages/angular/build/src/builders/library/tests/behavior/build_spec.ts b/packages/angular/build/src/builders/library/tests/behavior/build_spec.ts index e55a414c1ada..8cba60855f2d 100644 --- a/packages/angular/build/src/builders/library/tests/behavior/build_spec.ts +++ b/packages/angular/build/src/builders/library/tests/behavior/build_spec.ts @@ -230,5 +230,47 @@ describeLibraryBuilder(executeLibraryBuilder, LIBRARY_BUILDER_INFO, (harness) => }), ); }); + + it('should write and reuse .tsbuildinfo in the persistent cache directory when cache is enabled', async () => { + harness.useProject('lib', { + root: 'projects/lib', + sourceRoot: 'projects/lib/src', + cli: { + cache: { + enabled: true, + environment: 'all', + path: '.angular/cache', + }, + }, + }); + + harness.useTarget('build', { + ...BASE_OPTIONS, + }); + + const firstBuild = await harness.executeOnce(); + expect(firstBuild.result?.success).toBeTrue(); + harness + .expectFile('.angular/cache/0.0.0-PLACEHOLDER/lib/angular-build-library/.tsbuildinfo') + .toExist(); + + await harness.writeFile( + 'projects/lib/src/public-api.ts', + ` + export * from './lib/lib.service'; + export * from './lib/lib.component'; + export const CACHED_BUILD = true; + `, + ); + + const secondBuild = await harness.executeOnce(); + expect(secondBuild.result?.success).toBeTrue(); + const fesmContent = harness.readFile('dist/lib/fesm2022/lib.mjs'); + expect(fesmContent).toContain('LibComponent'); + expect(fesmContent).toContain('CACHED_BUILD'); + const dtsContent = harness.readFile('dist/lib/types/lib.d.ts'); + expect(dtsContent).toContain('LibComponent'); + expect(dtsContent).toContain('CACHED_BUILD'); + }); }); });