feat(cli): keep an editor's output off stdout when config profile edit prints JSON

The editor shared the command's stdout, so anything it or its wrapper printed landed in front of the result document. With structured output its stdout now goes to stderr. The README no longer lists env among the text-only commands: it prints a map in JSON and YAML.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura committed 2026-10-11 10:18:14 -05:00
1 parent a559f7716d
commit 89d341ff92
3 files changed
+58 -3

No files matched your search

+3 -2
View File
@@ -314,8 +314,9 @@ All commands support `--help`.
Every tree takes the same flags; only the defaults differ. With `--json` (or `-o json`, `-o yaml`)
a command prints one document on stdout and nothing else; when it fails, the error is on stderr
and stdout is empty. Three commands print text whatever the format, because their output is not a
document: `logs` (the container's own output), `env` (shell code) and `completion` (a script). Marks are plain ASCII without
and stdout is empty. Two commands print text whatever the format, because their output is not a
document: `logs` (the container's own output) and `completion` (a script). `env` prints shell
code in text mode and the variables as a map in JSON or YAML. Marks are plain ASCII without
`--no-emoji` too whenever the output is not a terminal (a pipe, a file, CI logs) or `NO_COLOR` is
set. `--json` wins when it is combined with `--output`.
@@ -225,7 +225,7 @@ public class ConfigProfileCommand implements Callable<Integer> {
command.add(file.toString());
int exit;
try {
exit = new ProcessBuilder(command).inheritIO().start().waitFor();
exit = editorProcess(command, printer.format() != OutputFormat.text).start().waitFor();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
printer.error("Interrupted while " + editor.get(0) + " was open.\nRe-run '" + prefix + "edit " + name + "'.");
@@ -263,6 +263,24 @@ public class ConfigProfileCommand implements Callable<Integer> {
return 0;
}
/**
* How the editor is started. It shares this command's terminal, so an interactive editor
* works. With structured output its stdout goes to stderr instead: stdout carries the one
* result document, and anything an editor or its wrapper prints there would break it.
*/
public static ProcessBuilder editorProcess(List<String> command, boolean structured) {
ProcessBuilder editor = new ProcessBuilder(command).inheritIO();
if (structured) {
// /dev/stderr is this process's own stderr, still the terminal when only stdout is
// captured. Where there is no such file (Windows), the editor's output is dropped.
Path stderr = Path.of("/dev/stderr");
editor.redirectOutput(Files.isWritable(stderr)
? ProcessBuilder.Redirect.appendTo(stderr.toFile())
: ProcessBuilder.Redirect.DISCARD);
}
return editor;
}
/**
* The editor to run, as a command and its arguments: {@code VISUAL} first, then
* {@code EDITOR}, as other tools do. Empty when neither is set.
@@ -176,6 +176,42 @@ class ErgonomicsTest {
assertFalse(r.out().contains("saved ("), r.out());
}
/**
* With structured output the editor's stdout must not be this command's stdout, or whatever
* the editor prints lands in front of the result. A real child process shows it: its stdout is
* captured in a file here, as a shell redirect would.
*/
@Test
@DisabledOnOs(OS.WINDOWS)
void anEditorsOutputStaysOffStdoutInStructuredMode() throws Exception {
Path noisy = editorThat("echo 'Waiting for your editor to close the file...'");
Path captured = tempDir.resolve("stdout.txt");
Path errors = tempDir.resolve("stderr.txt");
ProcessBuilder structured = ConfigProfileCommand.editorProcess(List.of(noisy.toString(), "file"), true);
assertNotEquals(ProcessBuilder.Redirect.INHERIT, structured.redirectOutput());
assertEquals(ProcessBuilder.Redirect.INHERIT, structured.redirectInput(), "the editor still reads the terminal");
// The same decision, observed from outside: a shell runs the editor the way the command
// would, with fd 1 and fd 2 captured separately.
String redirect = structured.redirectOutput().file() == null ? "/dev/null" : structured.redirectOutput().file().toString();
Process shell = new ProcessBuilder("sh", "-c", "'" + noisy + "' file >> " + redirect)
.redirectOutput(captured.toFile()).redirectError(errors.toFile()).start();
assertEquals(0, shell.waitFor());
assertEquals("", Files.readString(captured), "nothing from the editor on stdout");
assertTrue(Files.readString(errors).contains("Waiting for your editor"), "it went to stderr instead");
}
@Test
void inTextModeTheEditorSharesTheTerminal() {
ProcessBuilder text = ConfigProfileCommand.editorProcess(List.of("vi", "file"), false);
assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectOutput());
assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectInput());
assertEquals(ProcessBuilder.Redirect.INHERIT, text.redirectError());
}
/** VISUAL wins over EDITOR, and a value with arguments is a command line. */
@Test
@DisabledOnOs(OS.WINDOWS)