Skip to content

add support for retaining multiline comments#546

Merged
natefinch merged 9 commits intomasterfrom
nf/nowrap
Mar 25, 2026
Merged

add support for retaining multiline comments#546
natefinch merged 9 commits intomasterfrom
nf/nowrap

Conversation

@natefinch
Copy link
Copy Markdown
Member

@natefinch natefinch commented Mar 25, 2026

So I wrote out a beautiful long comment on my project's magefile package declaration, talking about the environment variables it supports etc... and mage mangled it.

I think this has been asked for before, and now it's here: retaining line returns in comments that get printed out as help text.

add

//mage:multiline

to your magefile or set MAGEFILE_MULTILINE=true in your environment, and mage will retain line returns from your comments in your help docs.

Note that this is a generation-time check, so if you use -compile, the compiled binary will be stuck with whatever choice was set on the machine that generated the binary. Since it can be part of the magefile itself, it didn't make sense to me to make it a runtime decision.

fixes #358

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds an opt-in mechanism to preserve multiline doc comments (newlines/blank lines) when Mage renders package/target help text, addressing #358.

Changes:

  • Add multiline-aware parsing of package/function doc strings, controlled by //mage:multiline and MAGEFILE_MULTILINE (plus a new CLI flag).
  • Wire the new toggle through invocation/runtime config and into imported-package parsing.
  • Update docs and add test coverage + new testdata magefiles for multiline behavior.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
parse/parse.go Adds Multiline support to parsing and preserves newlines in descriptions/comments when enabled.
parse/parse_test.go Updates tests for the new PrimaryPackage(..., multiline) signature.
mg/runtime.go Introduces MAGEFILE_MULTILINE env var constant and accessor.
mage/main.go Adds Invocation.Multiline, a new CLI flag, and passes multiline toggle into parsing.
mage/main_test.go Adds tests validating multiline help/list output and tag override behavior.
mage/testdata/multiline/magefile.go New testdata magefile used to validate env-var-controlled multiline output.
mage/testdata/multiline/tag/magefile.go New testdata magefile used to validate //mage:multiline tag override behavior.
mage/magefile_tmpl.go Updates mage -init template to include the multiline tag.
sh/testmain_test.go Modifies package-level TestMain used by sh tests/helpers.
site/content/environment/_index.en.md Documents MAGEFILE_MULTILINE and reorders environment variable docs.
site/content/magefiles/_index.en.md Documents multiline help retention and shows //mage:multiline usage.
site/content/index.md Updates homepage example to include //mage:multiline.
Comments suppressed due to low confidence (1)

mg/runtime.go:107

  • Same issue as the MultilineEnv constant comment: Multiline() says it retains “CRLF line endings in comments for the generated main file”, but the behavior is about preserving newlines in help descriptions. Updating this comment will make the API clearer to callers.
// Multiline reports whether the user has requested to retain CRLF line endings
// in comments for the generated main file.
func Multiline() bool {
	b, _ := strconv.ParseBool(os.Getenv(MultilineEnv))
	return b
}

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 28 to 43
@@ -40,7 +38,6 @@ func TestMain(m *testing.M) {
if helperCmd {
_, _ = fmt.Fprintln(os.Stderr, stderr)
_, _ = fmt.Fprintln(os.Stdout, stdout)
os.Exit(exitCode) //nolint:revive // sub-process test helper requires explicit exit
os.Exit(exitCode) //nolint:revive // required for test helper command
}
os.Exit(m.Run()) //nolint:revive // TestMain with branching logic needs explicit exit
}
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

TestMain no longer calls flag.Parse() and never calls os.Exit(m.Run()). Per testing package semantics, flags are not parsed before TestMain, and returning without running m.Run() will skip all tests in this package (and printArgs/helperCmd will never be set). Restore flag.Parse() and ensure the non-helper path exits with m.Run() so normal tests still execute.

Copilot uses AI. Check for mistakes.
@natefinch natefinch merged commit 707313f into master Mar 25, 2026
22 checks passed
@natefinch natefinch deleted the nf/nowrap branch March 25, 2026 18:13
preminger added a commit to yaklabco/stave that referenced this pull request Mar 28, 2026
preminger added a commit to yaklabco/stave that referenced this pull request Mar 28, 2026
* chore: port
magefile/mage#546

* chore: port
magefile/mage#543

* chore: update CHANGELOG.md

* docs: update to reflect changes
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.

Option -h doesn't respect comment newlines.

2 participants