fix(agentctl): extend the inherited PATH key when prepending the GitHub CLI shim (#2415)

This commit is contained in:
João Manso
2026-08-08 14:44:54 +01:00
committed by GitHub
parent f8acc2a7f5
commit 247c0591c2
2 changed files with 93 additions and 5 deletions
@@ -570,7 +570,7 @@ func CollectAgentEnvWithError(additional map[string]string) ([]string, error) {
return nil, fmt.Errorf("compose indexed Git config: %w", err)
}
if envMap[githubauth.CredentialBrokerURLEnv] != "" {
prependPathEntry(envMap, envMap[githubauth.CredentialCLIShimDirEnv])
prependPathEntry(envMap, envMap[githubauth.CredentialCLIShimDirEnv], runtime.GOOS == "windows")
configureGitHubCLIStartupEnv(envMap)
}
@@ -659,12 +659,13 @@ func isBashEnvNameByte(value byte, first bool) bool {
return letter || value == '_' || !first && value >= '0' && value <= '9'
}
func prependPathEntry(env map[string]string, entry string) {
func prependPathEntry(env map[string]string, entry string, caseInsensitive bool) {
if entry == "" {
return
}
key := searchPathKey(env, caseInsensitive)
cleanEntry := filepath.Clean(entry)
parts := filepath.SplitList(env["PATH"])
parts := filepath.SplitList(env[key])
filtered := make([]string, 0, len(parts)+1)
filtered = append(filtered, entry)
for _, part := range parts {
@@ -672,7 +673,35 @@ func prependPathEntry(env map[string]string, entry string) {
filtered = append(filtered, part)
}
}
env["PATH"] = strings.Join(filtered, string(os.PathListSeparator))
env[key] = strings.Join(filtered, string(os.PathListSeparator))
}
// searchPathKey returns the key env already carries the executable search path
// under. Windows inherits it as "Path" and matches variable names
// case-insensitively, so writing "PATH" leaves the inherited entry in place and
// adds a second variable holding only the shim directory. The agent is launched
// through `cmd /c`, which then resolves against that variable alone and dies
// with "'npx' is not recognized" — the shim dir is all it can see.
//
// Unix variable names are case-sensitive, so a "Path" there is a genuinely
// different variable and must not be mistaken for the search path; the fold is
// gated on caseInsensitive, matching how environmentValue in
// internal/tools/installer scopes the same lookup to Windows. The flag is a
// parameter rather than a runtime.GOOS read inside so the Windows branch stays
// testable on every runner. An exact "PATH" always wins, so a caller-supplied
// key still takes precedence over an inherited case variant.
func searchPathKey(env map[string]string, caseInsensitive bool) string {
if _, ok := env["PATH"]; ok {
return "PATH"
}
if caseInsensitive {
for key := range env {
if strings.EqualFold(key, "PATH") {
return key
}
}
}
return "PATH"
}
func envBool(env []string, key string) bool {
@@ -24,13 +24,72 @@ func TestNewInstanceConfigNormalizesMcpProviders(t *testing.T) {
func TestCollectAgentEnvKeepsGitHubCLIShimAheadOfProfilePath(t *testing.T) {
t.Setenv("KANDEV_GITHUB_CREDENTIAL_BROKER_URL", "https://kandev.example/api/github/credentials/resolve")
t.Setenv("KANDEV_GITHUB_CLI_SHIM_DIR", "/kandev/shims")
env := CollectAgentEnv(map[string]string{"PATH": "/profile/bin:/usr/bin"})
// The profile path is joined with the platform separator rather than a
// literal ":" so SplitList sees two entries on Windows too — hardcoding the
// Unix separator made this fail there with the shim ahead of one unsplit entry.
profilePath := strings.Join([]string{"/profile/bin", "/usr/bin"}, string(os.PathListSeparator))
env := CollectAgentEnv(map[string]string{"PATH": profilePath})
want := strings.Join([]string{"/kandev/shims", "/profile/bin", "/usr/bin"}, string(os.PathListSeparator))
if got := envSliceValue(env, "PATH"); got != want {
t.Fatalf("PATH = %q, want %q", got, want)
}
}
// Windows hands the search path down as "Path". Writing "PATH" used to leave
// that untouched and add a second variable holding only the shim directory, so
// `cmd /c npx …` resolved against the shim dir alone and the agent died with
// "'npx' is not recognized". Both branches are exercised through the parameter
// rather than runtime.GOOS so the regression is caught on every runner.
func TestSearchPathKey(t *testing.T) {
inherited := map[string]string{"Path": "/node/bin"}
if got := searchPathKey(inherited, true); got != "Path" {
t.Fatalf("searchPathKey(case-insensitive) = %q, want the inherited %q", got, "Path")
}
if got := searchPathKey(inherited, false); got != "PATH" {
t.Fatalf("searchPathKey(case-sensitive) = %q, want %q — a Unix \"Path\" is a different variable", got, "PATH")
}
both := map[string]string{"PATH": "/exact", "Path": "/variant"}
if got := searchPathKey(both, true); got != "PATH" {
t.Fatalf("searchPathKey with both keys = %q, want the exact match to win", got)
}
}
func TestPrependPathEntryExtendsTheKeyItFound(t *testing.T) {
env := map[string]string{
"PATH": strings.Join([]string{"/node/bin", "/usr/bin"}, string(os.PathListSeparator)),
}
prependPathEntry(env, "/kandev/shims", false)
want := strings.Join([]string{"/kandev/shims", "/node/bin", "/usr/bin"}, string(os.PathListSeparator))
if env["PATH"] != want {
t.Fatalf("PATH = %q, want %q", env["PATH"], want)
}
if len(env) != 1 {
t.Fatalf("prependPathEntry left %d variables, want the one it extended: %v", len(env), env)
}
}
// The regression itself: an environment carrying only the Windows-style "Path"
// must have that entry extended, not shadowed by a freshly created "PATH"
// holding the shim directory alone.
func TestPrependPathEntryExtendsInheritedWindowsPathKey(t *testing.T) {
env := map[string]string{
"Path": strings.Join([]string{"/node/bin", "/usr/bin"}, string(os.PathListSeparator)),
}
prependPathEntry(env, "/kandev/shims", true)
if _, duplicated := env["PATH"]; duplicated {
t.Fatalf("prependPathEntry added a second search-path variable: %v", env)
}
want := strings.Join([]string{"/kandev/shims", "/node/bin", "/usr/bin"}, string(os.PathListSeparator))
if env["Path"] != want {
t.Fatalf("Path = %q, want %q", env["Path"], want)
}
}
func TestCollectAgentEnvGitHubCLIShimSurvivesLoginShell(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Bash login-shell behavior is Unix-specific")