agent: a portable VS Code reads its User folder in its data folder

YOUIMARK's Windows has Scoop's VS Code 1.143, a portable build: magpie
showed the editor, but its Dir stayed %APPDATA%\Code\User, which doesn't
exist — code.cmd on PATH was detection enough — so the provider, models
and settings.json magpie wrote went where the build never reads.

Before: vscodeUserDir always took the OS's place for the User folder,
%APPDATA% on Windows; only detection ever looked at the editor itself,
by its launcher alone, so any portable build was found and then read
and written in a folder it doesn't use.

Now: a build whose launcher has a data folder beside it is portable, as
VS Code itself takes a build for portable by that folder alone — a zip's
own data folder, or Scoop's, whose apps\vscode\current\data is the
persisted one, current a junction into apps\vscode. Its User folder is
data\user-data\User there: found, wired, connected and disconnected
there. The launcher is found as detection finds it, and a Scoop shim
stands for the launcher the .shim beside it names, which proc.Unwrap
doesn't read. Zoo Code, which keeps its settings in VS Code's User
folder, is laid out by the same function and follows it. An installed
build's launcher has no data folder beside it, and keeps the OS's place;
so does a portable build before its first run, whose launcher alone is
still detection enough, its writes then making the folder where it
reads. VSCodium portable is left as it was: its data folder is named by
its product.json, which no report asks after.

This is the other half of the branch the Copilot-launcher fix came from
(taken along with 6f435677c, since reworked); it shares no code with
that one — this half is only where a portable build keeps its User
folder, not what magpie takes a copilot launcher for.

Run at this head (parent 841025836):
- New TestVSCodePortableUserDir: an installed build with the launcher on
  PATH keeps the OS's User folder; with the data folder beside the
  program the build's Dir is data\user-data\User, it is detected before
  its first run has made the folder, connecting writes magpie's group in
  the data folder's chatLanguageModels.json and disconnecting takes it
  out. New TestVSCodePortableScoopShim: the same through scoop's shims
  folder, the .shim naming the launcher in apps\vscode\current\bin, and
  a shim with no data folder beside the launcher it names staying an
  installed build. Both are red with vscodeUserDir taken back to the
  OS's place ("the portable build's User folder is .../Application
  Support/Code/User", "Scoop's portable VS Code reads ...") and green
  with the change.
- go test -tags nogui ./internal/agent -count=1: green but for
  TestMothX, which fails the same without this change — this machine's
  PATH has a real mothx (fnm), which the test's LookPath finds.
- gofmt -l clean; go vet ./internal/agent for darwin, linux and windows;
  go build ./... for darwin and windows, and for linux ./internal/agent
  (the repo's linux build fails in wails's own code on this machine
  without this change too).

Issue: #3260
Generated-by: GLM-5.3-Flash (ZCode)

