Skip to content

Commit aab0894

Browse files
devversionjelbourn
authored andcommitted
build: add workaround for bug breaking ts_library hermeticity (#17225)
Patches `@bazel/typescript` to not be non-hermetic. We need to work around this because it impacts the Bazel development workflow significantly if file changes sometimes need a `bazel clean` (basically breaking incrementality). Issue is reported with in-depth details: bazel-contrib/rules_nodejs#1208.
1 parent cc9d127 commit aab0894

File tree

3 files changed

+91
-3
lines changed

3 files changed

+91
-3
lines changed

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ yarn_install(
4040
data = [
4141
"//:angular-tsconfig.json",
4242
"//:tools/bazel/flat_module_factory_resolution.patch",
43+
"//:tools/bazel/manifest_externs_hermeticity.patch",
4344
"//:tools/bazel/postinstall-patches.js",
4445
"//:tools/bazel/rollup_windows_arguments.patch",
4546
"//:tools/npm/check-npm.js",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
diff --git node_modules/@bazel/typescript/internal/build_defs.bzl node_modules/@bazel/typescript/internal/build_defs.bzl
2+
index 7fef44c..4e25ed7 100644
3+
--- node_modules/@bazel/typescript/internal/build_defs.bzl
4+
+++ node_modules/@bazel/typescript/internal/build_defs.bzl
5+
@@ -93,25 +93,23 @@ def _filter_ts_inputs(all_inputs):
6+
if f.path.endswith(".js") or f.path.endswith(".ts") or f.path.endswith(".json")
7+
]
8+
9+
+def _filter_compile_outputs(all_outputs):
10+
+ return [
11+
+ f
12+
+ for f in all_outputs
13+
+ if not f.basename.endswith(".externs.js") and not f.basename.endswith(".es5.MF")
14+
+ ]
15+
+
16+
def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description = "prodmode"):
17+
externs_files = []
18+
action_inputs = inputs
19+
- action_outputs = []
20+
- for output in outputs:
21+
- if output.basename.endswith(".externs.js"):
22+
- externs_files.append(output)
23+
- elif output.basename.endswith(".es5.MF"):
24+
- ctx.actions.write(output, content = "")
25+
- else:
26+
- action_outputs.append(output)
27+
-
28+
- # TODO(plf): For now we mock creation of files other than {name}.js.
29+
- for externs_file in externs_files:
30+
- ctx.actions.write(output = externs_file, content = "")
31+
+ compile_outputs = _filter_compile_outputs(outputs)
32+
33+
# A ts_library that has only .d.ts inputs will have no outputs,
34+
# therefore there are no actions to execute
35+
- if not action_outputs:
36+
+ if not compile_outputs:
37+
+ for file in [f for f in outputs if not f in compile_outputs]:
38+
+ ctx.actions.write(output = file, content = "")
39+
return None
40+
41+
action_inputs.extend(_filter_ts_inputs(ctx.files.node_modules))
42+
@@ -146,7 +142,7 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description
43+
progress_message = "Compiling TypeScript (%s) %s" % (description, ctx.label),
44+
mnemonic = mnemonic,
45+
inputs = action_inputs,
46+
- outputs = action_outputs,
47+
+ outputs = outputs,
48+
# Use the built-in shell environment
49+
# Allow for users who set a custom shell that can locate standard binaries like tr and uname
50+
# See https://github.com/NixOS/nixpkgs/issues/43955#issuecomment-407546331
51+
@@ -163,7 +159,7 @@ def _compile_action(ctx, inputs, outputs, tsconfig_file, node_opts, description
52+
label = ctx.label,
53+
tsconfig = tsconfig_file,
54+
inputs = action_inputs,
55+
- outputs = action_outputs,
56+
+ outputs = compile_outputs,
57+
compiler = ctx.executable.compiler,
58+
)
59+
60+
diff --git node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js
61+
index 0346123..3d9bc64 100644
62+
--- node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js
63+
+++ node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js
64+
@@ -307,6 +307,23 @@
65+
}
66+
else {
67+
diagnostics = emitWithTypescript(program, compilationTargets, transforms);
68+
+ if (bazelOpts.manifest) {
69+
+ fs.writeFileSync(bazelOpts.manifest, "");
70+
+ }
71+
+ if (bazelOpts.tsickleExternsPath) {
72+
+ fs.writeFileSync(bazelOpts.tsickleExternsPath, "");
73+
+ if (bazelOpts.tsickleGenerateExterns && compilerHost.provideExternalModuleDtsNamespace) {
74+
+ for (const extern of compilationTargets) {
75+
+ if (!extern.isDeclarationFile)
76+
+ continue;
77+
+ const outputBaseDir = options.outDir;
78+
+ const relativeOutputPath = compilerHost.relativeOutputPath(extern.fileName);
79+
+ mkdirp(outputBaseDir, path.dirname(relativeOutputPath));
80+
+ const outputPath = path.join(outputBaseDir, relativeOutputPath);
81+
+ fs.writeFileSync(outputPath, "");
82+
+ }
83+
+ }
84+
+ }
85+
}
86+
if (diagnostics.length > 0) {
87+
console.error(bazelDiagnostics.format(bazelOpts.target, diagnostics));

tools/bazel/postinstall-patches.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ shelljs.exec('ngc -p angular-tsconfig.json');
2929
searchAndReplace(
3030
/(this\.transformTypesToClosure) = bazelOpts\.tsickle;/, '$1 = false;',
3131
'node_modules/@bazel/typescript/internal/tsc_wrapped/compiler_host.js');
32-
searchAndReplace(
33-
'bazelOpts\.tsickleExternsPath', 'null',
34-
'node_modules/@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js');
3532

3633
// Workaround for https://github.com/angular/angular/issues/32389. We need to ensure
3734
// that tsickle is available for esm5 output re-compilations.
@@ -99,6 +96,9 @@ searchAndReplace(
9996
`$1 + [m for dep in ctx.attr.deps if hasattr(dep, "angular") for m in dep.angular.metadata],`,
10097
'node_modules/@angular/bazel/src/ng_module.bzl');
10198

99+
// Workaround for: https://github.com/bazelbuild/rules_nodejs/issues/1208.
100+
shelljs.cat(path.join(__dirname, './manifest_externs_hermeticity.patch')).exec('patch -p0');
101+
102102
/**
103103
* Reads the specified file and replaces matches of the search expression
104104
* with the given replacement. Throws if no changes were made.

0 commit comments

Comments
 (0)