Skip to content

refactor: batch Bash compat rules — empty array, empty-argv, variadic-args, exec-stderr-silence#74

Closed
olavostauros wants to merge 11 commits into
KnickKnackLabs:mainfrom
olavostauros:batch/bash-compat
Closed

refactor: batch Bash compat rules — empty array, empty-argv, variadic-args, exec-stderr-silence#74
olavostauros wants to merge 11 commits into
KnickKnackLabs:mainfrom
olavostauros:batch/bash-compat

Conversation

@olavostauros

@olavostauros olavostauros commented Jun 26, 2026

Copy link
Copy Markdown

Summary

Stacked batch 3/4. See ricon-family/olavo#9.

Consolidates 4 Bash compatibility lint rules (supersedes #62, #59, #63, #67). These rules target macOS Bash 3.2 compatibility and common bash scripting pitfalls.

Rules included

Rule Issue Description
bash-empty-array-expansions #57 Flag empty array expansions under nounset (macOS Bash 3.2 compat)
bash-empty-argv-forwarding Flag empty argv forwarding under nounset
variadic-args #21 Flag unsafe variadic argument patterns
exec-stderr-silence Flag persistent exec ... 2>/dev/null stderr suppression

Commits

Commit Description
b5040ec feat: add lint rule for Bash nounset empty array expansions
d29959d style: collapse 3-line section comment blocks
d809be9 fix: make bash-empty-array-expansions task executable
827405e feat: add lint rule for Bash nounset empty-argv forwarding
b4f0f1e style: collapse 3-line section comment blocks
bdf41bb fix: make task executable, remove decorative rulers
4e7b537 feat: add variadic-args lint rule (#21)
6d1b69c style: replace decorative rulers with single-line headers
2cb1d30 feat(lint): add exec-stderr-silence rule

Validation

  • mise run test — 312 tests passing
  • git diff --check — clean

The codebase_target_for_rule() function treated the entire scope value as
a single path, so a TOML string like or-true = ".mise/tasks lib" would
produce a single target /repo/.mise/tasks lib (with space) instead of
two separate targets /repo/.mise/tasks and /repo/lib.

This meant multi-path scope overrides either errored (path doesn't exist)
or silently scanned nothing — users thought they had multi-path coverage
but got false passes.

Changes:
- lib/codebase-config.sh: Replace codebase_target_for_rule() with
  codebase_targets_for_rule() that splits the scope value on whitespace
  and emits one target line per token.
- .mise/tasks/lint/_default: Iterate over the multi-line output so each
  target is linted independently.
- test/lint/default.bats: Add test covering multi-path scope with
  space-separated path list.

The fix uses shell word-splitting on the scope string (unquoted  in
a for loop), which naturally handles TOML string values with internal spaces.
Each token is resolved as a relative path against the repo root, or passed
through unchanged if absolute.

Fixes KnickKnackLabs#22
Adds first-class lint groups/presets so repos can opt into convention
bundles with a single @maintained-tool reference instead of enumerating
every lint rule individually.

Includes:
- lib/lint-groups.sh — built-in group registry, expansion, and
  discoverability functions
- lib/codebase-config.sh — source lint-groups.sh, expand @group
  references in codebase_configured_lint_rules(), support lint_exclude
- .mise/tasks/lint/_default — show expanded group count in preamble;
  updated error hint to mention groups
- .mise/tasks/lint/groups — new lint:groups discoverability task
- test/lint/lint-groups/ — 17 BATS tests

Closes KnickKnackLabs#69
Adds bash-empty-array-expansions lint rule that flags ${array[@]}
and ${array[*]} under set -u (nounset), suggesting the Bash-3-safe
alternate-value form ${arr[@]+"${arr[@]}"}.

Shares nounset detection pattern with bash-empty-argv-forwarding (KnickKnackLabs#48):
- has_nounset() - detect set -u / set -eu / set -euo pipefail
- is_safe_context() - exclude for loops and local array assignments
- has_safe_form() - exclude already-safe alternate-value forms

Standard text-based ripgrep approach with 11 fixture scenarios and
15 BATS tests. ShellCheck-clean.

Implements KnickKnackLabs#57 (KnickKnackLabs/codebase)
Adds bash-empty-argv-forwarding lint rule that flags "$@" and "${@}"
as command arguments in files with nounset enabled (set -u). This pattern
causes fatal unbound-variable errors on macOS Bash 3.2 when the argument
list is empty, while Linux Bash 5+ handles it silently.

The rule follows the established text-based ripgrep pattern from or-true,
caller-pwd-contract, etc. It correctly excludes safe contexts (for loops,
array assignments), already-safe alternate-value forms (${@+"$@"}),
and files without nounset.

Implements KnickKnackLabs#48 (KnickKnackLabs/codebase)
Scans .mise/tasks/* for #USAGE directives with var=#true, then flags
dangerous consumption of the resulting usage_* vars:
  - eval 'ARRAY=${usage_X}'    → ERROR: injection vector
  - read -ra ARRAY <<< "$usage_X"  → WARN: loses quoting

The correct pattern uses xargs printf for bash or shlex.split() for
Python tasks, per mise-conventions.md.
…r suppression

Adds text-based detection for 'exec' with stderr redirection
(2>/dev/null, 2>&-, 2>!) that persists for the current shell.

- Detection via bash regex: \bexec\b[^#]*2> with dangerous target
- Fixtures: clean, dirty, ignored-inline, ignored-file, broad-walk, no-toml, empty
- 17 BATS tests covering detection, ignore mechanisms, discovery, edge cases
- Self-hosting verified: task file does not self-report
- Follows or-true pattern for output format, ignore conventions, and scope
@olavostauros

Copy link
Copy Markdown
Author

Closing this stacked batch in favor of individual re-evaluation. See ricon-family/olavo#9 for context.

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.

1 participant