Skip to content

(BugFix) bindings: don't overwrite an existing go.mod - #88

Merged
asteurer merged 1 commit into
bytecodealliance:mainfrom
jfleitz:fix/bindings-preserve-go-mod
Nov 16, 2026
Merged

asteurer merged 1 commit into
bytecodealliance:mainfrom
jfleitz:fix/bindings-preserve-go-mod

Conversation

@jfleitz

@jfleitz jfleitz commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Problem

componentize-go bindings without --pkg-name (standalone bindings) writes the
go.mod that wit-bindgen-go generates, replacing whatever was there:

module wit_component

go 1.25

require (
    go.bytecodealliance.org/pkg <REMOTE_PKG_VERSION>
)

This means:

  • It can downgrade go.bytecodealliance.org/pkg. (ran into this with an existing go project):
    A project pinned pkg v0.4.1 to get the cabi_realloc GC fix, but regenerated bindings with componentize-go
    0.4.3, which rewrote go.mod to v0.4.0. go mod tidy kept it, and the
    component kept trapping on multi-MiB values:

    runtime.netpoll
    runtime.startTheWorldWithSema
    runtime.gcStart
    ...
    cabi_realloc
    Caused by:
        wasm trap: cannot leave component instance
    

The project's go.mod had 0.4.1 before the bindings step, and the downgrade happened behind the scenes.
It only showed up when we checked which pkg version was linked into the .wasm.

  • It drops the user's go directive, other require lines, replace and
    tool directives, and comments on every run.

Change

When a go.mod already exists in the output directory, keep it and change only what the generated code depends on:

  • pkg requirement: if go.bytecodealliance.org/pkg is missing or lower than the version the bindings were generated for (wit_bindgen_go::remote_pkg_version()), raise it to that version. A newer requirement is never lowered.
  • module path: set it to the generated one (wit_component), because the standalone bindings import their own packages through that path.
    • Everything else is left as is. When there's no go.mod, the generated one is written exactly as before. When something changes, bindings prints one line saying what (suppressed by --quiet).
    • Versions are compared as semver with the v prefix stripped, using the existing semver dependency, so Go pseudo-versions such as v0.2.4-0.20260911130647-2495ff7eca86 sort below v0.4.1 as Go orders them.
    • The --pkg-name hint now reads "make sure your 'go.mod' file requires at least: …", so it no longer suggests replacing a newer pin.
  • examples updated:
    • examples/sdk/pkg and examples/sdk/component now require go.bytecodealliance.org/pkg v0.4.1 instead of v0.4.0, so the one example that commits a pkg requirement starts from the version with the cabi_realloc GC fix.
    • The other examples and test fixtures generate their go.mod (it's git-ignored), so this change covers them.

Testing

  • New unit tests in cmd_bindings.rs: raising a requirement in a require (...) block and in a single-line require, raising from a pseudo-version, never lowering a newer version, leaving an equal version alone, adding a missing requirement while keeping tool lines, ignoring replace directives and module paths that only share a prefix, and setting the module path.
  • cargo test --lib: 15 passed. cargo test -p tests: 7 passed, including fixture_memory_pressure. cargo clippy and cargo fmt --check are clean.
  • Manual: regenerated standalone bindings over an existing go.mod requiring pkg v0.4.0 with module example.com/worker, go 1.27.1 and a tool line. The result has module wit_component, pkg v0.4.1, and the go and tool lines preserved, and the component builds and links pkg@v0.4.1. A go.mod pinning v0.5.0 was left untouched.

Signed-off-by: Jeremy Fleitz jeremy@cosmonic.com

Signed-off-by: Jeremy Fleitz <jeremy@cosmonic.com>

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

Thanks @jfleitz!

@asteurer
asteurer merged commit 42611fd into bytecodealliance:main Nov 16, 2026
5 checks passed
@jfleitz

jfleitz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Hey @asteurer @dicej ! Can you create a new release for componentize-go, so that we have the fix from this PR in it?

@asteurer

Copy link
Copy Markdown
Contributor

Happily! I'll cut a release right now. Thanks for pinging me.

@jfleitz
jfleitz deleted the fix/bindings-preserve-go-mod branch November 24, 2026 14:07
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