Skip to content

Commit 388d8fe

Browse files
committed
fix(@angular/build): invalidate watched files regardless of Windows drive letter casing
A Windows junction can resolve a linked library path with a different drive letter casing from the path reported by the watcher. Match the paths consistently when invalidating both the esbuild load results and the cached bundle. Fixes angular#34728
1 parent 612cde0 commit 388d8fe

3 files changed

Lines changed: 76 additions & 27 deletions

File tree

‎packages/angular/build/src/tools/esbuild/bundler-context.ts‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from 'esbuild';
1919
import assert from 'node:assert';
2020
import { basename, extname, isAbsolute, join, normalize, relative } from 'node:path';
21+
import { canonicalizePath } from '../../utils/path';
2122
import { SERVER_GENERATED_EXTERNALS } from '../../utils/server-rendering/manifest';
2223
import {
2324
type BuildOutputFile,
@@ -306,7 +307,7 @@ export class BundlerContext {
306307
: join(this.workspaceRoot, input);
307308

308309
if (!isInternal) {
309-
this.watchFiles.add(normalizedAbsoluteInput);
310+
this.watchFiles.add(canonicalizePath(normalizedAbsoluteInput));
310311
}
311312

312313
if (this.#loadCache) {
@@ -317,9 +318,7 @@ export class BundlerContext {
317318
if (cachedLoad?.watchFiles) {
318319
for (const file of cachedLoad.watchFiles) {
319320
if (!isInternalAngularFile(file)) {
320-
this.watchFiles.add(
321-
isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file),
322-
);
321+
this.watchFiles.add(this.#normalizeWatchFile(file));
323322
}
324323
}
325324
}
@@ -445,6 +444,10 @@ export class BundlerContext {
445444
};
446445
}
447446

447+
#normalizeWatchFile(file: string): string {
448+
return canonicalizePath(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file));
449+
}
450+
448451
#addErrorsToWatch(result: BuildFailure | BuildResult): void {
449452
const entryPoints = this.#esbuildOptions?.entryPoints;
450453
if (entryPoints) {
@@ -456,19 +459,19 @@ export class BundlerContext {
456459
continue;
457460
}
458461

459-
this.watchFiles.add(isAbsolute(entry) ? normalize(entry) : join(this.workspaceRoot, entry));
462+
this.watchFiles.add(this.#normalizeWatchFile(entry));
460463
}
461464
}
462465

463466
for (const error of result.errors) {
464467
let file = error.location?.file;
465468
if (file && !isInternalAngularFile(file)) {
466-
this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file));
469+
this.watchFiles.add(this.#normalizeWatchFile(file));
467470
}
468471
for (const note of error.notes ?? []) {
469472
file = note.location?.file;
470473
if (file && !isInternalAngularFile(file)) {
471-
this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file));
474+
this.watchFiles.add(this.#normalizeWatchFile(file));
472475
}
473476
}
474477
}
@@ -491,7 +494,7 @@ export class BundlerContext {
491494
continue;
492495
}
493496

494-
this.watchFiles.add(isAbsolute(file) ? normalize(file) : join(this.workspaceRoot, file));
497+
this.watchFiles.add(this.#normalizeWatchFile(file));
495498
}
496499
}
497500
}
@@ -513,8 +516,8 @@ export class BundlerContext {
513516
let isCandidateReady = true;
514517
for (const file of files) {
515518
if (
516-
file !== normalize(file) ||
517-
(!isAbsolute(file) && !files.has(normalize(join(this.workspaceRoot, file))))
519+
file !== canonicalizePath(normalize(file)) ||
520+
(!isAbsolute(file) && !files.has(this.#normalizeWatchFile(file)))
518521
) {
519522
isCandidateReady = false;
520523
break;
@@ -526,21 +529,21 @@ export class BundlerContext {
526529
} else {
527530
const normalizedFiles = new Set<string>();
528531
for (const file of files) {
529-
const normalized = normalize(file);
532+
const normalized = canonicalizePath(normalize(file));
530533
normalizedFiles.add(normalized);
531534
if (!isAbsolute(normalized)) {
532-
normalizedFiles.add(normalize(join(this.workspaceRoot, normalized)));
535+
normalizedFiles.add(this.#normalizeWatchFile(normalized));
533536
}
534537
}
535538
candidateFiles = normalizedFiles;
536539
}
537540
} else {
538541
const normalizedFiles = new Set<string>();
539542
for (const file of files) {
540-
const normalized = normalize(file);
543+
const normalized = canonicalizePath(normalize(file));
541544
normalizedFiles.add(normalized);
542545
if (!isAbsolute(normalized)) {
543-
normalizedFiles.add(normalize(join(this.workspaceRoot, normalized)));
546+
normalizedFiles.add(this.#normalizeWatchFile(normalized));
544547
}
545548
}
546549
candidateFiles = normalizedFiles;

‎packages/angular/build/src/tools/esbuild/bundler-context_spec.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,53 @@ describe('BundlerContext', () => {
101101
expect(loadCache.get('file:/workspace/src/app.css')).toBeUndefined();
102102
});
103103

104+
if (process.platform === 'win32') {
105+
it('should rebuild when a watched dependency has a differently cased drive letter', async () => {
106+
const workspaceDir = await mkdtemp(join(tempRoot, 'drive-letter-'));
107+
await writeFile(join(workspaceDir, 'main.mjs'), '');
108+
109+
const libraryPath = 'd:\\linked-library\\fesm2022\\library.mjs';
110+
let contents = 'console.log("before");';
111+
const context = new BundlerContext(workspaceDir, true, (loadCache) => ({
112+
absWorkingDir: workspaceDir,
113+
bundle: true,
114+
entryPoints: ['main.mjs'],
115+
plugins: [
116+
{
117+
name: 'test-linked-library',
118+
setup(build) {
119+
build.onLoad(
120+
{ filter: /main\.mjs$/ },
121+
createCachedLoad(loadCache, () => ({
122+
contents,
123+
loader: 'js',
124+
watchFiles: [libraryPath],
125+
})),
126+
);
127+
},
128+
},
129+
],
130+
}));
131+
132+
try {
133+
const initial = await context.bundle();
134+
assert(!initial.errors);
135+
expect(initial.outputFiles[0].text).toContain('before');
136+
137+
contents = 'console.log("after");';
138+
expect(
139+
context.invalidate(new Set(['D:\\linked-library\\fesm2022\\library.mjs'])),
140+
).toBeTrue();
141+
142+
const rebuilt = await context.bundle();
143+
assert(!rebuilt.errors);
144+
expect(rebuilt.outputFiles[0].text).toContain('after');
145+
} finally {
146+
await context.dispose();
147+
}
148+
});
149+
}
150+
104151
it('should not invalidate unrelated contexts sharing a load cache', async () => {
105152
const workspaceDir = await mkdtemp(join(tempRoot, 'shared-cache-'));
106153
const compAPath = join(workspaceDir, 'comp-a.css');

‎packages/angular/build/src/tools/esbuild/load-result-cache.ts‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import type { OnLoadResult, PluginBuild } from 'esbuild';
1010
import { normalize } from 'node:path';
11+
import { canonicalizePath } from '../../utils/path';
1112

1213
export interface LoadResultCache {
1314
get(path: string): OnLoadResult | Promise<OnLoadResult | undefined> | undefined;
@@ -68,12 +69,14 @@ export class MemoryLoadResultCache implements LoadResultCache {
6869
}
6970
}
7071

71-
const currentNormalizedWatchFiles = new Set(result.watchFiles?.map(normalize) ?? []);
72+
const currentNormalizedWatchFiles = new Set(
73+
result.watchFiles?.map((file) => canonicalizePath(normalize(file))),
74+
);
7275

7376
// Clean up any previous file dependencies that are no longer referenced
7477
if (previousWatchFiles) {
7578
for (const watchFile of previousWatchFiles) {
76-
const normalizedWatchFile = normalize(watchFile);
79+
const normalizedWatchFile = canonicalizePath(normalize(watchFile));
7780
if (!currentNormalizedWatchFiles.has(normalizedWatchFile)) {
7881
const affected = this.#fileDependencies.get(normalizedWatchFile);
7982
if (affected) {
@@ -93,22 +96,18 @@ export class MemoryLoadResultCache implements LoadResultCache {
9396
}
9497

9598
this.#loadResults.set(path, result);
96-
if (result.watchFiles) {
97-
for (const watchFile of result.watchFiles) {
98-
// Normalize the watch file path to ensure OS consistent paths
99-
const normalizedWatchFile = normalize(watchFile);
100-
let affected = this.#fileDependencies.get(normalizedWatchFile);
101-
if (affected === undefined) {
102-
affected = new Set();
103-
this.#fileDependencies.set(normalizedWatchFile, affected);
104-
}
105-
affected.add(path);
99+
for (const watchFile of currentNormalizedWatchFiles) {
100+
let affected = this.#fileDependencies.get(watchFile);
101+
if (affected === undefined) {
102+
affected = new Set();
103+
this.#fileDependencies.set(watchFile, affected);
106104
}
105+
affected.add(path);
107106
}
108107
}
109108

110109
invalidate(path: string): boolean {
111-
const affectedPaths = this.#fileDependencies.get(path);
110+
const affectedPaths = this.#fileDependencies.get(canonicalizePath(normalize(path)));
112111
if (!affectedPaths) {
113112
return false;
114113
}

0 commit comments

Comments
 (0)