diff --git a/packages/angular/build/src/tools/esbuild/bundler-context.ts b/packages/angular/build/src/tools/esbuild/bundler-context.ts index 106619be2360..0bfd55b03763 100644 --- a/packages/angular/build/src/tools/esbuild/bundler-context.ts +++ b/packages/angular/build/src/tools/esbuild/bundler-context.ts @@ -18,6 +18,7 @@ import { } from 'esbuild'; import assert from 'node:assert'; import { basename, extname, isAbsolute, join, normalize, relative } from 'node:path'; +import { canonicalizePath } from '../../utils/path'; import { SERVER_GENERATED_EXTERNALS } from '../../utils/server-rendering/manifest'; import { type BuildOutputFile, @@ -306,7 +307,7 @@ export class BundlerContext { : join(this.workspaceRoot, input); if (!isInternal) { - this.watchFiles.add(normalizedAbsoluteInput); + this.watchFiles.add(canonicalizePath(normalizedAbsoluteInput)); } if (this.#loadCache) { @@ -317,9 +318,7 @@ export class BundlerContext { if (cachedLoad?.watchFiles) { for (const file of cachedLoad.watchFiles) { if (!isInternalAngularFile(file)) { - this.watchFiles.add( - isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file), - ); + this.watchFiles.add(this.#normalizeWatchFile(file)); } } } @@ -445,6 +444,10 @@ export class BundlerContext { }; } + #normalizeWatchFile(file: string): string { + return canonicalizePath(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file)); + } + #addErrorsToWatch(result: BuildFailure | BuildResult): void { const entryPoints = this.#esbuildOptions?.entryPoints; if (entryPoints) { @@ -456,19 +459,19 @@ export class BundlerContext { continue; } - this.watchFiles.add(isAbsolute(entry) ? normalize(entry) : join(this.workspaceRoot, entry)); + this.watchFiles.add(this.#normalizeWatchFile(entry)); } } for (const error of result.errors) { let file = error.location?.file; if (file && !isInternalAngularFile(file)) { - this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file)); + this.watchFiles.add(this.#normalizeWatchFile(file)); } for (const note of error.notes ?? []) { file = note.location?.file; if (file && !isInternalAngularFile(file)) { - this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file)); + this.watchFiles.add(this.#normalizeWatchFile(file)); } } } @@ -491,7 +494,7 @@ export class BundlerContext { continue; } - this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file)); + this.watchFiles.add(this.#normalizeWatchFile(file)); } } } @@ -508,39 +511,27 @@ export class BundlerContext { return false; } - let candidateFiles: ReadonlySet; + let candidateFiles: ReadonlySet | undefined; if (files instanceof Set) { - let isCandidateReady = true; + candidateFiles = files; for (const file of files) { if ( - file !== normalize(file) || - (!isAbsolute(file) && !files.has(normalize(join(this.workspaceRoot, file)))) + file !== canonicalizePath(normalize(file)) || + (!isAbsolute(file) && !files.has(this.#normalizeWatchFile(file))) ) { - isCandidateReady = false; + candidateFiles = undefined; break; } } + } - if (isCandidateReady) { - candidateFiles = files; - } else { - const normalizedFiles = new Set(); - for (const file of files) { - const normalized = normalize(file); - normalizedFiles.add(normalized); - if (!isAbsolute(normalized)) { - normalizedFiles.add(normalize(join(this.workspaceRoot, normalized))); - } - } - candidateFiles = normalizedFiles; - } - } else { + if (!candidateFiles) { const normalizedFiles = new Set(); for (const file of files) { - const normalized = normalize(file); + const normalized = canonicalizePath(normalize(file)); normalizedFiles.add(normalized); if (!isAbsolute(normalized)) { - normalizedFiles.add(normalize(join(this.workspaceRoot, normalized))); + normalizedFiles.add(this.#normalizeWatchFile(normalized)); } } candidateFiles = normalizedFiles; diff --git a/packages/angular/build/src/tools/esbuild/bundler-context_spec.ts b/packages/angular/build/src/tools/esbuild/bundler-context_spec.ts index b98f266e36a7..681bafde8533 100644 --- a/packages/angular/build/src/tools/esbuild/bundler-context_spec.ts +++ b/packages/angular/build/src/tools/esbuild/bundler-context_spec.ts @@ -101,6 +101,53 @@ describe('BundlerContext', () => { expect(loadCache.get('file:/workspace/src/app.css')).toBeUndefined(); }); + if (process.platform === 'win32') { + it('should rebuild when a watched dependency has a differently cased drive letter', async () => { + const workspaceDir = await mkdtemp(join(tempRoot, 'drive-letter-')); + await writeFile(join(workspaceDir, 'main.mjs'), ''); + + const libraryPath = 'd:\\linked-library\\fesm2022\\library.mjs'; + let contents = 'console.log("before");'; + const context = new BundlerContext(workspaceDir, true, (loadCache) => ({ + absWorkingDir: workspaceDir, + bundle: true, + entryPoints: ['main.mjs'], + plugins: [ + { + name: 'test-linked-library', + setup(build) { + build.onLoad( + { filter: /main\.mjs$/ }, + createCachedLoad(loadCache, () => ({ + contents, + loader: 'js', + watchFiles: [libraryPath], + })), + ); + }, + }, + ], + })); + + try { + const initial = await context.bundle(); + assert(!initial.errors); + expect(initial.outputFiles[0].text).toContain('before'); + + contents = 'console.log("after");'; + expect( + context.invalidate(new Set(['D:\\linked-library\\fesm2022\\library.mjs'])), + ).toBeTrue(); + + const rebuilt = await context.bundle(); + assert(!rebuilt.errors); + expect(rebuilt.outputFiles[0].text).toContain('after'); + } finally { + await context.dispose(); + } + }); + } + it('should not invalidate unrelated contexts sharing a load cache', async () => { const workspaceDir = await mkdtemp(join(tempRoot, 'shared-cache-')); const compAPath = join(workspaceDir, 'comp-a.css'); diff --git a/packages/angular/build/src/tools/esbuild/load-result-cache.ts b/packages/angular/build/src/tools/esbuild/load-result-cache.ts index 402069cff1a5..3f52506740dc 100644 --- a/packages/angular/build/src/tools/esbuild/load-result-cache.ts +++ b/packages/angular/build/src/tools/esbuild/load-result-cache.ts @@ -8,6 +8,7 @@ import type { OnLoadResult, PluginBuild } from 'esbuild'; import { normalize } from 'node:path'; +import { canonicalizePath } from '../../utils/path'; export interface LoadResultCache { get(path: string): OnLoadResult | Promise | undefined; @@ -68,12 +69,14 @@ export class MemoryLoadResultCache implements LoadResultCache { } } - const currentNormalizedWatchFiles = new Set(result.watchFiles?.map(normalize) ?? []); + const currentNormalizedWatchFiles = new Set( + result.watchFiles?.map((file) => canonicalizePath(normalize(file))), + ); // Clean up any previous file dependencies that are no longer referenced if (previousWatchFiles) { for (const watchFile of previousWatchFiles) { - const normalizedWatchFile = normalize(watchFile); + const normalizedWatchFile = canonicalizePath(normalize(watchFile)); if (!currentNormalizedWatchFiles.has(normalizedWatchFile)) { const affected = this.#fileDependencies.get(normalizedWatchFile); if (affected) { @@ -93,22 +96,18 @@ export class MemoryLoadResultCache implements LoadResultCache { } this.#loadResults.set(path, result); - if (result.watchFiles) { - for (const watchFile of result.watchFiles) { - // Normalize the watch file path to ensure OS consistent paths - const normalizedWatchFile = normalize(watchFile); - let affected = this.#fileDependencies.get(normalizedWatchFile); - if (affected === undefined) { - affected = new Set(); - this.#fileDependencies.set(normalizedWatchFile, affected); - } - affected.add(path); + for (const watchFile of currentNormalizedWatchFiles) { + let affected = this.#fileDependencies.get(watchFile); + if (affected === undefined) { + affected = new Set(); + this.#fileDependencies.set(watchFile, affected); } + affected.add(path); } } invalidate(path: string): boolean { - const affectedPaths = this.#fileDependencies.get(path); + const affectedPaths = this.#fileDependencies.get(canonicalizePath(normalize(path))); if (!affectedPaths) { return false; }