Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error on invalid -//foo and -@repo//foo options #16563

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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