Skip to content

Commit

Permalink
[6.4.0] Ignore Starlark options on commands with `allowResidue = Fals…
Browse files Browse the repository at this point in the history
…e` (#19417)

Ensures that `bazel version` does not fail when a `common` line in
`.bazelrc` contains a Starlark option (similar for `sync` and
`shutdown`). There is very little chance for users being confused about
these commands accepting and silently ignoring Starlark flags.

Fixes
#18130 (comment)

Closes #19363.

Commit
954b4d9

PiperOrigin-RevId: 562959337
Change-Id: Ifb4f44d63f1801f6c5a8c2f421138b4014c49a06

Co-authored-by: Fabian Meumertzheim <fabian@meumertzhe.im>
  • Loading branch information
bazel-io and fmeum committed Sep 6, 2023
1 parent 556396f commit a9f196a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -758,10 +758,8 @@ public ImmutableList<String> parseArgsAsExpansionOfOption(
private void addResidueFromResult(OptionsParserImplResult result) throws OptionsParsingException {
residue.addAll(result.getResidue());
postDoubleDashResidue.addAll(result.postDoubleDashResidue);
if (!allowResidue && (!getSkippedArgs().isEmpty() || !residue.isEmpty())) {
String errorMsg =
"Unrecognized arguments: "
+ Joiner.on(' ').join(Iterables.concat(getSkippedArgs(), residue));
if (!allowResidue && !residue.isEmpty()) {
String errorMsg = "Unrecognized arguments: " + Joiner.on(' ').join(residue);
throw new OptionsParsingException(errorMsg);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,24 +437,6 @@ public void testParseOptions_residue() {
assertThat(optionHandler.getRcfileNotes()).isEmpty();
}

@Test
public void testParseOptions_disallowResidue_skippedArgsLeadToFailure() throws Exception {
ImmutableList<Class<? extends OptionsBase>> optionsClasses =
ImmutableList.of(TestOptions.class, CommonCommandOptions.class, ClientOptions.class);

BlazeOptionHandlerTestHelper helper =
new BlazeOptionHandlerTestHelper(
optionsClasses,
/* allowResidue= */ false,
/* aliasFlag= */ null,
/* skipStarlarkPrefixes= */ true);
OptionsParser parser = helper.getOptionsParser();

OptionsParsingException e =
assertThrows(OptionsParsingException.class, () -> parser.parse("--//f=1"));
assertThat(e).hasMessageThat().isEqualTo("Unrecognized arguments: --//f=1");
}

@Test
public void testParseOptions_explicitOption() {
optionHandler.parseOptions(
Expand Down
14 changes: 14 additions & 0 deletions src/test/py/bazel/options_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ def testCommonPseudoCommand_unsupportedOptionValue(self):
stderr,
)

def testCommonPseudoCommand_allowResidueFalseCommandIgnoresStarlarkOptions(
self,
):
self.ScratchFile("WORKSPACE.bazel")
self.ScratchFile(
".bazelrc",
[
"common --@foo//bar:flag",
],
)

# Check that version doesn't fail.
self.RunBazel(["version"])


if __name__ == "__main__":
unittest.main()

0 comments on commit a9f196a

Please sign in to comment.