From 258b5be36e64d61ecbc492945dcaf5460fbacad7 Mon Sep 17 00:00:00 2001 From: Andrew Steurer <94206073+asteurer@users.noreply.github.com> Date: Wed, 14 Jan 2026 14:31:29 -0600 Subject: [PATCH 1/2] feat(go): using the 'mod-name' flag results in a more-intuitive Go package structure Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com> --- crates/go/src/lib.rs | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/crates/go/src/lib.rs b/crates/go/src/lib.rs index 60140017e..9c16c49b8 100644 --- a/crates/go/src/lib.rs +++ b/crates/go/src/lib.rs @@ -105,10 +105,6 @@ pub struct Opts { /// (or "wit_component" if `None`). #[cfg_attr(feature = "clap", clap(long))] pub mod_name: Option, - - /// The package name used in the top-level Wasm exports file (or "main" if `None`). - #[cfg_attr(feature = "clap", clap(long))] - pub pkg_name: Option, } impl Opts { @@ -853,18 +849,33 @@ impl WorldGenerator for Go { .collect::>() .join("\n"); - // If a package name isn't "main", there isn't a need for a main function. - let (package_name, main_func) = if let Some(pkg) = self.opts.pkg_name.as_deref() { - (pkg, "") + let (exports_file_path, package_name, main_func) = if self.opts.mod_name.is_some() { + // If a module name is specified, the generated files will be used as a library. + ("wit_exports/wit_exports.go", "wit_exports", "") } else { - let func = r#"// Unused, but present to make the compiler happy + // If a module name is NOT specified, the generated files will be used as a + // standalone executable. + files.push( + "go.mod", + format!( + "module {}\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {}", + self.opts.mod_name.as_deref().unwrap_or("wit_component"), + REPLACEMENT_PKG + ) + .as_bytes(), + ); + + ( + "wit_exports.go", + "main", + r#"// Unused, but present to make the compiler happy func main() {} -"#; - ("main", func) +"#, + ) }; files.push( - "wit_exports.go", + exports_file_path, &maybe_gofmt( self.opts.format, format!( @@ -888,15 +899,6 @@ var {SYNC_EXPORT_PINNER} = runtime.Pinner{{}} .as_bytes(), ), ); - files.push( - "go.mod", - format!( - "module {}\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {}", - self.opts.mod_name.as_deref().unwrap_or("wit_component"), - REPLACEMENT_PKG - ) - .as_bytes(), - ); for (prefix, interfaces) in [("export_", &self.export_interfaces), ("", &self.interfaces)] { for (name, data) in interfaces { From 5985c37d171420e08a15110dcb5fc8206ceb83dd Mon Sep 17 00:00:00 2001 From: Andrew Steurer <94206073+asteurer@users.noreply.github.com> Date: Wed, 14 Jan 2026 14:50:45 -0600 Subject: [PATCH 2/2] fix: small refactor Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com> --- crates/go/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/go/src/lib.rs b/crates/go/src/lib.rs index 9c16c49b8..1ee1a8ddd 100644 --- a/crates/go/src/lib.rs +++ b/crates/go/src/lib.rs @@ -38,12 +38,6 @@ fn remote_pkg(name: &str) -> String { format!(r#""github.com/bytecodealliance/wit-bindgen/{name}""#) } -/// This is the literal location of the Go package. -const REPLACEMENT_PKG: &str = concat!( - "github.com/bytecodealliance/wit-bindgen/crates/go/src/package v", - env!("CARGO_PKG_VERSION") -); - #[derive(Default, Debug, Copy, Clone)] pub enum Format { #[default] @@ -853,18 +847,24 @@ impl WorldGenerator for Go { // If a module name is specified, the generated files will be used as a library. ("wit_exports/wit_exports.go", "wit_exports", "") } else { - // If a module name is NOT specified, the generated files will be used as a - // standalone executable. + // This is the literal location of the Go package. + let replacement_pkg = concat!( + "github.com/bytecodealliance/wit-bindgen/crates/go/src/package v", + env!("CARGO_PKG_VERSION") + ); + files.push( "go.mod", format!( "module {}\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {}", self.opts.mod_name.as_deref().unwrap_or("wit_component"), - REPLACEMENT_PKG + replacement_pkg ) .as_bytes(), ); + // If a module name is NOT specified, the generated files will be used as a + // standalone executable. ( "wit_exports.go", "main",