Skip to content

Commit 4efab38

Browse files
authored
add --export-pkg-name to Go options (#1572)
This allows the user to specify different packages for imports vs. exports, which can be useful when building libraries. This also includes some minor code and doc cleanups.
1 parent ffbf78c commit 4efab38

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

crates/go/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ The generated files will reference the following files in the [bytecodealliance/
4343
- defines an `Option` type as required by the WIT world
4444
- defines a `Result` type as required by the WIT world
4545
- defines a `Unit` type as required by the WIT world
46-
- defines a `StreamReader` and `StreamWriter` types as required by the WIT world
47-
- defines a `FutureReader` and `FutureWriter` types as required by the WIT world
46+
- defines `StreamReader` and `StreamWriter` types as required by the WIT world
47+
- defines `FutureReader` and `FutureWriter` types as required by the WIT world
4848
- `go.bytecodealliance.org/pkg/wit/async` (if needed): defines low-level functions for integrating the Go scheduler with the component model async ABI
4949

50-
5150
Note that async support currently requires [a patched version of
5251
Go](https://github.com/dicej/go/releases/tag/go1.25.5-wasi-on-idle). Code
5352
generated for worlds that don't use any async features can be compiled using a

crates/go/src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ pub struct Opts {
116116
#[cfg_attr(feature = "clap", clap(long))]
117117
pub pkg_name: Option<String>,
118118

119+
/// When `--pkg-name` is specified, optionally specify a different package
120+
/// for exports.
121+
///
122+
/// This allows you to put the exports and imports in separate packages when
123+
/// building a library. If only `--pkg-name` is specified, this will
124+
/// default to that value.
125+
#[cfg_attr(feature = "clap", clap(long, requires = "pkg_name"))]
126+
pub export_pkg_name: Option<String>,
127+
119128
/// Print the version of the remote package being used for the shared WIT types.
120129
///
121130
/// Must be specified in addition to the `pkg-name` flag.
@@ -210,8 +219,12 @@ struct Go {
210219

211220
impl Go {
212221
/// Adds the bindings module prefix to a package name.
213-
fn mod_pkg(&self, name: &str) -> String {
214-
let prefix = self.opts.pkg_name.as_deref().unwrap_or("wit_component");
222+
fn mod_pkg(&self, for_export: bool, name: &str) -> String {
223+
let prefix = for_export
224+
.then_some(())
225+
.and(self.opts.export_pkg_name.as_deref())
226+
.or(self.opts.pkg_name.as_deref())
227+
.unwrap_or("wit_component");
215228
format!(r#""{prefix}/{name}""#)
216229
}
217230

@@ -236,7 +249,7 @@ impl Go {
236249
package
237250
};
238251
let prefix = format!("{package}.");
239-
imports.insert(self.mod_pkg(&package));
252+
imports.insert(self.mod_pkg(exported, &package));
240253
prefix
241254
}
242255
}
@@ -883,16 +896,14 @@ impl WorldGenerator for Go {
883896
files.push(
884897
"go.mod",
885898
format!(
886-
r#"module {}
899+
r#"module wit_component
887900
888901
go 1.25
889902
890903
require (
891-
go.bytecodealliance.org/pkg {}
904+
go.bytecodealliance.org/pkg {REMOTE_PKG_VERSION}
892905
)
893906
"#,
894-
self.opts.pkg_name.as_deref().unwrap_or("wit_component"),
895-
REMOTE_PKG_VERSION,
896907
)
897908
.as_bytes(),
898909
);
@@ -1778,7 +1789,7 @@ for index := 0; index < int({length}); index++ {{
17781789
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => {
17791790
let args = operands.join(", ");
17801791
let call = format!("{package}.{name}({args})");
1781-
self.imports.insert(self.generator.mod_pkg(&package));
1792+
self.imports.insert(self.generator.mod_pkg(true, &package));
17821793
call
17831794
}
17841795
FunctionKind::Constructor(ty) => {
@@ -1789,7 +1800,7 @@ for index := 0; index < int({length}); index++ {{
17891800
.unwrap()
17901801
.to_upper_camel_case();
17911802
let call = format!("{package}.Make{ty}({args})");
1792-
self.imports.insert(self.generator.mod_pkg(&package));
1803+
self.imports.insert(self.generator.mod_pkg(true, &package));
17931804
call
17941805
}
17951806
FunctionKind::Method(_) | FunctionKind::AsyncMethod(_) => {

0 commit comments

Comments
 (0)