Skip to content

Commit 787ec90

Browse files
fmeumcopybara-github
authored andcommitted
Merge use_repo buildifier fixups into a single command
Users only have to copy&paste a single command this way. Closes #19119. PiperOrigin-RevId: 551887901 Change-Id: Id285563e83a18ac9a4db3b2d4e8d731d5b3f5813
1 parent 1e34704 commit 787ec90

File tree

2 files changed

+26
-34
lines changed

2 files changed

+26
-34
lines changed

src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionMetadata.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
1818
import static com.google.common.collect.ImmutableSet.toImmutableSet;
19+
import static java.util.stream.Collectors.joining;
1920

2021
import com.google.common.base.Preconditions;
2122
import com.google.common.collect.ImmutableSet;
@@ -31,7 +32,6 @@
3132
import java.util.LinkedHashSet;
3233
import java.util.Optional;
3334
import java.util.Set;
34-
import java.util.stream.Collectors;
3535
import java.util.stream.Stream;
3636
import javax.annotation.Nullable;
3737
import net.starlark.java.annot.StarlarkBuiltin;
@@ -285,7 +285,7 @@ private static Optional<Event> generateFixupMessage(
285285
String.join(", ", indirectDepImports));
286286
}
287287

288-
var fixupCommands =
288+
var fixupCommand =
289289
Stream.of(
290290
makeUseRepoCommand(
291291
"use_repo_add",
@@ -315,19 +315,18 @@ private static Optional<Event> generateFixupMessage(
315315
extensionBzlFile,
316316
extensionName,
317317
rootUsage.getIsolationKey()))
318-
.flatMap(Optional::stream);
318+
.flatMap(Optional::stream)
319+
.collect(joining(" ", "buildozer ", " //MODULE.bazel:all"));
319320

320321
return Optional.of(
321322
Event.warn(
322323
location,
323324
message
324325
+ String.format(
325-
"%s ** You can use the following buildozer command(s) to fix these"
326+
"%s ** You can use the following buildozer command to fix these"
326327
+ " issues:%s\n\n"
327328
+ "%s",
328-
"\033[35m\033[1m",
329-
"\033[0m",
330-
fixupCommands.collect(Collectors.joining("\n")))));
329+
"\033[35m\033[1m", "\033[0m", fixupCommand)));
331330
}
332331

333332
private static Optional<String> makeUseRepoCommand(
@@ -354,8 +353,7 @@ private static Optional<String> makeUseRepoCommand(
354353
commandParts.add(extensionName);
355354
}
356355
commandParts.addAll(repos);
357-
return Optional.of(
358-
String.format("buildozer '%s' //MODULE.bazel:all", String.join(" ", commandParts)));
356+
return Optional.of(commandParts.stream().collect(joining(" ", "'", "'")));
359357
}
360358

361359
private Optional<ImmutableSet<String>> getRootModuleDirectDeps(Set<String> allRepos)

src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,16 +1845,13 @@ public void extensionMetadata() throws Exception {
18451845
+ "Imported, but reported as indirect dependencies by the extension:\n"
18461846
+ " indirect_dep, indirect_dev_dep\n"
18471847
+ "\n"
1848-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
1848+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
18491849
+ " issues:\033[0m\n"
18501850
+ "\n"
18511851
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep non_dev_as_dev_dep'"
1852-
+ " //MODULE.bazel:all\n"
1853-
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep"
1854-
+ " indirect_dep invalid_dep' //MODULE.bazel:all\n"
1855-
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep"
1856-
+ " missing_direct_dev_dep' //MODULE.bazel:all\n"
1857-
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
1852+
+ " 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep indirect_dep invalid_dep'"
1853+
+ " 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep missing_direct_dev_dep'"
1854+
+ " 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
18581855
+ " non_dev_as_dev_dep' //MODULE.bazel:all",
18591856
ImmutableSet.of(EventKind.WARNING));
18601857
}
@@ -1929,14 +1926,13 @@ public void extensionMetadata_all() throws Exception {
19291926
+ " extension (may cause the build to fail when used by other modules):\n"
19301927
+ " direct_dev_dep, indirect_dev_dep\n"
19311928
+ "\n"
1932-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
1929+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
19331930
+ " issues:\033[0m\n"
19341931
+ "\n"
19351932
+ "buildozer 'use_repo_add @ext//:defs.bzl ext direct_dev_dep indirect_dev_dep"
1936-
+ " missing_direct_dep missing_direct_dev_dep' //MODULE.bazel:all\n"
1937-
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext invalid_dep' //MODULE.bazel:all\n"
1938-
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext direct_dev_dep indirect_dev_dep"
1939-
+ " invalid_dev_dep' //MODULE.bazel:all",
1933+
+ " missing_direct_dep missing_direct_dev_dep' 'use_repo_remove @ext//:defs.bzl ext"
1934+
+ " invalid_dep' 'use_repo_remove dev @ext//:defs.bzl ext direct_dev_dep"
1935+
+ " indirect_dev_dep invalid_dev_dep' //MODULE.bazel:all",
19401936
ImmutableSet.of(EventKind.WARNING));
19411937
}
19421938

