Skip to content

Commit 3c2005c

Browse files
committed
fix(@angular/build): correct JS/TS file paths when running under Bazel
The compiler plugin now rewrites Bazel file paths if they were resolved outside of the execution root. This behavior is only used if Bazel environment variables are present.
1 parent a019e7b commit 3c2005c

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

packages/angular/build/src/tools/esbuild/angular/compiler-plugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { SharedTSCompilationState, getSharedCompilationState } from './compilati
2929
import { ComponentStylesheetBundler } from './component-stylesheets';
3030
import { FileReferenceTracker } from './file-reference-tracker';
3131
import { setupJitPluginCallbacks } from './jit-plugin-callbacks';
32+
import { rewriteForBazel } from './rewrite-bazel-paths';
3233
import { SourceFileCache } from './source-file-cache';
3334

3435
export interface CompilerPluginOptions {
@@ -411,8 +412,8 @@ export function createCompilerPlugin(
411412
});
412413

413414
build.onLoad({ filter: /\.[cm]?[jt]sx?$/ }, async (args) => {
414-
const request = path.normalize(
415-
pluginOptions.fileReplacements?.[path.normalize(args.path)] ?? args.path,
415+
const request = rewriteForBazel(
416+
path.normalize(pluginOptions.fileReplacements?.[path.normalize(args.path)] ?? args.path),
416417
);
417418
const isJS = /\.[cm]?js$/.test(request);
418419

@@ -478,13 +479,14 @@ export function createCompilerPlugin(
478479
return {
479480
contents,
480481
loader,
482+
resolveDir: path.dirname(request),
481483
};
482484
});
483485

484486
build.onLoad(
485487
{ filter: /\.[cm]?js$/ },
486488
createCachedLoad(pluginOptions.loadResultCache, async (args) => {
487-
let request = args.path;
489+
let request = rewriteForBazel(args.path);
488490
if (pluginOptions.fileReplacements) {
489491
const replacement = pluginOptions.fileReplacements[path.normalize(args.path)];
490492
if (replacement) {
@@ -505,6 +507,7 @@ export function createCompilerPlugin(
505507
return {
506508
contents,
507509
loader: 'js',
510+
resolveDir: path.dirname(request),
508511
watchFiles: request !== args.path ? [request] : undefined,
509512
};
510513
},
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { join, relative } from 'node:path';
10+
11+
const bazelBinDirectory = process.env['BAZEL_BINDIR'];
12+
const bazelExecRoot = process.env['JS_BINARY__EXECROOT'];
13+
14+
export function rewriteForBazel(path: string): string {
15+
if (!bazelBinDirectory || !bazelExecRoot) {
16+
return path;
17+
}
18+
19+
const fromExecRoot = relative(bazelExecRoot, path);
20+
if (!fromExecRoot.startsWith('..')) {
21+
return path;
22+
}
23+
24+
const fromBinDirectory = relative(bazelBinDirectory, path);
25+
if (fromBinDirectory.startsWith('..')) {
26+
return path;
27+
}
28+
29+
return join(bazelExecRoot, fromBinDirectory);
30+
}

0 commit comments

Comments
 (0)