Skip to content

fix: escape backticks in package comment for generated code#550

Open
Yanhu007 wants to merge 1 commit intomagefile:masterfrom
Yanhu007:fix/backtick-in-package-comment
Open

fix: escape backticks in package comment for generated code#550
Yanhu007 wants to merge 1 commit intomagefile:masterfrom
Yanhu007:fix/backtick-in-package-comment

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #537

Problem

When a magefile's package comment contains backticks, the generated mage_output_file.go breaks:

// Source magefile:
// This is a magefile, to be called with `go tool mage`.
package main

// Generated code (broken):
_fmt.Println(`This is a magefile, to be called with `go tool mage`.
`)

The backticks in the comment terminate the raw string literal, producing invalid Go.

Fix

Use %q (Go quoted string) instead of embedding in a raw backtick literal:

// Before:
_fmt.Println(`{{.}}\n`)

// After:
_fmt.Println({{printf "%q" .}} + "\n")

This properly escapes any special characters including backticks.

Testing

All mage tests pass.

When a magefile's package comment contains backticks (e.g.
"called with `go tool mage`"), the generated mage_output_file.go
embeds the comment in a raw string literal delimited by backticks.
The backticks in the comment terminate the string early, producing
invalid Go code.

Use fmt's %q verb to produce a properly quoted string instead of
a raw backtick literal.

Fixes magefile#537
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.

Bug: Backticks in magefile package comment breaks mage_output_file.go

1 participant