From f371448bf047b294557f6fc1ecec04dcefc62f93 Mon Sep 17 00:00:00 2001 From: Klaus Aehlig Date: Wed, 25 Sep 2019 08:55:51 -0700 Subject: [PATCH] Also for default attributes correctly convert strings to labels ...by taking the applicable repository mapping into account, instead of simply assuming it is empty. Related to #7130, #3115. Change-Id: I7f3274b7e238ec0a9ad00643b738fbb12b84a872 PiperOrigin-RevId: 271133642 --- .../lib/analysis/skylark/SkylarkAttr.java | 8 ++- src/test/shell/bazel/workspace_test.sh | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java index b8636b5e1e60bc..bfef40d1e46f87 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/skylark/SkylarkAttr.java @@ -25,6 +25,7 @@ import com.google.devtools.build.lib.analysis.config.TransitionFactories; import com.google.devtools.build.lib.analysis.config.transitions.SplitTransition; import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory; +import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.Attribute; import com.google.devtools.build.lib.packages.Attribute.AllowedValueSet; @@ -145,7 +146,12 @@ private static Attribute.Builder createAttribute( } else if (defaultValue instanceof SkylarkLateBoundDefault) { builder.value((SkylarkLateBoundDefault) defaultValue); } else { - builder.defaultValue(defaultValue, env.getGlobals().getLabel(), DEFAULT_ARG); + builder.defaultValue( + defaultValue, + new BuildType.LabelConversionContext( + (Label) env.getGlobals().getLabel(), + BazelStarlarkContext.from(env).getRepoMapping()), + DEFAULT_ARG); } } diff --git a/src/test/shell/bazel/workspace_test.sh b/src/test/shell/bazel/workspace_test.sh index c0412edb183de1..46120eb89d8be8 100755 --- a/src/test/shell/bazel/workspace_test.sh +++ b/src/test/shell/bazel/workspace_test.sh @@ -1163,4 +1163,65 @@ EOF } +function test_renaming_visibility_via_default() { + mkdir local_a + touch local_a/WORKSPACE + cat > local_a/BUILD <<'EOF' +genrule( + name = "x", + outs = ["x.txt"], + cmd = "echo Hello World > $@", + visibility = ["@foo//data:__pkg__"], +) +EOF + mkdir mainrepo + cd mainrepo + cat > WORKSPACE <<'EOF' +workspace(name="foo") +local_repository( + name = "source", + path = "../local_a", +) +EOF + mkdir data + cat > data/BUILD <<'EOF' +genrule( + name = "y", + srcs = ["@source//:x"], + cmd = "cp $< $@", + outs = ["y.txt"], + visibility = ["@foo//:__pkg__"], +) +EOF + cat > datarule.bzl <<'EOF' +def _data_impl(ctx): + output = ctx.actions.declare_file(ctx.label.name + ".txt") + ctx.actions.run_shell( + inputs = ctx.files._data, + outputs = [output], + command = "cp $1 $2", + arguments = [ctx.files._data[0].path, output.path], + ) + +data = rule( + implementation = _data_impl, + attrs = { + "_data": attr.label(default="@foo//data:y"), + }, + outputs = { "txt" : "%{name}.txt"}, +) +EOF + cat >BUILD <<'EOF' +load("//:datarule.bzl", "data") +data(name="it") +EOF + echo; echo not remapping main repo; echo + bazel build --incompatible_remap_main_repo=false @foo//:it \ + || fail "Expected success" + + echo; echo remapping main repo; echo + bazel build --incompatible_remap_main_repo=true @foo//:it \ + || fail "Expected success" + +} run_suite "workspace tests"