Skip to content

Commit 1ca47eb

Browse files
fmeumbazel-io
authored andcommitted
Fix common .bazelrc behavior for flag expansions
When a flag specified with `common` in a `.bazelrc` file expanded to a flag unsupported by the current command, it resulted in an error rather than the flag being ignored. Fixes bazelbuild#18130 (comment) Closes bazelbuild#20720. PiperOrigin-RevId: 597271727 Change-Id: Ieaba4dc00e13495a859e1eedf802759ad7dbf774
1 parent 71cca16 commit 1ca47eb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ private OptionsParserImplResult parse(
545545
parsedOption = result.parsedOptionDescription;
546546
}
547547
if (parsedOption.isPresent()) {
548-
handleNewParsedOption(parsedOption.get());
548+
handleNewParsedOption(parsedOption.get(), fallbackData);
549549
}
550550
priority = OptionPriority.nextOptionPriority(priority);
551551
}
@@ -645,7 +645,7 @@ void setOptionValueAtSpecificPriorityWithoutExpansion(
645645
}
646646

647647
/** Takes care of tracking the parsed option's value in relation to other options. */
648-
private void handleNewParsedOption(ParsedOptionDescription parsedOption)
648+
private void handleNewParsedOption(ParsedOptionDescription parsedOption, OptionsData fallbackData)
649649
throws OptionsParsingException {
650650
OptionDefinition optionDefinition = parsedOption.getOptionDefinition();
651651
ExpansionBundle expansionBundle = setOptionValue(parsedOption);
@@ -659,7 +659,7 @@ private void handleNewParsedOption(ParsedOptionDescription parsedOption)
659659
optionDefinition.hasImplicitRequirements() ? parsedOption : null,
660660
optionDefinition.isExpansionOption() ? parsedOption : null,
661661
expansionBundle.expansionArgs,
662-
/* fallbackData= */ null);
662+
fallbackData);
663663
if (!optionsParserImplResult.getResidue().isEmpty()) {
664664

665665
// Throw an assertion here, because this indicates an error in the definition of this

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,6 +2480,40 @@ public void fallbackOptions_optionsParsingDifferently() {
24802480
assertThat(e).hasCauseThat().isInstanceOf(DuplicateOptionDeclarationException.class);
24812481
}
24822482

2483+
public static class ExpandingOptions extends OptionsBase {
2484+
@Option(
2485+
name = "foo",
2486+
category = "one",
2487+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
2488+
effectTags = {OptionEffectTag.NO_OP},
2489+
expansion = {"--nobar"},
2490+
defaultValue = "null")
2491+
public Void foo;
2492+
}
2493+
2494+
public static class ExpandingOptionsFallback extends OptionsBase {
2495+
@Option(
2496+
name = "bar",
2497+
category = "one",
2498+
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
2499+
effectTags = {OptionEffectTag.NO_OP},
2500+
defaultValue = "true")
2501+
public boolean bar;
2502+
}
2503+
2504+
@Test
2505+
public void fallbackOptions_expansionToNegativeBooleanFlag() throws OptionsParsingException {
2506+
OpaqueOptionsData fallbackData =
2507+
OptionsParser.getFallbackOptionsData(
2508+
ImmutableList.of(ExpandingOptions.class, ExpandingOptionsFallback.class));
2509+
OptionsParser parser = OptionsParser.builder().optionsClasses(ExpandingOptions.class).build();
2510+
parser.parseWithSourceFunction(
2511+
PriorityCategory.RC_FILE, o -> ".bazelrc", ImmutableList.of("--foo"), fallbackData);
2512+
2513+
assertThat(parser.getOptions(ExpandingOptions.class)).isNotNull();
2514+
assertThat(parser.getOptions(ExpandingOptionsFallback.class)).isNull();
2515+
}
2516+
24832517
private static OptionInstanceOrigin createInvocationPolicyOrigin() {
24842518
return createInvocationPolicyOrigin(/*implicitDependent=*/ null, /*expandedFrom=*/ null);
24852519
}

0 commit comments

Comments
 (0)