From 8d1a4b60e17057c99169d26b331938a8e0e3c2ed Mon Sep 17 00:00:00 2001 From: Sahilb315 Date: Mon, 13 Jul 2026 15:37:02 +0530 Subject: [PATCH] fix: clarify system-install doctor alias and shim path checks Use UserBinDir for PATH checks and pass aliases as not required under system install without treating that as active interception. Co-authored-by: Cursor --- cmd/setup/doctor.go | 20 +++++++++----------- cmd/setup/doctor_system_test.go | 13 +++++++++++-- internal/shim/shim.go | 24 +++++++++++++++++++----- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/cmd/setup/doctor.go b/cmd/setup/doctor.go index 42e94a0..3dc1845 100644 --- a/cmd/setup/doctor.go +++ b/cmd/setup/doctor.go @@ -30,6 +30,8 @@ const ( checkProtectionNpm = "protection-npm" checkProtectionPip = "protection-pip" checkCA = "ca-cert" + + aliasesInstalledMessage = "Shell aliases installed" ) func NewDoctorCommand() *cobra.Command { @@ -100,7 +102,7 @@ func runCoreChecks(cfg *config.RuntimeConfig) []doctor.CheckResult { if err != nil { return doctor.CheckResult{ Status: doctor.StatusWarn, - Message: "Event log directory not found (PMG still runs without event logs)", + Message: "Event log directory not found", } } if !info.IsDir() { @@ -138,13 +140,13 @@ func runCoreChecks(cfg *config.RuntimeConfig) []doctor.CheckResult { if installed { return doctor.CheckResult{ Status: doctor.StatusPass, - Message: "Shell aliases installed", + Message: aliasesInstalledMessage, } } if shim.SystemShimsInstalled() { return doctor.CheckResult{ - Status: doctor.StatusWarn, - Message: "Aliases not installed (optional with system shims)", + Status: doctor.StatusPass, + Message: "No aliases (system install)", } } return doctor.CheckResult{ @@ -196,11 +198,7 @@ func runCoreChecks(cfg *config.RuntimeConfig) []doctor.CheckResult { Message: "System shim directory is in PATH", } } - userDir := "" - if home, err := os.UserHomeDir(); err == nil { - userDir = filepath.Join(home, ".pmg", "bin") - } - if pathContainsDir(pathEntries, userDir) { + if userDir, err := shim.UserBinDir(); err == nil && pathContainsDir(pathEntries, userDir) { return doctor.CheckResult{ Status: doctor.StatusPass, Message: "Shim directory is in PATH", @@ -346,10 +344,10 @@ func runProtectionChecks(coreResults []doctor.CheckResult) []doctor.CheckResult func isInterceptionActive(coreResults []doctor.CheckResult) bool { for _, r := range coreResults { - if r.Name == checkShellAliases && r.Status == doctor.StatusPass { + if r.Name == checkShimInPath && r.Status == doctor.StatusPass { return true } - if r.Name == checkShimInPath && r.Status == doctor.StatusPass { + if r.Name == checkShellAliases && r.Status == doctor.StatusPass && r.Message == aliasesInstalledMessage { return true } } diff --git a/cmd/setup/doctor_system_test.go b/cmd/setup/doctor_system_test.go index b7b5eeb..5522bf2 100644 --- a/cmd/setup/doctor_system_test.go +++ b/cmd/setup/doctor_system_test.go @@ -13,11 +13,20 @@ func TestPathContainsDir(t *testing.T) { assert.False(t, pathContainsDir([]string{"/usr/bin"}, "")) } -func TestSystemShimsWithoutPathDoNotActivateInterception(t *testing.T) { +func TestSystemInstallAliasesPassDoesNotActivateInterception(t *testing.T) { results := []doctor.CheckResult{ - {Name: checkShellAliases, Status: doctor.StatusWarn}, + {Name: checkShellAliases, Status: doctor.StatusPass, Message: "No aliases (system install)"}, {Name: checkShimInPath, Status: doctor.StatusFail}, } assert.False(t, isInterceptionActive(results)) } + +func TestAliasesInstalledActivatesInterception(t *testing.T) { + results := []doctor.CheckResult{ + {Name: checkShellAliases, Status: doctor.StatusPass, Message: aliasesInstalledMessage}, + {Name: checkShimInPath, Status: doctor.StatusFail}, + } + + assert.True(t, isInterceptionActive(results)) +} diff --git a/internal/shim/shim.go b/internal/shim/shim.go index b724bfe..f8bcaf3 100644 --- a/internal/shim/shim.go +++ b/internal/shim/shim.go @@ -48,8 +48,13 @@ func NewDefaultShimManager() (*ShimManager, error) { return nil, err } + binDir, err := UserBinDir() + if err != nil { + return nil, err + } + return &ShimManager{config: ShimConfig{ - BinDir: filepath.Join(homeDir, ".pmg", "bin"), + BinDir: binDir, HomeDir: homeDir, PMGBin: pmgBin, PackageManagers: aliasCfg.PackageManagers, @@ -140,6 +145,15 @@ func (m *ShimManager) GetBinDir() string { return m.config.BinDir } +// UserBinDir returns the per-user PMG shim directory (~/.pmg/bin). +func UserBinDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", fmt.Errorf("failed to get home directory: %w", err) + } + return filepath.Join(homeDir, ".pmg", "bin"), nil +} + func (m *ShimManager) writeShimScript(pm string) error { shimPath := filepath.Join(m.config.BinDir, pm) pmgBin := shellQuote(m.config.PMGBin) @@ -236,12 +250,12 @@ func (m *ShimManager) removePathFromShells() error { return nil } -// UserShimsInstalled reports whether the per-user shim directory (~/.pmg/bin) -// contains at least one shim script. +// UserShimsInstalled reports whether the per-user shim directory contains at +// least one shim script. func UserShimsInstalled() bool { - homeDir, err := os.UserHomeDir() + binDir, err := UserBinDir() if err != nil { return false } - return shimsPresent(filepath.Join(homeDir, ".pmg", "bin")) + return shimsPresent(binDir) }