Skip to content

Commit

Permalink
Make repository-local labels in visibility declarations actually be r…
Browse files Browse the repository at this point in the history
…epository-local.

Fixes bazelbuild#765.

--
MOS_MIGRATED_REVID=112027627
  • Loading branch information
lberki authored and damienmg committed Jan 13, 2016
1 parent eea8efa commit 17ed2ce
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 35 deletions.
15 changes: 14 additions & 1 deletion src/main/java/com/google/devtools/build/lib/cmdline/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.google.devtools.build.lib.cmdline;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.cmdline.LabelValidator.BadLabelException;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
Expand Down Expand Up @@ -43,6 +44,18 @@
public final class Label implements Comparable<Label>, Serializable, SkylarkPrintableValue {
public static final PathFragment EXTERNAL_PACKAGE_NAME = new PathFragment("external");

/**
* Package names that aren't made relative to the current repository because they mean special
* things to Bazel.
*/
private static final ImmutableSet<PathFragment> ABSOLUTE_PACKAGE_NAMES = ImmutableSet.of(
// dependencies that are a function of the configuration
new PathFragment("tools/defaults"),
// Visibility is labels aren't actually targets
new PathFragment("visibility"),
// There is only one //external package
Label.EXTERNAL_PACKAGE_NAME);

public static final PackageIdentifier EXTERNAL_PACKAGE_IDENTIFIER =
PackageIdentifier.createInDefaultRepo(EXTERNAL_PACKAGE_NAME);

Expand Down Expand Up @@ -386,7 +399,7 @@ public Label resolveRepositoryRelative(Label relative) {

if (packageIdentifier.getRepository().isDefault()
|| !relative.packageIdentifier.getRepository().isDefault()
|| relative.packageIdentifier.getPackageFragment().equals(EXTERNAL_PACKAGE_NAME)) {
|| ABSOLUTE_PACKAGE_NAMES.contains(relative.getPackageIdentifier().getPackageFragment())) {
return relative;
} else {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.BuildType.SelectorList;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.vfs.PathFragment;

import javax.annotation.Nullable;

Expand All @@ -32,19 +30,6 @@
* data before exposing it to consumers.
*/
public abstract class AbstractAttributeMapper implements AttributeMap {

/**
* Package names that aren't made relative to the current repository because they mean special
* things to Bazel.
*/
private static final ImmutableSet<PathFragment> ABSOLUTE_PACKAGE_NAMES = ImmutableSet.of(
// dependencies that are a function of the configuration
new PathFragment("tools/defaults"),
// Visibility is labels aren't actually targets
new PathFragment("visibility"),
// There is only one //external package
Label.EXTERNAL_PACKAGE_NAME);

private final Package pkg;
private final RuleClass ruleClass;
private final Label ruleLabel;
Expand Down Expand Up @@ -172,13 +157,7 @@ protected void visitLabels(Attribute attribute, AcceptsLabelAttribute observer)
Object value = get(attribute.getName(), type);
if (value != null) { // null values are particularly possible for computed defaults.
for (Label label : extractLabels(type, value)) {
Label absoluteLabel;
if (label.getPackageIdentifier().getRepository().isDefault()
&& ABSOLUTE_PACKAGE_NAMES.contains(label.getPackageIdentifier().getPackageFragment())) {
absoluteLabel = label;
} else {
absoluteLabel = ruleLabel.resolveRepositoryRelative(label);
}
Label absoluteLabel = ruleLabel.resolveRepositoryRelative(label);
observer.acceptLabelAttribute(absoluteLabel, attribute);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public String getName() {

private void convertAndProcess(
Package.LegacyBuilder pkgBuilder, Location location, Object value)
throws EvalException, ConversionException {
throws EvalException {
T typedValue = type.convert(value, "'package' argument", pkgBuilder.getBuildFileLabel());
process(pkgBuilder, location, typedValue);
}
Expand Down Expand Up @@ -180,7 +180,7 @@ private DefaultVisibility() {
@Override
protected void process(Package.LegacyBuilder pkgBuilder, Location location,
List<Label> value) {
pkgBuilder.setDefaultVisibility(getVisibility(value));
pkgBuilder.setDefaultVisibility(getVisibility(pkgBuilder.getBuildFileLabel(), value));
}
}

Expand Down Expand Up @@ -698,7 +698,7 @@ static Runtime.NoneType callExportsFiles(Object srcs, Object visibilityO, Object

RuleVisibility visibility = EvalUtils.isNullOrNone(visibilityO)
? ConstantRuleVisibility.PUBLIC
: getVisibility(BuildType.LABEL_LIST.convert(
: getVisibility(pkgBuilder.getBuildFileLabel(), BuildType.LABEL_LIST.convert(
visibilityO,
"'exports_files' operand",
pkgBuilder.getBuildFileLabel()));
Expand Down Expand Up @@ -848,15 +848,15 @@ static Runtime.NoneType callPackageFunction(String name, Object packagesO, Objec
}
}

public static RuleVisibility getVisibility(List<Label> original) {
public static RuleVisibility getVisibility(Label ruleLabel, List<Label> original) {
RuleVisibility result;

result = ConstantRuleVisibility.tryParse(original);
if (result != null) {
return result;
}

result = PackageGroupsRuleVisibility.tryParse(original);
result = PackageGroupsRuleVisibility.tryParse(ruleLabel, original);
return result;
}

Expand All @@ -882,7 +882,7 @@ private static BaseFunction newPackageFunction(
return new BaseFunction("package", FunctionSignature.namedOnly(0, argumentNames)) {
@Override
public Object call(Object[] arguments, FuncallExpression ast, Environment env)
throws EvalException, ConversionException {
throws EvalException {

Package.LegacyBuilder pkgBuilder = getContext(env, ast).pkgBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ public class PackageGroupsRuleVisibility implements RuleVisibility {
private final List<PackageSpecification> directPackages;
private final List<Label> declaredLabels;

public PackageGroupsRuleVisibility(List<Label> labels) {
public PackageGroupsRuleVisibility(Label ruleLabel, List<Label> labels) {
declaredLabels = ImmutableList.copyOf(labels);
ImmutableList.Builder<PackageSpecification> directPackageBuilder = ImmutableList.builder();
ImmutableList.Builder<Label> packageGroupBuilder = ImmutableList.builder();

for (Label label : labels) {
PackageSpecification specification = PackageSpecification.fromLabel(label);
Label resolved = ruleLabel.resolveRepositoryRelative(label);
PackageSpecification specification = PackageSpecification.fromLabel(resolved);
if (specification != null) {
directPackageBuilder.add(specification);
} else {
packageGroupBuilder.add(label);
packageGroupBuilder.add(resolved);
}
}

Expand Down Expand Up @@ -75,7 +76,7 @@ public List<Label> getDeclaredLabels() {
* @return The resulting visibility object. A list of labels can always be
* parsed into a PackageGroupsRuleVisibility.
*/
public static PackageGroupsRuleVisibility tryParse(List<Label> labels) {
return new PackageGroupsRuleVisibility(labels);
public static PackageGroupsRuleVisibility tryParse(Label ruleLabel, List<Label> labels) {
return new PackageGroupsRuleVisibility(ruleLabel, labels);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1578,7 +1578,7 @@ private Integer setRuleAttributeValue(Rule rule,
rule.reportError(rule.getLabel() + ": //visibility:legacy_public only allowed in package "
+ "declaration", eventHandler);
}
rule.setVisibility(PackageFactory.getVisibility(attrList));
rule.setVisibility(PackageFactory.getVisibility(rule.getLabel(), attrList));
}

rule.setAttributeValue(attr, converted, /*explicit=*/true);
Expand Down
25 changes: 25 additions & 0 deletions src/test/shell/bazel/external_correctness_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,29 @@ EOF
assert_contains 1.0 bazel-genfiles/external/remote2/x.out
}

function test_visibility_in_external_repo() {
REMOTE=$TEST_TMPDIR/r
mkdir -p $REMOTE/v

cat > $REMOTE/BUILD <<EOF
package(default_visibility=["//v:v"])
filegroup(name='fg1') # Inherits default visibility
filegroup(name='fg2', visibility=["//v:v"])
EOF

cat > $REMOTE/v/BUILD <<EOF
package_group(name="v", packages=["//"])
EOF

cat > WORKSPACE <<EOF
local_repository(name = "r", path = "$REMOTE")
EOF

cat > BUILD <<EOF
filegroup(name = "fg", srcs=["@r//:fg1", "@r//:fg2"])
EOF

bazel build //:fg || fail "Build failed"
}

run_suite "//external correctness tests"

0 comments on commit 17ed2ce

Please sign in to comment.