Skip to content

Commit

Permalink
Add a flag to enable unambiguous stringification of Labels
Browse files Browse the repository at this point in the history
Work towards #15916

RELNOTES[INC]: Added a new flag --incompatible_unambiguous_label_stringification, which causes labels in the main repo to stringify into unambiguous forms starting with an @. See #15916 for more information.

PiperOrigin-RevId: 466346190
Change-Id: I302b8893d35a3eae7795754f7b5c1ac3839bb519
  • Loading branch information
Wyverald authored and copybara-github committed Aug 9, 2022
1 parent d1820a7 commit c12dd9b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -613,18 +613,24 @@ public boolean isImmutable() {

@Override
public void repr(Printer printer) {
// TODO(wyv): Consider using StarlarkSemantics here too for optional unambiguity.
printer.append("Label(");
printer.repr(getCanonicalForm());
printer.append(")");
}

@Override
public void str(Printer printer, StarlarkSemantics semantics) {
printer.append(getCanonicalForm());
if (semantics.getBool(BuildLanguageOptions.INCOMPATIBLE_UNAMBIGUOUS_LABEL_STRINGIFICATION)) {
printer.append(getUnambiguousCanonicalForm());
} else {
printer.append(getCanonicalForm());
}
}

@Override
public String expandToCommandLine() {
// TODO(wyv): Consider using StarlarkSemantics here too for optional unambiguity.
return getCanonicalForm();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,19 @@ public final class BuildLanguageOptions extends OptionsBase {
+ "migration instructions.")
public boolean incompatibleUseCcConfigureFromRulesCc;

@Option(
name = "incompatible_unambiguous_label_stringification",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help =
"When true, Bazel will stringify the label @//foo:bar to @//foo:bar, instead of"
+ " //foo:bar. This only affects the behavior of str(), the % operator, and so on;"
+ " the behavior of repr() is unchanged. See"
+ " https://github.com/bazelbuild/bazel/issues/15916 for more information.")
public boolean incompatibleUnambiguousLabelStringification;

@Option(
name = "incompatible_depset_for_libraries_to_link_getter",
defaultValue = "true",
Expand Down Expand Up @@ -667,6 +680,9 @@ public StarlarkSemantics toStarlarkSemantics() {
INCOMPATIBLE_DO_NOT_SPLIT_LINKING_CMDLINE, incompatibleDoNotSplitLinkingCmdline)
.setBool(
INCOMPATIBLE_USE_CC_CONFIGURE_FROM_RULES_CC, incompatibleUseCcConfigureFromRulesCc)
.setBool(
INCOMPATIBLE_UNAMBIGUOUS_LABEL_STRINGIFICATION,
incompatibleUnambiguousLabelStringification)
.setBool(
INCOMPATIBLE_DEPSET_FOR_LIBRARIES_TO_LINK_GETTER,
incompatibleDepsetForLibrariesToLinkGetter)
Expand Down Expand Up @@ -751,6 +767,8 @@ public StarlarkSemantics toStarlarkSemantics() {
"-incompatible_struct_has_no_methods";
public static final String INCOMPATIBLE_USE_CC_CONFIGURE_FROM_RULES_CC =
"-incompatible_use_cc_configure_from_rules";
public static final String INCOMPATIBLE_UNAMBIGUOUS_LABEL_STRINGIFICATION =
"-incompatible_unambiguous_label_stringification";
public static final String INCOMPATIBLE_VISIBILITY_PRIVATE_ATTRIBUTES_AT_DEFINITION =
"-incompatible_visibility_private_attributes_at_definition";
public static final String INCOMPATIBLE_TOP_LEVEL_ASPECTS_REQUIRE_PROVIDERS =
Expand Down
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/lib/cmdline/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/cmdline:parallel_visitor",
"//src/main/java/com/google/devtools/build/lib/cmdline:query_exception_marker_interface",
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/packages/semantics",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/main/java/net/starlark/java/eval",
"//src/test/java/com/google/devtools/build/lib/testutil:TestThread",
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/com/google/devtools/build/lib/cmdline/LabelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.collect.ImmutableMap;
import com.google.common.testing.EqualsTester;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.regex.Pattern;
import net.starlark.java.eval.Starlark;
Expand Down Expand Up @@ -429,4 +430,14 @@ public void testStarlarkStrAndRepr() throws Exception {
assertThat(Starlark.str(label, StarlarkSemantics.DEFAULT)).isEqualTo("//x:x");
assertThat(Starlark.repr(label)).isEqualTo("Label(\"//x:x\")");
}

@Test
public void testStarlarkStr_unambiguous() throws Exception {
Label label = Label.parseCanonical("//x");
StarlarkSemantics semantics =
StarlarkSemantics.builder()
.setBool(BuildLanguageOptions.INCOMPATIBLE_UNAMBIGUOUS_LABEL_STRINGIFICATION, true)
.build();
assertThat(Starlark.str(label, semantics)).isEqualTo("@//x:x");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ private static BuildLanguageOptions buildRandomOptions(Random rand) throws Excep
"--incompatible_visibility_private_attributes_at_definition=" + rand.nextBoolean(),
"--incompatible_require_linker_input_cc_api=" + rand.nextBoolean(),
"--incompatible_use_cc_configure_from_rules_cc=" + rand.nextBoolean(),
"--incompatible_unambiguous_label_stringification=" + rand.nextBoolean(),
"--internal_starlark_flag_test_canary=" + rand.nextBoolean(),
"--max_computation_steps=" + rand.nextLong());
}
Expand Down Expand Up @@ -205,6 +206,8 @@ private static StarlarkSemantics buildRandomSemantics(Random rand) {
.setBool(BuildLanguageOptions.INCOMPATIBLE_REQUIRE_LINKER_INPUT_CC_API, rand.nextBoolean())
.setBool(
BuildLanguageOptions.INCOMPATIBLE_USE_CC_CONFIGURE_FROM_RULES_CC, rand.nextBoolean())
.setBool(
BuildLanguageOptions.INCOMPATIBLE_UNAMBIGUOUS_LABEL_STRINGIFICATION, rand.nextBoolean())
.setBool(StarlarkSemantics.PRINT_TEST_MARKER, rand.nextBoolean())
.set(BuildLanguageOptions.MAX_COMPUTATION_STEPS, rand.nextLong())
.build();
Expand Down

0 comments on commit c12dd9b

Please sign in to comment.