Skip to content

Commit 287e179

Browse files
iancha1992fmeum
andauthored
Report dev/non-dev deps imported via non-dev/dev usages (#18922)
If a repository generated by a module extension corresponds to a dev tag but is imported on a non-dev usage proxy (or vice versa), the build can fail if the module is used by other modules. `ModuleExtensionMetadata` already generated fixup commands for this case, but now also explains the situation and impact. Closes #18832. PiperOrigin-RevId: 547589845 Change-Id: I58a63b1aa6b60849a350202097515c7840540800 Co-authored-by: Fabian Meumertzheim <[email protected]>
1 parent 70facd3 commit 287e179

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,28 @@ private static Optional<Event> generateFixupMessage(
252252
String.join(", ", missingImports));
253253
}
254254

255+
var nonDevImportsOfDevDeps =
256+
ImmutableSortedSet.copyOf(Sets.intersection(expectedDevImports, actualImports));
257+
if (!nonDevImportsOfDevDeps.isEmpty()) {
258+
message +=
259+
String.format(
260+
"Imported as a regular dependency, but reported as a dev dependency by the "
261+
+ "extension (may cause the build to fail when used by other modules):\n"
262+
+ " %s\n\n",
263+
String.join(", ", nonDevImportsOfDevDeps));
264+
}
265+
266+
var devImportsOfNonDevDeps =
267+
ImmutableSortedSet.copyOf(Sets.intersection(expectedImports, actualDevImports));
268+
if (!devImportsOfNonDevDeps.isEmpty()) {
269+
message +=
270+
String.format(
271+
"Imported as a dev dependency, but reported as a regular dependency by the "
272+
+ "extension (may cause the build to fail when used by other modules):\n"
273+
+ " %s\n\n",
274+
String.join(", ", devImportsOfNonDevDeps));
275+
}
276+
255277
var indirectDepImports =
256278
ImmutableSortedSet.copyOf(
257279
Sets.difference(Sets.intersection(allActualImports, allRepos), allExpectedImports));

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,13 +1761,15 @@ public void extensionMetadata() throws Exception {
17611761
" ext,",
17621762
" 'indirect_dep',",
17631763
" 'invalid_dep',",
1764+
" 'dev_as_non_dev_dep',",
17641765
" my_direct_dep = 'direct_dep',",
17651766
")",
17661767
"ext_dev = use_extension('@ext//:defs.bzl', 'ext', dev_dependency = True)",
17671768
"use_repo(",
17681769
" ext_dev,",
17691770
" 'indirect_dev_dep',",
17701771
" 'invalid_dev_dep',",
1772+
" 'non_dev_as_dev_dep',",
17711773
" my_direct_dev_dep = 'direct_dev_dep',",
17721774
")");
17731775
scratch.file(workspaceRoot.getRelative("BUILD").getPathString());
@@ -1796,9 +1798,12 @@ public void extensionMetadata() throws Exception {
17961798
" data_repo(name='missing_direct_dev_dep')",
17971799
" data_repo(name='indirect_dep')",
17981800
" data_repo(name='indirect_dev_dep')",
1801+
" data_repo(name='dev_as_non_dev_dep')",
1802+
" data_repo(name='non_dev_as_dev_dep')",
17991803
" return ctx.extension_metadata(",
1800-
" root_module_direct_deps=['direct_dep', 'missing_direct_dep'],",
1801-
" root_module_direct_dev_deps=['direct_dev_dep', 'missing_direct_dev_dep'],",
1804+
" root_module_direct_deps=['direct_dep', 'missing_direct_dep', 'non_dev_as_dev_dep'],",
1805+
" root_module_direct_dev_deps=['direct_dev_dep', 'missing_direct_dev_dep',"
1806+
+ " 'dev_as_non_dev_dep'],",
18021807
" )",
18031808
"ext=module_extension(implementation=_ext_impl)");
18041809

@@ -1822,19 +1827,28 @@ public void extensionMetadata() throws Exception {
18221827
+ " build to fail):\n"
18231828
+ " missing_direct_dep, missing_direct_dev_dep\n"
18241829
+ "\n"
1830+
+ "Imported as a regular dependency, but reported as a dev dependency by the"
1831+
+ " extension (may cause the build to fail when used by other modules):\n"
1832+
+ " dev_as_non_dev_dep\n"
1833+
+ "\n"
1834+
+ "Imported as a dev dependency, but reported as a regular dependency by the"
1835+
+ " extension (may cause the build to fail when used by other modules):\n"
1836+
+ " non_dev_as_dev_dep\n"
1837+
+ "\n"
18251838
+ "Imported, but reported as indirect dependencies by the extension:\n"
18261839
+ " indirect_dep, indirect_dev_dep\n"
18271840
+ "\n"
18281841
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
18291842
+ " issues:\033[0m\n"
18301843
+ "\n"
1831-
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep' //MODULE.bazel:all\n"
1832-
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext indirect_dep invalid_dep'"
1833-
+ " //MODULE.bazel:all\n"
1834-
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext missing_direct_dev_dep'"
1844+
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep non_dev_as_dev_dep'"
18351845
+ " //MODULE.bazel:all\n"
1836-
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep'"
1837-
+ " //MODULE.bazel:all",
1846+
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep"
1847+
+ " indirect_dep invalid_dep' //MODULE.bazel:all\n"
1848+
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep"
1849+
+ " missing_direct_dev_dep' //MODULE.bazel:all\n"
1850+
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
1851+
+ " non_dev_as_dev_dep' //MODULE.bazel:all",
18381852
ImmutableSet.of(EventKind.WARNING));
18391853
}
18401854

@@ -1904,6 +1918,10 @@ public void extensionMetadata_all() throws Exception {
19041918
+ " build to fail):\n"
19051919
+ " missing_direct_dep, missing_direct_dev_dep\n"
19061920
+ "\n"
1921+
+ "Imported as a dev dependency, but reported as a regular dependency by the"
1922+
+ " extension (may cause the build to fail when used by other modules):\n"
1923+
+ " direct_dev_dep, indirect_dev_dep\n"
1924+
+ "\n"
19071925
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
19081926
+ " issues:\033[0m\n"
19091927
+ "\n"
@@ -1983,6 +2001,10 @@ public void extensionMetadata_allDev() throws Exception {
19832001
+ " build to fail):\n"
19842002
+ " missing_direct_dep, missing_direct_dev_dep\n"
19852003
+ "\n"
2004+
+ "Imported as a regular dependency, but reported as a dev dependency by the"
2005+
+ " extension (may cause the build to fail when used by other modules):\n"
2006+
+ " direct_dep, indirect_dep\n"
2007+
+ "\n"
19862008
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
19872009
+ " issues:\033[0m\n"
19882010
+ "\n"

0 commit comments

Comments
 (0)