Skip to content

Commit

Permalink
Remove obsolete Label.getDefaultCanonicalForm method.
Browse files Browse the repository at this point in the history
Also move some processing to PackageIdentifier and clean up related methods.

PiperOrigin-RevId: 359276948
  • Loading branch information
katre authored and copybara-github committed Feb 24, 2021
1 parent 31d160a commit 0b1e612
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ private static void createStubFile(
Substitution.of(
"%enable_host_version_warning%",
boolToLiteral(common.shouldWarnAboutHostVersionUponFailure())),
Substitution.of(
"%target%", ruleContext.getRule().getLabel().getDefaultCanonicalForm()),
Substitution.of("%target%", ruleContext.getRule().getLabel().getCanonicalForm()),
Substitution.of(
"%python_version_from_config%", versionToLiteral(common.getVersion())),
Substitution.of("%python_version_from_attr%", versionToLiteral(attrVersion)),
Expand Down
11 changes: 1 addition & 10 deletions src/main/java/com/google/devtools/build/lib/cmdline/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ public String toString() {
* <p>invariant: {@code parseAbsolute(x.getCanonicalForm(), false).equals(x)}
*/
public String getCanonicalForm() {
return getDefaultCanonicalForm();
return packageIdentifier.getCanonicalForm() + ":" + name;
}

public String getUnambiguousCanonicalForm() {
Expand All @@ -465,15 +465,6 @@ public String getWorkspaceName() {
return packageIdentifier.getRepository().strippedName();
}

/**
* Renders this label in canonical form, except with labels in the main and default repositories
* conflated.
*/
public String getDefaultCanonicalForm() {
String repository = packageIdentifier.getRepository().getDefaultCanonicalForm();
return repository + "//" + packageIdentifier.getPackageFragment() + ":" + name;
}

/**
* Renders this label in shorthand form.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,18 @@ public PackageIdentifier makeAbsolute() {
return create(RepositoryName.MAIN, pkgName);
}

/** Returns the package in label syntax format. */
public String getCanonicalForm() {
String repository = getRepository().getCanonicalForm();
return repository + "//" + getPackageFragment();
}

/**
* Returns the name of this package.
*
* <p>There are certain places that expect the path fragment as the package name ('foo/bar') as a
* package identifier. This isn't specific enough for packages in other repositories, so their
* stringified version is '@baz//foo/bar'.</p>
* stringified version is '@baz//foo/bar'.
*/
@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public String getName() {
* Returns the repository name, except that the main repo is conflated with the default repo
* ({@code "@"} becomes the empty string).
*/
public String getDefaultCanonicalForm() {
public String getCanonicalForm() {
return isMain() ? "" : getName();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void processOutput(Iterable<Target> partialResult) throws IOException {
writer.append(' ');
}
Label label = target.getLabel();
writer.append(label.getDefaultCanonicalForm()).append(lineTerm);
writer.append(label.getCanonicalForm()).append(lineTerm);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void processOutput(Iterable<Target> partialResult) throws IOException {
.append(": ")
.append(target.getTargetKind())
.append(" ")
.append(target.getLabel().getDefaultCanonicalForm())
.append(target.getLabel().getCanonicalForm())
.append(lineTerm);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private static void outputToStreamOrSave(
if (toSave != null) {
toSave.add(new RankAndLabel(rank, label));
} else {
out.print(rank + " " + label.getDefaultCanonicalForm() + lineTerminator);
out.print(rank + " " + label.getCanonicalForm() + lineTerminator);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public int compareTo(RankAndLabel o) {

@Override
public String toString() {
return rank + " " + label.getDefaultCanonicalForm();
return rank + " " + label.getCanonicalForm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private static boolean matchesConfig(
errorMessage +=
String.format(
" (it is allowlisted to %s//tools/... only)",
getToolsRepository(ruleContext).getDefaultCanonicalForm());
getToolsRepository(ruleContext).getCanonicalForm());
}
if (selectRestriction.getErrorMessage() != null) {
errorMessage += ". " + selectRestriction.getErrorMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ private void parseArg(
// Use the canonical form to ensure we don't have
// duplicate options getting into the starlark options map.
unparsedOptions.put(
buildSettingTarget.getLabel().getDefaultCanonicalForm(),
new Pair<>(value, buildSettingTarget));
buildSettingTarget.getLabel().getCanonicalForm(), new Pair<>(value, buildSettingTarget));
} else {
boolean booleanValue = true;
// check --noflag form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public void testRunfilesDir() throws Exception {

@Test
public void testGetDefaultCanonicalForm() throws Exception {
assertThat(RepositoryName.create("").getDefaultCanonicalForm()).isEqualTo("");
assertThat(RepositoryName.create("@").getDefaultCanonicalForm()).isEqualTo("");
assertThat(RepositoryName.create("@foo").getDefaultCanonicalForm()).isEqualTo("@foo");
assertThat(RepositoryName.create("").getCanonicalForm()).isEqualTo("");
assertThat(RepositoryName.create("@").getCanonicalForm()).isEqualTo("");
assertThat(RepositoryName.create("@foo").getCanonicalForm()).isEqualTo("@foo");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ public void nonselectableAllowlistedOption_OutOfToolsPackage() throws Exception
String.format(
"option 'nonselectable_allowlisted_option' cannot be used in a config_setting (it is "
+ "allowlisted to %s//tools/... only)",
RepositoryName.create(TestConstants.TOOLS_REPOSITORY).getDefaultCanonicalForm()),
RepositoryName.create(TestConstants.TOOLS_REPOSITORY).getCanonicalForm()),
"config_setting(",
" name = 'badoption',",
" values = {",
Expand Down

0 comments on commit 0b1e612

Please sign in to comment.