feat: improve builder#165
Conversation
WalkthroughAdds ChangesFeature Flag Builder
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 3❌ Failed checks (1 warning, 2 inconclusive)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 golangci-lint (2.12.2)level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies" Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #165 +/- ##
==========================================
- Coverage 68.52% 68.49% -0.03%
==========================================
Files 56 56
Lines 5417 5438 +21
==========================================
+ Hits 3712 3725 +13
- Misses 1494 1498 +4
- Partials 211 215 +4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
NumaryBot
left a comment
There was a problem hiding this comment.
✅ Approve — automated review
The new builder API emits syntactically valid feature declarations and preserves existing BuildProgram behavior. I did not find any discrete regressions introduced by the patch.
No findings.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
builder/builder_test.go (1)
53-68: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a dedicated test for invalid-flag panic behavior.
This PR introduces a strict panic contract for invalid flags, but current coverage only asserts the happy path. Add a focused test to lock panic behavior and prevent silent contract drift.
Example test addition
+func TestBuildProgramWithFeatureFlagsInvalidFlagPanics(t *testing.T) { + stmt := builder.StmtSend( + builder.ExprMonetary( + builder.ExprAsset("USD/2"), + builder.ExprNumberBigInt(big.NewInt(1)), + ), + builder.SrcAccount(builder.ExprAccount("src")), + builder.DestAccount(builder.ExprAccount("dest")), + ) + + defer func() { + if recover() == nil { + t.Fatalf("expected panic for invalid feature flag") + } + }() + + builder.BuildProgramWithFeatureFlags([]string{"Invalid-Flag"}, stmt) +}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@builder/builder_test.go` around lines 53 - 68, The current test coverage only validates the happy path where valid feature flags are passed to the BuildProgramWithFeatureFlags function. Add a new test function that specifically tests the panic behavior when an invalid or unsupported flag is provided to BuildProgramWithFeatureFlags. The test should verify that the function panics with an appropriate error message when given an invalid flag, ensuring the strict panic contract is properly covered and preventing regression of the panic behavior contract.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@builder/builder.go`:
- Around line 175-189: The BuildProgramWithFeatureFlags function validates
featureFlags after calling BuildProgram on the statements, which allows side
effects from BuildProgram to execute even when invalid flags would cause a
panic. Move the flag validation loop that calls checkIsFlagValid before the call
to BuildProgram to ensure invalid flags are caught immediately and prevent any
statement execution or side effects from occurring before the panic.
---
Nitpick comments:
In `@builder/builder_test.go`:
- Around line 53-68: The current test coverage only validates the happy path
where valid feature flags are passed to the BuildProgramWithFeatureFlags
function. Add a new test function that specifically tests the panic behavior
when an invalid or unsupported flag is provided to BuildProgramWithFeatureFlags.
The test should verify that the function panics with an appropriate error
message when given an invalid flag, ensuring the strict panic contract is
properly covered and preventing regression of the panic behavior contract.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 0137bf64-5872-438a-8436-36decab91be4
📒 Files selected for processing (2)
builder/builder.gobuilder/builder_test.go
| func BuildProgramWithFeatureFlags( | ||
| featureFlags []string, | ||
| statements ...Statement, | ||
| ) (map[string]string, VarsEnv, string) { | ||
| knownBindings, varsEnv, script := BuildProgram(statements...) | ||
|
|
||
| var flagsArgs strings.Builder | ||
| for index, flag := range featureFlags { | ||
| if !checkIsFlagValid(flag) { | ||
| // Yes, we are panicking instead of returning an error here. | ||
| // That's desidered: flags are meant to be passed manually. Not computed, created conditionally, etc. | ||
| // If a feature flag is wrong we want to crash the thing immediately instead of having the user handle that, log that, or whatever. | ||
| panic(fmt.Sprintf("Invalid argument: the `%s` feature flag is invalid. Only flags matching `^[a-z-]+$` are accepted.", flag)) | ||
| } | ||
|
|
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Validate flags before building the script to preserve fail-fast behavior.
Line 179 builds statements before validating featureFlags. Since Statement is executable code, callers can trigger side effects/panics even when flags are invalid, which contradicts the “panic right away” contract documented in Lines 172-173.
Suggested fix
func BuildProgramWithFeatureFlags(
featureFlags []string,
statements ...Statement,
) (map[string]string, VarsEnv, string) {
- knownBindings, varsEnv, script := BuildProgram(statements...)
-
var flagsArgs strings.Builder
for index, flag := range featureFlags {
if !checkIsFlagValid(flag) {
panic(fmt.Sprintf("Invalid argument: the `%s` feature flag is invalid. Only flags matching `^[a-z-]+$` are accepted.", flag))
}
@@
flagsArgs.WriteString(flag)
flagsArgs.WriteByte('"')
}
+ knownBindings, varsEnv, script := BuildProgram(statements...)
updatedScript := fmt.Sprintf("#![feature(%s)]\n%s", flagsArgs.String(), script)
return knownBindings, varsEnv, updatedScript
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| func BuildProgramWithFeatureFlags( | |
| featureFlags []string, | |
| statements ...Statement, | |
| ) (map[string]string, VarsEnv, string) { | |
| knownBindings, varsEnv, script := BuildProgram(statements...) | |
| var flagsArgs strings.Builder | |
| for index, flag := range featureFlags { | |
| if !checkIsFlagValid(flag) { | |
| // Yes, we are panicking instead of returning an error here. | |
| // That's desidered: flags are meant to be passed manually. Not computed, created conditionally, etc. | |
| // If a feature flag is wrong we want to crash the thing immediately instead of having the user handle that, log that, or whatever. | |
| panic(fmt.Sprintf("Invalid argument: the `%s` feature flag is invalid. Only flags matching `^[a-z-]+$` are accepted.", flag)) | |
| } | |
| func BuildProgramWithFeatureFlags( | |
| featureFlags []string, | |
| statements ...Statement, | |
| ) (map[string]string, VarsEnv, string) { | |
| var flagsArgs strings.Builder | |
| for index, flag := range featureFlags { | |
| if !checkIsFlagValid(flag) { | |
| // Yes, we are panicking instead of returning an error here. | |
| // That's desidered: flags are meant to be passed manually. Not computed, created conditionally, etc. | |
| // If a feature flag is wrong we want to crash the thing immediately instead of having the user handle that, log that, or whatever. | |
| panic(fmt.Sprintf("Invalid argument: the `%s` feature flag is invalid. Only flags matching `^[a-z-]+$` are accepted.", flag)) | |
| } | |
| flagsArgs.WriteString(flag) | |
| flagsArgs.WriteByte('"') | |
| } | |
| knownBindings, varsEnv, script := BuildProgram(statements...) | |
| updatedScript := fmt.Sprintf("#![feature(%s)]\n%s", flagsArgs.String(), script) | |
| return knownBindings, varsEnv, updatedScript | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@builder/builder.go` around lines 175 - 189, The BuildProgramWithFeatureFlags
function validates featureFlags after calling BuildProgram on the statements,
which allows side effects from BuildProgram to execute even when invalid flags
would cause a panic. Move the flag validation loop that calls checkIsFlagValid
before the call to BuildProgram to ensure invalid flags are caught immediately
and prevent any statement execution or side effects from occurring before the
panic.
No description provided.