Files
w3cjandClaude Opus 5.5 b6954e1e11 refactor(vmname): a VM's name is a vmname.VM, parsed where it's read
vmname.VM is the name of every machine devvm runs on a backend: a
project VM, an image's machine (devvm-img-<key>), a build VM and a
remake's aside. Fields, parameters, results and map keys that held a VM's
name as a string now hold a VM, from the backend interface down to the
CLI's --json structs, whose output is byte for byte the same.

Boundary parsers, the only places a string becomes a VM:
- vmname.Parse: a project VM's name (state's vm.json name, export file
  names, devvm _links's argument, the forwarder's -vm, project.Nth)
- vmname.ParseListed: one path element, for what a backend lists
- VM.UnmarshalText: JSON (Lima's list, proxy config and events, links'
  targets in state, link lists); empty stays no VM
- vmname.Image(key), VM.Aside(), and typed constants for Lima's build VMs
- msg.M.VM: the VM a message's function took, read back

vmname's functions on names became methods (Hash, Slug, Short, Aside,
AsideHash, ImageKey, IsProject); Lima's image prefix moved to vmname;
vmname.Join joins VM lists. Messages' vm and vm_name fields are typed
by the generator. Seven messages whose field took something else became
what= (backend_lacks, backend_unreviewed, backend_unreviewed_used,
starting, deleting, configuring, running_script) and rm_vm_failed's
name=: the text is unchanged, the names-mode scripts follow.
Review.RequireFor names an image or the tools being built.

Typed sites per package (added lines with vmname.VM, non-test): msg 347
(generated), vm 124, lima 48, backendtest 32, vmssh 31, netpolicy 22,
backend 16, gitprotect 14, proxy 11, cli 11, project 10, state 8,
ports 6, paths 3, sshagent 2, sshconfig 2, tools 2.

The structure test's owners rule now fails on a field, parameter or
result named for a VM (vm, vms, otherVM, vmName) typed string, []string
or map[string]…, and on converting anything but a literal to vmname.VM
outside the files it lists; internal/stubtest is free. depguard holds
internal/vmname to the standard library, and the proxy may import it.

ParseListed and UnmarshalText refuse a name starting with -, which would read as a flag in limactl's argv, and vmname.Image refuses a key that's empty, holds a / or NUL, or makes a project VM's name, so Lima's ImageSize and DeleteImage no longer check it themselves. The check also fails on a *string VM name and an alias of vmname.VM, and says what it doesn't see.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 19:36:28 -07:00

99 lines
3.0 KiB
Go

package vm
import (
"fmt"
"os"
"sync"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/platform"
"github.com/devvmhq/devvm/internal/state"
"github.com/devvmhq/devvm/internal/vmname"
)
// holdCreate takes the creation lock of vm's project folder
// (paths.Dirs.CreateLock), by the hash vm's name ends with, and refuses
// while another devvm holds it (create_running), at once: it never waits
// on that devvm's questions. Up holds it from when it finds the folder has
// no VM, or one whose creation didn't finish, until the new VM's creation
// is recorded (creation.created); devvm rm holds it while it deletes a VM
// (holdForRm), and devvm gc --orphans while it forgets one
// (Leftovers.Forget). release deletes the lock file, then lets
// go of it, so no file of the VM's stays outside its folder; it may be
// called more than once.
func holdCreate(e *Env, vm vmname.VM) (release func(), err error) {
path, err := createLock(e, vm)
if err != nil {
return nil, err
}
if err := state.PrivateDir(e.Dirs.Run()); err != nil {
return nil, err
}
unlock, ok, err := platform.TryLockFile(path)
switch {
case err != nil:
return nil, err
case !ok:
return nil, msg.CreateRunning(vm)
}
return sync.OnceFunc(func() {
_ = os.Remove(path)
unlock()
}), nil
}
// createLock is the creation lock of vm's project folder, by the hash
// vm's name ends with.
func createLock(e *Env, vm vmname.VM) (string, error) {
h, ok := vm.Hash()
if !ok {
return "", fmt.Errorf("invalid VM name %q", vm)
}
return e.Dirs.CreateLock(h), nil
}
// dropCreateLock deletes the creation lock of vm's project folder that a
// devvm killed while it held it left, for the VM's teardown; one a devvm
// holds stays, and its holder deletes it.
func dropCreateLock(e *Env, vm vmname.VM) {
path, err := createLock(e, vm)
if err != nil {
return
}
if _, err := os.Lstat(path); err != nil {
return
}
if unlock, ok, err := platform.TryLockFile(path); err == nil && ok {
_ = os.Remove(path)
unlock()
}
}
// holdForRm is holdCreate for t, for devvm rm: a VM whose creation
// didn't finish may be one another devvm is making, and one whose
// machine the backend lost may be being made again. Under the lock it
// reads t's state again: one that changed since t was found was made
// meanwhile, and is refused (create_running) as one still being made is.
func holdForRm(e *Env, t *target) (release func(), err error) {
if release, err = holdCreate(e, t.f.Name); err != nil {
return nil, err
}
st, err := state.ReadVM(e.Dirs, t.f.Name)
switch {
case err != nil:
release()
return nil, err
case !sameCreation(st, t.st):
release()
return nil, msg.CreateRunning(t.f.Name)
}
return release, nil
}
// sameCreation reports whether a and b, a folder's VM's state read twice,
// are the one creation at the one point: made (Create) as the one VM, and
// both recorded as made (created), or neither.
func sameCreation(a, b *state.VM) bool {
return a.Exists() == b.Exists() && a.ID == b.ID && a.Created.Equal(b.Created)
}