@@ -2012,14 +2008,12 @@ public void extensionMetadata_allDev() throws Exception {
20122008
+ " extension (may cause the build to fail when used by other modules):\n"
20132009
+ " direct_dep, indirect_dep\n"
20142010
+ "\n"
2015-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
2011+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
20162012
+ " issues:\033[0m\n"
20172013
+ "\n"
20182014
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext direct_dep indirect_dep invalid_dep'"
2019-
+ " //MODULE.bazel:all\n"
2020-
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext direct_dep indirect_dep"
2021-
+ " missing_direct_dep missing_direct_dev_dep' //MODULE.bazel:all\n"
2022-
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext invalid_dev_dep'"
2015+
+ " 'use_repo_add dev @ext//:defs.bzl ext direct_dep indirect_dep missing_direct_dep"
2016+
+ " missing_direct_dev_dep' 'use_repo_remove dev @ext//:defs.bzl ext invalid_dev_dep'"
20232017
+ " //MODULE.bazel:all",
20242018
ImmutableSet.of(EventKind.WARNING));
20252019
}
@@ -2133,11 +2127,11 @@ public void extensionMetadata_isolated() throws Exception {
21332127
+ "Imported, but reported as indirect dependencies by the extension:\n"
21342128
+ " indirect_dep\n"
21352129
+ "\n"
2136-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
2130+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
21372131
+ " issues:\033[0m\n"
21382132
+ "\n"
2139-
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' //MODULE.bazel:all\n"
2140-
+ "buildozer 'use_repo_remove ext1 indirect_dep' //MODULE.bazel:all",
2133+
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' 'use_repo_remove ext1"
2134+
+ " indirect_dep' //MODULE.bazel:all",
21412135
ImmutableSet.of(EventKind.WARNING));
21422136
assertContainsEvent(
21432137
"WARNING /ws/MODULE.bazel:8:21: The module extension ext defined in @ext//:defs.bzl"
@@ -2147,7 +2141,7 @@ public void extensionMetadata_isolated() throws Exception {
21472141
+ " build to fail):\n"
21482142
+ " missing_direct_dep\n"
21492143
+ "\n"
2150-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
2144+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
21512145
+ " issues:\033[0m\n"
21522146
+ "\n"
21532147
+ "buildozer 'use_repo_add ext2 missing_direct_dep' //MODULE.bazel:all",
@@ -2217,11 +2211,11 @@ public void extensionMetadata_isolatedDev() throws Exception {
22172211
+ "Imported, but reported as indirect dependencies by the extension:\n"
22182212
+ " indirect_dep\n"
22192213
+ "\n"
2220-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
2214+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
22212215
+ " issues:\033[0m\n"
22222216
+ "\n"
2223-
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' //MODULE.bazel:all\n"
2224-
+ "buildozer 'use_repo_remove ext1 indirect_dep' //MODULE.bazel:all",
2217+
+ "buildozer 'use_repo_add ext1 direct_dep missing_direct_dep' 'use_repo_remove ext1"
2218+
+ " indirect_dep' //MODULE.bazel:all",
22252219
ImmutableSet.of(EventKind.WARNING));
22262220
assertContainsEvent(
22272221
"WARNING /ws/MODULE.bazel:8:21: The module extension ext defined in @ext//:defs.bzl"
@@ -2231,7 +2225,7 @@ public void extensionMetadata_isolatedDev() throws Exception {
22312225
+ " build to fail):\n"
22322226
+ " missing_direct_dep\n"
22332227
+ "\n"
2234-
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
2228+
+ "\033[35m\033[1m ** You can use the following buildozer command to fix these"
22352229
+ " issues:\033[0m\n"
22362230
+ "\n"
22372231
+ "buildozer 'use_repo_add ext2 missing_direct_dep' //MODULE.bazel:all",

0 commit comments

Comments
 (0)