Files
w3cjandClaude Opus 5.5 20f97b10eb feat(vm): a new VM's tools resolve while its image builds
A first run resolved the tools' versions, downloading devvm's own mise
first, only after base had built: 10 s on the mini, 5 s on fedora, in
series. Neither needs the other, so create now starts Versions.Lock in
the background as an image starts to build (resolveWhile's start, which
build.Env.Building calls once an image is found unbuilt and its features
are chosen) and waits for it after, cancelling it when the build fails.
When every image is built already, nothing starts in the background, and
Files resolves in the foreground with its status lines as before.

While the build runs, the resolve says nothing, as the build has the
terminal: a held Reporter keeps its messages, shows no status line and
refuses every question as without a terminal, and its commands' lines
in the run log are tagged resolve. Once the build ends, done releases
it: what it held is said, what it does from then on shows, and devvm
waits under resolving_tools when it's still running. done returns its
error, so a failure is said once and the launch doesn't resolve again.
A VM whose lock is already there reads it at once.

TestHeld, TestResolveShowsAfterBuild and TestBuildingCalled fail when
held shows a status line, when the wait shows none, and when Building
isn't called. Docs: images.md's Versions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 22:57:15 -07:00

201 lines
7.0 KiB
Go

package vm
import (
"context"
"io/fs"
"os"
"path/filepath"
"slices"
"sync"
"syscall"
"testing"
"time"
"github.com/devvmhq/devvm/assets"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/platform"
"github.com/devvmhq/devvm/internal/tools"
)
// resolveWhile reads the tools lock in the background and its done waits
// for it, stopped first or not; with no tools it does nothing. Both end,
// so a launch never leaves it running.
func TestResolveWhile(t *testing.T) {
d := paths.Dirs{Data: t.TempDir(), Config: t.TempDir()}
config, err := fs.ReadFile(assets.FS, "shared-tools/mise.toml")
if err != nil {
t.Fatal(err)
}
lock, err := os.ReadFile(filepath.Join("..", "..", "testdata", "mise.lock"))
if err != nil {
t.Fatal(err)
}
for name, data := range map[string][]byte{d.ToolLock(): lock, d.ToolLockConfig(): config} {
if err := os.WriteFile(name, data, 0o600); err != nil {
t.Fatal(err)
}
}
var said []string
env := tools.New(d, assets.FS, "0.1.0-test", &msg.Fake{OnInfo: func(m msg.M) { said = append(said, m.ID()) }}, platform.Host{})
v := tools.Versions{Env: env}
start, done := resolveWhile(t.Context(), v, false)
start()
if err := done(false); err != nil {
t.Error(err)
}
for _, stop := range []bool{false, true} {
start, done := resolveWhile(t.Context(), v, true)
start()
start() // once, however many images build
if err := done(stop); err != nil {
t.Error(err)
}
}
// Nothing built, so nothing started: done has nothing to wait for,
// and the launch resolves in the foreground (Files).
if _, done := resolveWhile(t.Context(), v, true); done(false) != nil {
t.Error("done without a start failed")
}
if len(said) != 0 {
t.Errorf("said %q: the lock was there, so nothing resolved", said)
}
}
// held keeps what's said until release, in order, shows no status line
// and asks nothing, refusing each question as without a terminal; once
// released, everything goes through.
func TestHeld(t *testing.T) {
var said, showed []string
asked := false
h := &held{Reporter: &msg.Fake{
OnInfo: func(m msg.M) { said = append(said, "info "+m.ID()) },
OnWarn: func(m msg.M) { said = append(said, "warn "+m.ID()) },
OnStatus: func(m msg.M) { showed = append(showed, m.ID()) },
OnAsk: func(msg.M) bool { asked = true; return true },
Terminal: true,
}}
h.Info(msg.ResolvingTools())
h.Warn(msg.ToolsNeedResolve())
h.Status(msg.DownloadingMise("1"))()
q := msg.ResolvingTools().AnsweredBy("--yes")
if yes, err := h.Ask(q); yes || err == nil || asked {
t.Errorf("Ask while held = %v, %v, asked %v: want it refused", yes, err, asked)
}
if _, err := h.Confirm(q); err == nil || asked {
t.Errorf("Confirm while held = %v, asked %v: want it refused", err, asked)
}
if _, err := h.Answer(q); err == nil || asked {
t.Errorf("Answer while held = %v, asked %v: want it refused", err, asked)
}
if len(said) != 0 || len(showed) != 0 {
t.Fatalf("while held: said %q, showed %q", said, showed)
}
h.release()
if want := []string{"info resolving_tools", "warn tools_need_resolve"}; !slices.Equal(said, want) {
t.Errorf("released: said %q, want %q", said, want)
}
h.Info(msg.ResolvingTools())
h.Status(msg.DownloadingMise("1"))()
if yes, err := h.Ask(q); !yes || err != nil {
t.Errorf("Ask once released = %v, %v", yes, err)
}
if len(said) != 3 || !slices.Equal(showed, []string{"downloading_mise"}) {
t.Errorf("once released: said %q, showed %q", said, showed)
}
}
// A resolve still running when base's build ends, or when base was built
// already and the build was quick, shows what it does from then on, under
// a status line of its own while devvm waits for it.
func TestResolveShowsAfterBuild(t *testing.T) {
d := paths.Dirs{Data: t.TempDir(), Config: t.TempDir()}
fifo := filepath.Join(t.TempDir(), "go")
if err := syscall.Mkfifo(fifo, 0o600); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var showed []string
r := &msg.Fake{OnStatus: func(m msg.M) {
mu.Lock()
showed = append(showed, m.ID())
mu.Unlock()
if m.ID() == "resolving_tools" {
// The stub mise goes on once devvm shows it waits.
go func() {
if f, err := os.OpenFile(fifo, os.O_WRONLY, 0); err == nil { //nolint:gosec // the test's own fifo
_ = f.Close()
}
}()
}
}}
env := tools.New(d, assets.FS, "0.1.0-test", r, platform.Host{})
env.Mise = filepath.Join(t.TempDir(), "mise")
if err := os.WriteFile(env.Mise, []byte("#!/bin/sh\nread x < "+fifo+"\nexit 1\n"), 0o700); err != nil { //nolint:gosec // a program: the resolve runs it
t.Fatal(err)
}
// A resolve that never shows its wait ends with the test's deadline
// rather than hang.
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
defer cancel()
start, done := resolveWhile(ctx, tools.Versions{Env: env}, true)
start()
if err := done(false); err == nil || ctx.Err() != nil {
t.Errorf("done = %v, context %v: want the resolve's failure", err, ctx.Err())
}
mu.Lock()
defer mu.Unlock()
if !slices.Contains(showed, "resolving_tools") {
t.Errorf("showed %q while devvm waited, want resolving_tools", showed)
}
}
// A resolve in the background says nothing while base builds: no status
// line, and what it says waits for done, which says it once with its
// failure; a resolve still running when done stops it ends at once.
func TestResolveWhileQuiet(t *testing.T) {
for _, tc := range []struct {
name string
mise string // its script
stop bool
wantErr bool
want []string // said, once done
}{
{name: "fails", mise: "exit 1", wantErr: true, want: []string{"resolving_tools"}},
{name: "stopped while it runs", mise: "exec sleep 600", stop: true, want: []string{"resolving_tools"}},
} {
t.Run(tc.name, func(t *testing.T) {
d := paths.Dirs{Data: t.TempDir(), Config: t.TempDir()}
var mu sync.Mutex
var said, showed []string
r := &msg.Fake{
OnInfo: func(m msg.M) { mu.Lock(); said = append(said, m.ID()); mu.Unlock() },
OnStatus: func(m msg.M) { mu.Lock(); showed = append(showed, m.ID()); mu.Unlock() },
}
env := tools.New(d, assets.FS, "0.1.0-test", r, platform.Host{})
env.Mise = filepath.Join(t.TempDir(), "mise")
if err := os.WriteFile(env.Mise, []byte("#!/bin/sh\n"+tc.mise+"\n"), 0o700); err != nil { //nolint:gosec // a program: the resolve runs it
t.Fatal(err)
}
start, done := resolveWhile(t.Context(), tools.Versions{Env: env}, true)
start()
mu.Lock()
early, earlyShowed := slices.Clone(said), slices.Clone(showed)
mu.Unlock()
err := done(tc.stop)
if (err != nil) != tc.wantErr {
t.Errorf("done = %v, want an error %v", err, tc.wantErr)
}
mu.Lock()
defer mu.Unlock()
if len(early) != 0 || len(earlyShowed) != 0 || !slices.Equal(said, tc.want) {
t.Errorf("said %q and showed %q before done, %q after; want %q after alone", early, earlyShowed, said, tc.want)
}
// Stopped, it shows nothing; waited for, only its own wait.
if slices.ContainsFunc(showed, func(s string) bool { return tc.stop || s != "resolving_tools" }) {
t.Errorf("showed %q", showed)
}
})
}
}