Skip to content

fix(v2): sort generated enums in enum-only packages#5786

Open
Saramanda9988 wants to merge 3 commits into
wailsapp:masterfrom
Saramanda9988:fix/v2-enum-ordering
Open

fix(v2): sort generated enums in enum-only packages#5786
Saramanda9988 wants to merge 3 commits into
wailsapp:masterfrom
Saramanda9988:fix/v2-enum-ordering

Conversation

@Saramanda9988

@Saramanda9988 Saramanda9988 commented Jul 15, 2026

Copy link
Copy Markdown

Description

Wails v2.13.0 sorts generated enums when their Go package also contains structs that need TypeScript model generation. However, enum-only packages are handled by a separate "outstanding enums" path that still iterates over a Go map directly.

Because Go map iteration order is unspecified, running binding generation multiple times can reorder otherwise unchanged enum declarations in models.ts, producing noisy source control diffs. Such as modals.ts

This change:

  • Sorts enum names before generating enums from enum-only packages.
  • Extracts the shared enum sorting and generation logic into addEnumsToGenerator, so packages with structs and enum-only packages use the same deterministic path.
  • Adds a regression test covering a package with enums but no generated structs.

This is a follow-up to #4664, which fixed enum ordering for packages that also contain generated structs.

Type of change

Please select the option that is relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration using wails doctor.

  • Windows
  • macOS
  • Linux

Test Configuration

# Wails
┌───────────────────┐
| Version | v2.13.0 |
└───────────────────┘

# System
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
| OS           | Windows 10 Home China   |
| Version      | 2009 (Build: 22631)   |
| ID           | 23H2   |
| Branding     | Windows 11 家庭中文版   |
| Go Version   | go1.25.7   |
| Platform     | windows   |
| Architecture | amd64   |
| CPU          | 13th Gen Intel(R) Core(TM) i7-13650HX   |
| GPU 1        | Virtual Display Driver (MikeTheTech) -Driver: 23.40.36.27   |
| GPU 2        | OrayIddDriver Device (Shanghai Best Oray Information Technology Co., Ltd.) - Driver: 17.50.19.949 |
| GPU 3        | NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA) - Driver: 32.0.15.9186   |
| Memory       | 32GB   |
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

# Dependencies
┌───────────────────────────────────────────────────────┐
| Dependency | Package Name | Status    | Version|
| WebView2   | N/A          | Installed | 150.0.4078.65|
| Nodejs     | N/A          | Installed | 22.18.0|
| npm        | N/A          | Installed | 11.4.2|
| *upx       | N/A          | Available ||
| *nsis      | N/A          | Installed | v3.11|
||
└─────────────── * - Optional Dependency ───────────────┘

# Diagnosis
Optional package(s) installation details: 
  - upx : Available at https://upx.github.io/
  

Checklist:

  • (v2 only) I have updated website/src/pages/changelog.mdx with details of this PR (v3 changelog entries are added automatically)
  • My code follows the general coding style of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Summary by CodeRabbit

  • Bug Fixes

    • Fixed generated enum ordering for packages containing only enums.
    • Enum declarations now appear in consistent alphabetical order across generated TypeScript output.
  • Tests

    • Added coverage to verify deterministic ordering and enum member generation.
  • Documentation

    • Updated the changelog with the enum ordering fix.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

GenerateModels now centralizes sorted, de-duplicated enum generation for both struct-containing and enum-only packages. New binding coverage verifies alphabetical enum output for enum-only packages, and the changelog documents the fix.

Changes

Enum generation

Layer / File(s) Summary
Shared enum generation helper
v2/internal/binding/binding.go
A shared helper sorts enum names, skips previously generated fully qualified names, and replaces duplicated generation loops.
Enum ordering coverage
v2/internal/binding/binding_test/*, website/src/pages/changelog.mdx
A new test verifies alphabetical ordering and numeric values for enum-only packages, and the unreleased changelog records the fix.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • wailsapp/wails#4664: Related deterministic enum ordering changes in the binding generation path.

Suggested reviewers: leaanthony

Poem

I sorted the enums, neat as can be,
A, M, then Z for all to see.
No duplicate names sneak through the gate,
The bindings now march in ordered state.
— A pleased little rabbit 🐇

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: deterministic sorting for enums in enum-only packages.
Description check ✅ Passed The description includes the change summary, context, testing details, and checklist, with only the explicit Fixes # issue line not filled in.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 modules listed in go.work or their 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Jul 15, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
v2/internal/binding/binding.go (1)

90-107: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Fix variable name typo and consider updating the seen slice.

There is a minor typo in the variable name fqemumname (should be fqenumname). Additionally, while the code checks if the enum is in the seen slice, it never adds the enum to seen after adding it to the generator. If the intention is to prevent duplicate processing of the same enum name across different iterations, you should add it to the seen slice.

♻️ Proposed refactor
-		fqemumname := packageName + "." + enumName
-		if seen.Contains(fqemumname) {
+		fqenumname := packageName + "." + enumName
+		if seen.Contains(fqenumname) {
 			continue
 		}
 		w.AddEnum(enum)
+		seen.Add(fqenumname)
🤖 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 `@v2/internal/binding/binding.go` around lines 90 - 107, In
addEnumsToGenerator, rename the fqemumname variable to fqenumname and, after
w.AddEnum(enum), record fqenumname in the seen StringSlicer so subsequent
iterations skip already-added enums.
🤖 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.

Nitpick comments:
In `@v2/internal/binding/binding.go`:
- Around line 90-107: In addEnumsToGenerator, rename the fqemumname variable to
fqenumname and, after w.AddEnum(enum), record fqenumname in the seen
StringSlicer so subsequent iterations skip already-added enums.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 0429ebf6-9171-44bf-922a-54225b54d1d7

📥 Commits

Reviewing files that changed from the base of the PR and between f681ae8 and 436c999.

📒 Files selected for processing (4)
  • v2/internal/binding/binding.go
  • v2/internal/binding/binding_test/binding_enum_ordering_test.go
  • v2/internal/binding/binding_test/binding_test.go
  • website/src/pages/changelog.mdx

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

Labels

bindings Documentation Improvements or additions to documentation v2-only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant