Skip to content

Commit

Permalink
Improve error on invalid -//foo and -@repo//foo options
Browse files Browse the repository at this point in the history
`-//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.
  • Loading branch information
fmeum committed Oct 30, 2022
1 parent 902a0b5 commit 81b8589
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,16 @@ private OptionsParserImplResult parse(
continue; // not an option arg
}

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

arg = swapShorthandAlias(arg);

if (arg.equals("--")) { // "--" means all remaining args aren't options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,30 @@ public void setOptionValueAtSpecificPriorityWithoutExpansion_expandedFlag_setsVa
.containsExactly("--second=hello");
}

@Test
public void negativeTargetPatternsInOptions_failsDistinctively() {
OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build();
OptionsParsingException e = assertThrows(OptionsParsingException.class,
() -> parser.parse("//foo", "-//bar", "//baz"));
assertThat(e).hasMessageThat().contains("-//bar");
assertThat(e).hasMessageThat()
.contains("Negative target patterns can only appear after the end of options marker");
assertThat(e).hasMessageThat()
.contains("Flags corresponding to Starlark-defined build settings always start with '--'");
}

@Test
public void negativeExternalTargetPatternsInOptions_failsDistinctively() {
OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build();
OptionsParsingException e = assertThrows(OptionsParsingException.class,
() -> parser.parse("//foo", "-@repo//bar", "//baz"));
assertThat(e).hasMessageThat().contains("-@repo//bar");
assertThat(e).hasMessageThat()
.contains("Negative target patterns can only appear after the end of options marker");
assertThat(e).hasMessageThat()
.contains("Flags corresponding to Starlark-defined build settings always start with '--'");
}

private static OptionInstanceOrigin createInvocationPolicyOrigin() {
return createInvocationPolicyOrigin(/*implicitDependent=*/ null, /*expandedFrom=*/ null);
}
Expand Down

0 comments on commit 81b8589

Please sign in to comment.