Skip to content

Commit eda2fd0

Browse files
committed
fix: write wasm build artifact to the reported path
1 parent 8479abc commit eda2fd0

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

internal/service/build/service.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,26 @@ func (s *Service) Wasm(ctx context.Context, options WasmOptions) (WasmResult, er
8585
if err := s.fs.MkdirAll(outDir, 0o755); err != nil {
8686
return WasmResult{}, err
8787
}
88+
compileArtifact, err := filepath.Abs(artifact)
89+
if err != nil {
90+
return WasmResult{}, fmt.Errorf("resolve artifact path: %w", err)
91+
}
8892

8993
entry := defaultString(options.Entry, project.Entry)
9094
compiler := "go"
9195
if options.TinyGo {
9296
compiler = "tinygo"
9397
}
94-
if err := s.compile(ctx, compiler, options.Path, entry, artifact, options.Optimize); err != nil {
98+
if err := s.compile(ctx, compiler, options.Path, entry, compileArtifact, options.Optimize); err != nil {
9599
return WasmResult{}, err
96100
}
101+
exists, err := s.fs.Exists(artifact)
102+
if err != nil {
103+
return WasmResult{}, err
104+
}
105+
if !exists {
106+
return WasmResult{}, fmt.Errorf("wasm build reported success but artifact is missing: %s", artifact)
107+
}
97108

98109
files := []string{artifact}
99110
if !options.NoShim {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package build
2+
3+
import (
4+
"context"
5+
"io"
6+
"os"
7+
"path/filepath"
8+
"testing"
9+
10+
"github.com/paolo/flare-edge-cli/internal/domain/config"
11+
"github.com/paolo/flare-edge-cli/internal/infra/configstore"
12+
"github.com/paolo/flare-edge-cli/internal/infra/process"
13+
"github.com/paolo/flare-edge-cli/internal/infra/toolchain"
14+
"github.com/paolo/flare-edge-cli/internal/support/fs"
15+
)
16+
17+
type fakeRunner struct {
18+
last process.Command
19+
}
20+
21+
func (r *fakeRunner) Run(_ context.Context, cmd process.Command) (process.Result, error) {
22+
r.last = cmd
23+
24+
output := ""
25+
for index := 0; index+1 < len(cmd.Args); index++ {
26+
if cmd.Args[index] == "-o" {
27+
output = cmd.Args[index+1]
28+
break
29+
}
30+
}
31+
if output == "" {
32+
return process.Result{}, nil
33+
}
34+
if !filepath.IsAbs(output) {
35+
output = filepath.Join(cmd.Dir, output)
36+
}
37+
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil {
38+
return process.Result{}, err
39+
}
40+
if err := os.WriteFile(output, []byte("wasm"), 0o644); err != nil {
41+
return process.Result{}, err
42+
}
43+
return process.Result{}, nil
44+
}
45+
46+
func (r *fakeRunner) Stream(_ context.Context, _ process.Command, _, _ io.Writer) error {
47+
return nil
48+
}
49+
50+
func TestWasmUsesStableArtifactPathAndRequiresArtifact(t *testing.T) {
51+
tmpDir := t.TempDir()
52+
projectDir := filepath.Join(tmpDir, "services", "orchestrator")
53+
filesystem := fs.New()
54+
store := configstore.New(filesystem)
55+
56+
project := config.DefaultProject("orchestrator", "github.com/paolo/orchestrator", "main", "edge-http", config.DefaultCompatibilityDate, "")
57+
if err := store.SaveProject(projectDir, project); err != nil {
58+
t.Fatalf("save project: %v", err)
59+
}
60+
61+
runner := &fakeRunner{}
62+
service := NewService(store, filesystem, runner, &toolchain.GoToolchain{})
63+
64+
result, err := service.Wasm(context.Background(), WasmOptions{
65+
Path: projectDir,
66+
NoShim: true,
67+
})
68+
if err != nil {
69+
t.Fatalf("build wasm: %v", err)
70+
}
71+
72+
expectedArtifact := filepath.Join(projectDir, "dist", "app.wasm")
73+
if result.Artifact != expectedArtifact {
74+
t.Fatalf("unexpected artifact path: %q", result.Artifact)
75+
}
76+
outputIndex := -1
77+
for index := 0; index+1 < len(runner.last.Args); index++ {
78+
if runner.last.Args[index] == "-o" {
79+
outputIndex = index + 1
80+
break
81+
}
82+
}
83+
if outputIndex == -1 {
84+
t.Fatalf("expected -o flag in args: %v", runner.last.Args)
85+
}
86+
if !filepath.IsAbs(runner.last.Args[outputIndex]) {
87+
t.Fatalf("compiler output path should be absolute, got %q", runner.last.Args[outputIndex])
88+
}
89+
if runner.last.Args[outputIndex] != expectedArtifact {
90+
t.Fatalf("unexpected compiler output path: %q", runner.last.Args[outputIndex])
91+
}
92+
exists, err := filesystem.Exists(expectedArtifact)
93+
if err != nil {
94+
t.Fatalf("stat artifact: %v", err)
95+
}
96+
if !exists {
97+
t.Fatalf("expected artifact to exist at %s", expectedArtifact)
98+
}
99+
}

0 commit comments

Comments
 (0)