@@ -111,6 +111,12 @@ func cleanTmp() error {
111
111
return nil
112
112
}
113
113
114
+ type flushCacheItem struct {
115
+ Version string
116
+ Path []string
117
+ Envs map [string ]* string
118
+ }
119
+
114
120
func envFlag (ctx * cli.Context ) error {
115
121
shellName := ctx .String ("shell" )
116
122
if shellName == "" {
@@ -166,15 +172,16 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
166
172
curToolVersion .SortedMap ,
167
173
}
168
174
169
- flushCache , err := cache .NewFileCache (filepath .Join (manager .PathMeta .CurTmpPath , "flush_env.cache" ))
175
+ flushCacheFile := filepath .Join (manager .PathMeta .CurTmpPath , "flush_env.cache" )
176
+ flushCache , err := cache .NewFileCache (flushCacheFile )
170
177
if err != nil {
171
178
return nil , err
172
179
}
173
180
defer flushCache .Close ()
174
181
175
182
var (
176
183
sdkEnvs []* internal.SdkEnv
177
- finalSdks = make (map [string ]struct {} )
184
+ finalSdks = make (map [string ]* flushCacheItem )
178
185
cacheLen = flushCache .Len ()
179
186
)
180
187
@@ -185,21 +192,30 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
185
192
}
186
193
if lookupSdk , err := manager .LookupSdk (name ); err == nil {
187
194
vv , ok := flushCache .Get (name )
188
- if ok && string (vv ) == version {
189
- logger .Debugf ("Hit cache, skip flush envrionment, %s@%s\n " , name , version )
190
- finalSdks [name ] = struct {}{}
191
- return nil
195
+ if ok {
196
+ item := flushCacheItem {}
197
+ if err = vv .Unmarshal (& item ); err != nil {
198
+ _ = os .Remove (flushCacheFile )
199
+ flushCache .Clear ()
200
+ }
201
+ if item .Version == version {
202
+ logger .Debugf ("Hit cache, skip flush envrionment, %s@%s\n " , name , version )
203
+ finalSdks [name ] = & item
204
+ return nil
205
+ }
192
206
} else {
193
- logger .Debugf ("No hit cache, name: %s cache: %s , expected: %s \n " , name , string ( vv ) , version )
207
+ logger .Debugf ("No hit cache, name: %s, expected: %s \n " , name , version )
194
208
}
195
209
v := internal .Version (version )
196
210
if keys , err := lookupSdk .EnvKeys (v , internal .ShellLocation ); err == nil {
197
- flushCache .Set (name , cache .Value (version ), cache .NeverExpired )
198
-
199
- sdkEnvs = append (sdkEnvs , & internal.SdkEnv {
200
- Sdk : lookupSdk , Env : keys ,
201
- })
202
- finalSdks [name ] = struct {}{}
211
+ item := flushCacheItem {
212
+ Version : version ,
213
+ Path : keys .Paths .Slice (),
214
+ Envs : keys .Variables ,
215
+ }
216
+ value , _ := cache .NewValue (& item )
217
+ flushCache .Set (name , value , cache .NeverExpired )
218
+ finalSdks [name ] = & item
203
219
}
204
220
}
205
221
return nil
@@ -211,12 +227,30 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
211
227
// Remove the old cache
212
228
if cacheLen != len (finalSdks ) {
213
229
for _ , sdkname := range flushCache .Keys () {
214
- if _ , ok := finalSdks [sdkname ]; ! ok {
230
+ item , ok := finalSdks [sdkname ]
231
+ // Remove the corresponding environment variable
232
+ if ! ok {
215
233
linkPath := filepath .Join (manager .PathMeta .CurTmpPath , sdkname )
216
234
logger .Debugf ("Remove unused sdk link: %s\n " , linkPath )
217
235
_ = os .Remove (linkPath )
218
236
flushCache .Remove (sdkname )
237
+ newEnvs := make (env.Vars )
238
+ for k , _ := range item .Envs {
239
+ newEnvs [k ] = nil
240
+ }
241
+ item .Envs = newEnvs
242
+ item .Path = make ([]string , 0 )
243
+ }
244
+ paths := env .NewPaths (env .EmptyPaths )
245
+ for _ , p := range item .Path {
246
+ paths .Add (p )
219
247
}
248
+ sdkEnvs = append (sdkEnvs , & internal.SdkEnv {
249
+ Sdk : nil , Env : & env.Envs {
250
+ Variables : item .Envs ,
251
+ Paths : paths ,
252
+ },
253
+ })
220
254
}
221
255
}
222
256
return sdkEnvs , nil
0 commit comments