Skip to content

Commit

Permalink
Merge use_repo buildifier fixups into a single command
Browse files Browse the repository at this point in the history
Users only have to copy&paste a single command this way.

Closes bazelbuild#19119.

PiperOrigin-RevId: 551887901
Change-Id: Id285563e83a18ac9a4db3b2d4e8d731d5b3f5813
  • Loading branch information
fmeum authored and iancha1992 committed Jul 31, 2023
1 parent b7d6313 commit 4cb09f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.stream.Collectors.joining;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
Expand All @@ -31,7 +32,6 @@
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.starlark.java.annot.StarlarkBuiltin;
Expand Down Expand Up @@ -285,7 +285,7 @@ private static Optional<Event> generateFixupMessage(
String.join(", ", indirectDepImports));
}

var fixupCommands =
var fixupCommand =
Stream.of(
makeUseRepoCommand(
"use_repo_add",
Expand Down Expand Up @@ -315,19 +315,18 @@ private static Optional<Event> generateFixupMessage(
extensionBzlFile,
extensionName,
rootUsage.getIsolationKey()))
.flatMap(Optional::stream);
.flatMap(Optional::stream)
.collect(joining(" ", "buildozer ", " //MODULE.bazel:all"));

return Optional.of(
Event.warn(
location,
message
+ String.format(
"%s ** You can use the following buildozer command(s) to fix these"
"%s ** You can use the following buildozer command to fix these"
+ " issues:%s\n\n"
+ "%s",
"\033[35m\033[1m",
"\033[0m",
fixupCommands.collect(Collectors.joining("\n")))));
"\033[35m\033[1m", "\033[0m", fixupCommand)));
}

private static Optional<String> makeUseRepoCommand(
Expand All @@ -354,8 +353,7 @@ private static Optional<String> makeUseRepoCommand(
commandParts.add(extensionName);
}
commandParts.addAll(repos);
return Optional.of(
String.format("buildozer '%s' //MODULE.bazel:all", String.join(" ", commandParts)));
return Optional.of(commandParts.stream().collect(joining(" ", "'", "'")));
}

private Optional<ImmutableSet<String>> getRootModuleDirectDeps(Set<String> allRepos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1841,16 +1841,13 @@ public void extensionMetadata() throws Exception {
+ "Imported, but reported as indirect dependencies by the extension:\n"
+ " indirect_dep, indirect_dev_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep non_dev_as_dev_dep'"
+ " //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep"
+ " indirect_dep invalid_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep"
+ " missing_direct_dev_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
+ " 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep indirect_dep invalid_dep'"
+ " 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep missing_direct_dev_dep'"
+ " 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
+ " non_dev_as_dev_dep' //MODULE.bazel:all",
ImmutableSet.of(EventKind.WARNING));
}
Expand Down Expand Up @@ -1925,14 +1922,13 @@ public void extensionMetadata_all() throws Exception {
+ " extension (may cause the build to fail when used by other modules):\n"
+ " direct_dev_dep, indirect_dev_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add @ext//:defs.bzl ext direct_dev_dep indirect_dev_dep"
+ " missing_direct_dep missing_direct_dev_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext invalid_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext direct_dev_dep indirect_dev_dep"
+ " invalid_dev_dep' //MODULE.bazel:all",
+ " missing_direct_dep missing_direct_dev_dep' 'use_repo_remove @ext//:defs.bzl ext"
+ " invalid_dep' 'use_repo_remove dev @ext//:defs.bzl ext direct_dev_dep"
+ " indirect_dev_dep invalid_dev_dep' //MODULE.bazel:all",
ImmutableSet.of(EventKind.WARNING));
}

Expand Down Expand Up @@ -2008,14 +2004,12 @@ public void extensionMetadata_allDev() throws Exception {
+ " extension (may cause the build to fail when used by other modules):\n"
+ " direct_dep, indirect_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext direct_dep indirect_dep invalid_dep'"
+ " //MODULE.bazel:all\n"
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext direct_dep indirect_dep"
+ " missing_direct_dep missing_direct_dev_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext invalid_dev_dep'"
+ " 'use_repo_add dev @ext//:defs.bzl ext direct_dep indirect_dep missing_direct_dep"
+ " missing_direct_dev_dep' 'use_repo_remove dev @ext//:defs.bzl ext invalid_dev_dep'"
+ " //MODULE.bazel:all",
ImmutableSet.of(EventKind.WARNING));
}
Expand Down Expand Up @@ -2129,11 +2123,11 @@ public void extensionMetadata_isolated() throws Exception {
+ "Imported, but reported as indirect dependencies by the extension:\n"
+ " indirect_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove ext1 indirect_dep' //MODULE.bazel:all",
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' 'use_repo_remove ext1"
+ " indirect_dep' //MODULE.bazel:all",
ImmutableSet.of(EventKind.WARNING));
assertContainsEvent(
"WARNING /ws/MODULE.bazel:8:21: The module extension ext defined in @ext//:defs.bzl"
Expand All @@ -2143,7 +2137,7 @@ public void extensionMetadata_isolated() throws Exception {
+ " build to fail):\n"
+ " missing_direct_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add ext2 missing_direct_dep' //MODULE.bazel:all",
Expand Down Expand Up @@ -2213,11 +2207,11 @@ public void extensionMetadata_isolatedDev() throws Exception {
+ "Imported, but reported as indirect dependencies by the extension:\n"
+ " indirect_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' //MODULE.bazel:all\n"
+ "buildozer 'use_repo_remove ext1 indirect_dep' //MODULE.bazel:all",
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' 'use_repo_remove ext1"
+ " indirect_dep' //MODULE.bazel:all",
ImmutableSet.of(EventKind.WARNING));
assertContainsEvent(
"WARNING /ws/MODULE.bazel:8:21: The module extension ext defined in @ext//:defs.bzl"
Expand All @@ -2227,7 +2221,7 @@ public void extensionMetadata_isolatedDev() throws Exception {
+ " build to fail):\n"
+ " missing_direct_dep\n"
+ "\n"
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
+ " issues:\033[0m\n"
+ "\n"
+ "buildozer 'use_repo_add ext2 missing_direct_dep' //MODULE.bazel:all",
Expand Down

0 comments on commit 4cb09f7

Please sign in to comment.