Skip to content

Commit 8aa38a4

Browse files
committed
fix(client): preserve complete command previews
1 parent 3f82f76 commit 8aa38a4

3 files changed

Lines changed: 61 additions & 10 deletions

File tree

‎packages/client-runtime/src/work-log/commandLabel.test.ts‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ describe("ignored command prefixes", () => {
1919
[".\\rtk git status", "git", "git status"],
2020
["CI=1 env -u DEBUG sudo -u root rtk git status", "git", "git status"],
2121
["/bin/zsh -lc 'rtk git status'", "git", "git status"],
22-
["command rtk git status", "git", "'git' 'status'"],
23-
["cd repo && rtk git status", "git", "git status"],
2422
["rtk git status && git diff", "git", "git status && git diff"],
2523
['& "C:\\Tools\\rtk" git status', "git", "git status"],
2624
["rtk \\\n git status", "git", "git status"],
@@ -29,6 +27,32 @@ describe("ignored command prefixes", () => {
2927
expect(commandDisplayText(command, prefixes)).toBe(preview);
3028
});
3129

30+
it.each([
31+
"command rtk git status",
32+
"cd repo && rtk git status",
33+
"sh -c 'rtk git status' && npm test",
34+
"bash -c 'rtk git status' && npm test",
35+
"bash -c 'rtk git status'; npm test",
36+
"bash -c 'rtk git status' && git status",
37+
"sh -c 'rtk git status' git status",
38+
"timeout 10 rtk git status; npm test",
39+
])("preserves shell boundaries and other commands in %s", (command) => {
40+
expect(commandProgramName(command, prefixes)).toBe("git");
41+
expect(commandDisplayText(command, prefixes)).toBe(command);
42+
});
43+
44+
it.each([
45+
["rtk git status; npm test", "git status; npm test"],
46+
["rtk git status ; npm test", "git status ; npm test"],
47+
["rtk git status&&npm test", "git status&&npm test"],
48+
["rtk git status\nnpm test", "git status\nnpm test"],
49+
["wrapper rtk git status; npm test", "git status; npm test"],
50+
["rtk sh -c 'rtk git status' && npm test", "sh -c 'rtk git status' && npm test"],
51+
["rtk cat <<EOF\nhello\nEOF\nnpm test", "cat <<EOF\nhello\nEOF\nnpm test"],
52+
])("retains the original command suffix when removing prefixes in %s", (command, preview) => {
53+
expect(commandDisplayText(command, prefixes)).toBe(preview);
54+
});
55+
3256
it.each([
3357
"rtk",
3458
"rtk --version",

‎packages/client-runtime/src/work-log/commandLabel.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,6 +1108,7 @@ function parseCommandProgramName(
11081108
context: CommandProgramContext,
11091109
segmentsRemaining: number,
11101110
labelContext?: CommandLabelContext,
1111+
allowDisplayRewrite = false,
11111112
): string | null {
11121113
if (depth >= 8 || segmentsRemaining <= 0) return null;
11131114
const commandWithoutComments = commandWithoutLeadingShellComments(command);
@@ -1235,18 +1236,29 @@ function parseCommandProgramName(
12351236
const targetOffset = tokenOffsets?.[targetIndex];
12361237
if (target && !target.startsWith("-") && targetOffset !== undefined) {
12371238
const remainder = firstCommand.slice(targetOffset);
1238-
const displayCommand = commandSplit.remainingCommand
1239-
? `${remainder} ${commandSplit.separator} ${commandSplit.remainingCommand}`
1240-
: remainder;
1239+
const source = command.trim();
1240+
const sourceFirstCommand = commandSplit.firstCommand.trim();
1241+
const sourceOffset =
1242+
source.startsWith(sourceFirstCommand) && sourceFirstCommand.endsWith(remainder)
1243+
? sourceFirstCommand.length - remainder.length
1244+
: null;
1245+
const displayCommand =
1246+
sourceOffset !== null
1247+
? source.slice(sourceOffset)
1248+
: commandSplit.remainingCommand
1249+
? `${remainder} ${commandSplit.separator} ${commandSplit.remainingCommand}`
1250+
: remainder;
1251+
const rewriteDisplay = allowDisplayRewrite && sourceOffset !== null;
12411252
const program = commandProgramNameInternal(
12421253
displayCommand,
12431254
depth + 1,
12441255
"exec",
12451256
segmentsRemaining,
12461257
labelContext,
1258+
rewriteDisplay,
12471259
);
12481260
if (program !== null && !labelContext.unresolvedPrefix) {
1249-
labelContext.displayCommand ??= displayCommand;
1261+
if (rewriteDisplay) labelContext.displayCommand ??= displayCommand;
12501262
return program;
12511263
}
12521264
}
@@ -1444,6 +1456,7 @@ function commandProgramNameInternal(
14441456
context: CommandProgramContext,
14451457
segmentsRemaining = MAX_COMMAND_SEGMENTS,
14461458
labelContext?: CommandLabelContext,
1459+
allowDisplayRewrite = false,
14471460
): string | null {
14481461
if (segmentsRemaining <= 0) return null;
14491462
const calledProgram = powerShellCallOperatorProgramName(command);
@@ -1456,14 +1469,21 @@ function commandProgramNameInternal(
14561469
"exec",
14571470
segmentsRemaining,
14581471
labelContext,
1472+
allowDisplayRewrite,
14591473
) ?? calledProgram
14601474
);
14611475
}
14621476
return calledProgram;
14631477
}
14641478
return (
1465-
parseCommandProgramName(command, depth, context, segmentsRemaining, labelContext) ??
1466-
(context === "shell" ? literalCommandAliasProgramName(command) : null)
1479+
parseCommandProgramName(
1480+
command,
1481+
depth,
1482+
context,
1483+
segmentsRemaining,
1484+
labelContext,
1485+
allowDisplayRewrite,
1486+
) ?? (context === "shell" ? literalCommandAliasProgramName(command) : null)
14671487
);
14681488
}
14691489

@@ -1515,7 +1535,8 @@ export function commandDisplayText(
15151535
const display = withoutShellWrapper(command);
15161536
if (ignoredCommandPrefixes.length === 0) return withVisibleControlCharacters(display);
15171537
const context = commandLabelContext(ignoredCommandPrefixes);
1518-
commandProgramNameInternal(display, 0, "shell", MAX_COMMAND_SEGMENTS, context);
1538+
// Nested shell payloads and reconstructed tokens cannot replace the whole preview.
1539+
commandProgramNameInternal(display, 0, "shell", MAX_COMMAND_SEGMENTS, context, true);
15191540
return withVisibleControlCharacters(context.displayCommand ?? display);
15201541
}
15211542

‎packages/contracts/src/settings.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ describe("ignored command prefix settings", () => {
3333
}
3434
});
3535

36-
it.each([[42], [""], [" "], "rtk", null])("rejects invalid lists: %j", (prefixes) => {
36+
it.each([
37+
{ prefixes: [42] },
38+
{ prefixes: [""] },
39+
{ prefixes: [" "] },
40+
{ prefixes: "rtk" },
41+
{ prefixes: null },
42+
])("rejects invalid lists: $prefixes", ({ prefixes }) => {
3743
expect(() => decodeClientSettings({ ignoredCommandPrefixes: prefixes })).toThrow();
3844
});
3945
});

0 commit comments

Comments
 (0)