Skip to content

fix: preserve underscores in formula struct names - #177

Merged
luoliwoshang merged 1 commit into
xgo-dev:mainfrom
MeteorsLiu:fix/formula-struct-name-suffix
Sep 2, 2026
Merged

fix: preserve underscores in formula struct names#177
luoliwoshang merged 1 commit into
xgo-dev:mainfrom
MeteorsLiu:fix/formula-struct-name-suffix

Conversation

@MeteorsLiu

Copy link
Copy Markdown
Collaborator

Summary

  • derive the generated Formula struct name by trimming the full _llar.gox suffix
  • preserve underscores in names such as cpu_features
  • add a regression fixture and correct the classfile guidance

Testing

  • go test -ldflags="-checklinkname=0" ./internal/formula -count=1
  • go test -ldflags="-checklinkname=0" ./internal/modules -count=1
  • full ./... reaches the existing TestE2E_RealLibpngBuild Clang 21 failure, reproduced unchanged on goplus/main

@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@fennoai fennoai 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.

Review: preserve underscores in classfile struct name

The core change is correct and well-targeted. Switching from strings.Cut(filename, "_") to strings.TrimSuffix(filepath.Base(path), "_llar.gox") properly fixes the truncation of names like cpu_features -> cpu, matches xgo's own classfile naming convention, and using filepath.Base additionally handles paths with directory components. The doc comment, AGENTS.md, and a targeted regression test with testdata were added in sync.

On the removed validation (formula.go:125): dropping the old !ok error for filenames without _ is not a functional regression. If a name lacks the _llar.gox suffix, TrimSuffix returns it unchanged and the following interp.GetType (line 128) fails closed with a clear "struct name not found" error. The production caller (internal/modules/source.go:109) also enforces HasSuffix(path, defaultFormulaSuffix) before loading, so the missing-suffix case is effectively unreachable. Security and performance passes found no issues.

Follow-up (out of this PR's scope)

internal/modules/comparator.go:55 still derives its struct name with strings.Cut(filepath.Base(path), "_") — the exact pattern this PR replaced. A _cmp.gox file with an underscore in its name (e.g. cpu_features_cmp.gox) would resolve to struct cpu and fail GetType. Worth a follow-up applying the same TrimSuffix(..., "_cmp.gox") fix there.

Optional

  • Consider a small subtest for a valid, parseable file lacking the _llar.gox suffix to lock in the "GetType safety net" error behavior this change now relies on.
  • The _llar.gox literal is now hard-coded in several places (formula.go:125, plus defaultFormulaSuffix in source.go:23 and the Ext registration in ixgo/classfile.go:31). Sharing one constant would prevent drift.

Comment thread AGENTS.md
### Key Points

1. **Struct Name Derivation**: The struct name comes from `strings.Cut(filename, "_")` - the part before the first underscore
1. **Struct Name Derivation**: The struct name comes from `strings.TrimSuffix(filename, "_llar.gox")`, preserving underscores in names such as `cpu_features`

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.

[P3] Doc snippet drops filepath.Base(), now inconsistent with code

This quotes strings.TrimSuffix(filename, "_llar.gox"), but the actual code is strings.TrimSuffix(filepath.Base(path), "_llar.gox") (formula.go:125). The filepath.Base step is load-bearing: path can carry directory components, so the snippet as written wouldn't handle a nested path. Either match the code exactly or keep it conceptual ("the filename without the suffix") as line 60 does.

@luoliwoshang luoliwoshang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@luoliwoshang
luoliwoshang merged commit c852d23 into xgo-dev:main Sep 2, 2026
8 checks passed
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.

2 participants