bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Make variable substitution in local_defines in C++ rule does not accept labels from `data`

Open joelwilliamson opened this issue 4 years ago • 1 comments

Description of the problem / feature request:

In C++ rules, local_defines allow "Make" variable substitution, including execpath: List of defines to add to the compile line. Subject to "Make" variable substitution and Bourne shell tokenization. (https://docs.bazel.build/versions/main/be/c-cpp.html#cc_binary.local_defines)

In "Make" variable substitution, C++ rules are documented to be able to use labels referenced in the data attribute: All referenced labels must appear in the consuming rule's srcs, output files, or deps. Otherwise the build fails. C++ rules can also reference labels in data. (https://docs.bazel.build/versions/main/be/make-variables.html)

However, if a local_defines rule tries to do "VAR=\"$(execpath :data_label)\"", the expansion fails with the error "label ':data_label' in $(location) expression is not a declared prerequisite of this rule."

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

filegroup(
  name = "data_label",
  srcs  = ["data_file"],
)

cc_library(
  name = "lib",
  srcs = ["lib.cc"],
  hdrs = ["lib.hh"],
  data = [":data_label"],
  local_defines = ["VAR=\"$(execpath :data_label)\""],
)

What operating system are you running Bazel on?

Linux

What's the output of bazel info release?

2021/08/31 23:23:18 Using unreleased version at commit 662cf54de7a103db30e04ebae2d2b919437c4846 development version

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

Have you found anything relevant by searching the web?

https://github.com/bazelbuild/bazel/commit/cfb6989621c698890eca189e423ddb82ae89e3e0#diff-dded2426bd501014f754eeb69f0eb986a5db2a8908ff219106af4c76a5da09e2R531

    ImmutableMap.Builder<Label, ImmutableCollection<Artifact>> builder = ImmutableMap.builder();

    if (ruleContext.attributes().has("deps", LABEL_LIST)) {
      for (TransitiveInfoCollection current : ruleContext.getPrerequisites("deps")) {
        builder.put(
            AliasProvider.getDependencyLabel(current),
            current.getProvider(FileProvider.class).getFilesToBuild().toList());
      }
    }

I think this should also get do getPrerequisites("data")

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

joelwilliamson avatar Sep 01 '21 03:09 joelwilliamson

Is there a work-around available for this?

If not, can this be pack-ported to bazel 5? https://github.com/bazelbuild/bazel/pull/13938

I'm still seeing this on bazel 5.3.1.

garymm avatar Sep 19 '22 18:09 garymm

This seems fixed on Bazel 6. I think it should be closed.

garymm avatar Mar 10 '23 20:03 garymm

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

github-actions[bot] avatar May 14 '24 01:05 github-actions[bot]