11use crate :: { Compile , LanguageMethods , Runner , Verify } ;
22use anyhow:: Result ;
3- use std:: borrow:: Cow ;
43use std:: env;
54use std:: fs;
65use std:: path:: { Path , PathBuf } ;
76use std:: process:: Command ;
8- use wasm_encoder:: { Encode as _, Section as _} ;
9- use wit_component:: StringEncoding ;
107
118pub struct Go ;
129
@@ -52,54 +49,24 @@ impl LanguageMethods for Go {
5249 }
5350
5451 fn compile ( & self , runner : & Runner < ' _ > , compile : & Compile < ' _ > ) -> Result < ( ) > {
55- enum CliVersion {
56- P2 ,
57- P3 ,
58- }
59-
6052 let output = compile. output . with_extension ( "core.wasm" ) ;
6153
62- // As of this writing, Go-generated modules are not compatible with the
63- // `wasi_snapshot_preview1.command` adapter (we get unbounded recursion
64- // during initialization), we we must use the
65- // `wasi_snapshot_preview1.reactor` one instead. For `runner.go`, that
66- // means we must export `wasi:cli/run` explicitly and thus embed an
67- // extra component type custom section to cover that. For non-async
68- // tests, that will be `wasi:cli/run@0.2.6#run`, but for async ones it
69- // will be `wasi:cli/run@0.3.0-rc-2025-09-16#run`.
70-
71- let cli_version = if let Some ( name @ "runner.go" ) =
72- compile. component . path . file_name ( ) . unwrap ( ) . to_str ( )
73- {
74- let runner = fs:: read_to_string ( & compile. component . path ) ?;
75- fs:: write ( compile. bindings_dir . join ( name) , runner. as_bytes ( ) ) ?;
76- Some (
77- if runner. contains ( "//go:wasmexport wasi:cli/run@0.2.6#run" ) {
78- CliVersion :: P2
79- } else {
80- CliVersion :: P3
81- } ,
82- )
83- } else {
84- // Tests which involve importing and/or exporting more than one
85- // interface may require more than one file since we can't define
86- // more than one package in a single file in Go (AFAICT). Here we
87- // search for files related to `compile.component.path` based on a
88- // made-up naming convention. For example, if the filename is
89- // `test.go`, then we'll also include `${prefix}+test.go` for any
90- // value of `${prefix}`.
91- for path in all_paths ( & compile. component . path ) ? {
92- let test = fs:: read_to_string ( & path) ?;
93- let package_name = package_name ( & test) ;
94- let package_dir = compile. bindings_dir . join ( package_name) ;
95- fs:: create_dir_all ( & package_dir) ?;
96- fs:: write (
97- & package_dir. join ( path. file_name ( ) . unwrap ( ) ) ,
98- test. as_bytes ( ) ,
99- ) ?;
100- }
101- None
102- } ;
54+ // Tests which involve importing and/or exporting more than one
55+ // interface may require more than one file since we can't define more
56+ // than one package in a single file in Go (AFAICT). Here we search for
57+ // files related to `compile.component.path` based on a made-up naming
58+ // convention. For example, if the filename is `test.go`, then we'll
59+ // also include `${prefix}+test.go` for any value of `${prefix}`.
60+ for path in all_paths ( & compile. component . path ) ? {
61+ let test = fs:: read_to_string ( & path) ?;
62+ let package_name = package_name ( & test) ;
63+ let package_dir = compile. bindings_dir . join ( package_name) ;
64+ fs:: create_dir_all ( & package_dir) ?;
65+ fs:: write (
66+ & package_dir. join ( path. file_name ( ) . unwrap ( ) ) ,
67+ test. as_bytes ( ) ,
68+ ) ?;
69+ }
10370
10471 runner. run_command (
10572 Command :: new ( "go" )
@@ -113,57 +80,6 @@ impl LanguageMethods for Go {
11380 . arg ( "-ldflags=-checklinkname=0" ) ,
11481 ) ?;
11582
116- if let Some ( version) = cli_version {
117- let mut resolve = wit_parser:: Resolve :: default ( ) ;
118- let pkg = resolve. push_str (
119- "run.wit" ,
120- match version {
121- CliVersion :: P2 => {
122- "package wasi:cli@0.2.6;
123-
124- interface run {
125- run: func() -> result;
126- }
127-
128- world command {
129- export run;
130- }"
131- }
132- CliVersion :: P3 => {
133- "package wasi:cli@0.3.0-rc-2025-09-16;
134-
135- interface run {
136- run: async func() -> result;
137- }
138-
139- world command {
140- export run;
141- }"
142- }
143- } ,
144- ) ?;
145- let cli = resolve. select_world ( & [ pkg] , Some ( & "command" ) ) ?;
146-
147- let ( pkg, _) = resolve. push_path ( & compile. component . bindgen . wit_path ) ?;
148- let test = resolve. select_world ( & [ pkg] , Some ( & compile. component . bindgen . world ) ) ?;
149-
150- let mut module = fs:: read ( & output) ?;
151- for ( world, name) in [
152- ( cli, "component-type-wasi-cli" ) ,
153- ( test, "component-type-test" ) ,
154- ] {
155- let encoded =
156- wit_component:: metadata:: encode ( & resolve, world, StringEncoding :: UTF8 , None ) ?;
157- let section = wasm_encoder:: CustomSection {
158- name : Cow :: Borrowed ( name) ,
159- data : Cow :: Borrowed ( & encoded) ,
160- } ;
161- module. push ( section. id ( ) ) ;
162- section. encode ( & mut module) ;
163- }
164- fs:: write ( & output, & module) ?;
165- }
166-
16783 runner. convert_p1_to_component ( & output, compile) ?;
16884
16985 Ok ( ( ) )
0 commit comments