diff --git a/internal/env/path.go b/internal/env/path.go index 5262c41a..2f109453 100644 --- a/internal/env/path.go +++ b/internal/env/path.go @@ -17,6 +17,8 @@ package env import ( + "fmt" + "github.com/version-fox/vfox/internal/logger" "github.com/version-fox/vfox/internal/util" "os" "path/filepath" @@ -43,12 +45,13 @@ func (p *Paths) Merge(other *Paths) *Paths { } // ToBinPaths returns a BinPaths from Paths which contains only executable files. -func (p *Paths) ToBinPaths() *Paths { +func (p *Paths) ToBinPaths() (*Paths, error) { bins := NewPaths(EmptyPaths) for _, path := range p.Slice() { dir, err := os.ReadDir(path) if err != nil { - return nil + logger.Debugf("Failed to read bin paths:%s: %v", path, err) + return nil, fmt.Errorf("failed to read bin paths:%s: %w", path, err) } for _, d := range dir { if d.IsDir() { @@ -60,7 +63,7 @@ func (p *Paths) ToBinPaths() *Paths { } } } - return bins + return bins, nil } // NewPaths returns a new Paths. diff --git a/internal/sdk.go b/internal/sdk.go index 650886d3..c6229dbe 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -379,7 +379,10 @@ func (b *Sdk) Use(version Version, scope UseScope) error { return fmt.Errorf("failed to read tool versions, err:%w", err) } - bins := keys.Paths.ToBinPaths() + bins, err := keys.Paths.ToBinPaths() + if err != nil { + return err + } for _, bin := range bins.Slice() { binShim := shim.NewShim(bin, b.sdkManager.PathMeta.GlobalShimsPath) if err = binShim.Generate(); err != nil { @@ -428,7 +431,10 @@ func (b *Sdk) useInHook(version Version, scope UseScope) error { if err != nil { return err } - binPaths := envKeys.Paths.ToBinPaths() + binPaths, err := envKeys.Paths.ToBinPaths() + if err != nil { + return err + } sdkEnv := SdkEnv{ Sdk: b, @@ -732,9 +738,11 @@ func (b *Sdk) ClearCurrentEnv() error { return err } fmt.Println("Cleaning up the shims...") - for _, p := range keys.Paths.ToBinPaths().Slice() { - if err = shim.NewShim(p, b.sdkManager.PathMeta.GlobalShimsPath).Clear(); err != nil { - return err + if paths, err := keys.Paths.ToBinPaths(); err == nil { + for _, p := range paths.Slice() { + if err = shim.NewShim(p, b.sdkManager.PathMeta.GlobalShimsPath).Clear(); err != nil { + return err + } } }