Skip to content

Commit 4bf0167

Browse files
fmeumcopybara-github
authored andcommitted
Report dev/non-dev deps imported via non-dev/dev usages
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
1 parent 0c9ea83 commit 4bf0167

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
@@ -253,6 +253,28 @@ private static Optional<Event> generateFixupMessage(
253253
String.join(", ", missingImports));
254254
}
255255

256+
var nonDevImportsOfDevDeps =
257+
ImmutableSortedSet.copyOf(Sets.intersection(expectedDevImports, actualImports));
258+
if (!nonDevImportsOfDevDeps.isEmpty()) {
259+
message +=
260+
String.format(
261+
"Imported as a regular dependency, but reported as a dev dependency by the "
262+
+ "extension (may cause the build to fail when used by other modules):\n"
263+
+ " %s\n\n",
264+
String.join(", ", nonDevImportsOfDevDeps));
265+
}
266+
267+
var devImportsOfNonDevDeps =
268+
ImmutableSortedSet.copyOf(Sets.intersection(expectedImports, actualDevImports));
269+
if (!devImportsOfNonDevDeps.isEmpty()) {
270+
message +=
271+
String.format(
272+
"Imported as a dev dependency, but reported as a regular dependency by the "
273+
+ "extension (may cause the build to fail when used by other modules):\n"
274+
+ " %s\n\n",
275+
String.join(", ", devImportsOfNonDevDeps));
276+
}
277+
256278
var indirectDepImports =
257279
ImmutableSortedSet.copyOf(
258280
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
@@ -1765,13 +1765,15 @@ public void extensionMetadata() throws Exception {
17651765
" ext,",
17661766
" 'indirect_dep',",
17671767
" 'invalid_dep',",
1768+
" 'dev_as_non_dev_dep',",
17681769
" my_direct_dep = 'direct_dep',",
17691770
")",
17701771
"ext_dev = use_extension('@ext//:defs.bzl', 'ext', dev_dependency = True)",
17711772
"use_repo(",
17721773
" ext_dev,",
17731774
" 'indirect_dev_dep',",
17741775
" 'invalid_dev_dep',",
1776+
" 'non_dev_as_dev_dep',",
17751777
" my_direct_dev_dep = 'direct_dev_dep',",
17761778
")");
17771779
scratch.file(workspaceRoot.getRelative("BUILD").getPathString());
@@ -1800,9 +1802,12 @@ public void extensionMetadata() throws Exception {
18001802
" data_repo(name='missing_direct_dev_dep')",
18011803
" data_repo(name='indirect_dep')",
18021804
" data_repo(name='indirect_dev_dep')",
1805+
" data_repo(name='dev_as_non_dev_dep')",
1806+
" data_repo(name='non_dev_as_dev_dep')",
18031807
" return ctx.extension_metadata(",
1804-
" root_module_direct_deps=['direct_dep', 'missing_direct_dep'],",
1805-
" root_module_direct_dev_deps=['direct_dev_dep', 'missing_direct_dev_dep'],",
1808+
" root_module_direct_deps=['direct_dep', 'missing_direct_dep', 'non_dev_as_dev_dep'],",
1809+
" root_module_direct_dev_deps=['direct_dev_dep', 'missing_direct_dev_dep',"
1810+
+ " 'dev_as_non_dev_dep'],",
18061811
" )",
18071812
"ext=module_extension(implementation=_ext_impl)");
18081813

@@ -1826,19 +1831,28 @@ public void extensionMetadata() throws Exception {
18261831
+ " build to fail):\n"
18271832
+ " missing_direct_dep, missing_direct_dev_dep\n"
18281833
+ "\n"
1834+
+ "Imported as a regular dependency, but reported as a dev dependency by the"
1835+
+ " extension (may cause the build to fail when used by other modules):\n"
1836+
+ " dev_as_non_dev_dep\n"
1837+
+ "\n"
1838+
+ "Imported as a dev dependency, but reported as a regular dependency by the"
1839+
+ " extension (may cause the build to fail when used by other modules):\n"
1840+
+ " non_dev_as_dev_dep\n"
1841+
+ "\n"
18291842
+ "Imported, but reported as indirect dependencies by the extension:\n"
18301843
+ " indirect_dep, indirect_dev_dep\n"
18311844
+ "\n"
18321845
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
18331846
+ " issues:\033[0m\n"
18341847
+ "\n"
1835-
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep' //MODULE.bazel:all\n"
1836-
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext indirect_dep invalid_dep'"
1837-
+ " //MODULE.bazel:all\n"
1838-
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext missing_direct_dev_dep'"
1848+
+ "buildozer 'use_repo_add @ext//:defs.bzl ext missing_direct_dep non_dev_as_dev_dep'"
18391849
+ " //MODULE.bazel:all\n"
1840-
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep'"
1841-
+ " //MODULE.bazel:all",
1850+
+ "buildozer 'use_repo_remove @ext//:defs.bzl ext dev_as_non_dev_dep"
1851+
+ " indirect_dep invalid_dep' //MODULE.bazel:all\n"
1852+
+ "buildozer 'use_repo_add dev @ext//:defs.bzl ext dev_as_non_dev_dep"
1853+
+ " missing_direct_dev_dep' //MODULE.bazel:all\n"
1854+
+ "buildozer 'use_repo_remove dev @ext//:defs.bzl ext indirect_dev_dep invalid_dev_dep"
1855+
+ " non_dev_as_dev_dep' //MODULE.bazel:all",
18421856
ImmutableSet.of(EventKind.WARNING));
18431857
}
18441858

@@ -1908,6 +1922,10 @@ public void extensionMetadata_all() throws Exception {
19081922
+ " build to fail):\n"
19091923
+ " missing_direct_dep, missing_direct_dev_dep\n"
19101924
+ "\n"
1925+
+ "Imported as a dev dependency, but reported as a regular dependency by the"
1926+
+ " extension (may cause the build to fail when used by other modules):\n"
1927+
+ " direct_dev_dep, indirect_dev_dep\n"
1928+
+ "\n"
19111929
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
19121930
+ " issues:\033[0m\n"
19131931
+ "\n"
@@ -1987,6 +2005,10 @@ public void extensionMetadata_allDev() throws Exception {
19872005
+ " build to fail):\n"
19882006
+ " missing_direct_dep, missing_direct_dev_dep\n"
19892007
+ "\n"
2008+
+ "Imported as a regular dependency, but reported as a dev dependency by the"
2009+
+ " extension (may cause the build to fail when used by other modules):\n"
2010+
+ " direct_dep, indirect_dep\n"
2011+
+ "\n"
19902012
+ "\033[35m\033[1m ** You can use the following buildozer command(s) to fix these"
19912013
+ " issues:\033[0m\n"
19922014
+ "\n"

0 commit comments

Comments
 (0)