Skip to content

Commit 9be27b3

Browse files
authored
fix(cli): quote the typed workflow in the apply mismatch hint and tidy recovery text (#10633)
- shell-quote the workflow named in the SOURCE_WORKFLOW_MISMATCH get command, so a name with a space stays one argument - drop the doubled period after invalid-lock problems - find prints 'read by: unknown' instead of 'none' when the source does not compile - drop stale operation counts from deriveCommandPath TSDoc; rename build.ts commandPath to commandLabel
1 parent 141cd6d commit 9be27b3

5 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎packages/sim-cli/src/commands/protocol/workflow-dir.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function readBase(
126126
const { base } = parsed
127127
if (workflowId !== undefined && base.workflowId !== workflowId) {
128128
throw new SourceDirError(
129-
`${directory} holds the source of workflow ${base.workflowId}, not ${workflowId}. Apply it to ${base.workflowId}, or write this workflow's source with \`sim workflows source get ${workflowId} --dir <dir>\`.`,
129+
`${directory} holds the source of workflow ${base.workflowId}, not ${workflowId}. Apply it to ${base.workflowId}, or write this workflow's source with \`sim workflows source get ${shellWord(workflowId)} --dir <dir>\`.`,
130130
{ code: 'SOURCE_WORKFLOW_MISMATCH' }
131131
)
132132
}
@@ -153,7 +153,7 @@ export async function readLock(directory: string): Promise<WorkflowLockV1> {
153153
const parsed = parseWorkflowLock(await readJson(directory, SOURCE_LOCK_FILE, GET_HINT))
154154
if (parsed.lock) return parsed.lock
155155
throw new SourceDirError(
156-
`${SOURCE_LOCK_FILE} in ${directory} is not a valid lock: ${parsed.diagnostics.map(lockProblem).join('; ')}. ${lockRecoveryHint()}`
156+
`${SOURCE_LOCK_FILE} in ${directory} is not a valid lock: ${parsed.diagnostics.map(lockProblem).join('; ')} ${lockRecoveryHint()}`
157157
)
158158
}
159159

‎packages/sim-cli/src/commands/protocol/workflow-source-find.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ function printNumbered(lines: readonly string[], first: number, indent: string):
306306
lines.forEach((text, at) => printLine(`${indent}${String(first + at).padStart(width)} ${text}`))
307307
}
308308

309-
function printFound(found: FoundBlock): void {
309+
function printFound(found: FoundBlock, compiles: boolean): void {
310310
const kind = found.type ? `${found.type}, ` : ''
311311
printLine(`${found.name} (${kind}key ${found.key}, ${found.ident})`)
312312
if (found.declaration !== '') printNumbered(found.declaration.split('\n'), found.line, '')
@@ -319,6 +319,10 @@ function printFound(found: FoundBlock): void {
319319
const edgeRow = (edge: FoundEdge) => `${edge.file}:${edge.line} ${edge.statement}`
320320
section('in', found.incoming.map(edgeRow))
321321
section('out', found.outgoing.map(edgeRow))
322+
if (!compiles) {
323+
printLine('read by: unknown (the source does not compile)')
324+
return
325+
}
322326
section(
323327
'read by',
324328
found.readers.map(
@@ -379,7 +383,7 @@ export function attachWorkflowSourceFind(source: Command): void {
379383
}
380384
found.forEach((block, position) => {
381385
if (position > 0) printLine('')
382-
printFound(block)
386+
printFound(block, compiled.workflow !== null)
383387
})
384388
if (more > 0) printLine(`\n${more} more blocks match; name one exactly to print it.`)
385389
}

‎packages/sim-cli/src/commands/protocol/workflow-source-roundtrip.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { execFileSync } from 'node:child_process'
12
import {
23
existsSync,
34
mkdirSync,
@@ -325,6 +326,25 @@ describe('sim workflows source get --dir → apply --dir', () => {
325326
expect(JSON.parse(read('.sim/base.json'))).toEqual(baseFor(NEW_WORKFLOW_ID, SECOND))
326327
await expect(apply()).rejects.toThrow(`holds the source of workflow ${NEW_WORKFLOW_ID}`)
327328
})
329+
it('names the typed workflow as one shell word in the recovery command for a mismatched apply', async () => {
330+
await get()
331+
332+
const error = await sim(
333+
'workflows',
334+
'source',
335+
'apply',
336+
'Lead Router',
337+
'--dir',
338+
dir,
339+
'--yes'
340+
).catch((caught: unknown) => caught)
341+
342+
expect(error).toBeInstanceOf(Error)
343+
const typed = /`sim workflows source get (.+) --dir <dir>`/.exec((error as Error).message)?.[1]
344+
expect(typed).toBeDefined()
345+
const words = execFileSync('sh', ['-c', `printf '%s\\n' ${typed}`], { encoding: 'utf8' })
346+
expect(words.split('\n').filter(Boolean)).toEqual(['Lead Router'])
347+
})
328348
})
329349

330350
describe('the source directory never reaches outside --dir', () => {

‎packages/sim-cli/src/runtime/build.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function argumentSyntax(command: Command): string {
6363
.join(' ')
6464
}
6565

66-
function commandPath(command: Command): string {
66+
function commandLabel(command: Command): string {
6767
const names: string[] = []
6868
let current: Command | null = command
6969
while (current) {
@@ -134,16 +134,16 @@ function longFlagSyntax(option: Option): string {
134134

135135
/** The command line with its positionals and every required flag, as `--help` would read it. */
136136
function usageExample(command: Command): string {
137-
const parts = [commandPath(command), argumentSyntax(command)]
137+
const parts = [commandLabel(command), argumentSyntax(command)]
138138
for (const option of command.options) if (option.mandatory) parts.push(longFlagSyntax(option))
139139
return parts.filter(Boolean).join(' ')
140140
}
141141

142142
function optionList(command: Command): string {
143143
const flags = command.options.filter((option) => !option.hidden).map((option) => option.flags)
144144
return flags.length > 0
145-
? `Options for ${commandPath(command)}: ${flags.join(', ')}`
146-
: `${commandPath(command)} takes no options.`
145+
? `Options for ${commandLabel(command)}: ${flags.join(', ')}`
146+
: `${commandLabel(command)} takes no options.`
147147
}
148148

149149
/** What each guided command's own option parse left unknown, recorded by {@link addParseErrorGuidance}. */
@@ -181,7 +181,7 @@ function unknownBesideMissing(command: Command): string | undefined {
181181
const typed = token.split('=', 1)[0]
182182
const missing = missingMandatory(command).flatMap((option) => (option.long ? [option.long] : []))
183183
const suggestion = didYouMean(typed, missing)
184-
return `${typed} is not an option of ${commandPath(command)}.${suggestion ? ` (${suggestion})` : ''}`
184+
return `${typed} is not an option of ${commandLabel(command)}.${suggestion ? ` (${suggestion})` : ''}`
185185
}
186186

187187
/**
@@ -308,7 +308,7 @@ function siblingsDeclaring(command: Command, flag: string): string[] {
308308
.filter((candidate) =>
309309
candidate.options.some((option) => option.long === flag || option.short === flag)
310310
)
311-
.map((owner) => ` ${commandPath(owner)} — ${owner.description().split('\n', 1)[0]}`)
311+
.map((owner) => ` ${commandLabel(owner)} — ${owner.description().split('\n', 1)[0]}`)
312312
}
313313

314314
/**
@@ -358,7 +358,7 @@ function parseErrorGuidance(command: Command, code: string | undefined): string[
358358
const token = unknownOptionToken(command)
359359
if (!token) return []
360360
if (looksLikeAnId(token) && command.registeredArguments.length > 0) {
361-
return [`Example: ${commandPath(command)} -- ${token}`]
361+
return [`Example: ${commandLabel(command)} -- ${token}`]
362362
}
363363
const flag = token.split('=', 1)[0]
364364
if (flag === '--json') return [optionList(command), 'For JSON output, pass --output json.']

‎packages/sim-cli/src/runtime/derive.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const ACTION_SEGMENTS = new Set([
2626
* Derives a command path from an operation's route.
2727
*
2828
* `<resource> [sub-resource] <verb>`, where the verb comes from the method and
29-
* whether the path ends in a parameter (an item) or not (a collection). This
30-
* covers 41 of the 47 operations; the rest are named in the CLI contract.
29+
* whether the path ends in a parameter (an item) or not (a collection).
30+
* Covers most operations; the contract names the rest (`command`).
3131
*/
3232
export function deriveCommandPath(operation: V2OperationName): string[] {
3333
const spec = V2_OPERATIONS[operation]

0 commit comments

Comments
 (0)