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 (),
@@ -821,7 +836,7 @@ private RegularRunnableExtension loadRegularRunnableExtension(
821
836
}
822
837
823
838
ModuleExtension extension = (ModuleExtension ) exported ;
824
- ImmutableMap <String , String > envVars =
839
+ ImmutableMap <String , Optional < String > > envVars =
825
840
RepositoryFunction .getEnvVarValues (env , ImmutableSet .copyOf (extension .getEnvVariables ()));
826
841
if (envVars == null ) {
827
842
return null ;
@@ -832,15 +847,15 @@ private RegularRunnableExtension loadRegularRunnableExtension(
832
847
private final class RegularRunnableExtension implements RunnableExtension {
833
848
private final BzlLoadValue bzlLoadValue ;
834
849
private final ModuleExtension extension ;
835
- private final ImmutableMap <String , String > envVars ;
850
+ private final ImmutableMap <String , Optional < String >> staticEnvVars ;
836
851
837
852
RegularRunnableExtension (
838
853
BzlLoadValue bzlLoadValue ,
839
854
ModuleExtension extension ,
840
- ImmutableMap <String , String > envVars ) {
855
+ ImmutableMap <String , Optional < String >> staticEnvVars ) {
841
856
this .bzlLoadValue = bzlLoadValue ;
842
857
this .extension = extension ;
843
- this .envVars = envVars ;
858
+ this .staticEnvVars = staticEnvVars ;
844
859
}
845
860
846
861
@ Override
@@ -851,8 +866,8 @@ public ModuleExtensionEvalFactors getEvalFactors() {
851
866
}
852
867
853
868
@ Override
854
- public ImmutableMap <String , String > getEnvVars () {
855
- return envVars ;
869
+ public ImmutableMap <String , Optional < String >> getStaticEnvVars () {
870
+ return staticEnvVars ;
856
871
}
857
872
858
873
@ 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