Skip to content

Commit ed83759

Browse files
shrey150claude
andcommitted
fix(cli): trim the id stored by contexts add
cubic: the empty-check trimmed the id but the raw value was saved, so a whitespace-padded id would be stored and later resolved as a bogus context id. Persist the normalized (trimmed) id and echo it back. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent b944a95 commit ed83759

2 files changed

Lines changed: 12 additions & 4 deletions

File tree

packages/cli/src/commands/cloud/contexts/add.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ export default class ContextsAdd extends BrowseCommand {
3838
if (!isValidContextName(args.name)) {
3939
fail(`Invalid context name "${args.name}". ${contextNameRequirement()}`);
4040
}
41-
if (args.id.trim().length === 0) {
41+
// Normalize before storing so whitespace-padded input isn't saved and later
42+
// resolved as a bogus context id.
43+
const id = args.id.trim();
44+
if (id.length === 0) {
4245
fail("Context ID cannot be empty.");
4346
}
4447
if (!flags.force && (await getContextAlias(args.name))) {
@@ -49,9 +52,9 @@ export default class ContextsAdd extends BrowseCommand {
4952
}
5053

5154
await saveContextAlias(args.name, {
52-
id: args.id,
55+
id,
5356
createdAt: new Date().toISOString(),
5457
});
55-
outputJson({ name: args.name, id: args.id });
58+
outputJson({ name: args.name, id });
5659
}
5760
}

packages/cli/tests/contexts-named.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,16 @@ describe("named contexts (end to end through the CLI)", () => {
213213
expect(dup.exitCode).not.toBe(0);
214214
expect(dup.stderr).toContain("already exists locally");
215215

216+
// Padded id + --force: repoints and stores the trimmed id.
216217
const forced = await runCli(
217-
["cloud", "contexts", "add", "team-login", newId, "--force"],
218+
["cloud", "contexts", "add", "team-login", ` ${newId} `, "--force"],
218219
{ env },
219220
);
220221
expect(forced.exitCode).toBe(0);
222+
expect(JSON.parse(forced.stdout)).toEqual({
223+
name: "team-login",
224+
id: newId,
225+
});
221226
expect(
222227
JSON.parse(await readFile(storePath, "utf8")).contexts,
223228
).toMatchObject({ "team-login": { id: newId } });

0 commit comments

Comments
 (0)