agent: VS Code's bundled copilot launcher is not the Copilot CLI (#3269)

Tony-ooo's Windows (magpie 0.1.1320): VS Code 1.143 ships a copilot
launcher of its own in <install>\bin\copilot-shim and puts it on the
user's PATH, while the CLI was never installed and ~\.copilot doesn't
exist. magpie ls and magpie agents still said Copilot CLI was installed,
and it showed up as a Library target; renaming the shim folder away made
the row go.

Detected's last resort took any command of the agent's Bin name that
binAt found, and cliBin (what CLI magpie offers to update) took the same
launcher. An Agent may now turn away a command found for its Bin
(binNot, binsOwn). Copilot CLI turns away one an editor keeps, told by
the product.json beside the editor's program (editorLauncher, as
vscodeCodeBinary tells an install). Ported from ggbdpq's patch on the
issue.

Changed from the patch:
- editorLauncher resolves a symlink first and looks in bin's own folder
  and the two above it only. The patch walked up to the root, so a
  product.json in any folder above (a home, a checkout of an editor's
  source) turned away a real CLI under it.
- BinPath and cliHeld (gone.go) go through binsOwn too. BinPath returned
  the shim, and cliHeld counted it as the CLI holding ~/.copilot.
- The windows-tagged test is dropped: it wrote into a folder of the real
  registry PATH and passed without the fix.

Run at the pushed head (parent 3e5aa1151):
- mote: gofmt -l clean; go vet ./internal/agent for darwin, linux and
  windows; go test ./internal/agent -count=1 -timeout 45m: ok (371.2s)
- Windows box (win-run, Go 1.26.8): TestCopilotLauncherFromAnEditorIsNotTheCLI
  passes (the symlink row is skipped there), run at this change on
  a808c31a7. The whole internal/agent package there has the same 14
  reds with and without this change: TestCopilotFolderFromAnIDEPluginIsNotTheCLI and the others
  are in #1827, #1388 and #4115 (TestT3CodeCodexInstance added there).

Broken by hand:
- the patch's unbounded walk without EvalSymlinks: the "product.json
  four levels up" and "a link to the shim" rows go red.
- binNot: nil: 6 rows go red (detected, cliBin, BinPath for the shim in
  each layout).

Not tried: VS Code 1.143 itself on the Windows box, or the reporter's
magpie ls. If the shim comes first on PATH and a real CLI later, Detected
still says not installed; cliBin falls back to proc.FindTool, Detected
doesn't.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-31 10:07:24 +08:00
co-authored by ggbdpq
parent 3e5aa11519
commit 6f435677c3
5 changed files with 162 additions and 9 deletions
+13 -1
View File
@@ -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
+39
View File
@@ -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
// <install>\bin\copilot-shim, with <install>\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
+13 -7
View File
@@ -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
+96
View File
@@ -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")
}
}
+1 -1
View File
@@ -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
}