fix: preserve underscores in formula struct names - #177
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
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.goxsuffix to lock in the "GetType safety net" error behavior this change now relies on. - The
_llar.goxliteral is now hard-coded in several places (formula.go:125, plusdefaultFormulaSuffixinsource.go:23and theExtregistration inixgo/classfile.go:31). Sharing one constant would prevent drift.
| ### 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` |
There was a problem hiding this comment.
[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.
Summary
_llar.goxsuffixcpu_featuresTesting
go test -ldflags="-checklinkname=0" ./internal/formula -count=1go test -ldflags="-checklinkname=0" ./internal/modules -count=1./...reaches the existingTestE2E_RealLibpngBuildClang 21 failure, reproduced unchanged ongoplus/main