Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird behavior with alias and target_compatible_with #17663

Closed
tgeng opened this issue Mar 4, 2023 · 6 comments
Closed

Weird behavior with alias and target_compatible_with #17663

tgeng opened this issue Mar 4, 2023 · 6 comments
Assignees
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) platform: apple team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug

Comments

@tgeng
Copy link
Contributor

tgeng commented Mar 4, 2023

Description of the bug:

Setting target_compatible_with on alias does not work as expected. See repro below.

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Consider the following top-level BUILD.bazel file. (full repro bazel-alias-bug.zip)

filegroup(
  name = "source",
  srcs = ["foo.c"],
)

alias(
  name = "source_alias",
  actual = "source",
  target_compatible_with = ["@platforms//cpu:x86_64"],
)

alias(
  name = "source_alias2",
  actual = select({
    "@platforms//cpu:x86_64": "source",
    "//conditions:default": "source",
  }),
  target_compatible_with = ["@platforms//cpu:x86_64"],
)

cc_library(
  name = "foo",
  srcs = ["source_alias"],
)

cc_library(
  name = "foo2",
  srcs = ["source_alias2"],
)

foo and foo2 should behave the same. However, on an M1 Mac with Bazel 6.0.0, I have

 ~/tmp/bazel_alias_bug  master ───────────────────────────────────────────────────────────────────── system py  22:39:33
> bazel build :foo
INFO: Analyzed target //:foo (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:foo up-to-date:
  bazel-bin/libfoo.a
INFO: Elapsed time: 0.081s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
 ~/tmp/bazel_alias_bug  master ───────────────────────────────────────────────────────────────────── system py  22:39:34
> bazel build :foo2
ERROR: Target //:foo2 is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //:foo2 (602a7d)
    //:source_alias2 (602a7d)   <-- target platform (@local_config_platform//:host) didn't satisfy constraint @platforms//cpu:x86_64
INFO: Elapsed time: 0.044s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

With Bazel 5.4.0, I have

> bazel build :foo
Starting local Bazel server and connecting to it...
ERROR: Target //:foo is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //:foo
    //:source_alias   <-- target platform (null) didn't satisfy constraint @platforms//cpu:x86_64
INFO: Elapsed time: 7.002s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (38 packages loaded, 263 targets configured)
 ~/tmp/bazel_alias_bug  master !1 ────────────────────────────────────────────────────────────── 7s  system py  22:40:16
> bazel build :foo2
ERROR: Target //:foo2 is incompatible and cannot be built, but was explicitly requested.
Dependency chain:
    //:foo2
    //:source_alias2   <-- target platform (@local_config_platform//:host) didn't satisfy constraint @platforms//cpu:x86_64
INFO: Elapsed time: 0.088s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 2 targets configured)

Behavior of foo2 is correct in both cases. foo is both incorrect in 5.4.0 and 6.0.0.

Which operating system are you running Bazel on?

macOS

What is the output of bazel info release?

release 5.4.0 and release 6.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@sgowroji sgowroji added team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug platform: apple untriaged labels Mar 5, 2023
@gregestren gregestren self-assigned this Mar 17, 2023
@gregestren
Copy link
Contributor

Assigned to self for triage.

@gregestren
Copy link
Contributor

gregestren commented Mar 20, 2023

I can reproduce.

I wonder if this is because the logic doesn't check source files for compatibility, although I'm not sure what makes the select() different.

@philsc for reference.

@gregestren
Copy link
Contributor

Maybe #14096 explains the different in Bazel 6.0?

@gregestren gregestren added P3 We're not considering working on this, but happy to review a PR. (No assignee) and removed untriaged labels Mar 20, 2023
@philsc
Copy link
Contributor

philsc commented Mar 22, 2023

The difference in 6.0 is indeed caused by the refactor in #14096. I thought I got this working, but apparently it doesn't work for alias without a select().

In particular, the platformInfo is null for the non-select alias():

platformInfo =
unloadedToolchainContexts != null ? unloadedToolchainContexts.getTargetPlatform() : null;

I haven't dug into why, but I'm guessing it has something to do with the fact that we want to be able to create aliases to platform constraints. In which case we don't want to evaluate toolchain information unless necessary.

@philsc
Copy link
Contributor

philsc commented Mar 26, 2023

Okay, piggy backing on @gregestren 's #14310, this patch appears to fix the issue:

diff --git a/src/main/java/com/google/devtools/build/lib/packages/Rule.java b/src/main/java/com/google/devtools/build/lib/packages/Rule.java
index a931906337..d7a9fef42c 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/Rule.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/Rule.java
@@ -1225,8 +1225,10 @@ public class Rule implements Target, DependencyFilter.AttributeInfoProvider {
       return true;
     } else if (mode == ToolchainResolutionMode.HAS_SELECT) {
       RawAttributeMapper attr = RawAttributeMapper.of(this);
-      return (attr.has(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE)
-          && !attr.get(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE, BuildType.LABEL_LIST).isEmpty());
+      return ((attr.has(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE)
+          && !attr.get(RuleClass.CONFIG_SETTING_DEPS_ATTRIBUTE, BuildType.LABEL_LIST).isEmpty()) ||
+          (attr.has(RuleClass.TARGET_COMPATIBLE_WITH_ATTR)
+           && !attr.get(RuleClass.TARGET_COMPATIBLE_WITH_ATTR, BuildType.LABEL_LIST).isEmpty()));
     } else {
       return false;
     }

@gregestren , WDYT about that? It seems fairly innocuous. At least compared to the existing logic.

Happy to rename the HAS_SELECT to something more generic too.

@gregestren
Copy link
Contributor

@philsc that sounds good. Thanks for getting to the bottom of this.

I don't know what the right updated name to HAS_SELECT would be. But conceptually I think it'd be some term expressing "needs toolchain resolution for reasons outside the rules-specific implementation logic".

fweikert pushed a commit to fweikert/bazel that referenced this issue May 25, 2023
Currently the `target_compatible_with` on `alias()` targets only works
when a `select()` is involved. That's because `alias()` targets by
default don't have a toolchains evaluated.

This patch changes the behaviour by making it so all `alias()` targets
with a `target_compatible_with` attribute are skipped appropriately.
When `target_compatible_with` is present, then toolchains are
evaluated for `alias()` targets.

The implementation is basically an enhancement to @gregestren's
1c3a245 (bazelbuild#14310). In
addition to resolving toolchains when a `select()` is present, Bazel
will now also resolve toolchains when a `target_compatible_with`
attribute is set.

I also took this opportunity to rename `HAS_SELECT` to something
more appropriate. The name may be too long now, but I feel
that at least it's descriptive.

Fixes bazelbuild#17663

Closes bazelbuild#17983.

PiperOrigin-RevId: 522446438
Change-Id: I80bce3e840553b75960022545df7f27ad1d1d363
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 We're not considering working on this, but happy to review a PR. (No assignee) platform: apple team-Configurability platforms, toolchains, cquery, select(), config transitions type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants