Skip to content

Commit b017038

Browse files
committed
fix: treat empty env vars as unset in vscodeUserDir/getMcpUrl + test isolation
- vscodeUserDir(): use || instead of ?? so empty APPDATA/XDG_CONFIG_HOME fall back to the homedir-based default instead of producing a relative path - getMcpUrl(): trim and check truthiness so CLERK_MCP_URL="" doesn't win - clients.test.ts, user-scope.test.ts: clear XDG_CONFIG_HOME/APPDATA in beforeEach and restore in afterEach to prevent VS Code path leaking to the runner's real config directory (fixes CI test failure)
1 parent d80abcd commit b017038

4 files changed

Lines changed: 56 additions & 3 deletions

File tree

packages/cli-core/src/commands/mcp/clients/clients.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,28 @@ const cases = [
6363
];
6464

6565
describe("client config paths + encoded shapes (homedir redirected)", () => {
66+
let origXdgConfigHome: string | undefined;
67+
let origAppData: string | undefined;
68+
6669
beforeEach(async () => {
6770
mockHome = await mkdtemp(join(realOs.tmpdir(), "clerk-mcp-clients-"));
71+
origXdgConfigHome = process.env.XDG_CONFIG_HOME;
72+
origAppData = process.env.APPDATA;
73+
process.env.XDG_CONFIG_HOME = "";
74+
process.env.APPDATA = "";
6875
});
6976

7077
afterEach(async () => {
78+
if (origXdgConfigHome === undefined) {
79+
delete process.env.XDG_CONFIG_HOME;
80+
} else {
81+
process.env.XDG_CONFIG_HOME = origXdgConfigHome;
82+
}
83+
if (origAppData === undefined) {
84+
delete process.env.APPDATA;
85+
} else {
86+
process.env.APPDATA = origAppData;
87+
}
7188
await rm(mockHome, { recursive: true, force: true });
7289
});
7390

packages/cli-core/src/commands/mcp/clients/paths.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export function userPath(...segments: string[]): string {
2525
*/
2626
export function vscodeUserDir(): string {
2727
const home = homedir();
28+
const appData = process.env.APPDATA?.trim();
29+
const xdgConfigHome = process.env.XDG_CONFIG_HOME?.trim();
2830
switch (platform()) {
2931
case "win32":
30-
return join(process.env.APPDATA ?? join(home, "AppData", "Roaming"), "Code", "User");
32+
return join(appData || join(home, "AppData", "Roaming"), "Code", "User");
3133
case "darwin":
3234
return join(home, "Library", "Application Support", "Code", "User");
3335
default:
34-
return join(process.env.XDG_CONFIG_HOME ?? join(home, ".config"), "Code", "User");
36+
return join(xdgConfigHome || join(home, ".config"), "Code", "User");
3537
}
3638
}
3739

packages/cli-core/src/commands/mcp/clients/user-scope.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,33 @@ describe("user-scope MCP clients (homedir redirected to a tmpdir)", () => {
9999
describe("install/uninstall across all clients (homedir + cwd redirected)", () => {
100100
let cwd: string;
101101
let originalCwd: string;
102+
let origXdgConfigHome: string | undefined;
103+
let origAppData: string | undefined;
102104

103105
beforeEach(async () => {
104106
originalCwd = process.cwd();
105107
cwd = await mkdtemp(join(realOs.tmpdir(), "clerk-mcp-all-cwd-"));
106108
mockHome = await mkdtemp(join(realOs.tmpdir(), "clerk-mcp-all-home-"));
109+
origXdgConfigHome = process.env.XDG_CONFIG_HOME;
110+
origAppData = process.env.APPDATA;
111+
process.env.XDG_CONFIG_HOME = "";
112+
process.env.APPDATA = "";
107113
process.chdir(cwd);
108114
mockIsAgent.mockReturnValue(true);
109115
});
110116

111117
afterEach(async () => {
112118
process.chdir(originalCwd);
119+
if (origXdgConfigHome === undefined) {
120+
delete process.env.XDG_CONFIG_HOME;
121+
} else {
122+
process.env.XDG_CONFIG_HOME = origXdgConfigHome;
123+
}
124+
if (origAppData === undefined) {
125+
delete process.env.APPDATA;
126+
} else {
127+
process.env.APPDATA = origAppData;
128+
}
113129
await rm(cwd, { recursive: true, force: true });
114130
await rm(mockHome, { recursive: true, force: true });
115131
mockIsAgent.mockReset();
@@ -152,6 +168,8 @@ describe("install/uninstall across all clients (homedir + cwd redirected)", () =
152168
describe("clerk doctor — checkMcp (homedir + cwd redirected)", () => {
153169
let cwd: string;
154170
let originalCwd: string;
171+
let origXdgConfigHome: string | undefined;
172+
let origAppData: string | undefined;
155173
const originalFetch = globalThis.fetch;
156174

157175
// Assign globalThis.fetch directly (cast to its own type) rather than via the
@@ -167,12 +185,26 @@ describe("clerk doctor — checkMcp (homedir + cwd redirected)", () => {
167185
originalCwd = process.cwd();
168186
cwd = await mkdtemp(join(realOs.tmpdir(), "clerk-mcp-check-cwd-"));
169187
mockHome = await mkdtemp(join(realOs.tmpdir(), "clerk-mcp-check-home-"));
188+
origXdgConfigHome = process.env.XDG_CONFIG_HOME;
189+
origAppData = process.env.APPDATA;
190+
process.env.XDG_CONFIG_HOME = "";
191+
process.env.APPDATA = "";
170192
process.chdir(cwd);
171193
mockIsAgent.mockReturnValue(true);
172194
});
173195

174196
afterEach(async () => {
175197
process.chdir(originalCwd);
198+
if (origXdgConfigHome === undefined) {
199+
delete process.env.XDG_CONFIG_HOME;
200+
} else {
201+
process.env.XDG_CONFIG_HOME = origXdgConfigHome;
202+
}
203+
if (origAppData === undefined) {
204+
delete process.env.APPDATA;
205+
} else {
206+
process.env.APPDATA = origAppData;
207+
}
176208
await rm(cwd, { recursive: true, force: true });
177209
await rm(mockHome, { recursive: true, force: true });
178210
globalThis.fetch = originalFetch;

packages/cli-core/src/lib/environment.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,5 +161,7 @@ export function getDashboardUrl(): string {
161161
* profile omits `mcpUrl`, mirroring `getDashboardUrl`.
162162
*/
163163
export function getMcpUrl(): string {
164-
return process.env.CLERK_MCP_URL ?? getCurrentEnv().mcpUrl ?? DEFAULT_MCP_URL;
164+
const envUrl = process.env.CLERK_MCP_URL?.trim();
165+
const profileUrl = getCurrentEnv().mcpUrl?.trim();
166+
return envUrl || profileUrl || DEFAULT_MCP_URL;
165167
}

0 commit comments

Comments
 (0)