Skip to content

gnovm: add -X flag to gno run and gno test#5985

Open
ygd58 wants to merge 2 commits into
gnolang:masterfrom
ygd58:feat/x-flag-run-test-1021
Open

gnovm: add -X flag to gno run and gno test#5985
ygd58 wants to merge 2 commits into
gnolang:masterfrom
ygd58:feat/x-flag-run-test-1021

Conversation

@ygd58

@ygd58 ygd58 commented Jul 20, 2026

Copy link
Copy Markdown

Adds a -X name=value flag (repeatable) to 'gno run' and 'gno test', mirroring 'go build -ldflags "-X pkg.name=value"'. Before parsing, matching package-level (unindented) 'var name = "..."' or 'var name string = "..."' declarations have their string literal initializer replaced with the given override value.

Note: 'gno build' and 'gno publish' do not currently exist as separate gno CLI subcommands (only run/test/lint/etc. do), so this covers the two existing commands that parse and execute .gno source directly.

Closes #1021

@github-actions github-actions Bot added the 📦 🤖 gnovm Issues or PRs gnovm related label Jul 20, 2026
@Gno2D2 Gno2D2 added the review/triage-pending PRs opened by external contributors that are waiting for the 1st review label Jul 20, 2026
@Gno2D2

Gno2D2 commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

🛠 PR Checks Summary

🔴 Pending initial approval by a review team member, or review from tech-staff

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
  • The pull request description provides enough details
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

🟢 Maintainers must be able to edit this pull request (more info)
🔴 Pending initial approval by a review team member, or review from tech-staff

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Automated Checks
Maintainers must be able to edit this pull request (more info)

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 The pull request was created from a fork (head branch repo: ygd58/gno)

Then

🟢 Requirement satisfied
└── 🟢 Maintainer can modify this pull request

Pending initial approval by a review team member, or review from tech-staff

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 The base branch matches this pattern: ^master$
    └── 🟢 Not (🔴 Pull request author is a member of the team: tech-staff)

Then

🔴 Requirement not satisfied
└── 🔴 If
    ├── 🔴 Condition
    │   └── 🔴 Or
    │       ├── 🔴 At least one of these user(s) reviewed the pull request: [aronpark1007 davd-gzl jefft0 notJoon omarsy MikaelVallenet] (with state "APPROVED")
    │       ├── 🔴 At least 1 user(s) of the team tech-staff reviewed pull request
    │       └── 🔴 This pull request is a draft
    └── 🔴 Else
        └── 🔴 And
            ├── 🟢 This label is applied to pull request: review/triage-pending
            └── 🔴 On no pull request

Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission
The pull request description provides enough details

If

🟢 Condition met
└── 🟢 And
    ├── 🟢 Not (🔴 Pull request author is a member of the team: core-contributors)
    └── 🟢 Not (🔴 Pull request author is user: dependabot[bot])

Can be checked by

  • team core-contributors

@notJoon notJoon left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think using a regex is a good or safe approach, because it would replace not only the variable names but also all raw strings in the code.

the original issue requires support for build, test and publish but it doesn't look like all of those requirements have been addressed. so I think this is not acceptable in its current state

Adds a -X name=value flag (repeatable) to 'gno run' and 'gno test',
mirroring 'go build -ldflags "-X pkg.name=value"'.

Rewritten from an earlier regex-based approach per review feedback
(PR gnolang#5985 / notJoon): a text-based regex can't distinguish a real
top-level var declaration from text that merely looks like one, e.g.
inside a backtick-quoted raw string literal elsewhere in the file.

This version parses the source with go/parser, walks only the file's
top-level Decls (so it never descends into function bodies, and never
touches local variables or string-literal *contents*), finds
package-level 'var name = "..."' declarations (including grouped
'var (...)' blocks, and both quoted and raw-string initializers) whose
name matches an override, replaces the literal's Value via
strconv.Quote, and re-serializes the file with go/printer. Only
top-level var (never const) declarations are eligible, matching
go build -X's own semantics.

Note: 'gno build' and 'gno publish' don't currently exist as separate
gno CLI subcommands (only run/test/lint/etc. do), so this covers the
two existing commands that parse and execute .gno source directly;
see the PR discussion for more on this.

Added a dedicated regression test for the exact scenario from review
(a raw string containing text that resembles a var declaration must
be left untouched), plus tests for grouped var blocks, const
declarations being skipped, and function-local variables being left
alone.

Closes gnolang#1021
@ygd58
ygd58 force-pushed the feat/x-flag-run-test-1021 branch from 6ebb08e to 7f7b0e8 Compare July 21, 2026 02:26
…nario

The previous version of this test only had ONE actual var declaration
(whose value happened to be a raw string containing fake-looking
declaration text), so patching that single declaration's entire value
-- including the embedded fake text -- was correct behavior, not a
bug. The test's assertion that the fake text should survive was wrong.

Rewritten with two separate declarations: a real "var myVar = ..." to
patch, and a different "var otherVar = <raw string>" whose raw string
value merely contains text resembling a var myVar declaration. This
actually exercises the scenario from review: only the real
declaration is patched, and otherVar's raw string content is
preserved byte-for-byte.
@ygd58
ygd58 force-pushed the feat/x-flag-run-test-1021 branch from 29e5d4e to 639f73f Compare July 21, 2026 04:58
@ygd58

ygd58 commented Jul 21, 2026

Copy link
Copy Markdown
Author

Thanks for the review, both points are addressed:

  1. Regex safety: rewrote patchXVars to use go/parser + go/printer instead of a text regex. It now walks only the parsed file's top-level Decls, so it can never match text inside a string literal (raw or otherwise) or a local variable in a function body -- only genuine package-level var name = "..." declarations (including grouped var (...) blocks) are eligible. Added a dedicated regression test for the exact raw-string scenario you described.

  2. build/test/publish scope: gno build and gno publish do not currently exist as separate subcommands in the gno CLI (only run, test, lint, etc. do -- see gnovm/cmd/gno/main.go). This PR covers the two existing commands that parse and execute .gno source directly (run and test). If there's a different command I'm missing that should also support -X, happy to add it -- let me know which one you had in mind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

📦 🤖 gnovm Issues or PRs gnovm related review/triage-pending PRs opened by external contributors that are waiting for the 1st review

Projects

Development

Successfully merging this pull request may close these issues.

gno {build,test,publish} -X (initialization parameters)

3 participants