Skip to content

Commit 28ebcdc

Browse files
iancha1992fmeum
andauthored
Improve error on invalid -//foo and -@repo//foo options (#18516)
`-//foo` and `-@repo//foo` are always invalid Bazel options, but are usually meant to be either negative target patterns (which have to come after the `--` separator) or Starlark options (which always start with `--`). With this commit, the error shown to the user mentions these two situations and how to fix the issue in each case. Closes #16563. PiperOrigin-RevId: 486920951 Change-Id: I9390d3859aa62c2b67eac05cb96a06209a9b7c36 Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent 4afed73 commit 28ebcdc

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/java/com/google/devtools/common/options/OptionsParserImpl.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,18 @@ private OptionsParserImplResult parse(
476476
continue; // not an option arg
477477
}
478478

479+
if (arg.startsWith("-//") || arg.startsWith("-@")) {
480+
// Fail with a helpful error when an invalid option looks like an absolute negative target
481+
// pattern or a typoed Starlark option.
482+
throw new OptionsParsingException(
483+
String.format(
484+
"Invalid options syntax: %s\n"
485+
+ "Note: Negative target patterns can only appear after the end of options"
486+
+ " marker ('--'). Flags corresponding to Starlark-defined build settings"
487+
+ " always start with '--', not '-'.",
488+
arg));
489+
}
490+
479491
arg = swapShorthandAlias(arg);
480492

481493
if (arg.equals("--")) { // "--" means all remaining args aren't options

src/test/java/com/google/devtools/common/options/OptionsParserTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,35 @@ public void setOptionValueAtSpecificPriorityWithoutExpansion_expandedFlag_setsVa
23692369
.containsExactly("--second=hello");
23702370
}
23712371

2372+
@Test
2373+
public void negativeTargetPatternsInOptions_failsDistinctively() {
2374+
OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build();
2375+
OptionsParsingException e =
2376+
assertThrows(OptionsParsingException.class, () -> parser.parse("//foo", "-//bar", "//baz"));
2377+
assertThat(e).hasMessageThat().contains("-//bar");
2378+
assertThat(e)
2379+
.hasMessageThat()
2380+
.contains("Negative target patterns can only appear after the end of options marker");
2381+
assertThat(e)
2382+
.hasMessageThat()
2383+
.contains("Flags corresponding to Starlark-defined build settings always start with '--'");
2384+
}
2385+
2386+
@Test
2387+
public void negativeExternalTargetPatternsInOptions_failsDistinctively() {
2388+
OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build();
2389+
OptionsParsingException e =
2390+
assertThrows(
2391+
OptionsParsingException.class, () -> parser.parse("//foo", "-@repo//bar", "//baz"));
2392+
assertThat(e).hasMessageThat().contains("-@repo//bar");
2393+
assertThat(e)
2394+
.hasMessageThat()
2395+
.contains("Negative target patterns can only appear after the end of options marker");
2396+
assertThat(e)
2397+
.hasMessageThat()
2398+
.contains("Flags corresponding to Starlark-defined build settings always start with '--'");
2399+
}
2400+
23722401
private static OptionInstanceOrigin createInvocationPolicyOrigin() {
23732402
return createInvocationPolicyOrigin(/*implicitDependent=*/ null, /*expandedFrom=*/ null);
23742403
}

0 commit comments

Comments
 (0)