Skip to content

Commit 45c31c9

Browse files
authored
fix: return no error from shims.RemoveAll when shims dir missing (#1967)
1 parent 3422874 commit 45c31c9

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

internal/cli/cli.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,16 @@ func reshimCommand(logger *log.Logger, tool, version string) (err error) {
13191319
return err
13201320
}
13211321

1322+
var plugin plugins.Plugin
1323+
1324+
if tool != "" {
1325+
plugin = plugins.New(conf, tool)
1326+
if err := plugin.Exists(); err != nil {
1327+
logger.Printf("No such plugin: %s", plugin.Name)
1328+
os.Exit(1)
1329+
return err
1330+
}
1331+
}
13221332
// if either tool or version are missing just regenerate all shims. This is
13231333
// fast enough now.
13241334
if tool == "" || version == "" {
@@ -1332,7 +1342,7 @@ func reshimCommand(logger *log.Logger, tool, version string) (err error) {
13321342

13331343
// If provided a specific version it could be something special like a path
13341344
// version so we need to generate it manually
1335-
return reshimToolVersion(conf, tool, version, os.Stdout, os.Stderr)
1345+
return reshimToolVersion(conf, plugin, version, os.Stdout, os.Stderr)
13361346
}
13371347

13381348
func shimVersionsCommand(logger *log.Logger, shimName string) error {
@@ -1504,9 +1514,10 @@ func loadPlugin(logger *log.Logger, conf config.Config, pluginName string) (plug
15041514
return plugin, err
15051515
}
15061516

1507-
func reshimToolVersion(conf config.Config, tool, versionStr string, out io.Writer, errOut io.Writer) error {
1517+
func reshimToolVersion(conf config.Config, plugin plugins.Plugin, versionStr string, out io.Writer, errOut io.Writer) error {
15081518
version := toolversions.Parse(versionStr)
1509-
return shims.GenerateForVersion(conf, plugins.New(conf, tool), version, out, errOut)
1519+
1520+
return shims.GenerateForVersion(conf, plugin, version, out, errOut)
15101521
}
15111522

15121523
func latestForPlugin(conf config.Config, toolName, pattern string, showStatus bool) error {

internal/shims/shims.go

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package shims
44
import (
55
"fmt"
66
"io"
7+
"io/fs"
78
"os"
89
"os/exec"
910
"path"
@@ -226,6 +227,11 @@ func RemoveAll(conf config.Config) error {
226227
shimDir := filepath.Join(conf.DataDir, shimDirName)
227228
entries, err := os.ReadDir(shimDir)
228229
if err != nil {
230+
if _, ok := err.(*fs.PathError); ok {
231+
// if directory doesn't exist we can just return because no shims can
232+
// possibly exist.
233+
return nil
234+
}
229235
return err
230236
}
231237

internal/shims/shims_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ func TestRemoveAll(t *testing.T) {
161161
assert.True(t, errors.Is(err, os.ErrNotExist))
162162
}
163163
})
164+
165+
t.Run("does not return error when shims directory does not exist", func(t *testing.T) {
166+
shimDir := Directory(conf)
167+
assert.Nil(t, os.RemoveAll(shimDir))
168+
assert.Nil(t, RemoveAll(conf))
169+
})
164170
}
165171

166172
func TestGenerateAll(t *testing.T) {

test/reshim_command.bats

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ teardown() {
1515
clean_asdf_dir
1616
}
1717

18+
@test "reshim should print error when plugin with name does not exist" {
19+
run asdf reshim non-existent 1.0
20+
[ "$status" -eq 1 ]
21+
[ "$output" = "No such plugin: non-existent" ]
22+
}
23+
1824
@test "reshim should allow prefixes of other versions" {
1925
run asdf install dummy 1.0.1
2026
run asdf install dummy 1.0

0 commit comments

Comments
 (0)