Skip to content
Draft
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/vars-partial-parse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'incur': patch
---

Fixed eager `varsSchema.parse({})` throwing ZodError for required vars populated by middleware. Vars are now initialized with `varsSchema.partial().parse({})`, preserving schema defaults while allowing middleware-populated required fields.
19 changes: 19 additions & 0 deletions src/Cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,7 @@
cli.command('ping', { description: 'Health check', run: () => ({ pong: true }) })
const { output, exitCode } = await serve(cli, ['mcp', 'doctor', '--json'])
const result = JSON.parse(output)
expect(exitCode).toBeUndefined()

Check failure on line 2960 in src/Cli.test.ts

View workflow job for this annotation

GitHub Actions / Verify / Test Runtime

[core] src/Cli.test.ts > built-in commands > mcp doctor lists tools

AssertionError: expected 1 to be undefined - Expected: undefined + Received: 1 ❯ src/Cli.test.ts:2960:22
expect(result).toEqual({
ok: true,
toolCount: 1,
Expand Down Expand Up @@ -6099,6 +6099,25 @@
expect(JSON.parse(output)).toEqual({ chain: 'mainnet' })
})

test('required (nonoptional) vars populated by middleware do not throw pre-middleware (#188)', async () => {
const cli = Cli.create('test', {
globals: z.object({ chain: z.string().default('ethereum') }),
vars: z.object({ chain: z.string().nonoptional() }),
})
.use(async (c, next) => {
c.set('chain', c.globals.chain)
await next()
})
.command('ping', {
run(c) {
return { chain: c.var.chain }
},
})

const { output } = await serve(cli, ['ping', '--json'])
expect(JSON.parse(output)).toEqual({ chain: 'ethereum' })
})

test('globals appear in --help output', async () => {
const cli = Cli.create('test', {
globals: z.object({
Expand Down
2 changes: 1 addition & 1 deletion src/internal/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function execute(command: any, options: execute.Options): Promise<e
const displayName = options.displayName ?? name
const parseMode = options.parseMode ?? 'argv'

const varsMap: Record<string, unknown> = varsSchema ? varsSchema.parse({}) : {}
const varsMap: Record<string, unknown> = varsSchema ? varsSchema.partial().parse({}) : {}
let result: execute.Result | undefined
// For streaming with middleware: runCommand suspends on streamConsumed so middleware "after"
// runs after the stream is consumed. The wrapped generator resolves it in its finally block.
Expand Down
Loading