15
15
16
16
package com .google .devtools .build .lib .bazel .bzlmod ;
17
17
18
- import com .google .common .base .Splitter ;
19
18
import com .google .common .base .Strings ;
20
19
import com .google .common .collect .ImmutableCollection ;
21
20
import com .google .common .collect .ImmutableList ;
22
21
import com .google .common .collect .ImmutableMap ;
23
- import com .google .common .collect .ImmutableSet ;
24
22
import com .google .common .collect .Maps ;
25
23
import com .google .devtools .build .lib .analysis .BlazeVersionInfo ;
26
24
import com .google .devtools .build .lib .bazel .BazelVersion ;
27
25
import com .google .devtools .build .lib .bazel .bzlmod .InterimModule .DepSpec ;
28
26
import com .google .devtools .build .lib .bazel .bzlmod .ModuleFileValue .RootModuleFileValue ;
29
- import com .google .devtools .build .lib .bazel .bzlmod .Version .ParseException ;
30
27
import com .google .devtools .build .lib .bazel .repository .RepositoryOptions .BazelCompatibilityMode ;
31
28
import com .google .devtools .build .lib .bazel .repository .RepositoryOptions .CheckDirectDepsMode ;
32
- import com .google .devtools .build .lib .cmdline .RepositoryName ;
33
29
import com .google .devtools .build .lib .events .Event ;
34
30
import com .google .devtools .build .lib .events .EventHandler ;
35
31
import com .google .devtools .build .lib .events .ExtendedEventHandler ;
36
32
import com .google .devtools .build .lib .server .FailureDetails .ExternalDeps .Code ;
37
- import com .google .devtools .build .lib .skyframe .ClientEnvironmentFunction ;
38
- import com .google .devtools .build .lib .skyframe .ClientEnvironmentValue ;
39
33
import com .google .devtools .build .lib .skyframe .PrecomputedValue .Precomputed ;
40
34
import com .google .devtools .build .skyframe .SkyFunction ;
41
35
import com .google .devtools .build .skyframe .SkyFunctionException ;
42
36
import com .google .devtools .build .skyframe .SkyFunctionException .Transience ;
43
37
import com .google .devtools .build .skyframe .SkyKey ;
44
38
import com .google .devtools .build .skyframe .SkyValue ;
45
39
import java .io .IOException ;
46
- import java .util .List ;
47
40
import java .util .Map ;
48
41
import java .util .Objects ;
49
- import java .util .Optional ;
50
42
import javax .annotation .Nullable ;
51
43
52
44
/**
@@ -59,22 +51,11 @@ public class BazelModuleResolutionFunction implements SkyFunction {
59
51
new Precomputed <>("check_direct_dependency" );
60
52
public static final Precomputed <BazelCompatibilityMode > BAZEL_COMPATIBILITY_MODE =
61
53
new Precomputed <>("bazel_compatibility_mode" );
62
- public static final Precomputed <List <String >> ALLOWED_YANKED_VERSIONS =
63
- new Precomputed <>("allowed_yanked_versions" );
64
-
65
- public static final String BZLMOD_ALLOWED_YANKED_VERSIONS_ENV = "BZLMOD_ALLOW_YANKED_VERSIONS" ;
66
54
67
55
@ Override
68
56
@ Nullable
69
57
public SkyValue compute (SkyKey skyKey , Environment env )
70
58
throws SkyFunctionException , InterruptedException {
71
-
72
- ClientEnvironmentValue allowedYankedVersionsFromEnv =
73
- (ClientEnvironmentValue )
74
- env .getValue (ClientEnvironmentFunction .key (BZLMOD_ALLOWED_YANKED_VERSIONS_ENV ));
75
- if (allowedYankedVersionsFromEnv == null ) {
76
- return null ;
77
- }
78
59
RootModuleFileValue root =
79
60
(RootModuleFileValue ) env .getValue (ModuleFileValue .KEY_FOR_ROOT_MODULE );
80
61
if (root == null ) {
@@ -104,12 +85,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
104
85
Objects .requireNonNull (BAZEL_COMPATIBILITY_MODE .get (env )),
105
86
env .getListener ());
106
87
107
- verifyYankedVersions (
108
- resolvedDepGraph ,
109
- parseYankedVersions (
110
- allowedYankedVersionsFromEnv .getValue (),
111
- Objects .requireNonNull (ALLOWED_YANKED_VERSIONS .get (env ))),
112
- env .getListener ());
88
+ checkNoYankedVersions (resolvedDepGraph );
113
89
114
90
ImmutableMap <ModuleKey , Module > finalDepGraph =
115
91
computeFinalDepGraph (resolvedDepGraph , root .getOverrides (), env .getListener ());
@@ -194,125 +170,10 @@ public static void checkBazelCompatibility(
194
170
}
195
171
}
196
172
197
- /**
198
- * Parse a set of allowed yanked version from command line flag (--allowed_yanked_versions) and
199
- * environment variable (ALLOWED_YANKED_VERSIONS). If `all` is specified, return Optional.empty();
200
- * otherwise returns the set of parsed modulel key.
201
- */
202
- private Optional <ImmutableSet <ModuleKey >> parseYankedVersions (
203
- String allowedYankedVersionsFromEnv , List <String > allowedYankedVersionsFromFlag )
204
- throws BazelModuleResolutionFunctionException {
205
- ImmutableSet .Builder <ModuleKey > allowedYankedVersionBuilder = new ImmutableSet .Builder <>();
206
- if (allowedYankedVersionsFromEnv != null ) {
207
- if (parseModuleKeysFromString (
208
- allowedYankedVersionsFromEnv ,
209
- allowedYankedVersionBuilder ,
210
- String .format (
211
- "envirnoment variable %s=%s" ,
212
- BZLMOD_ALLOWED_YANKED_VERSIONS_ENV , allowedYankedVersionsFromEnv ))) {
213
- return Optional .empty ();
214
- }
215
- }
216
- for (String allowedYankedVersions : allowedYankedVersionsFromFlag ) {
217
- if (parseModuleKeysFromString (
218
- allowedYankedVersions ,
219
- allowedYankedVersionBuilder ,
220
- String .format ("command line flag --allow_yanked_versions=%s" , allowedYankedVersions ))) {
221
- return Optional .empty ();
222
- }
223
- }
224
- return Optional .of (allowedYankedVersionBuilder .build ());
225
- }
226
-
227
- /**
228
- * Parse of a comma-separated list of module version(s) of the form '<module name>@<version>' or
229
- * 'all' from the string. Returns true if 'all' is present, otherwise returns false.
230
- */
231
- private boolean parseModuleKeysFromString (
232
- String input , ImmutableSet .Builder <ModuleKey > allowedYankedVersionBuilder , String context )
173
+ private static void checkNoYankedVersions (ImmutableMap <ModuleKey , InterimModule > depGraph )
233
174
throws BazelModuleResolutionFunctionException {
234
- ImmutableList <String > moduleStrs = ImmutableList .copyOf (Splitter .on (',' ).split (input ));
235
-
236
- for (String moduleStr : moduleStrs ) {
237
- if (moduleStr .equals ("all" )) {
238
- return true ;
239
- }
240
-
241
- if (moduleStr .isEmpty ()) {
242
- continue ;
243
- }
244
-
245
- String [] pieces = moduleStr .split ("@" , 2 );
246
-
247
- if (pieces .length != 2 ) {
248
- throw new BazelModuleResolutionFunctionException (
249
- ExternalDepsException .withMessage (
250
- Code .VERSION_RESOLUTION_ERROR ,
251
- "Parsing %s failed, module versions must be of the form '<module name>@<version>'" ,
252
- context ),
253
- Transience .PERSISTENT );
254
- }
255
-
256
- if (!RepositoryName .VALID_MODULE_NAME .matcher (pieces [0 ]).matches ()) {
257
- throw new BazelModuleResolutionFunctionException (
258
- ExternalDepsException .withMessage (
259
- Code .VERSION_RESOLUTION_ERROR ,
260
- "Parsing %s failed, invalid module name '%s': valid names must 1) only contain"
261
- + " lowercase letters (a-z), digits (0-9), dots (.), hyphens (-), and"
262
- + " underscores (_); 2) begin with a lowercase letter; 3) end with a lowercase"
263
- + " letter or digit." ,
264
- context ,
265
- pieces [0 ]),
266
- Transience .PERSISTENT );
267
- }
268
-
269
- Version version ;
270
- try {
271
- version = Version .parse (pieces [1 ]);
272
- } catch (ParseException e ) {
273
- throw new BazelModuleResolutionFunctionException (
274
- ExternalDepsException .withCauseAndMessage (
275
- Code .VERSION_RESOLUTION_ERROR ,
276
- e ,
277
- "Parsing %s failed, invalid version specified for module: %s" ,
278
- context ,
279
- pieces [1 ]),
280
- Transience .PERSISTENT );
281
- }
282
-
283
- allowedYankedVersionBuilder .add (ModuleKey .create (pieces [0 ], version ));
284
- }
285
- return false ;
286
- }
287
-
288
- private static void verifyYankedVersions (
289
- ImmutableMap <ModuleKey , InterimModule > depGraph ,
290
- Optional <ImmutableSet <ModuleKey >> allowedYankedVersions ,
291
- ExtendedEventHandler eventHandler )
292
- throws BazelModuleResolutionFunctionException , InterruptedException {
293
- // Check whether all resolved modules are either not yanked or allowed. Modules with a
294
- // NonRegistryOverride are ignored as their metadata is not available whatsoever.
295
175
for (InterimModule m : depGraph .values ()) {
296
- if (m .getKey ().equals (ModuleKey .ROOT ) || m .getRegistry () == null ) {
297
- continue ;
298
- }
299
- Optional <ImmutableMap <Version , String >> yankedVersions ;
300
- try {
301
- yankedVersions = m .getRegistry ().getYankedVersions (m .getKey ().getName (), eventHandler );
302
- } catch (IOException e ) {
303
- eventHandler .handle (
304
- Event .warn (
305
- String .format (
306
- "Could not read metadata file for module %s: %s" , m .getKey (), e .getMessage ())));
307
- continue ;
308
- }
309
- if (yankedVersions .isEmpty ()) {
310
- continue ;
311
- }
312
- String yankedInfo = yankedVersions .get ().get (m .getVersion ());
313
- if (yankedInfo != null
314
- && allowedYankedVersions .isPresent ()
315
- && !allowedYankedVersions .get ().contains (m .getKey ())) {
176
+ if (m .getYankedInfo ().isPresent ()) {
316
177
throw new BazelModuleResolutionFunctionException (
317
178
ExternalDepsException .withMessage (
318
179
Code .VERSION_RESOLUTION_ERROR ,
@@ -322,7 +183,7 @@ private static void verifyYankedVersions(
322
183
+ "continue using this version, allow it using the --allow_yanked_versions "
323
184
+ "flag or the BZLMOD_ALLOW_YANKED_VERSIONS env variable." ,
324
185
m .getKey (),
325
- yankedInfo ),
186
+ m . getYankedInfo (). get () ),
326
187
Transience .PERSISTENT );
327
188
}
328
189
}
0 commit comments