diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 860567c..29e1e38 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -821,7 +821,16 @@ function clientStoragePaths(platform, home, env) { const file = (root, name) => join(root, name, "User", "globalStorage", "storage.json") if (platform === "darwin") return [file(join(home, "Library", "Application Support"), "Trae CN")] if (platform === "win32") { - const roots = [env.APPDATA, join(home, "AppData", "Roaming")].filter((r, i, all) => r && all.indexOf(r) === i) + // the two roots are often the same folder: APPDATA and the home's own + // AppData\Roaming. They are compared as one system spells them, so a + // difference of separator or case alone doesn't read the same file twice + const spell = (r) => String(r ?? "").replace(/[\\/]+/g, "\\").replace(/\\+$/, "").toLowerCase() + const roots = [] + for (const r of [env.APPDATA, join(home, "AppData", "Roaming")]) { + if (!r) continue + if (roots.some((was) => spell(was) === spell(r))) continue + roots.push(r) + } return roots.flatMap((r) => ["Trae CN", "trae-cn"].map((n) => file(r, n))) } if (platform === "linux") return ["trae-cn", "Trae CN"].map((n) => file(env.XDG_CONFIG_HOME || join(home, ".config"), n)) diff --git a/packages/trae/ownclient.test.mjs b/packages/trae/ownclient.test.mjs index a1168c4..a25c08b 100644 --- a/packages/trae/ownclient.test.mjs +++ b/packages/trae/ownclient.test.mjs @@ -6,7 +6,7 @@ import { afterEach, beforeEach, expect, test } from "bun:test" import { createCipheriv, createHash, randomBytes } from "node:crypto" import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" -import { join } from "node:path" +import { join, sep } from "node:path" import { TraeCNAuthPlugin, _internal } from "./index.mjs" import { fakeTrae, json, signedIn } from "./fake.mjs" @@ -142,16 +142,35 @@ test("the client's codec decodes what it encodes, and refuses another header", ( test("where each OS keeps the client's storage.json", () => { const p = _internal.clientStoragePaths - expect(p("darwin", "/Users/a", {})).toEqual(["/Users/a/Library/Application Support/Trae CN/User/globalStorage/storage.json"]) - const w = p("win32", "/h", { APPDATA: "/h/Roam" }) - expect(w).toEqual([ + // the paths are joined with this system's separator: the composition is + // what is checked, so they are read as one system spells them + const same = (ps) => ps.map((x) => x.replaceAll(sep, "/")) + expect(same(p("darwin", "/Users/a", {}))).toEqual(["/Users/a/Library/Application Support/Trae CN/User/globalStorage/storage.json"]) + expect(same(p("win32", "/h", { APPDATA: "/h/Roam" }))).toEqual([ "/h/Roam/Trae CN/User/globalStorage/storage.json", "/h/Roam/trae-cn/User/globalStorage/storage.json", "/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json", "/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json", ]) - expect(p("linux", "/home/a", {})[0]).toBe("/home/a/.config/trae-cn/User/globalStorage/storage.json") - expect(p("linux", "/home/a", { XDG_CONFIG_HOME: "/x" })[0]).toBe("/x/trae-cn/User/globalStorage/storage.json") + expect(same(p("linux", "/home/a", {}))[0]).toBe("/home/a/.config/trae-cn/User/globalStorage/storage.json") + expect(same(p("linux", "/home/a", { XDG_CONFIG_HOME: "/x" }))[0]).toBe("/x/trae-cn/User/globalStorage/storage.json") + // a root named twice is asked once: APPDATA the same as the home's own + expect(same(p("win32", "/h", { APPDATA: "/h/AppData/Roaming" }))).toEqual([ + "/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json", + "/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json", + ]) + // and a different APPDATA is a root of its own, beside the home's + expect(same(p("win32", "/h", { APPDATA: "/other/Roam" }))).toEqual([ + "/other/Roam/Trae CN/User/globalStorage/storage.json", + "/other/Roam/trae-cn/User/globalStorage/storage.json", + "/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json", + "/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json", + ]) + // no APPDATA at all: the home's own alone + expect(same(p("win32", "/h", {}))).toEqual([ + "/h/AppData/Roaming/Trae CN/User/globalStorage/storage.json", + "/h/AppData/Roaming/trae-cn/User/globalStorage/storage.json", + ]) }) test("this machine's OS as the client names it", () => {