Skip to content

Commit 111e65b

Browse files
authored
fix: make CcInfo/cc dep in nodejs toolchain opt-in via include_headers attribute (#3760)
1 parent 2fdb873 commit 111e65b

20 files changed

+179
-68
lines changed

.bazelci/presubmit.yml

+38-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ tasks:
1616
- "--test_tag_filters=-skip-on-bazelci-ubuntu"
1717
test_targets:
1818
- "//..."
19+
ubuntu1804-headers:
20+
name: ubuntu1804-headers
21+
platform: ubuntu1804
22+
working_directory: "e2e/headers"
23+
build_targets:
24+
- "//..."
25+
test_flags:
26+
- "--test_tag_filters=-skip-on-bazelci-ubuntu"
27+
test_targets:
28+
- "//..."
1929
ubuntu1804-smoke:
2030
name: ubuntu1804-smoke
2131
platform: ubuntu1804
@@ -36,9 +46,28 @@ tasks:
3646
- "--test_tag_filters=-skip-on-bazelci-ubuntu"
3747
test_targets:
3848
- "//..."
39-
macos-smoke:
49+
macos:
4050
name: macos
4151
platform: macos
52+
build_targets:
53+
- "//..."
54+
test_flags:
55+
- "--test_tag_filters=-skip-on-bazelci-macos"
56+
test_targets:
57+
- "//..."
58+
macos-headers:
59+
name: macos-headers
60+
platform: macos
61+
working_directory: "e2e/headers"
62+
build_targets:
63+
- "//..."
64+
test_flags:
65+
- "--test_tag_filters=-skip-on-bazelci-macos"
66+
test_targets:
67+
- "//..."
68+
macos-smoke:
69+
name: macos-smoke
70+
platform: macos
4271
working_directory: "e2e/smoke"
4372
build_targets:
4473
- "//..."
@@ -66,12 +95,11 @@ tasks:
6695
- "--test_tag_filters=-skip-on-bazelci-windows"
6796
test_targets:
6897
- "//..."
69-
# Temporarily disabled RBE CI until cc toolchain failures are resolved
70-
# rbe_ubuntu1604-smoke:
71-
# name: rbe_ubuntu1604-smoke
72-
# platform: rbe_ubuntu1604
73-
# working_directory: "e2e/smoke"
74-
# build_targets:
75-
# - "//..."
76-
# test_targets:
77-
# - "//..."
98+
rbe_ubuntu1604-smoke:
99+
name: rbe_ubuntu1604-smoke
100+
platform: rbe_ubuntu1604
101+
working_directory: "e2e/smoke"
102+
build_targets:
103+
- "//..."
104+
test_targets:
105+
- "//..."

.bazelrc

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ common --nolegacy_external_runfiles
2828
# https://bazelbuild.slack.com/archives/C014RARENH0/p1691158021917459?thread_ts=1691156601.420349&cid=C014RARENH0
2929
common --check_direct_dependencies=off
3030

31+
# In the root MODULE.bazel file we don't set include_headers on the nodejs toolchain
32+
# so the `//nodejs/headers:current_node_cc_headers`` target will not build. This target
33+
# is instead tested in `e2e/headers``
34+
common --deleted_packages=nodejs/headers
35+
3136
# Load any settings specific to the current user.
3237
# .bazelrc.user should appear in .gitignore so that settings are not shared with team members
3338
# This needs to be last statement in this

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
test:
2020
uses: bazel-contrib/.github/.github/workflows/bazel.yaml@v6
2121
with:
22-
folders: '[".", "e2e/smoke", "e2e/nodejs_host"]'
22+
folders: '[".", "e2e/headers", "e2e/smoke", "e2e/nodejs_host"]'
2323
# stardoc generated docs fail on diff_test with Bazel 6.4.0 so don't test against it in root repository
2424
exclude: |
2525
[

docs/Core.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ UserBuildSettingInfo(<a href="#UserBuildSettingInfo-value">value</a>)
3434

3535
<pre>
3636
nodejs_repositories(<a href="#nodejs_repositories-name">name</a>, <a href="#nodejs_repositories-node_download_auth">node_download_auth</a>, <a href="#nodejs_repositories-node_repositories">node_repositories</a>, <a href="#nodejs_repositories-node_urls">node_urls</a>, <a href="#nodejs_repositories-node_version">node_version</a>,
37-
<a href="#nodejs_repositories-node_version_from_nvmrc">node_version_from_nvmrc</a>, <a href="#nodejs_repositories-kwargs">kwargs</a>)
37+
<a href="#nodejs_repositories-node_version_from_nvmrc">node_version_from_nvmrc</a>, <a href="#nodejs_repositories-include_headers">include_headers</a>, <a href="#nodejs_repositories-kwargs">kwargs</a>)
3838
</pre>
3939

4040
To be run in user's WORKSPACE to install rules_nodejs dependencies.
@@ -143,6 +143,14 @@ If set then the version found in the .nvmrc file is used instead of the one spec
143143

144144
Defaults to `None`
145145

146+
<h4 id="nodejs_repositories-include_headers">include_headers</h4>
147+
148+
Set headers field in NodeInfo provided by this toolchain.
149+
150+
This setting creates a dependency on a c++ toolchain.
151+
152+
Defaults to `False`
153+
146154
<h4 id="nodejs_repositories-kwargs">kwargs</h4>
147155

148156
Additional parameters

e2e/headers/.bazelrc

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Specifies desired output mode for running tests.
2+
# Valid values are
3+
# 'summary' to output only test status summary
4+
# 'errors' to also print test logs for failed tests
5+
# 'all' to print logs for all tests
6+
# 'streamed' to output logs for all tests in real time
7+
# (this will force tests to be executed locally one at a time regardless of --test_strategy value).
8+
common --test_output=errors
9+
10+
# Turn on --incompatible_strict_action_env which was on by default
11+
# in Bazel 0.21.0 but turned off again in 0.22.0. Follow
12+
# https://github.com/bazelbuild/bazel/issues/7026 for more details.
13+
# This flag is needed to so that the bazel cache is not invalidated
14+
# when running bazel via `yarn bazel`.
15+
# See https://github.com/angular/angular/issues/27514.
16+
common --incompatible_strict_action_env
17+
18+
# Turn off legacy external runfiles
19+
# This prevents accidentally depending on this feature, which Bazel will remove.
20+
common --nolegacy_external_runfiles

e2e/headers/.bazelversion

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../.bazelversion

e2e/headers/BUILD.bazel

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cc_test(
2+
name = "using_headers_test",
3+
srcs = ["using_headers.cc"],
4+
copts = select({
5+
"@platforms//os:windows": ["/std:c++14"],
6+
"//conditions:default": ["-std=c++14"],
7+
}),
8+
target_compatible_with = select({
9+
# Windows does not ship headers in the release artifact so this won't work yet.
10+
"@platforms//os:windows": ["@platforms//:incompatible"],
11+
"//conditions:default": [],
12+
}),
13+
deps = ["@rules_nodejs//nodejs/headers:current_node_cc_headers"],
14+
)

e2e/headers/MODULE.bazel

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
bazel_dep(name = "rules_nodejs", version = "0.0.0", dev_dependency = True)
2+
local_path_override(
3+
module_name = "rules_nodejs",
4+
path = "../..",
5+
)
6+
7+
bazel_dep(name = "platforms", version = "0.0.10", dev_dependency = True)
8+
9+
node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node", dev_dependency = True)
10+
node.toolchain(include_headers = True)

e2e/headers/WORKSPACE.bazel

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
local_repository(
4+
name = "rules_nodejs",
5+
path = "../..",
6+
)
7+
8+
load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
9+
10+
nodejs_register_toolchains(include_headers = True)
11+
12+
http_archive(
13+
name = "bazel_skylib",
14+
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
15+
urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz"],
16+
)

e2e/headers/WORKSPACE.bzlmod

Whitespace-only changes.
File renamed without changes.

e2e/smoke/BUILD.bazel

-15
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,3 @@ diff_test(
268268
file1 = "write_node_version_16",
269269
file2 = "thing_toolchain_16",
270270
)
271-
272-
cc_binary(
273-
name = "using_headers_test",
274-
srcs = ["using_headers.cc"],
275-
copts = select({
276-
"@platforms//os:windows": ["/std:c++14"],
277-
"//conditions:default": ["-std=c++14"],
278-
}),
279-
target_compatible_with = select({
280-
# Windows does not ship headers in the release artifact so this won't work yet.
281-
"@platforms//os:windows": ["@platforms//:incompatible"],
282-
"//conditions:default": [],
283-
}),
284-
deps = ["@rules_nodejs//nodejs:current_node_cc_headers"],
285-
)

e2e/smoke/WORKSPACE.bazel

-20
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,3 @@ load("@aspect_bazel_lib//lib:repositories.bzl", "aspect_bazel_lib_dependencies",
4444
aspect_bazel_lib_dependencies()
4545

4646
aspect_bazel_lib_register_toolchains()
47-
48-
#
49-
# RBE configuration
50-
#
51-
# See https://github.com/bazelbuild/continuous-integration/releases/tag/rules-1.0.0
52-
http_archive(
53-
name = "bazelci_rules",
54-
sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e",
55-
strip_prefix = "bazelci_rules-1.0.0",
56-
url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz",
57-
)
58-
59-
load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")
60-
61-
# Creates toolchain configuration for remote execution with BuildKite CI
62-
# for rbe_ubuntu1604
63-
rbe_preconfig(
64-
name = "buildkite_config",
65-
toolchain = "ubuntu1804-bazel-java11",
66-
)

e2e/smoke/WORKSPACE.bzlmod

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
3+
#
4+
# RBE configuration
5+
#
6+
# See https://github.com/bazelbuild/continuous-integration/releases/tag/rules-1.0.0
7+
http_archive(
8+
name = "bazelci_rules",
9+
sha256 = "eca21884e6f66a88c358e580fd67a6b148d30ab57b1680f62a96c00f9bc6a07e",
10+
strip_prefix = "bazelci_rules-1.0.0",
11+
url = "https://github.com/bazelbuild/continuous-integration/releases/download/rules-1.0.0/bazelci_rules-1.0.0.tar.gz",
12+
)
13+
14+
load("@bazelci_rules//:rbe_repo.bzl", "rbe_preconfig")
15+
16+
# Creates toolchain configuration for remote execution with BuildKite CI
17+
# for rbe_ubuntu1604
18+
rbe_preconfig(
19+
name = "buildkite_config",
20+
toolchain = "ubuntu1804-bazel-java11",
21+
)

nodejs/BUILD.bazel

-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
load("//nodejs/private:current_node_cc_headers.bzl", "current_node_cc_headers")
32
load("//nodejs/private:nodejs_toolchains_repo.bzl", "PLATFORMS")
43
load("//nodejs/private:user_build_settings.bzl", "user_args")
54

@@ -41,8 +40,3 @@ user_args(
4140
name = "default_args",
4241
build_setting_default = "--preserve-symlinks",
4342
)
44-
45-
# This target provides the C headers for whatever the current toolchain is
46-
# for the consuming rule. It basically acts like a cc_library by forwarding
47-
# on the providers for the underlying cc_library that the toolchain is using.
48-
current_node_cc_headers(name = "current_node_cc_headers")

nodejs/extensions.bzl

+8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ def _toolchain_extension(module_ctx):
2626
registrations[toolchain.name] = struct(
2727
node_version = toolchain.node_version,
2828
node_version_from_nvmrc = toolchain.node_version_from_nvmrc,
29+
include_headers = toolchain.include_headers,
2930
)
3031

3132
for k, v in registrations.items():
3233
nodejs_register_toolchains(
3334
name = k,
3435
node_version = v.node_version,
3536
node_version_from_nvmrc = v.node_version_from_nvmrc,
37+
include_headers = v.include_headers,
3638
register = False,
3739
)
3840

@@ -54,6 +56,12 @@ node = module_extension(
5456
5557
If set then the version found in the .nvmrc file is used instead of the one specified by node_version.""",
5658
),
59+
"include_headers": attr.bool(
60+
doc = """Set headers field in NodeInfo provided by this toolchain.
61+
62+
This setting creates a dependency on a c++ toolchain.
63+
""",
64+
),
5765
}),
5866
},
5967
)

nodejs/headers/BUILD.bazel

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//nodejs/private:current_node_cc_headers.bzl", "current_node_cc_headers")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
# This target provides the C headers for whatever the current toolchain is
6+
# for the consuming rule. It basically acts like a cc_library by forwarding
7+
# on the providers for the underlying cc_library that the toolchain is using.
8+
current_node_cc_headers(name = "current_node_cc_headers")

nodejs/private/current_node_cc_headers.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cc_library(
4343
srcs = ["foo.cc"],
4444
# If toolchain sets this already, you can omit.
4545
copts = ["-std=c++14"],
46-
deps = ["@rules_nodejs//:current_node_cc_headers"]
46+
deps = ["@rules_nodejs//nodejs/headers:current_node_cc_headers"]
4747
)
4848
```
4949
""",

nodejs/repositories.bzl

+25-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ _ATTRS = {
3131
"node_urls": attr.string_list(),
3232
"node_version": attr.string(),
3333
"node_version_from_nvmrc": attr.label(allow_single_file = True),
34+
"include_headers": attr.bool(),
3435
"platform": attr.string(
3536
doc = "Internal use only. Which platform to install as a toolchain. If unset, we assume the repository is named nodejs_[platform]",
3637
values = BUILT_IN_NODE_PLATFORMS,
@@ -241,6 +242,20 @@ filegroup(
241242
name = "npm_files",
242243
srcs = glob(["bin/nodejs/**"]) + [":node_files"],
243244
)
245+
""".format(
246+
node_bin_export = "\n \"%s\"," % node_bin,
247+
npm_bin_export = "\n \"%s\"," % npm_bin,
248+
npx_bin_export = "\n \"%s\"," % npx_bin,
249+
node_bin_label = node_bin_label,
250+
npm_bin_label = npm_bin_label,
251+
npx_bin_label = npx_bin_label,
252+
node_entry = node_entry,
253+
npm_entry = npm_entry,
254+
npx_entry = npx_entry,
255+
)
256+
257+
if repository_ctx.attr.include_headers:
258+
build_content += """
244259
cc_library(
245260
name = "headers",
246261
hdrs = glob(
@@ -256,17 +271,7 @@ cc_library(
256271
),
257272
includes = ["bin/nodejs/include/node"],
258273
)
259-
""".format(
260-
node_bin_export = "\n \"%s\"," % node_bin,
261-
npm_bin_export = "\n \"%s\"," % npm_bin,
262-
npx_bin_export = "\n \"%s\"," % npx_bin,
263-
node_bin_label = node_bin_label,
264-
npm_bin_label = npm_bin_label,
265-
npx_bin_label = npx_bin_label,
266-
node_entry = node_entry,
267-
npm_entry = npm_entry,
268-
npx_entry = npx_entry,
269-
)
274+
"""
270275

271276
if repository_ctx.attr.platform:
272277
build_content += """
@@ -276,14 +281,15 @@ nodejs_toolchain(
276281
node = ":node_bin",
277282
npm = ":npm",
278283
npm_srcs = [":npm_files"],
279-
headers = ":headers",
284+
headers = {headers},
280285
)
281286
# alias for backward compat
282287
alias(
283288
name = "node_toolchain",
284289
actual = ":toolchain",
285290
)
286-
"""
291+
""".format(headers = "\":headers\"" if repository_ctx.attr.include_headers else "None")
292+
287293
repository_ctx.file("BUILD.bazel", content = build_content)
288294

289295
def _strip_bin(path):
@@ -314,6 +320,7 @@ def nodejs_repositories(
314320
node_urls = [DEFAULT_NODE_URL],
315321
node_version = DEFAULT_NODE_VERSION,
316322
node_version_from_nvmrc = None,
323+
include_headers = False,
317324
**kwargs):
318325
"""To be run in user's WORKSPACE to install rules_nodejs dependencies.
319326
@@ -394,6 +401,10 @@ def nodejs_repositories(
394401
395402
If set then the version found in the .nvmrc file is used instead of the one specified by node_version.
396403
404+
include_headers: Set headers field in NodeInfo provided by this toolchain.
405+
406+
This setting creates a dependency on a c++ toolchain.
407+
397408
**kwargs: Additional parameters
398409
"""
399410
use_nvmrc = kwargs.pop("use_nvmrc", None)
@@ -411,6 +422,7 @@ WARNING: use_nvmrc attribute of node_repositories is deprecated; use node_versio
411422
node_urls = node_urls,
412423
node_version = node_version,
413424
node_version_from_nvmrc = node_version_from_nvmrc,
425+
include_headers = include_headers,
414426
**kwargs
415427
)
416428

0 commit comments

Comments
 (0)