Skip to content

Commit 6f64315

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 31f2384 commit 6f64315

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 5 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

@@ -484,7 +485,7 @@ export function createCompilerPlugin(
484485
build.onLoad(
485486
{ filter: /\.[cm]?js$/ },
486487
createCachedLoad(pluginOptions.loadResultCache, async (args) => {
487-
let request = args.path;
488+
let request = rewriteForBazel(args.path);
488489
if (pluginOptions.fileReplacements) {
489490
const replacement = pluginOptions.fileReplacements[path.normalize(args.path)];
490491
if (replacement) {
@@ -505,6 +506,7 @@ export function createCompilerPlugin(
505506
return {
506507
contents,
507508
loader: 'js',
509+
resolveDir: path.dirname(request),
508510
watchFiles: request !== args.path ? [request] : undefined,
509511
};
510512
},
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)