From 7e1e08b89f037c7592f6ea71529a010c7c64a876 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 12 Mar 2024 15:04:02 -0700 Subject: [PATCH] Fix `bazel mod tidy` failure with no changes 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 --- .../build/lib/bazel/commands/ModCommand.java | 5 +++ src/test/py/bazel/bzlmod/mod_command_test.py | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java b/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java index ec08bfafd50d55..d6df9854b00297 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/commands/ModCommand.java @@ -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); } diff --git a/src/test/py/bazel/bzlmod/mod_command_test.py b/src/test/py/bazel/bzlmod/mod_command_test.py index cc78dbad5f54c2..ef2097279f7196 100644 --- a/src/test/py/bazel/bzlmod/mod_command_test.py +++ b/src/test/py/bazel/bzlmod/mod_command_test.py @@ -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'),