bazelbuild / bazelbuild/rules_cc
Linkstamping broken on external repos
- Dominant language
- Starlark
- Stars
- 247
- Forks
- 196
- PR merge metrics
- No merged PRs in 30d
Description
Passing a linkstamp to a cc_library and using it, all within an external repo, currently crashes Bazel due to an internal error. This is a regression from the version built into Bazel.
### Repro
You can repro the issue by setting up a very simple workspace as follows:
`MODULE.bazel`
```starlark
module(name = "repro")
bazel_dep(name = "rules_cc", version = "0.2.22")
bazel_dep(name = "ext", version = "0.0.0")
local_path_override(module_name = "ext", path = "ext")
```
`BUILD.bazel`
```starlark
```
`ext/MODULE.bazel`
```starlark
module(name = "ext")
bazel_dep(name = "rules_cc", version = "0.2.22")
```
`ext/BUILD.bazel`
```starlark
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
cc_library(
name = "stamped",
linkstamp = "linkstamp.cc",
)
cc_binary(
name = "app",
srcs = ["main.cc"],
deps = [":stamped"],
)
```
`ext/main.cc`
```c
#include
int main(void) {
printf("Hello, World!\n");
return 0;
}
```
`ext/linkstamp.cc`
```c
```
Then trying to build or run the app crashes bazel:
```bash
$ bazel run @ext//:app
Starting local Bazel server (9.2.0) and connecting to it...
Analyzing: target @@ext+//:app (92 packages loaded, 539 targets configured)
FATAL: bazel crashed due to an internal error. Printing stack trace:
java.lang.RuntimeException: Unrecoverable error while evaluating node 'ConfiguredTargetKey{label=@@ext+//:app, config=BuildConfigurationKey[a7a71fd16b14112464272f754633480c74c664f2488224c592ebf2dfc7a06b18]}' (requested by nodes 'ConfiguredTargetKey{label=@@ext+//:app, config=BuildConfigurationKey[e722a217ced723dfeb49874b2007895ae0dc9a9d49d004d95320b88c609c409a]}')
at com.google.devtools.build.skyframe.AbstractParallelEvaluator$Evaluate.run(AbstractParallelEvaluator.java:552)
at com.google.devtools.build.lib.concurrent.AbstractQueueVisitor$WrappedRunnable.run(AbstractQueueVisitor.java:435)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.compute(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.compute(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask$InterruptibleTask.exec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(Unknown Source)
at java.base/java.util.concurrent.ForkJoinPool.runWorker(Unknown Source)
at java.base/java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
Caused by: net.starlark.java.eval.Starlark$UncheckedEvalException: IllegalStateException thrown during Starlark evaluation (@@ext+//:app)
at .declare_shareable_artifact(:0)
at ._map_linkstamps_to_outputs(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/link/cpp_link_action.bzl:272)
at .link_action(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/link/cpp_link_action.bzl:144)
at ._create_dynamic_link_actions(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/link/cc_linking_helper.bzl:346)
at .create_cc_link_actions(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/link/cc_linking_helper.bzl:205)
at .link(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/link/link.bzl:166)
at ._link(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/cc_common.bzl:127)
at ._create_transitive_linking_actions(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/rules_impl/cc_binary_impl.bzl:382)
at .cc_binary_impl(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/rules_impl/cc_binary_impl.bzl:661)
at ._impl(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/rules_impl/cc_binary_impl.bzl:852)
at .proxy(/home/tobi/.cache/bazel/_bazel_tobi/27178307ead1f1017af790e74778f1f3/external/rules_cc+/cc/private/rules_impl/function_providing_rule.bzl:30)
Caused by: java.lang.IllegalStateException: Output artifact '../ext+/_objs/ext+/linkstamp.o' not under package directory 'external/ext+' for target '@@ext+//:app'
at com.google.common.base.Preconditions.checkState(Preconditions.java:853)
at com.google.devtools.build.lib.analysis.RuleContext.getDerivedArtifact(RuleContext.java:746)
at com.google.devtools.build.lib.rules.cpp.CppLinkActionBuilder$LinkActionConstruction.create(CppLinkActionBuilder.java:73)
at com.google.devtools.build.lib.rules.cpp.CcStarlarkInternal$WrappedStarlarkActionFactory.createShareableArtifact(CcStarlarkInternal.java:454)
...
```
### Fix
I managed to create a simple patch that fixes this for me:
```patch
diff --git a/cc/private/link/cpp_link_action.bzl b/cc/private/link/cpp_link_action.bzl
index 8842e78..5e48f56 100644
--- a/cc/private/link/cpp_link_action.bzl
+++ b/cc/private/link/cpp_link_action.bzl
@@ -257,7 +257,7 @@ def _map_linkstamps_to_outputs(actions, linkstamps, output):
"""
map = {}
- stamp_output_dir = paths.join(paths.dirname(output.short_path), "_objs", output.basename)
+ stamp_output_dir = paths.join(paths.dirname(root_relative_path(output)), "_objs", output.basename)
linkstamps = set(linkstamps)
seen_linkstamp_sources = set()
for linkstamp in linkstamps:
@@ -267,7 +267,7 @@ def _map_linkstamps_to_outputs(actions, linkstamps, output):
linkstamp_file = linkstamp.file()
stamp_output_path = paths.join(
stamp_output_dir,
- paths.replace_extension(linkstamp_file.short_path, ".o"),
+ paths.replace_extension(root_relative_path(linkstamp_file), ".o"),
)
stamp_output_file = actions.declare_shareable_artifact(stamp_output_path)
map[linkstamp] = stamp_output_file
```
```bash
$ bazel run @ext//:app --override_module=rules_cc=~/repos/rules_cc
Starting local Bazel server (9.2.0) and connecting to it...
INFO: Analyzed target @@ext+//:app (92 packages loaded, 543 targets configured).
INFO: Found 1 target...
Target @@ext+//:app up-to-date:
bazel-bin/external/ext+/app
INFO: Elapsed time: 2.532s, Critical Path: 0.01s
INFO: 1 process: 8 action cache hit, 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Running command line: bazel-bin/external/ext+/app
Hello, World!
```
Contributor guide
Research direction
Start with cc/private/link/cpp_link_action.bzl, especially _map_linkstamps_to_outputs, and run the supplied external-repository reproduction with bazel run @ext//:app. Compare the proposed path handling with the artifact paths in the stack trace; the work is done when the external cc_library linkstamp builds and the app runs without an internal error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 78/100