Skip to content

Commit 137b35f

Browse files
fix: Be aware of ForcePrepend
Previously, the value of `ForcePrepend` was not used anywhere and was not behaving as specified. With this commit, the behavior will be as specified. Referenced Documents: - https://github.com/asdf-vm/asdf/blob/932ac468b7c24c2adef90a293a1f7280a0074cc4/docs/manage/configuration.md#asdf_force_prepend Background: @andrecloutier told about this issue me in a comment on another PullRequest. Thank you! - #2011 (comment)
1 parent 932ac46 commit 137b35f

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

Diff for: internal/cli/cli.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ func envCommand(logger *log.Logger, shimmedCommand string, args []string) error
524524
"ASDF_INSTALL_TYPE": parsedVersion.Type,
525525
"ASDF_INSTALL_VERSION": parsedVersion.Value,
526526
"ASDF_INSTALL_PATH": installs.InstallPath(conf, plugin, parsedVersion),
527-
"PATH": setPath(execPaths),
527+
"PATH": setPath(conf, execPaths),
528528
}
529529

530530
if parsedVersion.Type != "system" {
@@ -547,8 +547,13 @@ func envCommand(logger *log.Logger, shimmedCommand string, args []string) error
547547
return err
548548
}
549549

550-
func setPath(paths []string) string {
551-
return strings.Join(paths, ":") + ":" + os.Getenv("PATH")
550+
func setPath(conf config.Config, paths []string) string {
551+
currentPaths := os.Getenv("PATH")
552+
553+
if conf.ForcePrepend {
554+
return strings.Join(append(paths, currentPaths), ":")
555+
}
556+
return strings.Join(append([]string{currentPaths}, paths...), ":")
552557
}
553558

554559
func execCommand(logger *log.Logger, command string, args []string) error {
@@ -583,7 +588,7 @@ func execCommand(logger *log.Logger, command string, args []string) error {
583588
"ASDF_INSTALL_TYPE": parsedVersion.Type,
584589
"ASDF_INSTALL_VERSION": parsedVersion.Value,
585590
"ASDF_INSTALL_PATH": installs.InstallPath(conf, plugin, parsedVersion),
586-
"PATH": setPath(execPaths),
591+
"PATH": setPath(conf, execPaths),
587592
}
588593

589594
if parsedVersion.Type != "system" {

Diff for: internal/cli/cli_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cli
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
8+
"github.com/asdf-vm/asdf/internal/config"
9+
)
10+
11+
func TestSetPath(t *testing.T) {
12+
originalPaths := "/dummy/bin:/example/bin"
13+
t.Setenv("PATH", originalPaths)
14+
15+
t.Run("When ForcePrepend turned on", func(t *testing.T) {
16+
paths := setPath(
17+
config.Config{ForcePrepend: true},
18+
[]string{"/awesome/bin", "/cool/bin"},
19+
)
20+
21+
assert.Equal(t, paths, "/awesome/bin:/cool/bin:"+originalPaths, "Then prepend path directories to the front-most part of PATH env")
22+
})
23+
24+
t.Run("When ForcePrepend turned off", func(t *testing.T) {
25+
paths := setPath(
26+
config.Config{ForcePrepend: false},
27+
[]string{"/awesome/bin", "/cool/bin"},
28+
)
29+
30+
assert.Equal(t, paths, originalPaths+":/awesome/bin:/cool/bin", "Then append path directories to the hinder-most part of PATH env")
31+
})
32+
}

0 commit comments

Comments
 (0)