diff --git a/docs/content/docs/contributing/_index.md b/docs/content/docs/contributing/_index.md index e3501d2e..5f8efb58 100644 --- a/docs/content/docs/contributing/_index.md +++ b/docs/content/docs/contributing/_index.md @@ -136,11 +136,13 @@ forbidigo matches only the function a call names, so the checks that look at a c The structure test also finds a copy of an owner's code by its text, in `literals`. A string literal outside tests that holds `'\''`, the escaped quote a shell quoter writes, belongs in `internal/shell`, and one that holds `[0-9a-f]{14}`, the hash at the end of a project VM's name, belongs in `internal/vmname`. A quote written as a rune, such as `'\''` in Go, isn't a string literal, so it doesn't count. +The shared tools have one owner too, the table `image.SharedTools` in `internal/image/tool.go`. Each entry, an `image.ToolDef`, holds a tool's name, its kind (a toolchain or an agent), its members (the tools in the lock that its name stands for, such as pnpm and bun for node), the files in a project folder that call for it, and its installer, which is mise for every tool but rust, which rustup installs. `image.Toolchains`, `image.Agents`, `image.GroupTools`, the files that make devvm choose a toolchain for a new VM, the lock's check for downloads (`image.NoDownload`) and the `devcontainer.json` features that devvm maps to a toolchain all come from it. To add a shared tool, you add its entry, its members to `assets/shared-tools/mise.toml`, its policy as `assets/shared-tools/.policy`, and a section to its user page, `using/toolchains.md` or `using/coding-agents.md`. The [agreement tests](#agreement-tests) fail until the table and those files match. + A VM's state, `vm.json`, has one owner too, `internal/state`, which reads and writes the whole file and knows only its core fields. Every other package that keeps something of a VM's owns one section of it, with its own type, its name (`Section`), and an `Of` that reads it through `state.Section`: `internal/netpolicy` owns `net`, `internal/gitprotect` `git`, `internal/ports` `ports`, `internal/sshagent` `agent`, `internal/tools` `tools`, `internal/secrets` `secrets`, and `internal/vm` `size`, `clone`, `devcontainer`, `desktop` and `links` (`internal/vm/state.go`). A section's type keeps what the user asked for at its top level and what devvm made under `made`. A change goes through `state.VM.Update`, which takes the VM's lock, reads the file again, runs the change and writes the file once: the change sets its section with `state.SetSection` and works out a list's new value from the file as it is then, never from what the command read earlier, so a change another devvm made meanwhile is kept. A change that fails writes nothing. `Update` holds one VM's lock, so a change takes no other lock inside it, and a change to several VMs updates each in turn. `state.VM.Claim` is `Update` that also holds the claims lock, for what no two VMs may have (`ports.Allocator` chooses every host port through it), and `state.VM.Create` writes a new VM's whole state once, its ssh alias chosen under the claims lock. `internal/vm`'s `checkState` reads every section of the folder's VM's state as the VM is found, so a section that can't be read stops the command instead of reading as empty. A new section adds its check there. ### Agreement tests -When two places must agree but can't be one, a test checks that they agree. `TestEveryPartHasAPolicy` checks that every image part has its policy, `TestBuiltinPolicies` that every policy devvm ships parses, `TestEveryMessageIsUsed` that every message has a caller, `TestDocLinks` that every doc link is a message and a heading, and `TestReferenceIsCurrent` that the CLI reference matches the commands. A new pair of places that must agree gets such a test. +When two places must agree but can't be one, a test checks that they agree. `TestEveryPartHasAPolicy` checks that every image part has its policy, `TestSharedToolsPolicies` that the policies in `assets/shared-tools/` are one for each shared tool in `image.SharedTools`, `TestConfigTools` that `assets/shared-tools/mise.toml` lists each shared tool's members and nothing else, `TestBuiltinPolicies` that every policy devvm ships parses, `TestEveryMessageIsUsed` that every message has a caller, `TestDocLinks` that every doc link is a message and a heading, and `TestReferenceIsCurrent` that the CLI reference matches the commands. A new pair of places that must agree gets such a test. ### The consolidation pass diff --git a/internal/devcontainer/devcontainer.go b/internal/devcontainer/devcontainer.go index 79b29c40..d9fc827f 100644 --- a/internal/devcontainer/devcontainer.go +++ b/internal/devcontainer/devcontainer.go @@ -271,8 +271,8 @@ func (p *parser) value(key string, raw json.RawMessage, v any) error { } // features maps features to devvm's layers: devcontainers' own -// docker-in-docker to the docker layer, and their toolchains devvm has -// (node, python, rust, go) to its own. Their options are ignored; their +// docker-in-docker to the docker layer, and those named as one of devvm's +// toolchains (image.Toolchains) to it. Their options are ignored; their // scripts are never fetched or run. func (p *parser) features(raw json.RawMessage) error { var feats map[string]json.RawMessage diff --git a/internal/image/tool.go b/internal/image/tool.go index e0907d9d..54880ab1 100644 --- a/internal/image/tool.go +++ b/internal/image/tool.go @@ -18,31 +18,111 @@ import ( "github.com/w3cj/devvm/internal/msg" ) -// Toolchains are the shared toolchains a VM can have, in the fixed order -// a VM's list holds them (project.DetectFiles'). -var Toolchains = []string{"node", "python", "rust", "go"} +// ToolKind is what a shared tool is to a VM: a toolchain or a coding agent. +type ToolKind int -// Agents are the shared coding agents a VM can have, listed after its -// toolchains. -var Agents = []string{"claude", "codex", "opencode"} +const ( + // Toolchain is a language's tools: --tools chooses it, a project's + // files call for it, and a devcontainer.json's feature of its name + // (ghcr.io/devcontainers/features/) maps to it. + Toolchain ToolKind = iota + // Agent is a coding agent: --agents and devvm setup choose it. + Agent +) + +// Installer is what installs a shared tool in its image, in +// shared-tools/build.sh. +type Installer int + +const ( + // Mise installs it from the lock's downloads, whose checksums the lock + // records per platform. + Mise Installer = iota + // Rustup installs it: rustup, pinned in build.sh, checks the toolchain + // against its own signed manifest, so the lock has no download for it. + Rustup +) + +// ToolDef is a shared tool as devvm offers it: what a VM is given by its +// name. +type ToolDef struct { + // Name is the name a VM chooses it by, and its policy's file name, + // shared-tools/.policy. + Name string + Kind ToolKind + // Members are the tools in the lock and in shared-tools/mise.toml + // that its name stands for, itself first: node is node, pnpm and bun. + Members []string + // Detect are the files in a project folder's top level that call for + // it, in the order they're looked for: the first found is named. + Detect []string + // Installer is what installs it. + Installer Installer +} + +// SharedTools are the shared tools, in the fixed order a VM's list holds +// them: the toolchains, then the agents. Every list of shared tools +// derives from it: Toolchains, Agents, GroupTools, what a project's files +// call for, and the devcontainer features devvm maps. +var SharedTools = []ToolDef{ + {Name: "node", Kind: Toolchain, Members: []string{"node", "pnpm", "bun"}, Detect: []string{"package.json", "bun.lock", "bun.lockb"}}, + {Name: "python", Kind: Toolchain, Members: []string{"python", "uv"}, Detect: []string{"pyproject.toml", "requirements.txt", "uv.lock", "setup.py", "Pipfile"}}, + {Name: "rust", Kind: Toolchain, Members: []string{"rust"}, Detect: []string{"Cargo.toml"}, Installer: Rustup}, + {Name: "go", Kind: Toolchain, Members: []string{"go"}, Detect: []string{"go.mod"}}, + {Name: "claude", Kind: Agent, Members: []string{"claude"}}, + {Name: "codex", Kind: Agent, Members: []string{"codex"}}, + {Name: "opencode", Kind: Agent, Members: []string{"opencode"}}, +} + +// Toolchains are the shared toolchains' names, in SharedTools' order. +var Toolchains = namesOf(Toolchain) + +// Agents are the shared coding agents' names, in SharedTools' order, +// after the toolchains. +var Agents = namesOf(Agent) + +// namesOf is the names of SharedTools' tools of a kind, in its order. +func namesOf(k ToolKind) []string { + var out []string + for _, d := range SharedTools { + if d.Kind == k { + out = append(out, d.Name) + } + } + return out +} + +// Def returns the shared tool named name, false when none is. +func Def(name string) (ToolDef, bool) { + for _, d := range SharedTools { + if d.Name == name { + return d, true + } + } + return ToolDef{}, false +} // IsTool reports whether a name is a shared tool's (a toolchain or an // agent) rather than a layer's. func IsTool(name string) bool { - return slices.Contains(Toolchains, name) || slices.Contains(Agents, name) + _, ok := Def(name) + return ok } // IsAgent reports whether a name is an agent's. -func IsAgent(name string) bool { return slices.Contains(Agents, name) } +func IsAgent(name string) bool { + d, ok := Def(name) + return ok && d.Kind == Agent +} // SortTools returns shared tools' names in a VM's order, each once: the // toolchains in their fixed order, then the agents. Names that aren't // shared tools are left out. func SortTools(names []string) []string { var out []string - for _, t := range slices.Concat(Toolchains, Agents) { - if slices.Contains(names, t) { - out = append(out, t) + for _, d := range SharedTools { + if slices.Contains(names, d.Name) { + out = append(out, d.Name) } } return out @@ -59,18 +139,27 @@ func AgentsOf(names []string) []string { return out } -// GroupTools returns the tools in the lock behind a shared tool's name: -// node is node, pnpm and bun; python is python and uv. +// GroupTools returns the tools in the lock behind a shared tool's name, +// its Members: node is node, pnpm and bun; python is python and uv. Any +// other name stands for itself. func GroupTools(name string) []string { - switch name { - case "node": - return []string{"node", "pnpm", "bun"} - case "python": - return []string{"python", "uv"} + if d, ok := Def(name); ok { + return slices.Clone(d.Members) } return []string{name} } +// NoDownload reports whether a tool in the lock needs no download there, +// as its shared tool's installer fetches it: rust, which rustup installs. +func NoDownload(tool string) bool { + for _, d := range SharedTools { + if slices.Contains(d.Members, tool) { + return d.Installer != Mise + } + } + return false +} + // Normalize returns an image name's canonical form, so the same layers // always make one image: the layers bottom-up as given, each once, without // base, which every image is built on; base alone when there's nothing diff --git a/internal/image/tool_test.go b/internal/image/tool_test.go index f215452f..969236b4 100644 --- a/internal/image/tool_test.go +++ b/internal/image/tool_test.go @@ -2,13 +2,17 @@ package image import ( "errors" + "io/fs" "os" + "path" "path/filepath" "regexp" + "slices" "strings" "testing" "testing/fstest" + "github.com/w3cj/devvm/assets" "github.com/w3cj/devvm/internal/msg" ) @@ -214,4 +218,63 @@ func TestSortToolsAndGroups(t *testing.T) { if !IsTool("rust") || !IsTool("codex") || IsTool("pnpm") || IsTool("base") || !IsAgent("opencode") || IsAgent("node") { t.Error("IsTool or IsAgent is wrong") } + if got := GroupTools("nix"); !slices.Equal(got, []string{"nix"}) { + t.Errorf("GroupTools(nix) = %q", got) + } +} + +// The table's views: the toolchains, then the agents; rust alone needs +// no download in the lock. +func TestSharedTools(t *testing.T) { + if want := []string{"node", "python", "rust", "go"}; !slices.Equal(Toolchains, want) { + t.Errorf("Toolchains = %q, want %q", Toolchains, want) + } + if want := []string{"claude", "codex", "opencode"}; !slices.Equal(Agents, want) { + t.Errorf("Agents = %q, want %q", Agents, want) + } + for tool, want := range map[string]bool{"rust": true, "node": false, "pnpm": false, "claude": false, "nix": false} { + if got := NoDownload(tool); got != want { + t.Errorf("NoDownload(%s) = %v, want %v", tool, got, want) + } + } + if _, ok := Def("pnpm"); ok { + t.Error("Def(pnpm): a member isn't a shared tool") + } + seen := map[string]bool{} + for i, d := range SharedTools { + if i > 0 && d.Kind < SharedTools[i-1].Kind { + t.Errorf("%s: an agent before a toolchain", d.Name) + } + if len(d.Members) == 0 || d.Members[0] != d.Name { + t.Errorf("%s's members %q don't start with it", d.Name, d.Members) + } + if d.Kind == Agent && len(d.Detect) > 0 { + t.Errorf("%s: an agent with files that call for it", d.Name) + } + for _, m := range d.Members { + if seen[m] || !ToolName(m) { + t.Errorf("member %q: repeated or not a tool's name", m) + } + seen[m] = true + } + } +} + +// Each shared tool has its policy in shared-tools/, and every policy +// there is a shared tool's. +func TestSharedToolsPolicies(t *testing.T) { + files, err := fs.Glob(assets.FS, "shared-tools/*.policy") + if err != nil { + t.Fatal(err) + } + var got, want []string + for _, f := range files { + got = append(got, strings.TrimSuffix(path.Base(f), ".policy")) + } + for _, d := range SharedTools { + want = append(want, d.Name) + } + if slices.Sort(want); !slices.Equal(got, want) { + t.Errorf("shared-tools' policies are %q, want one per shared tool %q", got, want) + } } diff --git a/internal/project/project.go b/internal/project/project.go index b023be9d..3359aac1 100644 --- a/internal/project/project.go +++ b/internal/project/project.go @@ -17,6 +17,7 @@ import ( "unicode" "github.com/w3cj/devvm/internal/hostgit" + "github.com/w3cj/devvm/internal/image" "github.com/w3cj/devvm/internal/platform" "github.com/w3cj/devvm/internal/vmname" ) @@ -219,19 +220,26 @@ func physical(path string) string { } } -// markers are the files in a folder's top level that call for each -// toolchain, and for the nix layer, in the order Detect lists them: the -// same mix always gives the same image. -var markers = []struct { +// nixMarkers are a Nix project's own files, which call for the nix layer. +var nixMarkers = []string{"flake.nix", "shell.nix", "devenv.nix"} + +// marker is a part and the files in a folder's top level that call for it. +type marker struct { part string files []string -}{ - {"node", []string{"package.json", "bun.lock", "bun.lockb"}}, - {"python", []string{"pyproject.toml", "requirements.txt", "uv.lock", "setup.py", "Pipfile"}}, - {"rust", []string{"Cargo.toml"}}, - {"go", []string{"go.mod"}}, - // The nix layer, after the toolchains: a Nix project's own files. - {"nix", []string{"flake.nix", "shell.nix", "devenv.nix"}}, +} + +// markers are the files that call for each toolchain (image.SharedTools' +// Detect), then for the nix layer, in the order DetectFiles lists them: +// the same mix always gives the same image. +func markers() []marker { + var ms []marker + for _, d := range image.SharedTools { + if len(d.Detect) > 0 { + ms = append(ms, marker{d.Name, d.Detect}) + } + } + return append(ms, marker{image.NixLayer, nixMarkers}) } // nixShellMax is how much of a default.nix nixShell reads. @@ -262,7 +270,7 @@ type Found struct{ Name, File string } // for it: nix also for a default.nix that makes a shell. func DetectFiles(dir string) []Found { var found []Found - for _, m := range markers { + for _, m := range markers() { file := "" for _, f := range m.files { if _, err := os.Lstat(filepath.Join(dir, f)); err == nil { @@ -270,7 +278,7 @@ func DetectFiles(dir string) []Found { break } } - if file == "" && m.part == "nix" && nixShell(dir) { + if file == "" && m.part == image.NixLayer && nixShell(dir) { file = "default.nix" } if file != "" { diff --git a/internal/structure/structure_test.go b/internal/structure/structure_test.go index 2d83823b..5953945e 100644 --- a/internal/structure/structure_test.go +++ b/internal/structure/structure_test.go @@ -16,6 +16,7 @@ const ( ldflags = "set by -ldflags at build time" table = "a fixed table: set here, only read" fixture = "a test's fixed input: set here, only read" + derived = "a fixed table's view, worked out once from it, only read" ) // devvm is what the repo is held to. @@ -166,8 +167,9 @@ func devvmVars() map[string]string { "internal/backend/lima.sshLogin": table, "internal/cli.shells": table, "internal/gitprotect.Modes": table, - "internal/image.Toolchains": table, - "internal/image.Agents": table, + "internal/image.SharedTools": table, + "internal/image.Toolchains": derived, + "internal/image.Agents": derived, "internal/image.Builtins": table, "internal/backend/lima.conversionScripts": table, "internal/msg.docs": table, @@ -176,7 +178,7 @@ func devvmVars() map[string]string { "internal/msg/gen.initialisms": table, "internal/netpolicy.Env": table, "internal/netpolicy.directArgv": table, - "internal/project.markers": table, + "internal/project.nixMarkers": table, "internal/proxy.notPublic": table, "internal/proxy.nat64": table, "internal/proxy.githubMinting": table, diff --git a/internal/tools/lock.go b/internal/tools/lock.go index f2c71e98..9520668c 100644 --- a/internal/tools/lock.go +++ b/internal/tools/lock.go @@ -4,6 +4,8 @@ import ( "bufio" "bytes" "strings" + + "github.com/w3cj/devvm/internal/image" ) // Lock is a mise lock file as mise writes it: an entry per tool version, @@ -86,10 +88,10 @@ func (l Lock) VersionEntry(tool, version string) []byte { } // HasDownload reports whether a tool's first entry has a download (a URL, -// not a build from source) for every one of the platforms. Rust needs -// none: rustup installs it. +// not a build from source) for every one of the platforms. A tool whose +// installer isn't mise needs none (image.NoDownload): rustup installs rust. func (l Lock) HasDownload(tool string, platforms []string) bool { - if tool == "rust" { + if image.NoDownload(tool) { return true } ls := lines(l.Entry(tool)) diff --git a/internal/tools/tools_test.go b/internal/tools/tools_test.go index 83c96732..37e3e413 100644 --- a/internal/tools/tools_test.go +++ b/internal/tools/tools_test.go @@ -1494,11 +1494,16 @@ func TestMounts(t *testing.T) { } } -// shared-tools/mise.toml's tools, and the fixture lock has each. +// shared-tools/mise.toml's tools are the shared tools' members +// (image.SharedTools), each once, and the fixture lock has each. func TestConfigTools(t *testing.T) { got, _ := configTools(read(t, repo(t, "assets", "shared-tools", "mise.toml"))) - if want := []string{"node", "pnpm", "bun", "python", "uv", "go", "rust", "claude", "codex", "opencode"}; !slices.Equal(got, want) { - t.Errorf("configTools = %q, want %q", got, want) + var want []string + for _, d := range image.SharedTools { + want = append(want, d.Members...) + } + if slices.Sort(want); !slices.Equal(slices.Sorted(slices.Values(got)), want) { + t.Errorf("configTools = %q, want the shared tools' members %q", got, want) } if locked := lockTools(fixture(t)); !slices.Equal(slices.Sorted(slices.Values(got)), locked) { t.Errorf("the fixture lock has %q", locked) diff --git a/testdata/coverage/darwin.txt b/testdata/coverage/darwin.txt index e7de9ccd..78eee8af 100644 --- a/testdata/coverage/darwin.txt +++ b/testdata/coverage/darwin.txt @@ -16,7 +16,7 @@ internal/fetch 96.6 internal/from 91.3 internal/gitprotect 92.4 internal/hostgit 100.0 -internal/image 97.5 +internal/image 99.4 internal/jsonc 97.1 internal/msg 69.5 internal/msg/gen 93.3 @@ -24,7 +24,7 @@ internal/netpolicy 88.1 internal/paths 98.0 internal/platform 86.1 internal/ports 90.4 -internal/project 91.8 +internal/project 92.1 internal/proxy 90.1 internal/redact 96.0 internal/secrets 90.8