Reviewed and run by yetone at the batch head over cac655c14 (this commit,
the four others from ggbdpq's #4495/#4548/#4512/#3260 patches and the
#4548 follow-up), on mote:
- gofmt -l clean; go vet ./... for darwin, linux (-tags nogui), windows;
  go build ./... for darwin and windows.
- go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
  internal/gui, with two filed reds: TestSettingsSaveKeepsWhatItDoesNotSend
  (#4514), red alone at origin/main cac655c14 too, and TestColdTurnsTaken
  (#4510), red only in the full run. Rebased afterwards onto e94fb18b8,
  a usage-page change that touches none of these files.
- Windows (xiaomi-gpu): go test -run 'TestVSCode|TestClaudePicker'
  ./internal/agent ok. TestCopilotFolderFromAnIDEPluginIsNotTheCLI is red
  there at cac655c14 too (#1827).
- Broken by hand: vscodeUserDir's portable branch off -> TestVSCodePortableUserDir and TestVSCodePortableScoopShim red. Checked against Scoop: extras/vscode.json puts appsscode
This commit is contained in:
ggbdpq
2026-10-31 20:54:37 +08:00
committed by yetone
parent 6585315e32
commit 509d8e7d14
2 changed files with 178 additions and 1 deletions
+52 -1
View File
@@ -129,8 +129,13 @@ func vscodeOf(k vscodeKind, home, cfg string) *Agent {
}
// vscodeUserDir is the User folder of the VS Code k, where its settings.json
// is: its extensions' (Zoo Code's) settings too.
// is: its extensions' (Zoo Code's) settings too. A portable build's is in
// its data folder (vscodePortableUserDir); the OS's place is an installed
// build's.
func vscodeUserDir(k vscodeKind, home, cfg string) string {
if dir := vscodePortableUserDir(k); dir != "" {
return dir
}
switch runtime.GOOS {
case "darwin":
cfg = filepath.Join(home, "Library", "Application Support")
@@ -143,6 +148,52 @@ func vscodeUserDir(k vscodeKind, home, cfg string) string {
return filepath.Join(cfg, k.folder, "User")
}
// vscodePortableUserDir is the User folder of a portable VS Code k: a build
// that keeps a data folder beside its program keeps its user data in
// data\user-data there, not in %APPDATA% — a zip's own data folder, and
// Scoop's, whose apps\vscode\current\data is the persisted one, current a
// junction into apps\vscode (#3260). "" for an installed build, whose
// launcher has no data folder beside it: as VS Code itself takes a build
// for portable by that folder alone.
func vscodePortableUserDir(k vscodeKind) string {
bin := binAt(k.bin)
if bin == "" {
return ""
}
if at := scoopShim(bin); at != "" {
bin = at
}
if resolved, err := filepath.EvalSymlinks(bin); err == nil {
bin = resolved
}
dir := filepath.Dir(bin)
if !strings.EqualFold(filepath.Base(dir), "bin") {
return ""
}
data := filepath.Join(filepath.Dir(dir), "data")
if !isDir(data) {
return ""
}
return filepath.Join(data, "user-data", "User")
}
// scoopShim is the launcher a Scoop shim stands for: the path = line of the
// .shim beside it (code.cmd in scoop's shims folder is a small program that
// runs it, which proc.Unwrap doesn't read). "" when bin is no shim.
func scoopShim(bin string) string {
b, err := os.ReadFile(strings.TrimSuffix(bin, filepath.Ext(bin)) + ".shim")
if err != nil {
return ""
}
for _, line := range strings.Split(string(b), "\n") {
f := strings.Fields(line)
if len(f) >= 3 && f[0] == "path" && f[1] == "=" {
return strings.Trim(strings.Join(f[2:], " "), `"`)
}
}
return ""
}
func vscodeAt(dir string) *Agent { return vscodeKindAt(vscodeStable, dir) }
func vscodeKindAt(k vscodeKind, dir string) *Agent {
+126
View File
@@ -0,0 +1,126 @@
package agent
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/edit"
)
// portableVSCode lays VS Code out under home as a portable build is: its
// launcher in bin, and, when portable, its data folder beside the program —
// a zip's own, or Scoop's, whose apps\vscode\current\data is the persisted
// one (current a junction into apps\vscode, which a folder reads through
// the same). The launcher is put on PATH; with shims, a Scoop shim of it
// is, as scoop's shims folder has.
func portableVSCode(t *testing.T, home string, portable, shims bool) (user string) {
t.Helper()
app := filepath.Join(home, "Editor With Spaces")
binDir := filepath.Join(app, "bin")
if shims {
app = filepath.Join(home, "scoop", "apps", "vscode", "current")
binDir = filepath.Join(home, "scoop", "shims")
}
name := "code"
if runtime.GOOS == "windows" {
name += ".cmd"
t.Setenv("PATHEXT", ".CMD")
}
writeAt(t, filepath.Join(app, "bin", name), "launcher\n")
if err := os.Chmod(filepath.Join(app, "bin", name), 0o755); err != nil {
t.Fatal(err)
}
if shims {
writeAt(t, filepath.Join(binDir, name), "shim\n")
if err := os.Chmod(filepath.Join(binDir, name), 0o755); err != nil {
t.Fatal(err)
}
writeAt(t, filepath.Join(binDir, "code.shim"), "path = "+filepath.Join(app, "bin", name)+"\n")
}
if portable {
if err := os.MkdirAll(filepath.Join(app, "data"), 0o755); err != nil {
t.Fatal(err)
}
user = filepath.Join(app, "data", "user-data", "User")
}
t.Setenv("PATH", binDir)
return user
}
// installedUserDir is the User folder of an installed VS Code, where the OS
// puts it.
func installedUserDir(home string) string {
switch runtime.GOOS {
case "windows":
return filepath.Join(os.Getenv("APPDATA"), "Code", "User")
case "darwin":
return filepath.Join(home, "Library", "Application Support", "Code", "User")
}
return filepath.Join(os.Getenv("XDG_CONFIG_HOME"), "Code", "User")
}
// YOUIMARK's Windows has Scoop's VS Code, a portable build: magpie showed
// the editor, but its Dir stayed %APPDATA%\Code\User, which doesn't exist —
// code.cmd on PATH was detection enough — so its provider, models and
// settings.json went where the build never reads (#3260). A portable
// build's User folder is in its data folder: data\user-data\User beside the
// program. An installed build, whose launcher has no data folder beside it,
// keeps the OS's.
func TestVSCodePortableUserDir(t *testing.T) {
home, cfg := syncHome(t), os.Getenv("XDG_CONFIG_HOME")
// the launcher is taken through filepath.EvalSymlinks, which a Mac's
// temporary folders come out of one symlink longer
if resolved, err := filepath.EvalSymlinks(home); err == nil {
home = resolved
}
portableVSCode(t, home, false, false)
if a := vscode(home, cfg); a.Dir != installedUserDir(home) {
t.Fatalf("an installed build's User folder is %s, want %s", a.Dir, installedUserDir(home))
}
user := portableVSCode(t, home, true, false)
a := vscode(home, cfg)
if a.Dir != user {
t.Fatalf("the portable build's User folder is %s, want %s", a.Dir, user)
}
// before its first run has made the folder, its launcher alone is
// detection enough — and connecting writes there, where it reads
if !a.Detected() {
t.Fatal("the portable build isn't listed")
}
if err := a.Connect(); err != nil {
t.Fatal(err)
}
lm := filepath.Join(a.Dir, "chatLanguageModels.json")
if g, ok := edit.GetJSONItem(lm, vscodeGroup); !ok || gjson.Get(g, "models.0.id").String() != "relay/glm-4.6" {
t.Fatalf("not wired in the data folder:\n%s", readFile(lm))
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if _, ok := edit.GetJSONItem(lm, vscodeGroup); ok {
t.Fatalf("disconnected, magpie's group kept:\n%s", readFile(lm))
}
}
// Scoop's code.cmd is a shim: the launcher it stands for is named by the
// .shim beside it (proc.Unwrap doesn't read those), and the data folder is
// the persisted one. A shim of a launcher with no data folder beside it is
// an installed build; its steps run in that order, as a data folder made
// stays.
func TestVSCodePortableScoopShim(t *testing.T) {
home, cfg := syncHome(t), os.Getenv("XDG_CONFIG_HOME")
if resolved, err := filepath.EvalSymlinks(home); err == nil {
home = resolved
}
portableVSCode(t, home, false, true)
if a := vscode(home, cfg); a.Dir != installedUserDir(home) {
t.Fatalf("an installed build's User folder is %s, want %s", a.Dir, installedUserDir(home))
}
user := portableVSCode(t, home, true, true)
if a := vscode(home, cfg); a.Dir != user {
t.Fatalf("Scoop's portable VS Code reads %s, want %s", a.Dir, user)
}
}