diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java index d8ebb926153646..3f982afac26702 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java @@ -200,8 +200,14 @@ public Builder clearWorkspaceFileSuffixForTesting() { @CanIgnoreReturnValue public Builder setPrelude(String preludeLabelString) { + Preconditions.checkArgument( + preludeLabelString.startsWith("//"), + "Prelude label '%s' must start with '//'", + preludeLabelString); try { - this.preludeLabel = Label.parseAbsolute(preludeLabelString, ImmutableMap.of()); + // We're parsing this label as if it's in the main repository but it will actually get + // massaged into a label in the repository where the package being loaded resides. + this.preludeLabel = Label.parseCanonical(preludeLabelString); } catch (LabelSyntaxException e) { String errorMsg = String.format("Prelude label '%s' is invalid: %s", preludeLabelString, e.getMessage()); diff --git a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java index 6ecb937ff3e22f..fc353af241c0b0 100644 --- a/src/main/java/com/google/devtools/build/lib/cmdline/Label.java +++ b/src/main/java/com/google/devtools/build/lib/cmdline/Label.java @@ -126,6 +126,14 @@ public static Label parseCanonical(String raw) throws LabelSyntaxException { PackageIdentifier.create(repoName, PathFragment.create(parts.pkg)), parts.target); } + public static Label parseCanonicalUnchecked(String raw) { + try { + return parseCanonical(raw); + } catch (LabelSyntaxException e) { + throw new IllegalArgumentException(e); + } + } + /** Computes the repo name for the label, within the context of a current repo. */ private static RepositoryName computeRepoNameWithRepoContext( Parts parts, RepoContext repoContext) { diff --git a/src/main/java/com/google/devtools/build/lib/packages/ConstantRuleVisibility.java b/src/main/java/com/google/devtools/build/lib/packages/ConstantRuleVisibility.java index e8258abc6ee3af..7eab4860974a29 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/ConstantRuleVisibility.java +++ b/src/main/java/com/google/devtools/build/lib/packages/ConstantRuleVisibility.java @@ -15,7 +15,6 @@ import com.google.common.base.MoreObjects; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; @@ -45,8 +44,8 @@ public class ConstantRuleVisibility implements RuleVisibility { static { try { - PUBLIC_LABEL = Label.parseAbsolute("//visibility:public", ImmutableMap.of()); - PRIVATE_LABEL = Label.parseAbsolute("//visibility:private", ImmutableMap.of()); + PUBLIC_LABEL = Label.parseCanonical("//visibility:public"); + PRIVATE_LABEL = Label.parseCanonical("//visibility:private"); } catch (LabelSyntaxException e) { throw new IllegalStateException(e); } diff --git a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactoryHelper.java b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactoryHelper.java index 7a289623db5b69..1d07cc97f62137 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactoryHelper.java +++ b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceFactoryHelper.java @@ -18,7 +18,6 @@ import com.google.common.base.Strings; import com.google.common.base.Verify; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.cmdline.LabelSyntaxException; @@ -55,7 +54,7 @@ public static Rule createAndAddRepositoryRule( overwriteRule(pkg, rule); for (Map.Entry entry : ruleClass.getExternalBindingsFunction().apply(rule).entrySet()) { - Label nameLabel = Label.parseAbsolute("//external:" + entry.getKey(), ImmutableMap.of()); + Label nameLabel = Label.parseCanonical("//external:" + entry.getKey()); addBindRule(pkg, bindRuleClass, nameLabel, entry.getValue(), semantics, callstack); } // NOTE(wyv): What is this madness?? This is the only instance where a repository rule can diff --git a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceGlobals.java b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceGlobals.java index 1751bfc899b4dc..3145d11d315922 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/WorkspaceGlobals.java +++ b/src/main/java/com/google/devtools/build/lib/packages/WorkspaceGlobals.java @@ -145,7 +145,7 @@ public void bind(String name, Object actual, StarlarkThread thread) throws EvalException, InterruptedException { Label nameLabel; try { - nameLabel = Label.parseAbsolute("//external:" + name, ImmutableMap.of()); + nameLabel = Label.parseCanonical("//external:" + name); } catch (LabelSyntaxException e) { throw Starlark.errorf("%s", e.getMessage()); } @@ -156,7 +156,7 @@ public void bind(String name, Object actual, StarlarkThread thread) builder, ruleClass, nameLabel, - actual == NONE ? null : Label.parseAbsolute((String) actual, ImmutableMap.of()), + actual == NONE ? null : Label.parseCanonical((String) actual), thread.getSemantics(), thread.getCallStack()); } catch (InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) { diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java index 42f2e1df3a94f1..767fd54aee0846 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java @@ -19,7 +19,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.io.BaseEncoding; import com.google.devtools.build.lib.actions.FileValue; import com.google.devtools.build.lib.analysis.BlazeDirectories; @@ -206,7 +205,7 @@ private static boolean verifyLabelMarkerData(Rule rule, String key, String value RootedPath rootedPath; String fileKey = key.substring(5); if (LabelValidator.isAbsolute(fileKey)) { - rootedPath = getRootedPathFromLabel(Label.parseAbsolute(fileKey, ImmutableMap.of()), env); + rootedPath = getRootedPathFromLabel(Label.parseCanonical(fileKey), env); } else { // TODO(pcloudy): Removing checking absolute path, they should all be absolute label. PathFragment filePathFragment = PathFragment.create(fileKey); diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java index dec5009ddc9da4..36d7f0e4c945a1 100644 --- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java +++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java @@ -1226,11 +1226,7 @@ private LoadedPackage loadPackage( Label preludeLabel = null; // Can be null in tests. if (packageFactory != null) { - // Load the prelude from the same repository as the package being loaded. Can't use - // Label.resolveRepositoryRelative because rawPreludeLabel is in the main repository, not the - // default one, so it is resolved to itself. - // TODO(brandjon): Why can't we just replace the use of the main repository with the default - // repository in the prelude label? + // Load the prelude from the same repository as the package being loaded. Label rawPreludeLabel = packageFactory.getRuleClassProvider().getPreludeLabel(); if (rawPreludeLabel != null) { PackageIdentifier preludePackage = diff --git a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeStarlarkRuleFunctionsApi.java b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeStarlarkRuleFunctionsApi.java index 2c8d3efb59631d..6a2ecead06cd5f 100644 --- a/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeStarlarkRuleFunctionsApi.java +++ b/src/main/java/com/google/devtools/build/skydoc/fakebuildapi/FakeStarlarkRuleFunctionsApi.java @@ -173,9 +173,7 @@ public StarlarkCallable rule( @Override public Label label(String labelString, StarlarkThread thread) throws EvalException { try { - return Label.parseAbsolute( - labelString, - /* repositoryMapping= */ ImmutableMap.of()); + return Label.parseCanonical(labelString); } catch (LabelSyntaxException e) { throw Starlark.errorf("Illegal absolute label syntax: %s", labelString); }