diff --git a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java index 79b3abbdcba889..c471210918a939 100644 --- a/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java +++ b/src/main/java/com/google/devtools/common/options/OptionsParserImpl.java @@ -476,6 +476,14 @@ private OptionsParserImplResult parse( continue; // not an option arg } + if (arg.startsWith("-//") || arg.startsWith("-@")) { + // Do not fail on absolute negative target patterns that aren't separated by `--`. + // Relative negative target patterns (e.g. "-pkg:...") aren't readily distinguishable from + // options, so these still require a `--` separator. + unparsedArgs.add(arg); + continue; + } + arg = swapShorthandAlias(arg); if (arg.equals("--")) { // "--" means all remaining args aren't options diff --git a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java index 4bc56d1084881a..d4ac2dbb3cf55d 100644 --- a/src/test/java/com/google/devtools/common/options/OptionsParserTest.java +++ b/src/test/java/com/google/devtools/common/options/OptionsParserTest.java @@ -2369,6 +2369,15 @@ public void setOptionValueAtSpecificPriorityWithoutExpansion_expandedFlag_setsVa .containsExactly("--second=hello"); } + @Test + public void negativeTargetPatternsAddedToPostDoubleDashArgs() throws OptionsParsingException { + OptionsParser parser = OptionsParser.builder().optionsClasses(ExampleFoo.class).build(); + parser.parse("//foo", "-//bar", "-@repo//bar", "-@@repo//bar", "--", "//bar", "-//baz"); + assertThat(parser.getResidue()).containsExactly("//foo", "-//bar", "-@repo//bar", + "-@@repo//bar", "//bar", "-//baz").inOrder(); + assertThat(parser.getPostDoubleDashResidue()).containsExactly("//bar", "-//baz").inOrder(); + } + private static OptionInstanceOrigin createInvocationPolicyOrigin() { return createInvocationPolicyOrigin(/*implicitDependent=*/ null, /*expandedFrom=*/ null); }