Skip to content

Commit 408de5a

Browse files
committed
fix: ensure TinyGo always compiles entire Go module directory
- Remove conditional main.go detection logic that was still causing single-file compilation - Always pass the module directory to TinyGo instead of individual files - Enables multi-file Go package compilation for WebAssembly components - Resolves undefined function errors in multi-file Go components
1 parent 1587d59 commit 408de5a

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

go/defs.bzl

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,9 @@ 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-
# If no main.go, compile the entire module directory
248-
# This allows TinyGo to see all Go files in the package
249-
tinygo_args.append(go_module_files.path)
235+
# Use current directory approach - change to module directory and compile "."
236+
# This allows TinyGo to discover all .go files in the package
237+
tinygo_args.append(".")
250238

251239
# Validate that we have Go source files
252240
go_files = [src for src in ctx.files.srcs if src.extension == "go"]
@@ -394,12 +382,26 @@ def _compile_tinygo_module(ctx, tinygo, go_binary, wasm_opt_binary, wasm_tools,
394382
"echo \"DEBUG: Resolved GOROOT=$GOROOT\"",
395383
"echo \"DEBUG: Resolved PATH=$PATH\"",
396384
"",
397-
"# Execute TinyGo with resolved paths",
385+
"# Change to Go module directory and execute TinyGo",
386+
"cd \"$EXECROOT/{}\"".format(go_module_files.path),
387+
"echo \"DEBUG: Changed to directory: $(pwd)\"",
388+
"echo \"DEBUG: Files in directory: $(ls -la)\"",
389+
"",
398390
])
399391

400-
# Add the TinyGo command with arguments
392+
# Add the TinyGo command with arguments, adjusting output path to be absolute
401393
tinygo_cmd = "\"$EXECROOT/{}\"".format(tinygo.path) if not tinygo.path.startswith("/") else "\"{}\"".format(tinygo.path)
402-
script_content.append(tinygo_cmd + " " + " ".join(["\"%s\"" % arg for arg in tinygo_args]))
394+
395+
# Adjust the output path to be absolute since we're changing directories
396+
adjusted_args = []
397+
for arg in tinygo_args:
398+
if arg == wasm_module.path:
399+
# Make output path absolute
400+
adjusted_args.append("\"$EXECROOT/{}\"".format(wasm_module.path))
401+
else:
402+
adjusted_args.append("\"%s\"" % arg)
403+
404+
script_content.append(tinygo_cmd + " " + " ".join(adjusted_args))
403405

404406
ctx.actions.write(
405407
output = wrapper_script,

0 commit comments

Comments
 (0)