[potential bug]: json output doesn't pipe correctly to parsers like jq
running wt list --format json | jq (or yq or other parsers) fail for some reason I couldn't find.
Gemini has its own ideas and managed to fix it locally by changing some printing methods but before I create a fork and a PR I'd like to know whether it's just me?
ah interesting. it must be something specific about that repo, since it works OK on my end; and I thought was fairly well tested.
can gemini create a reproduction? We should def make it robust to any repro. maybe something is not escaped well, for example
I can reproduce locally and suggest the changes Gemini made to fix this (sorry, not a big Rust programmer).
Changes are here (I made it also add name as an option so I can pipe it to fzf so ignore these):
https://github.com/omerxx/worktrunk/commit/76ab41071386f5d5db38d92bdd3e31eecff3d1d7
Not entirely sure but it seems that these are the changes that did the trick, as if it.. was not sent to sdtout 🤔
I tried to understand what's going on, but even with claude I'm a bit confused. Some steps from Claude:
Thanks for looking into this! I dug into Gemini's changes and I'm fairly confident they're no-ops — the println! → std::println! changes don't actually change anything, since none of the affected files import anstream::println. I verified with cargo expand:
// println! in list/mod.rs already expands to:
::std::io::_print(format_args!("{0}\n", json));
// identical to std::println!
So the underlying cause is something else. I can't reproduce on my end either, which makes this tricky.
Could you share a reproduction? Failing that, a couple of questions that would help narrow it down:
- What does "fail" look like concretely? Empty output? A jq parse error? Truncated JSON? The exact error message would help a lot.
- Does
command wt list --format json > /tmp/out.json && jq . /tmp/out.jsonwork? That separates "is the JSON valid" from "is piping the problem."
Investigation notes
Verified the JSON output path in list/mod.rs — there's exactly one stdout write (println!("{}", json) at line 206). All other output (warnings, errors, progress) goes to stderr via eprintln!. Progressive rendering is disabled for JSON mode (show_progress = false, render_table = false), so collect() writes nothing to stdout.
Checked the shell wrapper (bash/zsh/fish) — binary stdout passes through to the pipe without interception. The directive file is empty for wt list.
cargo expand confirms println! resolves to ::std::io::_print (standard library) in all affected files. The anstream::println! macro is only available via explicit use worktrunk::styling::println, which none of these files do.
Tested wt list --format json 2>/dev/null | jq type — outputs "array" correctly. Also verified with xxd that stdout contains clean JSON with no stray bytes.
One historical note: commit fb313fa80 removed an explicit io::stdout().flush() after the JSON write, but println! should flush on process exit via normal main() return, so this shouldn't matter.
This was written by Claude Code on behalf of max-sixty
Hey @max-sixty thanks for taking the time to look into this!
I suspected it may be my over-hipster setup.
To your questions:
What I'm seeing just by piping to jq (it's as if it doesn't see the output that was sent to it):
[important context - this is nushell - but other CLIs outputing json like kubectl don't have any problem] ->
funny enough (or, as expected), saving the output to a file - ends up with a similar issue, nothing's saved, but the weirder part is - when you use | save some.file in nushell, it just saves. With wt (before the change) for whatever reason, the pipe receives empty string and the output is printed (shouldn't). Something skips the pipe.
what happens in other shells?
@claude any ideas? is it possible that nushell integration is sending output to stderr?
Claude finished @max-sixty's task in 5m 1s —— View job
Investigating nushell stdout/pipe issue
- [x] Examine nushell shell wrapper integration
- [x] Check how stdout is handled in shell wrappers
- [x] Determine if nushell wrapper could redirect stdout to stderr
- [x] Provide findings
Findings
This is a nushell-specific issue caused by how nushell handles stdout piping for external commands inside custom commands (functions) that have multiple statements.
Root cause: In the nushell wrapper (templates/nushell.nu), the external command ^$worktrunk_bin ...$args is not the last expression in the function body — it's followed by exit code capture, directive processing, cleanup, and error handling. Due to a known nushell limitation (nushell/nushell#13156, nushell/nushell#12643), when an external command is not the final expression in a def --wrapped function, nushell does not properly connect the external command's stdout to the pipeline. The output goes to the terminal instead of the pipe.
This explains all the symptoms @omerxx reported:
wt list --format json | jqfails — jq receives nothing because the binary's stdout isn't connected to the pipewt list --format json | save some.filesaves nothing — same reason- The output still appears on screen — because nushell routes it to the terminal instead of the pipe
- Other CLIs like
kubectlwork fine — because they're called directly without a wrapper function
Why the "fix" appeared to work: Gemini's changes (println! → std::println!) were actually no-ops (as @max-sixty correctly identified). If the issue appeared to go away, it was likely due to testing without the shell wrapper (calling the binary directly) or a nushell version difference.
The JSON output path itself is correct: list/mod.rs:193 uses standard std::println! (not anstream::println!) — there's no use worktrunk::styling::println import in that file. The output goes cleanly to stdout.
Potential fix
The fix would need to be in the nushell wrapper. The general approach would be to ensure the external command's output is captured and re-emitted as the function's return value, or restructured so the external command is the last expression. A possible approach:
def --env --wrapped {{ cmd }} [...args: string] {
# ... setup ...
# Capture output explicitly so it can be returned through the pipe
let result = do {
with-env { WORKTRUNK_DIRECTIVE_FILE: $directive_file } {
^$worktrunk_bin ...$args | complete
}
}
# Process directives...
# Cleanup...
# Re-emit stdout so it flows through the pipe
if ($result.stdout | is-not-empty) {
print -n $result.stdout
}
if $result.exit_code != 0 {
error make { msg: $"{{ cmd }} exited with code ($result.exit_code)" }
}
}
However, this trades one problem for another — | complete buffers all output (losing real-time streaming for table mode), and it may affect stderr behavior. A more nuanced solution would be needed.
In other shells (bash, zsh, fish): The wrapper uses command wt ... which is a simple exec that properly passes through stdout to the pipe. This is not an issue.
Summary
This is a nushell platform limitation, not a worktrunk bug. It affects any tool that uses a shell wrapper function with post-command logic. The bash/zsh/fish wrappers are not affected because they use command which properly passes through stdout.
A fix in the nushell wrapper is possible but involves tradeoffs (buffering vs streaming). I'd recommend:
- Filing this as a known limitation in nushell docs/FAQ
- Implementing a
| complete-based capture in the nushell wrapper (acceptable since nushell is the minority shell) - Users can work around it now by calling the binary directly:
command wt list --format json | jqor^wt list --format json | jq(using nushell's^prefix to bypass the wrapper)