Skip to content

Commit 0844373

Browse files
authored
fix(bazel/rules_angular): ensure correct typescript version is utilized in persistent worker runfiles
Updates the symlink_package rule to correctly propagate transitive source files, including standard library definitions, into the generated Bzlmod runfiles layout. Additionally, integrates the BazelSafeFilesystem instance within the persistent compiler worker to reliably resolve and load the workspace-provided TS toolchain at execution time.
1 parent 320dbd1 commit 0844373

File tree

13 files changed

+115
-60
lines changed

13 files changed

+115
-60
lines changed

MODULE.bazel.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/rules/rules_angular/MODULE.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ npm.npm_translate_lock(
7272
)
7373
use_repo(npm, "rules_angular_npm")
7474

75-
rules_angular = use_extension("//setup:extensions.bzl", "rules_angular")
76-
rules_angular.setup(
75+
rules_angular_dev = use_extension("//setup:extensions.bzl", "rules_angular", dev_dependency = True)
76+
rules_angular_dev.setup(
7777
angular_compiler_cli = "//:node_modules/@angular/compiler-cli",
7878
typescript = "//:node_modules/typescript",
7979
)
80+
81+
rules_angular = use_extension("//setup:extensions.bzl", "rules_angular")
8082
use_repo(rules_angular, "rules_angular_configurable_deps")

bazel/rules/rules_angular/MODULE.bazel.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
load("//setup:repositories.bzl", "configurable_deps_repo")
22

33
def _extension(ctx):
4+
root_setup = None
5+
generated = {}
46
for mod in ctx.modules:
7+
if mod.is_root and mod.tags.setup:
8+
root_setup = mod.tags.setup[0]
9+
elif not root_setup and mod.tags.setup:
10+
root_setup = mod.tags.setup[0]
11+
512
for attr in mod.tags.setup:
13+
if attr.name in generated:
14+
fail("The repository '{}' is already registered by another module. Please use a different name.".format(attr.name))
15+
generated[attr.name] = True
616
configurable_deps_repo(
717
name = attr.name,
818
angular_compiler_cli = attr.angular_compiler_cli,
919
typescript = attr.typescript,
1020
)
1121

22+
if "rules_angular_configurable_deps" not in generated:
23+
if root_setup:
24+
configurable_deps_repo(
25+
name = "rules_angular_configurable_deps",
26+
angular_compiler_cli = root_setup.angular_compiler_cli,
27+
typescript = root_setup.typescript,
28+
)
29+
1230
rules_angular = module_extension(
1331
implementation = _extension,
1432
tag_classes = {
1533
"setup": tag_class(attrs = {
1634
"name": attr.string(default = "rules_angular_configurable_deps"),
17-
"angular_compiler_cli": attr.string(mandatory = True),
18-
"typescript": attr.string(mandatory = True),
35+
"angular_compiler_cli": attr.label(mandatory = True),
36+
"typescript": attr.label(mandatory = True),
1937
}),
2038
},
2139
)

bazel/rules/rules_angular/src/private/symlink_package.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ def _symlink_impl(ctx):
6565
target_path = relative_file(src_manifest_path, destination_manifest_path),
6666
)
6767

68-
runfiles = ctx.runfiles(files = [destination, destination_build])
68+
runfiles = ctx.runfiles(
69+
files = [destination, destination_build],
70+
transitive_files = src[JsInfo].transitive_sources,
71+
)
6972

