Skip to content
Open
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
49 changes: 20 additions & 29 deletions packages/angular/build/src/tools/esbuild/bundler-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}
}
}
Expand All @@ -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));
}
}
}
Expand All @@ -508,39 +511,27 @@ export class BundlerContext {
return false;
}

let candidateFiles: ReadonlySet<string>;
let candidateFiles: ReadonlySet<string> | 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<string>();
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<string>();
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;
Expand Down
47 changes: 47 additions & 0 deletions packages/angular/build/src/tools/esbuild/bundler-context_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
25 changes: 12 additions & 13 deletions packages/angular/build/src/tools/esbuild/load-result-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<OnLoadResult | undefined> | undefined;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down