Skip to content

Commit e8f5d25

Browse files
committed
fix: compile entire Go module directory in go_wasm_component rule
- Fixed go_wasm_component rule to always pass module directory to TinyGo - Previously only passed main.go when present, causing 'undefined function' errors - Now TinyGo can see all Go files in the same package - Added multi-file test example with main.go + utils.go - Resolves issue where functions in separate files weren't accessible This enables proper multi-file Go component development with TinyGo.
1 parent 29cf608 commit e8f5d25

File tree

4 files changed

+52
-22
lines changed

4 files changed

+52
-22
lines changed

examples/go_component/BUILD.bazel

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,16 @@ go_wasm_component(
9999
optimization = "debug",
100100
world = "simple-test",
101101
)
102+
103+
# Test multi-file Go component to verify the fix
104+
go_wasm_component(
105+
name = "multi_file_test",
106+
srcs = [
107+
"main.go",
108+
"utils.go",
109+
],
110+
adapter = "//wasm/adapters:wasi_snapshot_preview1",
111+
go_mod = "go.mod",
112+
optimization = "debug",
113+
world = "simple-test",
114+
)

examples/go_component/main.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
package main
22

3+
import "fmt"
4+
35
func main() {
4-
// Minimal working TinyGo WASI Preview 2 WebAssembly component
5-
// This demonstrates the complete TinyGo + Component Model pipeline working!
6+
// Test calling functions from other Go files in same package
7+
result := TestFunction("Multi-file Go component test")
8+
fmt.Println(result)
9+
10+
// Test mathematical operations
11+
sum := Add(5, 3)
12+
product := Multiply(4, 6)
13+
fmt.Printf("Add(5, 3) = %d\n", sum)
14+
fmt.Printf("Multiply(4, 6) = %d\n", product)
15+
16+
// This demonstrates the complete TinyGo + Component Model pipeline working with multiple Go files!
617
}

examples/go_component/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
// TestFunction demonstrates calling functions across Go files
6+
func TestFunction(message string) string {
7+
return fmt.Sprintf("Utils: %s", message)
8+
}
9+
10+
// Add calculates the sum of two integers
11+
func Add(a, b int) int {
12+
return a + b
13+
}
14+
15+
// Multiply calculates the product of two integers
16+
func Multiply(a, b int) int {
17+
return a * b
18+
}

go/defs.bzl

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,14 @@ def _compile_tinygo_module(ctx, tinygo, go_binary, wasm_opt_binary, wasm_tools,
232232
ctx.attr.world,
233233
])
234234

235-
# Find main Go file path within the module directory
236-
main_go_found = False
237-
main_go_path = None
238-
for src in ctx.files.srcs:
239-
if src.basename == "main.go":
240-
main_go_path = go_module_files.path + "/main.go"
241-
main_go_found = True
242-
break
243-
244-
if main_go_found:
245-
tinygo_args.append(main_go_path)
246-
else:
247-
# Check if there's at least one Go file
248-
go_files = [src for src in ctx.files.srcs if src.extension == "go"]
249-
if not go_files:
250-
fail("No Go source files found for %s" % ctx.attr.name)
251-
252-
# Fallback: compile the entire Go module directory
253-
tinygo_args.append(go_module_files.path)
254-
print("Warning: No main.go found for %s, compiling entire module directory" % ctx.attr.name)
235+
# Always compile the entire Go module directory to include all source files
236+
# This ensures TinyGo can see all Go files in the same package, not just main.go
237+
tinygo_args.append(go_module_files.path)
238+
239+
# Validate that we have Go source files
240+
go_files = [src for src in ctx.files.srcs if src.extension == "go"]
241+
if not go_files:
242+
fail("No Go source files found for %s" % ctx.attr.name)
255243

256244
# THE BAZEL WAY: Use Bazel's toolchain path resolution
257245
# Calculate TINYGOROOT from tinygo binary path dynamically

0 commit comments

Comments
 (0)