Skip to content

Commit fcb08e9

Browse files
committed
build: add workaround for invalid imports generation in bazel
Applies a workaround for the invalid imports generation issue in the Bazel `ng_module` rule. Read more about this here: https://hackmd.io/@devversion/ryCOwIKIS.
1 parent d5e41a0 commit fcb08e9

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

tools/bazel/postinstall-patches.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,23 @@ shelljs.sed('-i', 'var indexFile;', `
4949
if (!shelljs.test('-f', 'node_modules/@angular/bazel/src/ng_package/rollup_bin.js')) {
5050
shelljs.cat(path.join(__dirname, './rollup_windows_arguments.patch')).exec('patch -p0');
5151
}
52+
53+
// Workaround for: https://hackmd.io/MlqFp-yrSx-0mw4rD7dnQQ?both. We only want to discard
54+
// the metadata of files in the bazel managed node modules. That way we keep the default
55+
// behavior of ngc-wrapped except for dependencies between sources of the library. This makes
56+
// the "generateCodeForLibraries" flag more accurate in the Bazel environment where previous
57+
// compilations should not be treated as external libraries. Read more about this in the document.
58+
shelljs.sed('-i', /if \((this\.options\.generateCodeForLibraries === false)/,
59+
`if (filePath.includes('node_modules/') && $1`,
60+
'node_modules/@angular/compiler-cli/src/transformers/compiler_host.js');
61+
// The three replacements below ensure that metadata files can be read by NGC and
62+
// that metadata files are collected as Bazel action inputs.
63+
shelljs.sed('-i', /(const NGC_ASSETS = \/[^(]+\()([^)]*)(\).*\/;)/, '$1$2|metadata.json$3',
64+
'node_modules/@angular/bazel/src/ngc-wrapped/index.js');
65+
shelljs.sed('-i', /^((\s*)results = depset\(dep.angular.summaries, transitive = \[results]\))$/,
66+
`$1#\n$2results = depset(dep.angular.metadata, transitive = [results])`,
67+
'node_modules/@angular/bazel/src/ng_module.bzl');
68+
shelljs.sed('-i',
69+
/^((\s*)results = depset\(target.angular.summaries if hasattr\(target, "angular"\) else \[]\))$/,
70+
`$1#\n$2results = depset(target.angular.metadata if hasattr(target, "angular") else [], transitive = [results])`,
71+
'node_modules/@angular/bazel/src/ng_module.bzl');

tools/defaults.bzl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ load("@npm_bazel_karma//:defs.bzl", _karma_web_test_suite = "karma_web_test_suit
66
load("@npm_bazel_protractor//:index.bzl", _protractor_web_test_suite = "protractor_web_test_suite")
77
load("@npm_bazel_typescript//:defs.bzl", _ts_library = "ts_library")
88
load("//:packages.bzl", "ANGULAR_LIBRARY_UMDS", "VERSION_PLACEHOLDER_REPLACEMENTS")
9-
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
109
load("//tools:default_module_name.bzl", "get_default_module_name")
10+
load("//tools/markdown-to-html:index.bzl", _markdown_to_html = "markdown_to_html")
1111

1212
_DEFAULT_TSCONFIG_BUILD = "//src:bazel-tsconfig-build.json"
1313
_DEFAULT_TSCONFIG_TEST = "//src:tsconfig-test"
@@ -44,13 +44,32 @@ def ts_library(tsconfig = None, deps = [], testonly = False, **kwargs):
4444
**kwargs
4545
)
4646

47-
def ng_module(deps = [], tsconfig = None, module_name = None, testonly = False, **kwargs):
47+
def ng_module(
48+
deps = [],
49+
srcs = [],
50+
tsconfig = None,
51+
module_name = None,
52+
flat_module_out_file = None,
53+
testonly = False,
54+
**kwargs):
4855
if not tsconfig:
4956
tsconfig = _getDefaultTsConfig(testonly)
5057

5158
if not module_name and not testonly:
5259
module_name = get_default_module_name()
5360

61+
if module_name and not flat_module_out_file:
62+
flat_module_out_file = "index"
63+
64+
# Workaround to avoid a lot of changes to the Bazel build rules. Since
65+
# for most targets the flat module out file is "index.js", we cannot
66+
# include "index.ts" (if present) as source-file. This would resolve
67+
# in a conflict in the metadata bundler. Once we switch to Ivy and
68+
# no longer need metadata bundles, we can remove this logic.
69+
if flat_module_out_file == "index":
70+
if "index.ts" in srcs:
71+
srcs.remove("index.ts")
72+
5473
local_deps = [
5574
# Add tslib because we use import helpers for all public packages.
5675
"@npm//tslib",
@@ -67,8 +86,10 @@ def ng_module(deps = [], tsconfig = None, module_name = None, testonly = False,
6786
local_deps = local_deps + [d]
6887

6988
_ng_module(
89+
srcs = srcs,
7090
type_check = _ENABLE_NG_TYPE_CHECKING,
7191
module_name = module_name,
92+
flat_module_out_file = flat_module_out_file,
7293
deps = local_deps,
7394
tsconfig = tsconfig,
7495
testonly = testonly,

0 commit comments

Comments
 (0)