diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 822188c2a..ec2bfe923 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -339,6 +339,18 @@ type Agent struct { // that is dirShared: a file only the agent (or its own app) writes // there (the Qoder app's status file, #2138). sign func() bool + // binNot, when set, says whether a binary found for Bin is not this + // agent's: another program's launcher of the same name (VS Code's + // copilot launcher, #3269), which detection and the CLI's lookup are + // not to take for the agent. + binNot func(bin string) bool +} + +// binsOwn says whether bin, as binAt or proc.FindTool found it, is the +// agent's own command: binNot, when set, turns away what another program +// puts there under the same name. +func (a *Agent) binsOwn(bin string) bool { + return bin != "" && (a.binNot == nil || !a.binNot(bin)) } // Running reports whether a process whose command line matches any pattern @@ -402,7 +414,7 @@ func (a *Agent) Detected() bool { if a.sign != nil && a.sign() { return true } - if a.Bin != "" && binAt(a.Bin) != "" { + if a.Bin != "" && a.binsOwn(binAt(a.Bin)) { return true } // one with no Bin, as its command's name is other tools' too, that diff --git a/internal/agent/agents.go b/internal/agent/agents.go index 9aa79e64d..9dae5c302 100644 --- a/internal/agent/agents.go +++ b/internal/agent/agents.go @@ -1574,6 +1574,11 @@ func copilot(home string) *Agent { // folder says nothing of the CLI: its settings.json or the copilot // command does (#1495) dirShared: true, + // VS Code 1.143 ships a copilot launcher of its own in + // bin\copilot-shim in its install folder, which is on the user's + // PATH, and running it only asks for the CLI to be installed + // (#3269): a command of the editor's is not the CLI + binNot: editorLauncher, Fields: []Field{{ Key: "model", Label: "model", Get: jsonGet(path, "model"), @@ -1608,6 +1613,40 @@ func copilot(home string) *Agent { } } +// editorLauncher reports whether bin is a launcher an editor ships beside +// itself, not the CLI it is named after (#3269): an editor's install is +// told by the product.json it keeps beside its program, as +// vscodeCodeBinary tells one. VS Code 1.143 puts its copilot launcher in +// \bin\copilot-shim, with \resources\app\product.json +// in a Windows or Linux install; a Mac's app keeps bin in +// Contents/Resources/app, beside product.json. So only bin's own folder +// and the two above it are looked in, never further up, where a folder of +// the user's (a home, a checkout of an editor's source) may hold a +// product.json of its own. A link on PATH is judged where it points. +// A copilot a user installed itself, in npm's global folder, Homebrew's, +// winget's Links or scoop's shims, is still the CLI. +func editorLauncher(bin string) bool { + if real, err := filepath.EvalSymlinks(bin); err == nil { + bin = real + } + dir, err := filepath.Abs(filepath.Dir(bin)) + if err != nil { + return false + } + for range 3 { + if isFile(filepath.Join(dir, "product.json")) || + isFile(filepath.Join(dir, "resources", "app", "product.json")) { + return true + } + up := filepath.Dir(dir) + if up == dir { + break + } + dir = up + } + return false +} + func crush(home, cfg string) *Agent { path := filepath.Join(cfg, "crush", "crush.json") // Crush saves the models its own picker chooses to its data file, and diff --git a/internal/agent/cliupdate.go b/internal/agent/cliupdate.go index 98bb070fe..74f3685bc 100644 --- a/internal/agent/cliupdate.go +++ b/internal/agent/cliupdate.go @@ -805,11 +805,14 @@ func (a *Agent) cliBin() (string, cliSpec) { return "", spec } bin := binAt(a.Bin) - if bin == "" { - bin = proc.FindTool(a.Bin) - } - if bin == "" { - return "", spec + if !a.binsOwn(bin) { + // a launcher of another program's is not the CLI: where else a + // user's tools are installed, then (VS Code's copilot shim, #3269) + if at := proc.FindTool(a.Bin); a.binsOwn(at) { + bin = at + } else { + return "", spec + } } if abs, err := filepath.Abs(bin); err == nil { bin = abs @@ -828,10 +831,13 @@ func (a *Agent) BinPath() string { bin, _ := a.cliBin() return bin } - if bin := binAt(a.Bin); bin != "" { + if bin := binAt(a.Bin); a.binsOwn(bin) { return bin } - return proc.FindTool(a.Bin) + if bin := proc.FindTool(a.Bin); a.binsOwn(bin) { + return bin + } + return "" } // CLI is the agent's CLI: its version, the newest, and whether magpie can diff --git a/internal/agent/copilot_shim_test.go b/internal/agent/copilot_shim_test.go new file mode 100644 index 000000000..1344173d0 --- /dev/null +++ b/internal/agent/copilot_shim_test.go @@ -0,0 +1,96 @@ +package agent + +import ( + "os" + "path/filepath" + "runtime" + "testing" +) + +// Tony-ooo's Windows has VS Code 1.143, which ships a copilot launcher of +// its own in bin\copilot-shim in its install folder and puts that on the +// user's PATH (#3269). The launcher runs only to ask for the CLI to be +// installed; it is not the CLI, and ~/.copilot doesn't exist. A copilot a +// user installed itself still is. +func TestCopilotLauncherFromAnEditorIsNotTheCLI(t *testing.T) { + home := t.TempDir() + name := "copilot" + if runtime.GOOS == "windows" { + name += ".cmd" + t.Setenv("PATHEXT", ".CMD") + } + put := func(dir string) { + t.Helper() + writeAt(t, filepath.Join(dir, name), "vscode-copilot-launcher\n") + if err := os.Chmod(filepath.Join(dir, name), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", dir) + } + // the shim in the install's bin, of the layout VS Code writes + install := filepath.Join(t.TempDir(), "Microsoft VS Code") + writeAt(t, filepath.Join(install, "resources", "app", "product.json"), `{"nameShort":"Visual Studio Code"}`) + put(filepath.Join(install, "bin", "copilot-shim")) + if copilot(home).Detected() { + t.Error("VS Code's copilot launcher is taken for the Copilot CLI") + } + if bin, _ := copilot(home).cliBin(); bin != "" { + t.Errorf("the CLI looked for is %s, the launcher", bin) + } + if bin := copilot(home).BinPath(); bin != "" { + t.Errorf("the agent's program is %s, the launcher", bin) + } + // a Mac's editor keeps its launcher inside the app proper, whose + // product.json is a level above bin + app := filepath.Join(t.TempDir(), "Visual Studio Code.app", "Contents", "Resources", "app") + writeAt(t, filepath.Join(app, "product.json"), `{"applicationName":"code"}`) + put(filepath.Join(app, "bin", "copilot-shim")) + if copilot(home).Detected() { + t.Error("the editor app's copilot launcher is taken for the Copilot CLI") + } + // a launcher in the editor's bin, no shim folder of its own + put(filepath.Join(install, "bin")) + if copilot(home).Detected() { + t.Error("a copilot launcher in the editor's bin is taken for the Copilot CLI") + } + // the CLI itself, where a user's installs put it, still is one + cli := t.TempDir() + writeAt(t, filepath.Join(cli, name), "") + if err := os.Chmod(filepath.Join(cli, name), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", cli) + if !copilot(home).Detected() { + t.Error("the copilot command on PATH no longer finds the Copilot CLI") + } + if bin, _ := copilot(home).cliBin(); bin == "" { + t.Error("the copilot command on PATH no longer stands for the CLI") + } + // a product.json further up than an editor keeps one, as a folder of + // the user's may hold (a checkout of VS Code's own source), says + // nothing of the CLI: npm's global bin three folders below it + far := t.TempDir() + writeAt(t, filepath.Join(far, "product.json"), `{"nameShort":"Code - OSS"}`) + npm := filepath.Join(far, "tools", "npm-global", "lib", "bin") + writeAt(t, filepath.Join(npm, name), "") + if err := os.Chmod(filepath.Join(npm, name), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", npm) + if !copilot(home).Detected() { + t.Error("a product.json far above the CLI's folder makes the CLI an editor's launcher") + } + if runtime.GOOS == "windows" { + return + } + // a link on PATH to the editor's launcher (a Linux package's + // /usr/bin link) is the launcher it points to + links := t.TempDir() + if err := os.Symlink(filepath.Join(install, "bin", "copilot-shim", name), filepath.Join(links, name)); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", links) + if copilot(home).Detected() { + t.Error("a link to the editor's launcher is taken for the Copilot CLI") + } +} diff --git a/internal/agent/gone.go b/internal/agent/gone.go index 5aa369a73..c0d148c56 100644 --- a/internal/agent/gone.go +++ b/internal/agent/gone.go @@ -32,7 +32,7 @@ var goneOS = runtime.GOOS // folder as the agent does, so the folder is no leftover; known is false // when magpie can't tell, which never counts as gone. func (a *Agent) cliHeld() (held, known bool) { - if a.Bin != "" && (binAt(a.Bin) != "" || proc.FindTool(a.Bin) != "") { + if a.Bin != "" && (a.binsOwn(binAt(a.Bin)) || a.binsOwn(proc.FindTool(a.Bin))) { if a.ID != "goose" { return true, true }