2121import com .google .common .collect .ImmutableList ;
2222import com .google .common .collect .ImmutableMap ;
2323import com .google .common .collect .ImmutableSet ;
24+ import com .google .common .collect .Maps ;
2425import com .google .devtools .build .lib .analysis .BlazeVersionInfo ;
2526import com .google .devtools .build .lib .bazel .BazelVersion ;
27+ import com .google .devtools .build .lib .bazel .bzlmod .InterimModule .DepSpec ;
2628import com .google .devtools .build .lib .bazel .bzlmod .ModuleFileValue .RootModuleFileValue ;
2729import com .google .devtools .build .lib .bazel .bzlmod .Version .ParseException ;
2830import com .google .devtools .build .lib .bazel .repository .RepositoryOptions .BazelCompatibilityMode ;
@@ -78,18 +80,18 @@ public SkyValue compute(SkyKey skyKey, Environment env)
7880 if (root == null ) {
7981 return null ;
8082 }
81- ImmutableMap <ModuleKey , Module > initialDepGraph = Discovery .run (env , root );
83+ ImmutableMap <ModuleKey , InterimModule > initialDepGraph = Discovery .run (env , root );
8284 if (initialDepGraph == null ) {
8385 return null ;
8486 }
8587
86- BazelModuleResolutionValue selectionResultValue ;
88+ Selection . Result selectionResult ;
8789 try {
88- selectionResultValue = Selection .run (initialDepGraph , root .getOverrides ());
90+ selectionResult = Selection .run (initialDepGraph , root .getOverrides ());
8991 } catch (ExternalDepsException e ) {
9092 throw new BazelModuleResolutionFunctionException (e , Transience .PERSISTENT );
9193 }
92- ImmutableMap <ModuleKey , Module > resolvedDepGraph = selectionResultValue .getResolvedDepGraph ();
94+ ImmutableMap <ModuleKey , InterimModule > resolvedDepGraph = selectionResult .getResolvedDepGraph ();
9395
9496 verifyRootModuleDirectDepsAreAccurate (
9597 initialDepGraph .get (ModuleKey .ROOT ),
@@ -109,40 +111,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
109111 Objects .requireNonNull (ALLOWED_YANKED_VERSIONS .get (env ))),
110112 env .getListener ());
111113
112- // Add repo spec to each module and remove registry
113- try {
114- ImmutableMap .Builder <ModuleKey , Module > mapBuilder = ImmutableMap .builder ();
115- for (Map .Entry <ModuleKey , Module > entry : resolvedDepGraph .entrySet ()) {
116- Module module = entry .getValue ();
117- // Only change modules with registry (not overridden)
118- if (module .getRegistry () != null ) {
119- RepoSpec moduleRepoSpec =
120- module
121- .getRegistry ()
122- .getRepoSpec (module .getKey (), module .getCanonicalRepoName (), env .getListener ());
123- ModuleOverride override = root .getOverrides ().get (entry .getKey ().getName ());
124- moduleRepoSpec = maybeAppendAdditionalPatches (moduleRepoSpec , override );
125- module = module .toBuilder ().setRepoSpec (moduleRepoSpec ).setRegistry (null ).build ();
126- }
127- mapBuilder .put (entry .getKey (), module );
128- }
129- resolvedDepGraph = mapBuilder .buildOrThrow ();
130- } catch (IOException e ) {
131- throw new BazelModuleResolutionFunctionException (
132- ExternalDepsException .withMessage (
133- Code .ERROR_ACCESSING_REGISTRY ,
134- "Unable to get module repo spec from registry: %s" ,
135- e .getMessage ()),
136- Transience .PERSISTENT );
137- }
114+ ImmutableMap <ModuleKey , Module > finalDepGraph =
115+ computeFinalDepGraph (resolvedDepGraph , root .getOverrides (), env .getListener ());
138116
139- return BazelModuleResolutionValue .create (
140- resolvedDepGraph , selectionResultValue .getUnprunedDepGraph ());
117+ return BazelModuleResolutionValue .create (finalDepGraph , selectionResult .getUnprunedDepGraph ());
141118 }
142119
143120 private static void verifyRootModuleDirectDepsAreAccurate (
144- Module discoveredRootModule ,
145- Module resolvedRootModule ,
121+ InterimModule discoveredRootModule ,
122+ InterimModule resolvedRootModule ,
146123 CheckDirectDepsMode mode ,
147124 EventHandler eventHandler )
148125 throws BazelModuleResolutionFunctionException {
@@ -151,14 +128,14 @@ private static void verifyRootModuleDirectDepsAreAccurate(
151128 }
152129
153130 boolean failure = false ;
154- for (Map .Entry <String , ModuleKey > dep : discoveredRootModule .getDeps ().entrySet ()) {
155- ModuleKey resolved = resolvedRootModule .getDeps ().get (dep .getKey ());
156- if (!dep .getValue ().equals (resolved )) {
131+ for (Map .Entry <String , DepSpec > dep : discoveredRootModule .getDeps ().entrySet ()) {
132+ ModuleKey resolved = resolvedRootModule .getDeps ().get (dep .getKey ()). toModuleKey () ;
133+ if (!dep .getValue ().toModuleKey (). equals (resolved )) {
157134 String message =
158135 String .format (
159136 "For repository '%s', the root module requires module version %s, but got %s in the"
160137 + " resolved dependency graph." ,
161- dep .getKey (), dep .getValue (), resolved );
138+ dep .getKey (), dep .getValue (). toModuleKey () , resolved );
162139 if (mode == CheckDirectDepsMode .WARNING ) {
163140 eventHandler .handle (Event .warn (message ));
164141 } else {
@@ -177,7 +154,9 @@ private static void verifyRootModuleDirectDepsAreAccurate(
177154 }
178155
179156 public static void checkBazelCompatibility (
180- ImmutableCollection <Module > modules , BazelCompatibilityMode mode , EventHandler eventHandler )
157+ ImmutableCollection <InterimModule > modules ,
158+ BazelCompatibilityMode mode ,
159+ EventHandler eventHandler )
181160 throws BazelModuleResolutionFunctionException {
182161 if (mode == BazelCompatibilityMode .OFF ) {
183162 return ;
@@ -189,7 +168,7 @@ public static void checkBazelCompatibility(
189168 }
190169
191170 BazelVersion curVersion = BazelVersion .parse (currentBazelVersion );
192- for (Module module : modules ) {
171+ for (InterimModule module : modules ) {
193172 for (String compatVersion : module .getBazelCompatibility ()) {
194173 if (!curVersion .satisfiesCompatibility (compatVersion )) {
195174 String message =
@@ -306,14 +285,14 @@ private boolean parseModuleKeysFromString(
306285 return false ;
307286 }
308287
309- private void verifyYankedVersions (
310- ImmutableMap <ModuleKey , Module > depGraph ,
288+ private static void verifyYankedVersions (
289+ ImmutableMap <ModuleKey , InterimModule > depGraph ,
311290 Optional <ImmutableSet <ModuleKey >> allowedYankedVersions ,
312291 ExtendedEventHandler eventHandler )
313292 throws BazelModuleResolutionFunctionException , InterruptedException {
314293 // Check whether all resolved modules are either not yanked or allowed. Modules with a
315294 // NonRegistryOverride are ignored as their metadata is not available whatsoever.
316- for (Module m : depGraph .values ()) {
295+ for (InterimModule m : depGraph .values ()) {
317296 if (m .getKey ().equals (ModuleKey .ROOT ) || m .getRegistry () == null ) {
318297 continue ;
319298 }
@@ -349,7 +328,7 @@ private void verifyYankedVersions(
349328 }
350329 }
351330
352- private RepoSpec maybeAppendAdditionalPatches (RepoSpec repoSpec , ModuleOverride override ) {
331+ private static RepoSpec maybeAppendAdditionalPatches (RepoSpec repoSpec , ModuleOverride override ) {
353332 if (!(override instanceof SingleVersionOverride )) {
354333 return repoSpec ;
355334 }
@@ -369,6 +348,66 @@ private RepoSpec maybeAppendAdditionalPatches(RepoSpec repoSpec, ModuleOverride
369348 .build ();
370349 }
371350
351+ @ Nullable
352+ private static RepoSpec computeRepoSpec (
353+ InterimModule interimModule , ModuleOverride override , ExtendedEventHandler eventHandler )
354+ throws BazelModuleResolutionFunctionException , InterruptedException {
355+ if (interimModule .getRegistry () == null ) {
356+ // This module has a non-registry override. We don't need to store the repo spec in this case.
357+ return null ;
358+ }
359+ try {
360+ RepoSpec moduleRepoSpec =
361+ interimModule
362+ .getRegistry ()
363+ .getRepoSpec (
364+ interimModule .getKey (), interimModule .getCanonicalRepoName (), eventHandler );
365+ return maybeAppendAdditionalPatches (moduleRepoSpec , override );
366+ } catch (IOException e ) {
367+ throw new BazelModuleResolutionFunctionException (
368+ ExternalDepsException .withMessage (
369+ Code .ERROR_ACCESSING_REGISTRY ,
370+ "Unable to get module repo spec from registry: %s" ,
371+ e .getMessage ()),
372+ Transience .PERSISTENT );
373+ }
374+ }
375+
376+ /**
377+ * Builds a {@link Module} from an {@link InterimModule}, discarding unnecessary fields and adding
378+ * extra necessary ones (such as the repo spec).
379+ */
380+ static Module moduleFromInterimModule (
381+ InterimModule interim , ModuleOverride override , ExtendedEventHandler eventHandler )
382+ throws BazelModuleResolutionFunctionException , InterruptedException {
383+ return Module .builder ()
384+ .setName (interim .getName ())
385+ .setVersion (interim .getVersion ())
386+ .setKey (interim .getKey ())
387+ .setRepoName (interim .getRepoName ())
388+ .setExecutionPlatformsToRegister (interim .getExecutionPlatformsToRegister ())
389+ .setToolchainsToRegister (interim .getToolchainsToRegister ())
390+ .setDeps (ImmutableMap .copyOf (Maps .transformValues (interim .getDeps (), DepSpec ::toModuleKey )))
391+ .setRepoSpec (computeRepoSpec (interim , override , eventHandler ))
392+ .setExtensionUsages (interim .getExtensionUsages ())
393+ .build ();
394+ }
395+
396+ private static ImmutableMap <ModuleKey , Module > computeFinalDepGraph (
397+ ImmutableMap <ModuleKey , InterimModule > resolvedDepGraph ,
398+ ImmutableMap <String , ModuleOverride > overrides ,
399+ ExtendedEventHandler eventHandler )
400+ throws BazelModuleResolutionFunctionException , InterruptedException {
401+ ImmutableMap .Builder <ModuleKey , Module > finalDepGraph = ImmutableMap .builder ();
402+ for (Map .Entry <ModuleKey , InterimModule > entry : resolvedDepGraph .entrySet ()) {
403+ finalDepGraph .put (
404+ entry .getKey (),
405+ moduleFromInterimModule (
406+ entry .getValue (), overrides .get (entry .getKey ().getName ()), eventHandler ));
407+ }
408+ return finalDepGraph .buildOrThrow ();
409+ }
410+
372411 static class BazelModuleResolutionFunctionException extends SkyFunctionException {
373412 BazelModuleResolutionFunctionException (ExternalDepsException e , Transience transience ) {
374413 super (e , transience );
0 commit comments