Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

feat(cli): give `sources` an output file, since stdout cannot be cleaned

EtienneLescot36381c64f979
diff --git a/.github/workflows/nix-build.yml b/.github/workflows/nix-build.yml
index 5d3e04535..81a2a4d95 100644
--- a/.github/workflows/nix-build.yml
+++ b/.github/workflows/nix-build.yml
@@ -116,43 +116,37 @@ jobs:
# `sources` enumerates displays, windows and microphones, so it
# exercises the capture stack rather than proving that a usage string
- # can be printed. Its output is the assertion: well-formed, non-empty.
- # Streams kept apart on purpose. A previous run piped both together and
- # the parse died on "[10767:0819/...:ERROR:dbus/bus.cc:405]" -- Chromium
- # diagnostics, which a headless host produces in quantity (no dbus, no
- # GPU). Whether those land on stdout decides whether this is a flaw in
- # the test or in the CLI: anything consuming `--json` breaks if they do,
- # and headless is exactly where a --json flag earns its keep.
- echo "--- openscreen sources --json ---"
+ # can be printed.
+ #
+ # Read through -o, not through stdout. This host has no dbus and no
+ # GPU, so Chromium and ANGLE write their own diagnostics to stdout
+ # ahead of anything the CLI emits, and no switch silences all of them:
+ # --disable-logging was tried and the wall survived it, because part of
+ # that output never passes through Chromium's logging. The file is the
+ # only channel the process fully owns. stdout is still captured, as the
+ # evidence for why the flag exists.
+ echo "--- openscreen sources -o ---"
RC=0
- run_cli $SANDBOX sources --json >/tmp/sources.json 2>/tmp/sources.err || RC=$?
-
- # Both streams get reported before anything decides the outcome. The
- # previous shape returned early on a timeout and threw away the very
- # thing the run was for: a hang left stdout unseen, so a run that cost
- # a runner slot said nothing about whether the logging fix had worked.
- # Whatever the process wrote before it was killed is still on disk.
- echo "--- stdout, first 400 bytes ---"
- head -c 400 /tmp/sources.json; echo
+ run_cli $SANDBOX sources -o /tmp/sources.json >/tmp/sources.out 2>/tmp/sources.err || RC=$?
+
+ echo "--- stdout, first 300 bytes (expected to be noisy) ---"
+ head -c 300 /tmp/sources.out; echo
echo "--- stderr, first 3 lines ---"
head -3 /tmp/sources.err || true
if [ "$RC" -eq 124 ] || [ "$RC" -eq 137 ]; then
- echo "::error::openscreen sources --json did not return within 120s. A CLI that hangs on a headless host cannot be scripted, and headless is what --json is for."
+ echo "::error::openscreen sources did not return within 120s. A CLI that hangs on a headless host cannot be scripted."
exit 1
fi
+ echo "--- the -o file ---"
+ cat /tmp/sources.json
+
python3 <<'PY'
import json
- raw = open("/tmp/sources.json").read().strip()
- if not raw:
- raise SystemExit("sources --json produced no output")
- try:
- json.loads(raw)
- print("parsed as a single JSON document")
- except json.JSONDecodeError:
- lines = [l for l in raw.splitlines() if l.strip()]
- for l in lines:
- json.loads(l)
- print(f"parsed as NDJSON, {len(lines)} record(s)")
+ doc = json.load(open("/tmp/sources.json"))
+ missing = [k for k in ("displays", "windows", "microphones") if k not in doc]
+ if missing:
+ raise SystemExit(f"-o file is missing {missing}")
+ print(f"displays={len(doc['displays'])} windows={len(doc['windows'])} microphones={len(doc['microphones'])}")
PY
diff --git a/docs/cli.md b/docs/cli.md
index 9e965bcc1..8eea3780d 100644
--- a/docs/cli.md
+++ b/docs/cli.md
@@ -92,8 +92,16 @@ GUI picker uses — so scripts and agents can choose `--display`, `--window`, an
```bash
openscreen sources # human-readable
openscreen sources --json
+openscreen sources -o sources.json # straight to a file
```
+On a host with no D-Bus and no GPU — a container, a CI runner, a server —
+Chromium and ANGLE write their own diagnostics to this process's stdout, ahead
+of anything the CLI emits, and no Chromium switch silences all of them: some of
+that output never passes through Chromium's logging at all. Piping `--json` into
+a parser therefore fails in exactly the environments `--json` exists for. Pass
+`-o <file>` there: the file is a channel nothing else can write to.
+
`--json` emits the payload on the final `done` event:
```json
diff --git a/electron/cli/args.test.ts b/electron/cli/args.test.ts
index 7f0b18e09..05b3dff73 100644
--- a/electron/cli/args.test.ts
+++ b/electron/cli/args.test.ts
@@ -157,6 +157,21 @@ describe("parseCliArgs", () => {
expect(parse(["sources", "extra-arg"])).toMatchObject({ kind: "error" });
});
+ it("parses the sources output file, resolved against cwd", () => {
+ expect(parse(["sources", "-o", "s.json"])).toMatchObject({
+ kind: "sources",
+ jsonOutPath: inCwd("s.json"),
+ });
+ expect(parse(["sources", "--json", "--out", "s.json"])).toMatchObject({
+ kind: "sources",
+ json: true,
+ jsonOutPath: inCwd("s.json"),
+ });
+ // The flag is optional; absent means stdout stays the only channel.
+ expect(parse(["sources"])).toMatchObject({ kind: "sources", jsonOutPath: undefined });
+ expect(parse(["sources", "--out"])).toMatchObject({ kind: "error" });
+ });
+
it("parses pack", () => {
expect(parse(["pack", "demo.openscreen", "--out", "bundle"])).toMatchObject({
kind: "pack",
diff --git a/electron/cli/args.ts b/electron/cli/args.ts
index 0dad8d07c..088c84d73 100644
--- a/electron/cli/args.ts
+++ b/electron/cli/args.ts
@@ -27,6 +27,15 @@ export type CliCommand = (
) & {
/** Machine-readable NDJSON output on stdout instead of human progress. */
json?: boolean;
+ /**
+ * Write the machine-readable result here instead of trusting stdout with it.
+ * On a host without dbus or a GPU -- a container, a CI runner, a server --
+ * Chromium and ANGLE write diagnostics straight to the process's stdout, ahead
+ * of anything this CLI emits, and no Chromium switch reaches all of them: some
+ * of that output does not go through Chromium's logging at all. A file is the
+ * one channel nothing else can reach.
+ */
+ jsonOutPath?: string;
};
const SUBCOMMANDS = new Set([
@@ -46,7 +55,7 @@ export const CLI_USAGE = `OpenScreen CLI
Usage:
openscreen export <project.openscreen> [options] Render a project to MP4/GIF
openscreen record [options] Record the screen headlessly
- openscreen sources [--json] List displays, windows and microphones
+ openscreen sources [--json] [-o <file>] List displays, windows and microphones
openscreen pack <project.openscreen> --out <dir> Copy project + media into one portable folder
openscreen captions <project.openscreen> Add auto-captions (on-device Whisper) to a project
[--min-words <n>] [--max-words <n>]
@@ -131,7 +140,7 @@ export function parseCliArgs(
try {
if (sub === "export") return parseExport(args.slice(1), cwd);
if (sub === "record") return parseRecord(args.slice(1), cwd);
- if (sub === "sources") return parseSources(args.slice(1));
+ if (sub === "sources") return parseSources(args.slice(1), cwd);
if (sub === "pack") return parsePack(args.slice(1), cwd);
if (sub === "captions") return parseCaptions(args.slice(1), cwd);
return parseInfo(args.slice(1), cwd);
@@ -344,16 +353,22 @@ function parseRecord(args: string[], cwd: string): CliCommand {
return request;
}
-function parseSources(args: string[]): CliCommand {
+function parseSources(args: string[], cwd: string): CliCommand {
let json = false;
- for (const arg of args) {
+ let jsonOutPath: string | undefined;
+ for (let i = 0; i < args.length; i++) {
+ const arg = args[i];
if (arg === "--json") {
json = true;
+ } else if (arg === "-o" || arg === "--out") {
+ const [value, next] = takeValue(args, i, arg);
+ jsonOutPath = resolvePath(value, cwd);
+ i = next;
} else {
throw new Error(`Unknown sources option: ${arg}`);
}
}
- return { kind: "sources", json };
+ return { kind: "sources", json, jsonOutPath };
}
export interface CliPackCommand {
diff --git a/electron/cli/cliMain.ts b/electron/cli/cliMain.ts
index 53478de48..e2f015b27 100644
--- a/electron/cli/cliMain.ts
+++ b/electron/cli/cliMain.ts
@@ -409,9 +409,27 @@ export function runCli(command: CliCommand): void {
if (result.success && command.kind === "captions" && result.projectData !== undefined) {
await writeProjectFile(command.projectPath, result.projectData);
}
+ // A file is the only output channel this process fully owns. Chromium
+ // and ANGLE write diagnostics straight to stdout on a host with no
+ // dbus and no GPU, ahead of anything emitted here, and no switch
+ // silences all of them -- part of that output never passes through
+ // Chromium's logging. So -o exists for the case stdout cannot serve.
+ if (
+ result.success &&
+ command.kind === "sources" &&
+ command.jsonOutPath &&
+ result.sources
+ ) {
+ await fs.writeFile(
+ command.jsonOutPath,
+ `${JSON.stringify(result.sources, null, 2)}
+`,
+ "utf8",
+ );
+ }
} catch (error) {
result.success = false;
- result.error = `Run succeeded but writing the project file failed: ${String(error)}`;
+ result.error = `Run succeeded but writing the output file failed: ${String(error)}`;
}
if (result.success) {