From e21f5e9efa243f7e90e05665e840829c7b6bc89f Mon Sep 17 00:00:00 2001 From: "gq.guo" Date: Fri, 27 Nov 2026 10:25:57 +0800 Subject: [PATCH] fix(plugin): re-link script plugins on update so the real version is detected `plugin update` for a script plugin reran the install script but read the version through the existing ~/.clime/plugins/clime- link, which only Install creates. If that link had drifted away from binary_path (for example a plugin's own self-upgrade renamed a new binary over the link, leaving a standalone stale copy), update kept reading the stale copy, reported "Plugin is up to date" and never refreshed the recorded version. - ScriptInstaller.Update now re-points the link at binary_path (or the binary found on PATH) after the script runs, sharing the resolution and linking code with Install. If the binary can't be located, the existing link is kept as before. - `clime init` now records binary_path in the local manifest, so update can re-link plugins whose binary name differs from the plugin name. Co-Authored-By: Claude Opus 5.5 --- cmd/init.go | 2 +- internal/installer/script.go | 80 ++++++++++++++++++++----------- internal/installer/script_test.go | 38 +++++++++++++++ 3 files changed, 90 insertions(+), 30 deletions(-) diff --git a/cmd/init.go b/cmd/init.go index 1115756..8dd8e76 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -117,7 +117,7 @@ plugin list when no URL has been recorded.`, return } - manifest.Add(pluginConfig.Name, version, pluginInstaller.PluginType(), pluginInstaller.Source(), "") + manifest.Add(pluginConfig.Name, version, pluginInstaller.PluginType(), pluginInstaller.Source(), pluginConfig.BinaryPath) if pluginConfig.Description != "" { manifest.SetDescription(pluginConfig.Name, pluginConfig.Description) } diff --git a/internal/installer/script.go b/internal/installer/script.go index 7567235..24fff05 100644 --- a/internal/installer/script.go +++ b/internal/installer/script.go @@ -39,41 +39,14 @@ func (installer *ScriptInstaller) Install(name string) (string, error) { return "", fmt.Errorf("install script failed: %w", err) } - binaryPath := installer.BinaryPath - if binaryPath != "" { - if strings.HasPrefix(binaryPath, "~/") { - home, err := os.UserHomeDir() - if err != nil { - return "", err - } - binaryPath = filepath.Join(home, binaryPath[2:]) - } - if _, err := os.Stat(binaryPath); err != nil { - return "", fmt.Errorf("binary not found at %s after install: %w", binaryPath, err) - } - } else { - // Auto-detect binary on PATH by name - found, err := installer.lookPath(name) - if err != nil { - return "", fmt.Errorf("binary %q not found on PATH after install; use --binary-path to specify its location", name) - } - binaryPath = found - } - - installDir, err := installer.pluginBinDir() + binaryPath, err := installer.resolveBinary(name) if err != nil { return "", err } - if err := os.MkdirAll(installDir, 0755); err != nil { + if err := installer.link(name, binaryPath); err != nil { return "", err } - linkPath := filepath.Join(installDir, plugin.BinPrefix+name) - os.Remove(linkPath) - if err := os.Symlink(binaryPath, linkPath); err != nil { - return "", fmt.Errorf("failed to create symlink: %w", err) - } - version := installer.DetectVersion(name) return version, nil } @@ -83,6 +56,15 @@ func (installer *ScriptInstaller) Update(name string, current plugin.ManifestEnt return nil, fmt.Errorf("failed to update plugin from script source %q: %w", installer.ScriptURL, err) } + // Re-point the managed link at the binary the script just installed. Without this, a link that + // drifted (e.g. replaced by a plugin's self-upgrade) keeps reporting the old version forever. + // If the binary can't be located, keep the existing link as before. + if binaryPath, err := installer.resolveBinary(name); err == nil { + if err := installer.link(name, binaryPath); err != nil { + return nil, err + } + } + version := installer.DetectVersion(name) installDir, err := installer.pluginBinDir() @@ -110,6 +92,46 @@ func (installer *ScriptInstaller) Uninstall(name string, entry plugin.ManifestEn return removePluginBinaryAndTarget(name) } +// resolveBinary returns the binary the install script produced: BinaryPath if set, else name on PATH. +func (installer *ScriptInstaller) resolveBinary(name string) (string, error) { + binaryPath := installer.BinaryPath + if binaryPath == "" { + found, err := installer.lookPath(name) + if err != nil { + return "", fmt.Errorf("binary %q not found on PATH after install; use --binary-path to specify its location", name) + } + return found, nil + } + if strings.HasPrefix(binaryPath, "~/") { + home, err := os.UserHomeDir() + if err != nil { + return "", err + } + binaryPath = filepath.Join(home, binaryPath[2:]) + } + if _, err := os.Stat(binaryPath); err != nil { + return "", fmt.Errorf("binary not found at %s after install: %w", binaryPath, err) + } + return binaryPath, nil +} + +// link (re)creates the managed clime- symlink pointing at binaryPath. +func (installer *ScriptInstaller) link(name, binaryPath string) error { + installDir, err := installer.pluginBinDir() + if err != nil { + return err + } + if err := os.MkdirAll(installDir, 0755); err != nil { + return err + } + linkPath := filepath.Join(installDir, plugin.BinPrefix+name) + os.Remove(linkPath) + if err := os.Symlink(binaryPath, linkPath); err != nil { + return fmt.Errorf("failed to create symlink: %w", err) + } + return nil +} + func (installer *ScriptInstaller) DetectVersion(name string) string { binPath, ok := installer.findPlugin(name) if !ok { diff --git a/internal/installer/script_test.go b/internal/installer/script_test.go index 3778018..89277c1 100644 --- a/internal/installer/script_test.go +++ b/internal/installer/script_test.go @@ -94,6 +94,9 @@ func TestScriptInstallerUpdateFallsBackToLatest(t *testing.T) { runVersion: func(binPath string) (string, error) { return "", nil }, + lookPath: func(name string) (string, error) { + return "", fmt.Errorf("not found") + }, } entry := plugin.ManifestEntry{ @@ -107,6 +110,41 @@ func TestScriptInstallerUpdateFallsBackToLatest(t *testing.T) { require.Equal(t, plugin.VersionLatest, result.LatestVersion) } +// A managed link that no longer points at binary_path (e.g. a plugin's self-upgrade renamed a new +// binary over it) must be re-pointed on update, or the stale copy's version is reported as current. +func TestScriptInstallerUpdateRelinksDriftedSymlink(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + pluginsDir, err := plugin.PluginBinDir() + require.NoError(t, err) + require.NoError(t, os.MkdirAll(pluginsDir, 0o755)) + + writeCLI := func(path, version string) { + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte("#!/bin/sh\necho "+version+"\n"), 0o755)) + } + stale := filepath.Join(os.Getenv("HOME"), "old", "tool") + writeCLI(stale, "v1.0.0") + linkPath := filepath.Join(pluginsDir, plugin.BinPrefix+"tool") + require.NoError(t, os.Symlink(stale, linkPath)) + + installed := filepath.Join(os.Getenv("HOME"), ".local", "bin", "tool") + s := NewScriptInstaller("https://example.com/install.sh", "~/.local/bin/tool") + s.runScript = func(string) error { writeCLI(installed, "v2.0.0"); return nil } + + result, err := s.Update("tool", plugin.ManifestEntry{ + Name: "tool", + Version: "1.0.0", + Type: plugin.SourceTypeScript, + Source: "https://example.com/install.sh", + }) + require.NoError(t, err) + require.True(t, result.Updated) + require.Equal(t, "2.0.0", result.LatestVersion) + target, err := os.Readlink(linkPath) + require.NoError(t, err) + require.Equal(t, installed, target) +} + func TestScriptInstallerInstallAutoDetectBinary(t *testing.T) { t.Parallel()