Skip to content

Commit

Permalink
Fix bazel mod tidy failure with no changes
Browse files Browse the repository at this point in the history
Buildozer has a non-zero exit code if it didn't make any changes.

Work towards #21651

Closes #21657.

PiperOrigin-RevId: 615184752
Change-Id: Ib89928974e2cd014cec968193d35fd74e2d01261
  • Loading branch information
fmeum authored and copybara-github committed Mar 12, 2024
1 parent ad21130 commit 98f29db
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ private BlazeCommandResult runTidy(
} catch (InterruptedException | CommandException e) {
String suffix = "";
if (e instanceof AbnormalTerminationException) {
if (((AbnormalTerminationException) e).getResult().getTerminationStatus().getRawExitCode()
== 3) {
// Buildozer exits with exit code 3 if it didn't make any changes.
return BlazeCommandResult.success();
}
suffix =
":\n" + new String(((AbnormalTerminationException) e).getResult().getStderr(), UTF_8);
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/py/bazel/bzlmod/mod_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,50 @@ def testModTidyAlwaysFormatsModuleFile(self):
[
'ext = use_extension("//:extension.bzl", "ext")',
'use_repo(ext, "dep")',
# This newline is from ScratchFile.
'',
],
module_file.read().split('\n'),
)

def testModTidyNoop(self):
self.ScratchFile(
'MODULE.bazel',
[
'ext = use_extension("//:extension.bzl", "ext")',
'use_repo(ext, "dep")',
],
)
self.ScratchFile('BUILD.bazel')
self.ScratchFile(
'extension.bzl',
[
'def _repo_rule_impl(ctx):',
' ctx.file("WORKSPACE")',
' ctx.file("BUILD", "filegroup(name=\'lala\')")',
'',
'repo_rule = repository_rule(implementation=_repo_rule_impl)',
'',
'def _ext_impl(ctx):',
' repo_rule(name="dep")',
' return ctx.extension_metadata(',
' root_module_direct_deps=["dep"],',
' root_module_direct_dev_deps=[],',
' )',
'',
'ext = module_extension(implementation=_ext_impl)',
],
)

# Verify that bazel mod tidy doesn't fail or change the file.
self.RunBazel(['mod', 'tidy'])

with open('MODULE.bazel', 'r') as module_file:
self.assertEqual(
[
'ext = use_extension("//:extension.bzl", "ext")',
'use_repo(ext, "dep")',
# This newline is from ScratchFile.
'',
],
module_file.read().split('\n'),
Expand Down

0 comments on commit 98f29db

Please sign in to comment.