Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/did-you-mean-root-fallback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'incur': patch
---

Fixed root fetch/command fallback bypassing "Did you mean?" suggestions when the input is a typo of a known command.
9 changes: 9 additions & 0 deletions src/Cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3732,6 +3732,15 @@ describe('fetch', async () => {
`)
})

test('root-level fetch with typo of known command → did you mean', async () => {
const cli = Cli.create('api', { description: 'API', fetch: app.fetch }).command('upgrade', {
run: () => ({ upgraded: true }),
})
const { output, exitCode } = await serve(cli, ['upgra'])
expect(exitCode).toBe(1)
expect(output).toContain("Did you mean 'upgrade'?")
})

test('root-level fetch with no args → root path', async () => {
const cli = Cli.create('api', { description: 'API', fetch: app.fetch })
// Hono returns 404 for / since we don't have a root route
Expand Down
15 changes: 12 additions & 3 deletions src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1079,9 +1079,18 @@ async function serveImpl(
const resolvedFormat = 'command' in resolved && (resolved as any).command.format
const format = formatExplicit ? formatFlag : resolvedFormat || options.format || 'toon'

// Fall back to root fetch when no subcommand matches
// Fall back to root fetch/command when no subcommand matches,
// but only if the token doesn't look like a typo of a known command.
const rootFallbackBlocked =
'error' in resolved &&
!resolved.path &&
(() => {
const candidates = [...resolved.commands.keys()]
for (const b of builtinCommands) candidates.push(b.name)
return suggest(resolved.error, candidates) !== undefined
})()
const effective =
'error' in resolved && options.rootFetch && !resolved.path
'error' in resolved && options.rootFetch && !resolved.path && !rootFallbackBlocked
? {
fetchGateway: {
_fetch: true as const,
Expand All @@ -1092,7 +1101,7 @@ async function serveImpl(
path: name,
rest: filtered,
}
: 'error' in resolved && options.rootCommand && !resolved.path
: 'error' in resolved && options.rootCommand && !resolved.path && !rootFallbackBlocked
? { command: options.rootCommand, path: name, rest: filtered }
: resolved

Expand Down
Loading