7073
return [
7174
DefaultInfo(

bazel/rules/rules_angular/src/worker/bazel_safe_filesystem.mts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {createRequire} from 'node:module';
12
import {NodeJSFileSystem, AbsoluteFsPath} from './angular_foundation_utils.mjs';
23

34
// Guarding against unexpected scenarios from within the Bazel worker.
@@ -20,4 +21,10 @@ export class BazelSafeFilesystem extends NodeJSFileSystem {
2021
removeDeep() {
2122
throw new Error('Not implemented');
2223
}
24+
25+
getDefaultLibLocation(): AbsoluteFsPath {
26+
const requireFn = createRequire(import.meta.filename);
27+
28+
return this.resolve(requireFn.resolve('typescript'), '..');
29+
}
2330
}

bazel/rules/rules_angular/src/worker/compiler_host_base.mts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,37 @@ import ts from 'typescript';
66

77
export type AngularHostFactoryFn = (fs: FileSystem, options: ts.CompilerOptions) => ts.CompilerHost;
88

9-
export function createBaseCompilerHost<Host extends ts.CompilerHost>(
9+
/**
10+
* A compiler host that is used for vanilla TypeScript compilations.
11+
* It extends the AngularCompilerHostForVanillaCompilations class and overrides
12+
* the methods in the base class to ensure the correct TS version is used.
13+
*/
14+
class CompilerHostForVanillaCompilations extends AngularCompilerHostForVanillaCompilations {
15+
constructor(fs: FileSystem, options: ts.CompilerOptions) {
16+
super(fs, options);
17+
}
18+
19+
override getDefaultLibFileName(options: ts.CompilerOptions): string {
20+
return this.fs.join(this.fs.getDefaultLibLocation(), ts.getDefaultLibFileName(options));
21+
}
22+
23+
override getSourceFile(
24+
fileName: string,
25+
languageVersion: ts.ScriptTarget,
26+
): ts.SourceFile | undefined {
27+
const text = this.readFile(fileName);
28+
return text !== undefined
29+
? ts.createSourceFile(fileName, text, languageVersion, true)
30+
: undefined;
31+
}
32+
}
33+
34+
/**
35+
* Creates a compiler host for the given options and file system.
36+
* If an Angular factory function is provided, it will be used to create the compiler host.
37+
* Otherwise, a compiler host for vanilla TypeScript compilations will be created.
38+
*/
39+
export function createBaseCompilerHost(
1040
options: ts.CompilerOptions,
1141
fs: FileSystem,
1242
angularFactoryFn: AngularHostFactoryFn | null,
@@ -15,7 +45,7 @@ export function createBaseCompilerHost<Host extends ts.CompilerHost>(
1545
? angularFactoryFn(fs, options)
1646
: // Even for vanilla compilations, we want to use a FS-respecting host. We can
1747
// just use the one from npm safely then.
18-
new AngularCompilerHostForVanillaCompilations(fs, options);
48+
new CompilerHostForVanillaCompilations(fs, options);
1949

2050
// Support `--traceResolution`.
2151
base.trace = (output) => console.error(output);

bazel/rules/rules_angular/src/worker/loop.mts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,9 @@ import {TsStructureIsReused} from './program_abstractions/structure_reused.mjs';
1313
import {VanillaTsProgram} from './program_abstractions/vanilla_ts.mjs';
1414
import {ProgramCache, WorkerProgramCacheEntry} from './program_cache.mjs';
1515
import {WorkRequest} from './protocol/worker.cjs';
16-
import {
17-
AbsoluteFsPath,
18-
NodeJSFileSystem,
19-
readConfiguration,
20-
FileSystem,
21-
setFileSystem,
22-
} from './angular_foundation_utils.mjs';
23-
import {
24-
ProgramDescriptor,
25-
ProgramDescriptorCtor,
26-
} from './program_abstractions/program_descriptor.mjs';
16+
import {AbsoluteFsPath, readConfiguration, setFileSystem} from './angular_foundation_utils.mjs';
17+
import {ProgramDescriptorCtor} from './program_abstractions/program_descriptor.mjs';
18+
import {BazelSafeFilesystem} from './bazel_safe_filesystem.mjs';
2719

2820
// Used for debug counting.
2921
let buildCount = 0;
@@ -70,7 +62,7 @@ export async function executeBuild(
7062
const fs =
7163
workerSortedInputFileNames !== null
7264
? new WorkerSandboxFileSystem(workerSortedInputFileNames)
73-
: new NodeJSFileSystem();
65+
: new BazelSafeFilesystem();
7466

7567
// Note: This is needed because functions like `readConfiguration` do not properly
7668
// re-use the passed `fs`, but call `getFileSystem`.

bazel/rules/rules_browsers/MODULE.bazel.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazel/rules/rules_browsers/test/MODULE.bazel.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)