@@ -50,12 +50,14 @@ public final class BazelStarlarkEnvironment {
50
50
private final ImmutableMap <String , Object > uninjectedBuildBzlNativeBindings ;
51
51
/** The "native" module fields for a WORKSPACE-loaded bzl module. */
52
52
private final ImmutableMap <String , Object > workspaceBzlNativeBindings ;
53
- /** The top-level predeclared symbols for a BUILD-loaded bzl module, before builtins injection. */
53
+ /** The top-level predeclared symbols for a BUILD-loaded bzl module before builtins injection. */
54
54
private final ImmutableMap <String , Object > uninjectedBuildBzlEnv ;
55
55
/** The top-level predeclared symbols for BUILD files, before builtins injection and prelude. */
56
56
private final ImmutableMap <String , Object > uninjectedBuildEnv ;
57
- /** The top-level predeclared symbols for a WORKSPACE-loaded bzl module. */
58
- private final ImmutableMap <String , Object > workspaceBzlEnv ;
57
+ /**
58
+ * The top-level predeclared symbols for a WORKSPACE-loaded bzl module before builtins injection.
59
+ */
60
+ private final ImmutableMap <String , Object > uninjectedWorkspaceBzlEnv ;
59
61
/** The top-level predeclared symbols for a bzl module in the {@code @_builtins} pseudo-repo. */
60
62
private final ImmutableMap <String , Object > builtinsBzlEnv ;
61
63
/** The top-level predeclared symbols for a bzl module in the Bzlmod system. */
@@ -75,7 +77,8 @@ public final class BazelStarlarkEnvironment {
75
77
this .workspaceBzlNativeBindings = createWorkspaceBzlNativeBindings (ruleClassProvider , version );
76
78
this .uninjectedBuildBzlEnv =
77
79
createUninjectedBuildBzlEnv (ruleClassProvider , uninjectedBuildBzlNativeBindings );
78
- this .workspaceBzlEnv = createWorkspaceBzlEnv (ruleClassProvider , workspaceBzlNativeBindings );
80
+ this .uninjectedWorkspaceBzlEnv =
81
+ createWorkspaceBzlEnv (ruleClassProvider , workspaceBzlNativeBindings );
79
82
// TODO(pcloudy): this should be a bzlmod specific environment, but keep using the workspace
80
83
// envirnment until we implement module rules.
81
84
this .bzlmodBzlEnv = createWorkspaceBzlEnv (ruleClassProvider , workspaceBzlNativeBindings );
@@ -122,9 +125,9 @@ public ImmutableMap<String, Object> getUninjectedBuildEnv() {
122
125
return uninjectedBuildEnv ;
123
126
}
124
127
125
- /** Returns the environment for WORKSPACE-loaded bzl files. */
126
- public ImmutableMap <String , Object > getWorkspaceBzlEnv () {
127
- return workspaceBzlEnv ;
128
+ /** Returns the environment for WORKSPACE-loaded bzl files before builtins injection . */
129
+ public ImmutableMap <String , Object > getUninjectedWorkspaceBzlEnv () {
130
+ return uninjectedWorkspaceBzlEnv ;
128
131
}
129
132
130
133
/** Returns the environment for bzl files in the {@code @_builtins} pseudo-repository. */
@@ -338,17 +341,57 @@ private static boolean injectionApplies(String key, Map<String, Boolean> overrid
338
341
* documentation for {@code --experimental_builtins_injection_override}. Non-injected symbols must
339
342
* still obey the above constraints.
340
343
*
341
- * @see StarlarkBuiltinsFunction
344
+ * @see com.google.devtools.build.lib.skyframe. StarlarkBuiltinsFunction
342
345
*/
343
346
public ImmutableMap <String , Object > createBuildBzlEnvUsingInjection (
344
347
Map <String , Object > exportedToplevels ,
345
348
Map <String , Object > exportedRules ,
346
349
List <String > overridesList )
347
350
throws InjectionException {
351
+ return createBuildBzlEnvUsingInjection (
352
+ exportedToplevels , exportedRules , overridesList , uninjectedBuildBzlNativeBindings );
353
+ }
354
+
355
+ /**
356
+ * Constructs an environment for a WORKSPACE-loaded bzl file based on the default environment, the
357
+ * maps corresponding to the {@code exported_toplevels} and {@code exported_rules} dicts, and the
358
+ * value of {@code --experimental_builtins_injection_override}.
359
+ *
360
+ * @see com.google.devtools.build.lib.skyframe.StarlarkBuiltinsFunction
361
+ */
362
+ public ImmutableMap <String , Object > createWorkspaceBzlEnvUsingInjection (
363
+ Map <String , Object > exportedToplevels ,
364
+ Map <String , Object > exportedRules ,
365
+ List <String > overridesList )
366
+ throws InjectionException {
367
+ return createBuildBzlEnvUsingInjection (
368
+ exportedToplevels , exportedRules , overridesList , workspaceBzlNativeBindings );
369
+ }
370
+
371
+ private ImmutableMap <String , Object > createBuildBzlEnvUsingInjection (
372
+ Map <String , Object > exportedToplevels ,
373
+ Map <String , Object > exportedRules ,
374
+ List <String > overridesList ,
375
+ Map <String , Object > nativeBase )
376
+ throws InjectionException {
348
377
Map <String , Boolean > overridesMap = parseInjectionOverridesList (overridesList );
349
378
379
+ Map <String , Object > env = new HashMap <>(ruleClassProvider .getEnvironment ());
380
+
381
+ // Determine "native" bindings.
382
+ // TODO(#11954): See above comment in createUninjectedBuildBzlEnv.
383
+ Map <String , Object > nativeBindings = new HashMap <>(nativeBase );
384
+ for (Map .Entry <String , Object > entry : exportedRules .entrySet ()) {
385
+ String key = entry .getKey ();
386
+ String name = getKeySuffix (key );
387
+ validateSymbolIsInjectable (name , nativeBindings .keySet (), ruleFunctions .keySet (), "rule" );
388
+ if (injectionApplies (key , overridesMap )) {
389
+ nativeBindings .put (name , entry .getValue ());
390
+ }
391
+ }
392
+ env .put ("native" , createNativeModule (nativeBindings ));
393
+
350
394
// Determine top-level symbols.
351
- Map <String , Object > env = new HashMap <>(uninjectedBuildBzlEnv );
352
395
for (Map .Entry <String , Object > entry : exportedToplevels .entrySet ()) {
353
396
String key = entry .getKey ();
354
397
String name = getKeySuffix (key );
@@ -362,19 +405,6 @@ public ImmutableMap<String, Object> createBuildBzlEnvUsingInjection(
362
405
}
363
406
}
364
407
365
- // Determine "native" bindings.
366
- // TODO(#11954): See above comment in createUninjectedBuildBzlEnv.
367
- Map <String , Object > nativeBindings = new HashMap <>(uninjectedBuildBzlNativeBindings );
368
- for (Map .Entry <String , Object > entry : exportedRules .entrySet ()) {
369
- String key = entry .getKey ();
370
- String name = getKeySuffix (key );
371
- validateSymbolIsInjectable (name , nativeBindings .keySet (), ruleFunctions .keySet (), "rule" );
372
- if (injectionApplies (key , overridesMap )) {
373
- nativeBindings .put (name , entry .getValue ());
374
- }
375
- }
376
-
377
- env .put ("native" , createNativeModule (nativeBindings ));
378
408
return ImmutableMap .copyOf (env );
379
409
}
380
410
0 commit comments