Skip to content

Commit 4adc0b7

Browse files
authored
Cleanup Wasm runtime creation (#3755)
1 parent 2d8edc8 commit 4adc0b7

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

private/buf/bufcli/cache.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package bufcli
1616

1717
import (
18+
"context"
1819
"errors"
1920
"fmt"
2021
"io/fs"
@@ -37,6 +38,7 @@ import (
3738
"github.com/bufbuild/buf/private/pkg/filelock"
3839
"github.com/bufbuild/buf/private/pkg/normalpath"
3940
"github.com/bufbuild/buf/private/pkg/storage/storageos"
41+
"github.com/bufbuild/buf/private/pkg/wasm"
4042
)
4143

4244
var (
@@ -173,17 +175,21 @@ func NewPluginDataProvider(container appext.Container) (bufplugin.PluginDataProv
173175
)
174176
}
175177

176-
// CreateWasmRuntimeCacheDir creates the cache directory for the Wasm runtime.
177-
//
178-
// This is used by the Wasm runtime to cache compiled Wasm plugins. This is an
179-
// implementation specific cache and opaque outside of the runtime. The runtime
180-
// will manage the cache versioning itself within this directory.
181-
func CreateWasmRuntimeCacheDir(container appext.Container) (string, error) {
178+
// NewWasmRuntime returns a new Wasm runtime while creating the required cache
179+
// directories.
180+
func NewWasmRuntime(ctx context.Context, container appext.Container) (wasm.Runtime, error) {
181+
// This is used by the Wasm runtime to cache compiled Wasm plugins. This is an
182+
// implementation specific cache and opaque outside of the runtime. The runtime
183+
// will manage the cache versioning itself within this directory.
182184
if err := createCacheDir(container.CacheDirPath(), v3CacheWasmRuntimeRelDirPath); err != nil {
183-
return "", err
185+
return nil, err
184186
}
185187
fullCacheDirPath := normalpath.Join(container.CacheDirPath(), v3CacheWasmRuntimeRelDirPath)
186-
return fullCacheDirPath, nil
188+
wasmRuntime, err := wasm.NewRuntime(ctx, wasm.WithLocalCacheDir(fullCacheDirPath))
189+
if err != nil {
190+
return nil, err
191+
}
192+
return wasmRuntime, nil
187193
}
188194

189195
// NewWKTStore returns a new bufwktstore.Store while creating the required cache directories.

private/buf/cmd/buf/command/beta/lsp/lsp.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/bufbuild/buf/private/pkg/app/appcmd"
3030
"github.com/bufbuild/buf/private/pkg/app/appext"
3131
"github.com/bufbuild/buf/private/pkg/ioext"
32-
"github.com/bufbuild/buf/private/pkg/wasm"
3332
"github.com/spf13/pflag"
3433
"go.lsp.dev/jsonrpc2"
3534
)
@@ -101,11 +100,7 @@ func run(
101100
return err
102101
}
103102

104-
wasmRuntimeCacheDir, err := bufcli.CreateWasmRuntimeCacheDir(container)
105-
if err != nil {
106-
return err
107-
}
108-
wasmRuntime, err := wasm.NewRuntime(ctx, wasm.WithLocalCacheDir(wasmRuntimeCacheDir))
103+
wasmRuntime, err := bufcli.NewWasmRuntime(ctx, container)
109104
if err != nil {
110105
return err
111106
}

private/buf/cmd/buf/command/breaking/breaking.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,7 @@ func run(
176176
if err != nil {
177177
return err
178178
}
179-
wasmRuntimeCacheDir, err := bufcli.CreateWasmRuntimeCacheDir(container)
180-
if err != nil {
181-
return err
182-
}
183-
wasmRuntime, err := wasm.NewRuntime(ctx, wasm.WithLocalCacheDir(wasmRuntimeCacheDir))
179+
wasmRuntime, err := bufcli.NewWasmRuntime(ctx, container)
184180
if err != nil {
185181
return err
186182
}

private/buf/cmd/buf/command/config/internal/internal.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/bufbuild/buf/private/pkg/slicesext"
3232
"github.com/bufbuild/buf/private/pkg/stringutil"
3333
"github.com/bufbuild/buf/private/pkg/syserror"
34-
"github.com/bufbuild/buf/private/pkg/wasm"
3534
"github.com/spf13/pflag"
3635
)
3736

@@ -183,11 +182,7 @@ func lsRun(
183182
return err
184183
}
185184
}
186-
wasmRuntimeCacheDir, err := bufcli.CreateWasmRuntimeCacheDir(container)
187-
if err != nil {
188-
return err
189-
}
190-
wasmRuntime, err := wasm.NewRuntime(ctx, wasm.WithLocalCacheDir(wasmRuntimeCacheDir))
185+
wasmRuntime, err := bufcli.NewWasmRuntime(ctx, container)
191186
if err != nil {
192187
return err
193188
}

private/buf/cmd/buf/command/lint/lint.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/bufbuild/buf/private/pkg/app/appcmd"
2828
"github.com/bufbuild/buf/private/pkg/app/appext"
2929
"github.com/bufbuild/buf/private/pkg/stringutil"
30-
"github.com/bufbuild/buf/private/pkg/wasm"
3130
"github.com/spf13/pflag"
3231
)
3332

@@ -122,11 +121,7 @@ func run(
122121
if err != nil {
123122
return err
124123
}
125-
wasmRuntimeCacheDir, err := bufcli.CreateWasmRuntimeCacheDir(container)
126-
if err != nil {
127-
return err
128-
}
129-
wasmRuntime, err := wasm.NewRuntime(ctx, wasm.WithLocalCacheDir(wasmRuntimeCacheDir))
124+
wasmRuntime, err := bufcli.NewWasmRuntime(ctx, container)
130125
if err != nil {
131126
return err
132127
}

0 commit comments

Comments
 (0)