Skip to content

Commit 76d2508

Browse files
committed
fix: remove unused environment variable
1 parent 385686b commit 76d2508

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

cmd/commands/env.go

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ func cleanTmp() error {
111111
return nil
112112
}
113113

114+
type flushCacheItem struct {
115+
Version string
116+
Path []string
117+
Envs map[string]*string
118+
}
119+
114120
func envFlag(ctx *cli.Context) error {
115121
shellName := ctx.String("shell")
116122
if shellName == "" {
@@ -166,15 +172,16 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
166172
curToolVersion.SortedMap,
167173
}
168174

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)
170177
if err != nil {
171178
return nil, err
172179
}
173180
defer flushCache.Close()
174181

175182
var (
176183
sdkEnvs []*internal.SdkEnv
177-
finalSdks = make(map[string]struct{})
184+
finalSdks = make(map[string]*flushCacheItem)
178185
cacheLen = flushCache.Len()
179186
)
180187

@@ -185,21 +192,30 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
185192
}
186193
if lookupSdk, err := manager.LookupSdk(name); err == nil {
187194
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+
}
192206
} 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)
194208
}
195209
v := internal.Version(version)
196210
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
203219
}
204220
}
205221
return nil
@@ -211,12 +227,30 @@ func aggregateEnvKeys(manager *internal.Manager) (internal.SdkEnvs, error) {
211227
// Remove the old cache
212228
if cacheLen != len(finalSdks) {
213229
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 {
215233
linkPath := filepath.Join(manager.PathMeta.CurTmpPath, sdkname)
216234
logger.Debugf("Remove unused sdk link: %s\n", linkPath)
217235
_ = os.Remove(linkPath)
218236
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)
219247
}
248+
sdkEnvs = append(sdkEnvs, &internal.SdkEnv{
249+
Sdk: nil, Env: &env.Envs{
250+
Variables: item.Envs,
251+
Paths: paths,
252+
},
253+
})
220254
}
221255
}
222256
return sdkEnvs, nil

internal/util/map.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *SortedMap[K, V]) Len() int {
6666
}
6767

6868
func (s *SortedMap[K, V]) Keys() []K {
69-
return s.keys
69+
return append([]K{}, s.keys...)
7070
}
7171

7272
func (s *SortedMap[K, V]) Merge(sortedMap *SortedMap[K, V]) {

0 commit comments

Comments
 (0)