Skip to content

Commit

Permalink
Also for default attributes correctly convert strings to labels
Browse files Browse the repository at this point in the history
...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
  • Loading branch information
aehlig authored and copybara-github committed Sep 25, 2019
1 parent b863997 commit f371448
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
61 changes: 61 additions & 0 deletions src/test/shell/bazel/workspace_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit f371448

Please sign in to comment.