Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions cmd/common/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const makefileName = "Makefile"
var defaultWasmOutput = filepath.Join("wasm", "workflow.wasm")

// getBuildCmd returns a single step that builds the workflow and returns the WASM bytes.
func getBuildCmd(workflowRootFolder, mainFile, language string) (func() ([]byte, error), error) {
// If stripSymbols is true, debug symbols are stripped from the binary to reduce size.
func getBuildCmd(workflowRootFolder, mainFile, language string, stripSymbols bool) (func() ([]byte, error), error) {
tmpPath := filepath.Join(workflowRootFolder, ".cre_build_tmp.wasm")
switch language {
case constants.WorkflowLanguageTypeScript:
Expand All @@ -33,11 +34,15 @@ func getBuildCmd(workflowRootFolder, mainFile, language string) (func() ([]byte,
}, nil
case constants.WorkflowLanguageGolang:
// Build the package (.) so all .go files (main.go, workflow.go, etc.) are compiled together
ldflags := "-buildid="
if stripSymbols {
ldflags = "-buildid= -w -s"
}
cmd := exec.Command(
"go", "build",
"-o", tmpPath,
"-trimpath",
"-ldflags=-buildid= -w -s",
"-ldflags="+ldflags,
".",
)
cmd.Dir = workflowRootFolder
Expand Down Expand Up @@ -68,11 +73,15 @@ func getBuildCmd(workflowRootFolder, mainFile, language string) (func() ([]byte,
}, nil
default:
// Build the package (.) so all .go files are compiled together
ldflags := "-buildid="
if stripSymbols {
ldflags = "-buildid= -w -s"
}
cmd := exec.Command(
"go", "build",
"-o", tmpPath,
"-trimpath",
"-ldflags=-buildid= -w -s",
"-ldflags="+ldflags,
".",
)
cmd.Dir = workflowRootFolder
Expand All @@ -90,8 +99,10 @@ func getBuildCmd(workflowRootFolder, mainFile, language string) (func() ([]byte,
}

// CompileWorkflowToWasm compiles the workflow at workflowPath and returns the WASM binary.
// It runs the sequence of commands from getBuildCmds (make build + copy for WASM, or single build for Go/TS), then reads the temp WASM file.
func CompileWorkflowToWasm(workflowPath string) ([]byte, error) {
// If stripSymbols is true, debug symbols are stripped to reduce binary size (used for deploy).
// If false, debug symbols are kept for better error messages (used for simulate).
// For custom builds (WASM language with Makefile), stripSymbols has no effect.
func CompileWorkflowToWasm(workflowPath string, stripSymbols bool) ([]byte, error) {
workflowRootFolder, workflowMainFile, err := WorkflowPathRootAndMain(workflowPath)
if err != nil {
return nil, fmt.Errorf("workflow path: %w", err)
Expand Down Expand Up @@ -122,7 +133,7 @@ func CompileWorkflowToWasm(workflowPath string) ([]byte, error) {
return nil, fmt.Errorf("unsupported workflow language for file %s", workflowMainFile)
}

buildStep, err := getBuildCmd(workflowRootFolder, workflowMainFile, language)
buildStep, err := getBuildCmd(workflowRootFolder, workflowMainFile, language, stripSymbols)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/common/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ func TestFindMakefileRoot(t *testing.T) {
func TestCompileWorkflowToWasm_Go_Success(t *testing.T) {
t.Run("basic_workflow", func(t *testing.T) {
path := deployTestdataPath("basic_workflow", "main.go")
wasm, err := CompileWorkflowToWasm(path)
wasm, err := CompileWorkflowToWasm(path, true)
require.NoError(t, err)
assert.NotEmpty(t, wasm)
})

t.Run("configless_workflow", func(t *testing.T) {
path := deployTestdataPath("configless_workflow", "main.go")
wasm, err := CompileWorkflowToWasm(path)
wasm, err := CompileWorkflowToWasm(path, true)
require.NoError(t, err)
assert.NotEmpty(t, wasm)
})

t.Run("missing_go_mod", func(t *testing.T) {
path := deployTestdataPath("missing_go_mod", "main.go")
wasm, err := CompileWorkflowToWasm(path)
wasm, err := CompileWorkflowToWasm(path, true)
require.NoError(t, err)
assert.NotEmpty(t, wasm)
})
}

func TestCompileWorkflowToWasm_Go_Malformed_Fails(t *testing.T) {
path := deployTestdataPath("malformed_workflow", "main.go")
_, err := CompileWorkflowToWasm(path)
_, err := CompileWorkflowToWasm(path, true)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to compile workflow")
assert.Contains(t, err.Error(), "undefined: sdk.RemovedFunctionThatFailsCompilation")
Expand All @@ -73,7 +73,7 @@ func TestCompileWorkflowToWasm_Wasm_Success(t *testing.T) {
_ = os.Remove(wasmPath)
t.Cleanup(func() { _ = os.Remove(wasmPath) })

wasm, err := CompileWorkflowToWasm(wasmPath)
wasm, err := CompileWorkflowToWasm(wasmPath, true)
require.NoError(t, err)
assert.NotEmpty(t, wasm)

Expand All @@ -89,14 +89,14 @@ func TestCompileWorkflowToWasm_Wasm_Fails(t *testing.T) {
wasmPath := filepath.Join(wasmDir, "workflow.wasm")
require.NoError(t, os.WriteFile(wasmPath, []byte("not really wasm"), 0600))

_, err := CompileWorkflowToWasm(wasmPath)
_, err := CompileWorkflowToWasm(wasmPath, true)
require.Error(t, err)
assert.Contains(t, err.Error(), "no Makefile found")
})

t.Run("make_build_fails", func(t *testing.T) {
path := deployTestdataPath("wasm_make_fails", "wasm", "workflow.wasm")
_, err := CompileWorkflowToWasm(path)
_, err := CompileWorkflowToWasm(path, true)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to compile workflow")
assert.Contains(t, err.Error(), "build output:")
Expand All @@ -120,7 +120,7 @@ func TestCompileWorkflowToWasm_TS_Success(t *testing.T) {
if err := install.Run(); err != nil {
t.Skipf("bun install failed (network or cre-sdk): %v", err)
}
wasm, err := CompileWorkflowToWasm(mainPath)
wasm, err := CompileWorkflowToWasm(mainPath, true)
if err != nil {
t.Skipf("TS compile failed (published cre-sdk may lack full layout): %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/workflow/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func execute(workflowFolder, outputPath string) error {
outputPath = cmdcommon.EnsureWasmExtension(outputPath)

ui.Dim("Compiling workflow...")
wasmBytes, err := cmdcommon.CompileWorkflowToWasm(resolvedPath)
wasmBytes, err := cmdcommon.CompileWorkflowToWasm(resolvedPath, true)
if err != nil {
ui.Error("Build failed:")
return fmt.Errorf("failed to compile workflow: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/workflow/deploy/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (h *handler) Compile() error {
h.runtimeContext.Workflow.Language = cmdcommon.GetWorkflowLanguage(workflowMainFile)
}

wasmFile, err = cmdcommon.CompileWorkflowToWasm(resolvedWorkflowPath)
wasmFile, err = cmdcommon.CompileWorkflowToWasm(resolvedWorkflowPath, true)
if err != nil {
ui.Error("Build failed:")
return fmt.Errorf("failed to compile workflow: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/workflow/deploy/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ func outputPathWithExtensions(path string) string {
// file content equals CompileWorkflowToWasm(workflowPath) + brotli + base64.
func assertCompileOutputMatchesUnderlying(t *testing.T, simulatedEnvironment *chainsim.SimulatedEnvironment, inputs Inputs, ownerType string) {
t.Helper()
wasm, err := cmdcommon.CompileWorkflowToWasm(inputs.WorkflowPath)
wasm, err := cmdcommon.CompileWorkflowToWasm(inputs.WorkflowPath, true)
require.NoError(t, err)
compressed, err := cmdcommon.CompressBrotli(wasm)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/workflow/simulate/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (h *handler) Execute(inputs Inputs) error {

spinner := ui.NewSpinner()
spinner.Start("Compiling workflow...")
wasmFileBinary, err = cmdcommon.CompileWorkflowToWasm(resolvedWorkflowPath)
wasmFileBinary, err = cmdcommon.CompileWorkflowToWasm(resolvedWorkflowPath, false)
spinner.Stop()
if err != nil {
ui.Error("Build failed:")
Expand Down
Loading