fix: make code generation deterministic#13
Conversation
Fix non-deterministic map iteration in generateGo function that caused generated Go code to have different key ordering on each run. Changed from direct map iteration to using the existing sortMap function to ensure consistent ordering of ClassMapStr entries. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
WalkthroughThe Changes
Sequence Diagram(s)sequenceDiagram
participant generateGo
participant sortMap
participant Cache
generateGo->>Cache: Retrieve cache map
generateGo->>sortMap: Pass cache map for sorting
sortMap-->>generateGo: Return sorted keys and values
loop For each sorted key
generateGo->>generateGo: Access Generated and Merged fields
generateGo->>generateGo: Construct map entry
end
Possibly related PRs
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 (1.64.8)level=warning msg="[runner] Can't run linter goanalysis_metalinter: buildir: failed to load package : could not load export data: no export data for "github.com/conneroisu/twerge"" 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (9)
✅ Files skipped from review due to trivial changes (6)
🧰 Additional context used🧬 Code Graph Analysis (1)examples/simple/views/view_templ.go (1)
🪛 LanguageToolREADME.md[uncategorized] ~378-~378: Possible missing comma found. (AI_HYDRA_LEO_MISSING_COMMA) 🪛 markdownlint-cli2 (0.17.2)doc/src/examples/tailwind-build.md310-310: Hard tabs (MD010, no-hard-tabs) 313-313: Hard tabs (MD010, no-hard-tabs) 316-316: Hard tabs (MD010, no-hard-tabs) 317-317: Hard tabs (MD010, no-hard-tabs) 318-318: Hard tabs (MD010, no-hard-tabs) 321-321: Hard tabs (MD010, no-hard-tabs) 322-322: Hard tabs (MD010, no-hard-tabs) ⏰ Context from checks skipped due to timeout of 90000ms (1)
🔇 Additional comments (3)
✨ Finishing Touches
🧪 Generate Unit Tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
tw.go (1)
216-230: Consider edge case handling for non-standard Generated field formats.The
extractNumberfunction handles the common case well, but there's a potential edge case: if theGeneratedfield doesn't follow the expected "tw-{num}" format (e.g., contains no hyphens or non-numeric suffixes), it returns 0, which could group unexpected formats together at the beginning of the sort order.Consider adding a comment to document this behavior:
// extractNumber extracts the numeric part from "tw-{num}" +// Returns 0 for non-standard formats, grouping them at the beginning func extractNumber(generated string) int {Or alternatively, you could make the sorting more robust by falling back to string comparison for non-standard formats:
func extractNumber(generated string) int { // Split by "-" and get the last part parts := strings.Split(generated, "-") if len(parts) < 2 { - return 0 + return -1 // Sort non-standard formats before numbered ones } // Convert to int num, err := strconv.Atoi(parts[len(parts)-1]) if err != nil { - return 0 + return -1 // Sort non-numeric suffixes before numbered ones } return num }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
tw.go(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: checks
- GitHub Check: MacOS Build (aarch64-darwin)
🔇 Additional comments (2)
tw.go (2)
174-182: LGTM! Deterministic code generation achieved.The modification correctly addresses the non-deterministic map iteration issue by using
sortMap(g.Cache())to ensure consistent ordering. The implementation properly iterates over sorted keys and uses indexed access to the aligned values slice, which will produce identical output across multiple runs.
192-213: Well-implemented sorting function with proper alignment.The
sortMapfunction correctly:
- Extracts keys from the map
- Sorts them by numeric suffix using
extractNumber- Creates an aligned values slice maintaining the sorted order
- Returns both slices for consistent iteration
This approach ensures deterministic ordering while maintaining the relationship between keys and their corresponding values.
- Rename gen.go to build.go in dashboard and simple examples - Update all documentation references from gen.go to build.go - Update command examples in README files and documentation - Maintain consistent naming convention across examples 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary
generateGofunction that caused generated Go code to have different key ordering on each runsortMapfunction to ensure consistent ordering ofClassMapStrentriesTest plan
🤖 Generated with Claude Code
Summary by CodeRabbit
Refactor
New Features
Documentation
gen.gotobuild.go.New Component