25
25
import com .google .common .collect .ImmutableList ;
26
26
import com .google .common .collect .ImmutableMap ;
27
27
import com .google .common .collect .ImmutableSet ;
28
+ import com .google .common .collect .ImmutableSortedMap ;
28
29
import com .google .common .collect .ImmutableTable ;
29
30
import com .google .common .collect .Iterables ;
30
31
import com .google .common .collect .Maps ;
@@ -230,6 +231,15 @@ public SkyValue compute(SkyKey skyKey, Environment env)
230
231
// result is taken from the lockfile, we can already populate the lockfile info. This is
231
232
// necessary to prevent the extension from rerunning when only the imports change.
232
233
if (lockfileMode == LockfileMode .UPDATE || lockfileMode == LockfileMode .REFRESH ) {
234
+ var envVariables =
235
+ ImmutableMap .<RepoRecordedInput .EnvVar , Optional <String >>builder ()
236
+ // The environment variable dependencies statically declared via the 'environ'
237
+ // attribute.
238
+ .putAll (RepoRecordedInput .EnvVar .wrap (extension .getStaticEnvVars ()))
239
+ // The environment variable dependencies dynamically declared via the 'getenv' method.
240
+ .putAll (moduleExtensionResult .getRecordedEnvVarInputs ())
241
+ .buildKeepingLast ();
242
+
233
243
lockFileInfo =
234
244
Optional .of (
235
245
new LockFileModuleExtension .WithFactors (
@@ -241,7 +251,7 @@ public SkyValue compute(SkyKey skyKey, Environment env)
241
251
GsonTypeAdapterUtil .SINGLE_EXTENSION_USAGES_VALUE_GSON , usagesValue ))
242
252
.setRecordedFileInputs (moduleExtensionResult .getRecordedFileInputs ())
243
253
.setRecordedDirentsInputs (moduleExtensionResult .getRecordedDirentsInputs ())
244
- .setEnvVariables (extension . getEnvVars ( ))
254
+ .setEnvVariables (ImmutableSortedMap . copyOf ( envVariables ))
245
255
.setGeneratedRepoSpecs (generatedRepoSpecs )
246
256
.setModuleExtensionMetadata (moduleExtensionMetadata )
247
257
.setRecordedRepoMappingEntries (
@@ -284,7 +294,11 @@ private SingleExtensionValue tryGettingValueFromLockFile(
284
294
+ extensionId
285
295
+ "' or one of its transitive .bzl files has changed" );
286
296
}
287
- if (!extension .getEnvVars ().equals (lockedExtension .getEnvVariables ())) {
297
+ if (didRecordedInputsChange (
298
+ env ,
299
+ directories ,
300
+ // didRecordedInputsChange expects possibly null String values.
301
+ Maps .transformValues (lockedExtension .getEnvVariables (), v -> v .orElse (null )))) {
288
302
diffRecorder .record (
289
303
"The environment variables the extension '"
290
304
+ extensionId
@@ -415,7 +429,7 @@ private static boolean didRepoMappingsChange(
415
429
private static boolean didRecordedInputsChange (
416
430
Environment env ,
417
431
BlazeDirectories directories ,
418
- ImmutableMap <? extends RepoRecordedInput , String > recordedInputs )
432
+ Map <? extends RepoRecordedInput , String > recordedInputs )
419
433
throws InterruptedException , NeedsSkyframeRestartException {
420
434
boolean upToDate = RepoRecordedInput .areAllValuesUpToDate (env , directories , recordedInputs );
421
435
if (env .valuesMissing ()) {
@@ -523,15 +537,15 @@ private BzlLoadValue loadBzlFile(
523
537
* <p>The general idiom is to "load" such a {@link RunnableExtension} object by getting as much
524
538
* information about it as needed to determine whether it can be reused from the lockfile (hence
525
539
* methods such as {@link #getEvalFactors()}, {@link #getBzlTransitiveDigest()}, {@link
526
- * #getEnvVars ()}). Then the {@link #run} method can be called if it's determined that we can't
527
- * reuse the cached results in the lockfile and have to re-run this extension.
540
+ * #getStaticEnvVars ()}). Then the {@link #run} method can be called if it's determined that we
541
+ * can't reuse the cached results in the lockfile and have to re-run this extension.
528
542
*/
529
543
private interface RunnableExtension {
530
544
ModuleExtensionEvalFactors getEvalFactors ();
531
545
532
546
byte [] getBzlTransitiveDigest ();
533
547
534
- ImmutableMap <String , String > getEnvVars ();
548
+ ImmutableMap <String , Optional < String >> getStaticEnvVars ();
535
549
536
550
@ Nullable
537
551
RunModuleExtensionResult run (
@@ -682,7 +696,7 @@ public byte[] getBzlTransitiveDigest() {
682
696
}
683
697
684
698
@ Override
685
- public ImmutableMap <String , String > getEnvVars () {
699
+ public ImmutableMap <String , Optional < String >> getStaticEnvVars () {
686
700
return ImmutableMap .of ();
687
701
}
688
702
@@ -774,6 +788,7 @@ public RunModuleExtensionResult run(
774
788
generatedRepoSpecs .put (name , repoSpec );
775
789
}
776
790
return RunModuleExtensionResult .create (
791
+ ImmutableMap .of (),
777
792
ImmutableMap .of (),
778
793
ImmutableMap .of (),
779
794
generatedRepoSpecs .buildOrThrow (),
@@ -820,7 +835,7 @@ private RegularRunnableExtension loadRegularRunnableExtension(
820
835
Transience .PERSISTENT );
821
836
}
822
837
823
- ImmutableMap <String , String > envVars =
838
+ ImmutableMap <String , Optional < String > > envVars =
824
839
RepositoryFunction .getEnvVarValues (env , ImmutableSet .copyOf (extension .getEnvVariables ()));
825
840
if (envVars == null ) {
826
841
return null ;
@@ -831,15 +846,15 @@ private RegularRunnableExtension loadRegularRunnableExtension(
831
846
private final class RegularRunnableExtension implements RunnableExtension {
832
847
private final BzlLoadValue bzlLoadValue ;
833
848
private final ModuleExtension extension ;
834
- private final ImmutableMap <String , String > envVars ;
849
+ private final ImmutableMap <String , Optional < String >> staticEnvVars ;
835
850
836
851
RegularRunnableExtension (
837
852
BzlLoadValue bzlLoadValue ,
838
853
ModuleExtension extension ,
839
- ImmutableMap <String , String > envVars ) {
854
+ ImmutableMap <String , Optional < String >> staticEnvVars ) {
840
855
this .bzlLoadValue = bzlLoadValue ;
841
856
this .extension = extension ;
842
- this .envVars = envVars ;
857
+ this .staticEnvVars = staticEnvVars ;
843
858
}
844
859
845
860
@ Override
@@ -850,8 +865,8 @@ public ModuleExtensionEvalFactors getEvalFactors() {
850
865
}
851
866
852
867
@ Override
853
- public ImmutableMap <String , String > getEnvVars () {
854
- return envVars ;
868
+ public ImmutableMap <String , Optional < String >> getStaticEnvVars () {
869
+ return staticEnvVars ;
855
870
}
856
871
857
872
@ Override
@@ -952,6 +967,7 @@ public RunModuleExtensionResult run(
952
967
return RunModuleExtensionResult .create (
953
968
moduleContext .getRecordedFileInputs (),
954
969
moduleContext .getRecordedDirentsInputs (),
970
+ moduleContext .getRecordedEnvVarInputs (),
955
971
threadContext .getGeneratedRepoSpecs (),
956
972
moduleExtensionMetadata ,
957
973
repoMappingRecorder .recordedEntries ());
@@ -1016,6 +1032,8 @@ abstract static class RunModuleExtensionResult {
1016
1032
1017
1033
abstract ImmutableMap <RepoRecordedInput .Dirents , String > getRecordedDirentsInputs ();
1018
1034
1035
+ abstract ImmutableMap <RepoRecordedInput .EnvVar , Optional <String >> getRecordedEnvVarInputs ();
1036
+
1019
1037
abstract ImmutableMap <String , RepoSpec > getGeneratedRepoSpecs ();
1020
1038
1021
1039
abstract Optional <ModuleExtensionMetadata > getModuleExtensionMetadata ();
@@ -1025,12 +1043,14 @@ abstract static class RunModuleExtensionResult {
1025
1043
static RunModuleExtensionResult create (
1026
1044
ImmutableMap <RepoRecordedInput .File , String > recordedFileInputs ,
1027
1045
ImmutableMap <RepoRecordedInput .Dirents , String > recordedDirentsInputs ,
1046
+ ImmutableMap <RepoRecordedInput .EnvVar , Optional <String >> recordedEnvVarInputs ,
1028
1047
ImmutableMap <String , RepoSpec > generatedRepoSpecs ,
1029
1048
Optional <ModuleExtensionMetadata > moduleExtensionMetadata ,
1030
1049
ImmutableTable <RepositoryName , String , RepositoryName > recordedRepoMappingEntries ) {
1031
1050
return new AutoValue_SingleExtensionEvalFunction_RunModuleExtensionResult (
1032
1051
recordedFileInputs ,
1033
1052
recordedDirentsInputs ,
1053
+ recordedEnvVarInputs ,
1034
1054
generatedRepoSpecs ,
1035
1055
moduleExtensionMetadata ,
1036
1056
recordedRepoMappingEntries );
0 commit comments