Skip to content

Commit e5ae5ea

Browse files
redsun82Copilot
andcommitted
Just: reject a separator value that would run, instead of warning about it
`JUST_CMD_RULE` is interpolated whole into a shell script, so a value that is not a shell comment gets executed -- with a zero exit status, no diagnostic, and once for every place the separator appears. The comment above it said so, and saying so was all that happened, which leaves the obligation with whoever presets the variable to have first read the file explaining why they mustn't. A rule is a single line by construction, so demanding one line starting with `#` rejects exactly the values that would run, without having to describe what running looks like. Reading the value once instead of twice is what makes that expressible at all. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 29c7a53 commit e5ae5ea

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

misc/just/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ for itself, and the count is one per `mod` reached, however deeply nested, plus
162162
the file itself. That is cheap, and modules agree anyway since they share a terminal,
163163
but it is worth knowing before counting measurements. Presetting `JUST_CMD_RULE` skips
164164
all of them, and is also how to fix the width in CI or in a recording — it is the whole
165-
separator and ends up in a shell script, so every line of it has to be a comment.
165+
separator and ends up in a shell script, so it has to be a single line starting with
166+
`#`. Anything else is rejected rather than documented against, because otherwise it
167+
runs, silently and once per place the separator appears.
166168

167169
With no terminal to ask — a pipe, a log, a shell without `stty` — it falls back to a
168170
fixed 57 columns, so logs and CI output are the same width every time. That is one

misc/just/defs.just

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,23 @@ error := f'{{ style("error") }}error{{ NORMAL }}: '
2828
# With no terminal the value is non-numeric and this falls back to a fixed 57 columns.
2929
# That is a branch, not a platform test: just runs `sh` everywhere.
3030
#
31-
# `JUST_CMD_RULE` is the whole separator and lands in a shell script, so a hand-set
32-
# value has to be a comment -- every line of it, if it has more than one. Nothing checks
33-
# that; a value that is not one gets run.
31+
# `JUST_CMD_RULE` is the whole separator and lands in a shell script, so a hand-set value
32+
# that is not a shell comment gets run -- silently, with a zero exit, once per place the
33+
# separator appears. Hence the check rather than a warning in prose: a rule is one line by
34+
# construction, so demanding one line starting with `#` rejects every value that would
35+
# run, without having to describe what running looks like.
3436
#
3537
# Exported and preferred over measuring: `shell()` runs on every parse, so a forwarded
3638
# verb would otherwise re-measure in every child it spawns, and `if` is lazy in its
3739
# branches. Inheritance needs a process, so a `mod` measures for itself; presetting
3840
# `JUST_CMD_RULE` skips measuring entirely.
39-
_rule := if env('JUST_CMD_RULE', '') != '' { env('JUST_CMD_RULE', '') } else { shell('''
41+
_given_rule := env('JUST_CMD_RULE', '')
42+
43+
_rule := if _given_rule == '' { shell('''
4044
w=$(stty size 2>/dev/null | cut -d" " -f2)
4145
case "$w" in '' | *[!0-9]*) w=57 ;; esac
4246
printf "%*s" $w "" | tr " " '#'
43-
''') }
47+
''') } else if _given_rule =~ '^#[^\n]*$' { _given_rule } else { error('JUST_CMD_RULE must be a single line starting with `#`') }
4448

4549
export JUST_CMD_RULE := _rule
4650

0 commit comments

Comments
 (0)