Files
praetorian-inc-wasmforge/internal/build/asset_cache.go
T
michaelweber 108f19cb36 fix(build): bundle sysshim into build_assets for distribution mode (#4)
When wasmforge runs as a standalone binary with no source tree on disk,
injectSysshimReplace and injectPuregoSysshimReplace failed with
"cannot find wasmforge module root for sysshim injection" because all
four findModuleRoot() fallback paths returned "".

Three-part fix:
- cmd/gen-build-assets: add addSysshimDir (recursive walk) and include
  the internal/sysshim tree under the "sysshim/" prefix in the bundle.
  File count rises from ~399 to 442.
- internal/build/asset_cache.go: new file providing
  ensureAssetsExtracted(), a process-lifetime sync.Once cache that
  extracts build_assets.tar.gz to a temp dir on first call.
- internal/build/compiler.go: add sysshimSourceBase() helper that
  prefers the on-disk module root (dev mode) and falls back to the
  extracted asset cache (distribution mode); rewire both injection
  functions to use it instead of calling findModuleRoot() directly.
2026-06-16 17:04:34 -04:00

43 lines
1.3 KiB
Go

package build
import (
"fmt"
"os"
"sync"
)
var (
cachedAssetDirOnce sync.Once
cachedAssetDir string
cachedAssetDirErr error
)
// ensureAssetsExtracted returns a process-lifetime temp directory containing
// the extracted contents of the embedded build_assets bundle. The first call
// performs the extraction; subsequent calls return the cached path.
//
// The directory is not cleaned up — it lives under the OS temp directory
// (e.g. /tmp on Linux, /var/folders/... on macOS) where the OS reclaims it
// eventually. This avoids re-extracting on every sysshim injection and lets
// the directory survive across multiple build pipeline stages within a
// single wasmforge invocation.
//
// Callers should access well-known subtrees via their bundled prefixes:
// "hostmod", "runtime", "names", "wazero", "sysshim", and "go.sum".
func ensureAssetsExtracted() (string, error) {
cachedAssetDirOnce.Do(func() {
dir, err := os.MkdirTemp("", "wasmforge-assets-*")
if err != nil {
cachedAssetDirErr = fmt.Errorf("creating asset cache dir: %w", err)
return
}
if err := extractBuildAssets(dir); err != nil {
os.RemoveAll(dir)
cachedAssetDirErr = fmt.Errorf("extracting build assets: %w", err)
return
}
cachedAssetDir = dir
})
return cachedAssetDir, cachedAssetDirErr
}