Skip to content

Warn when a template lacks the guard lines past ARGBASH_GO - #211

Open
gdevenyi wants to merge 1 commit into
matejak:masterfrom
gdevenyi:fix/warn-missing-guards
Open

Warn when a template lacks the guard lines past ARGBASH_GO#211
gdevenyi wants to merge 1 commit into
matejak:masterfrom
gdevenyi:fix/warn-missing-guards

Conversation

@gdevenyi

Copy link
Copy Markdown

Related to #205

The user code past ARGBASH_GO has to be enclosed in the # [ <-- needed because of Argbash and # ] <-- needed because of Argbash lines, otherwise m4 strips square brackets from it (for example "${_arg_leftovers[@]}" becomes "${_arg_leftovers@}", which is the symptom reported in #205) and the script gets corrupted silently.

argbash now prints a warning that names the guard lines when it generates a bash or POSIX script (without --strip) from a template that has ARGBASH_GO/ARGBASH_PREPARE but no guard line after it. Generation itself is unchanged.

New regression test test-missing-guards checks that the warning is printed for such a template and not printed for a regular one nor with --strip.

🤖 Generated with Claude Code

https://claude.ai/code/session_017DxhC3HmTM7TQUzkBZnyH2

Copilot AI lite review requested due to automatic review settings September 11, 2026 21:57

Copilot AI 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.

🟡 Changes recommended

Guard matching, macro-boundary handling, and POSIX test coverage remain unresolved.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Adds warnings when templates lack required Argbash guard lines, with regression coverage and synchronized generator updates.

Changes:

  • Adds guard detection and warnings.
  • Adds missing-guard fixture and regression tests.
  • Updates the generated executable and changelog.
File summaries
File Description
tests/regressiontests/Makefile Registers the regression test.
tests/regressiontests/make/tests/tests-base.m4 Defines warning assertions.
tests/regressiontests/gen-test-missing-guards.m4 Provides the malformed template fixture.
src/argbash.m4 Implements missing-guard warning logic.
ChangeLog Documents the bug fix.
bin/argbash Mirrors the generator changes.
Review details

Suppressed comments (3)

src/argbash.m4:209

  • The detector also matches ARGBASH_PREPARE, but this warning names only ARGBASH_GO and says the code is past ARGBASH_GO. For a DIY template using only ARGBASH_PREPARE, the diagnostic points at a marker that is not present and is misleading about the affected section.
		"Warning: The ARGBASH_GO line is not followed by the '# [ <-- needed because of Argbash' guard line (with the matching '# ] <-- needed because of Argbash' line at the end of the file)." \
		"Square brackets in the code past ARGBASH_GO will be stripped, and the script can't be regenerated correctly. See the 'Template layout' section of the documentation." >&2

src/argbash.m4:203

  • Because this expression is not bounded after GO|PREPARE, it also classifies names such as the existing # ARGBASH_GOO typo (tests/regressiontests/gen-test-misspelled.m4:6) as action macros. That produces a misleading missing-guards warning for an input with no action macro; require a token boundary while still accepting the supported (), [ and end-of-line forms.
		/^#[[:space:]]*ARGBASH_(GO|PREPARE)/ { seen_go = 1 }

tests/regressiontests/make/tests/tests-base.m4:352

  • The implementation explicitly supports both bash and POSIX script types via the *script check, but this regression only invokes the default bash type. Add equivalent --type posix-script assertions (at least the warning and --strip suppression) so the advertised POSIX path is covered; this suite already has dedicated POSIX tests.
ADD_TEST_BASH([test-missing-guards], [[
	$(ARGBASH_EXEC) $< -o - 2>&1 > /dev/null | grep -q 'needed because of Argbash'
	$(ARGBASH_EXEC) $< --strip user-content -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
	$(ARGBASH_EXEC) $(TESTDIR)/test-simple.m4 -o - 2>&1 > /dev/null | $(REVERSE) grep -q 'needed because of Argbash'
]], [$(TESTDIR)/test-simple.m4], [$(TESTDIR)/gen-test-missing-guards.m4])
  • Files reviewed: 5/6 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/argbash.m4
Comment on lines +202 to +206
awk '
/^#[[:space:]]*ARGBASH_(GO|PREPARE)/ { seen_go = 1 }
seen_go && /^#[[:space:]]*\[/ { seen_guard = 1 } # ] <-- needed because of Argbash
END { exit !(seen_go && !seen_guard) }
' "$1" || return 0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

A template with the opening guard but without the closing one does not pass silently: the unmatched quote makes autom4te fail with "end of file in string", and argbash reports the unmatched square bracket and aborts. The warning is meant for the case that is silent (no opening guard at all), so I am leaving the detector as it is.

@gdevenyi
gdevenyi force-pushed the fix/warn-missing-guards branch from 0e17c6d to d21217e Compare September 11, 2026 23:44
The user code past ARGBASH_GO has to be enclosed in the
'# [ <-- needed because of Argbash' and '# ] <-- needed because of Argbash'
lines, otherwise m4 strips square brackets from it (e.g. array
expansions such as "${arr[@]}" become "${arr@}"), and the script gets
corrupted silently.

Print a warning that names the guard lines when a bash or POSIX script
is generated (without --strip) from such a template.

Related to matejak#205

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017DxhC3HmTM7TQUzkBZnyH2
@gdevenyi
gdevenyi force-pushed the fix/warn-missing-guards branch from d21217e to 7400056 Compare September 13, 2026 02:21
@gdevenyi

Copy link
Copy Markdown
Author

Addressed the suppressed remarks as well: the detector requires a token boundary after ARGBASH_GO/ARGBASH_PREPARE (so the ARGBASH_GOO typo template no longer triggers it), the message mentions ARGBASH_PREPARE, and the test also covers --type posix-script and the misspelled-macro template.

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