Skip to content

print.mjs: reject option values it cannot use - #140

Open
rudycelekli wants to merge 1 commit into
wheresryan22:mainfrom
rudycelekli:fix/print-option-values-20261021
Open

rudycelekli wants to merge 1 commit into
wheresryan22:mainfrom
rudycelekli:fix/print-option-values-20261021

Conversation

@rudycelekli

Copy link
Copy Markdown
Contributor

What this changes

--out and --format now require their value, and an unknown --format is rejected right after the argument loop, before the spec module is imported.

Why

Both flags read argv[++index] without checking that it exists. A flag left last became undefined and fell through the ?? chain, so the existing rejection in the same file — unknown format "..."; use one of poster, drawing, detailed or all — was bypassed by the one input that most looks like a mistake.

On main:

$ node scripts/print.mjs examples/raptor-engine/sheet.mjs --format bogus
Error: unknown format "bogus"; use one of poster, drawing, detailed or all   # exit 1

$ node scripts/print.mjs examples/raptor-engine/sheet.mjs --format
wrote raptor-engine-poster-a4.html (3202 KB) and raptor-engine-poster-a4.pdf (599 KB, 1 page)   # exit 0

$ node scripts/print.mjs examples/raptor-engine/sheet.mjs --out
wrote raptor-engine-poster-a4.html (3202 KB) and raptor-engine-poster-a4.pdf (599 KB, 1 page)   # exit 0

No shipped spec sets format, so a bare --format always prints the poster default and exits 0: the sheet is not the one asked for and nothing says so.

Separately, the unknown-format check ran after await import(specFile), so a misspelled format on a figure whose light page is not built was reported as the missing page, or as a module error, rather than as the typo it was.

The later check is kept, because it still guards a format coming from the spec itself.

This is the same fix already recorded under Unreleased for capture.mjs, and matches drive.mjs, lines.mjs, turn-check.mjs and turn-bench.mjs.

Tests

  • scripts/print-options.test.mjs

It fails on main by name (# pass 0 # fail 1) and passes here (# pass 1 # fail 0). node --test scripts/print-sheet.test.mjs scripts/print-options.test.mjs is # tests 12 # pass 12 # fail 0.

Valid input still passes validation and is unaffected: --format poster|drawing|detailed|all, --out <name>, and no --format at all.

I did not run the full scripts/*.test.mjs suite (it needs Chrome and the temp react/typescript install). The change touches only main()'s argument parsing in print.mjs, which no other module imports.

Checks

  • Every example rebuilds, and git status --short shows only the changes I meant. (No example or kit change; git status --short shows only scripts/print.mjs, scripts/print-options.test.mjs and CHANGELOG.md.)
  • No comments in code.
  • Kit changes are opt-in, and every existing example is byte-identical. Not applicable: no kit/ change, and no committed page changes.
  • I built and looked at both themes. Not applicable: no visual change.
  • Figures with pipes, cables or round parts. Not applicable: no figure change.
  • node scripts/lines.mjs. Not applicable: no figure change.
  • Shaders: not touched.
  • 3D: not touched.
  • Every number in the figure, the readout, the caption and the docs is true.
  • A line under Unreleased in CHANGELOG.md.

Screenshots

No visual change and no committed page changes, so there is nothing to screenshot.

--out and --format read the next argv entry without checking that it
exists, so a flag left last silently became undefined and fell through
the ?? chain: a bare --format printed the spec's format, or poster, and
exited 0. The unknown-format check also ran only after the spec module
had been imported, so a misspelled format surfaced behind an unrelated
error.

Validate both flags, and the --format value, immediately after the parse
loop. The later check stays, because it still guards a format coming
from the spec itself.

@rahul05ranjan rahul05ranjan left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified this end to end on a clean checkout.

The premise is real: at main, scripts/print.mjs swallows a bare --format (argv[++index] gives undefined, line 225 falls through), the spec loads, and line 234's options.format ?? spec.format ?? "poster" silently picks a fallback — and the misspelled-format check at line 235 only runs after both the spec import and the built-page check, so --format sepia dies with no built page before it ever mentions the bad flag. I reproduced both on main with a minimal spec: the bare flag reaches the page-build error, sepia reaches the same error.

At this branch the behavior is what the changelog promises:

  • print.mjs /tmp/sheet-probe.mjs --format → --format needs a value at line 221, before the spec import.
  • --format sepia → unknown format "sepia" at line 232, also before the spec import.
  • node --test scripts/print-options.test.mjs passes (1/1). Nice touch that the test drives a nonexistent spec file — it actually proves the errors fire before the spec read rather than after.

The --out guard via the shared valueOf is the right shape; no other flag in that loop takes a value.

One nit: the unknown-format message suggests use one of ${FORMATS.join(", ")} or all, but all is handled before the loop at line 231 rather than being a member of FORMATS — correct as written, just worth knowing the list in the message never contains all itself, so the tail or all is doing the work.

From a security standpoint this is a fail-fast correctness fix rather than a boundary issue — print.mjs runs locally with trusted argv — but validating before the spec import also means a crafted spec module is no longer imported when the invocation itself is malformed, which is a reasonable side benefit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants