From 02aecb4ffb98c225440c64a11be708303b04214a Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Wed, 18 Nov 2026 18:34:29 -0300 Subject: [PATCH] task: outside a repository that pins Task, the installed Task runs `task build` in spin-ami ran the system's Task, which has none of go-tools' commands on its PATH, and failed; the fix was to type `go tool task`, every time, in every repository. Installed as `task` ahead of the system's (GOBIN=~/.local/bin go install .../cmd/task@latest), this command now is the `task` a person types: in a repository with a versions.yaml it runs the pinned Task with the tools, as before; with no versions.yaml above, it execs the next `task` on the PATH that is not itself, with the arguments as given, so every other project keeps its own. Co-Authored-By: Claude Opus 5.5 --- cmd/task/main.go | 32 ++++++++++++++++++++++++++- cmd/task/main_test.go | 40 ++++++++++++++++++++++++++++++++++ site/src/content/docs/start.md | 10 +++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/cmd/task/main.go b/cmd/task/main.go index 03857fe..c032de2 100644 --- a/cmd/task/main.go +++ b/cmd/task/main.go @@ -49,7 +49,15 @@ func run(ctx context.Context, args []string, base string, exec execFunc) error { } file, err := find(wd) if err != nil { - return err + // Outside a repository that pins Task, the Task that is installed: so this command can be + // installed as `task` itself (go install .../cmd/task@latest into a directory ahead of the + // system's), and `task` runs the pinned one with the tools where a repository has a + // versions.yaml and the system's everywhere else. + other := elsewhere(os.Getenv("PATH")) + if other == "" { + return err + } + return exec(other, append([]string{other}, args...), os.Environ()) } v, err := versions.Load(file) if err != nil { @@ -89,6 +97,28 @@ func withPath(env []string, path string) []string { return append(out, "PATH="+path) } +// elsewhere is the first executable named task on path that is not this program; "" when there +// is none. +func elsewhere(path string) string { + self, err := os.Executable() + if err != nil { // mutate-exempt: the running program always has a path on the platforms this runs on + return "" + } + me, err := os.Stat(self) + if err != nil { // mutate-exempt: the program that is running is there to stat + return "" + } + for _, dir := range filepath.SplitList(path) { + p := filepath.Join(dir, "task") + fi, err := os.Stat(p) + if err != nil || fi.IsDir() || fi.Mode()&0o111 == 0 || os.SameFile(fi, me) { + continue + } + return p + } + return "" +} + // find is versions.yaml in dir or the nearest directory above it, as Task finds its Taskfile. func find(dir string) (string, error) { for { diff --git a/cmd/task/main_test.go b/cmd/task/main_test.go index a5ce03a..bf4e7c6 100644 --- a/cmd/task/main_test.go +++ b/cmd/task/main_test.go @@ -135,6 +135,8 @@ func TestTaskIsThePinnedTaskWithTheLatestToolsOnItsPath(t *testing.T) { // pins no Task, one that does not read, no go-tools release to be had. func TestWhatCannotRunTaskIsSaid(t *testing.T) { _, base := repository(t) + // No other Task to run in its place. + t.Setenv("PATH", t.TempDir()) for _, tc := range []struct { name, file, base, says string }{ @@ -167,3 +169,41 @@ func TestWhatCannotRunTaskIsSaid(t *testing.T) { } }) } + +// Where no versions.yaml is above, it is the next Task on the PATH that runs, as it was given - +// never this program again, which would be a loop - and with none, the reason it cannot run. +func TestOutsideARepositoryTheInstalledTaskRuns(t *testing.T) { + _, base := repository(t) + t.Chdir(t.TempDir()) + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + me, aDir, notTask, system := t.TempDir(), t.TempDir(), t.TempDir(), t.TempDir() + if err := os.Symlink(self, filepath.Join(me, "task")); err != nil { + t.Fatal(err) + } + for dir, mode := range map[string]os.FileMode{notTask: 0o644, system: 0o755} { + if err := os.WriteFile(filepath.Join(dir, "task"), []byte("#!/bin/sh\n"), mode); err != nil { + t.Fatal(err) + } + } + if err := os.Mkdir(filepath.Join(aDir, "task"), 0o755); err != nil { + t.Fatal(err) + } + t.Setenv("PATH", strings.Join([]string{filepath.Join(t.TempDir(), "none"), me, aDir, notTask, system}, string(os.PathListSeparator))) + var c call + if err := run(t.Context(), []string{"build", "X=1"}, base, c.exec); err != nil { + t.Fatal(err) + } + want := filepath.Join(system, "task") + if !c.called || c.path != want || !slices.Equal(c.argv, []string{want, "build", "X=1"}) { + t.Errorf("exec'd %v %q %v; want %s with the arguments as given", c.called, c.path, c.argv, want) + } + + t.Setenv("PATH", me) + c = call{} + if err := run(t.Context(), nil, base, c.exec); err == nil || !strings.Contains(err.Error(), "no versions.yaml here or above") || c.called { + t.Errorf("only this program on the PATH: err = %v, exec'd %v", err, c.called) + } +} diff --git a/site/src/content/docs/start.md b/site/src/content/docs/start.md index 6174333..cc4127e 100644 --- a/site/src/content/docs/start.md +++ b/site/src/content/docs/start.md @@ -36,6 +36,16 @@ user's cache - Task held to its pin, the tools to the sha256 their release's `ch latest is asked at most every ten minutes, and a machine that cannot ask goes on with the one it has. Task's and the tools' own modules - about 450 together - are in no `go.mod` and no CI cache. +To type `task` rather than `go tool task`, install the command as `task` in a directory that comes +before the system's on your `PATH`: + +```sh +GOBIN=~/.local/bin go install github.com/spin-stack/go-tools/cmd/task@latest +``` + +In a repository with a `versions.yaml` it runs the Task that file pins, with the tools; anywhere +else it runs the next `task` on the `PATH`, as if it were not there. + No repository bumps the tools: a release of go-tools is in every repository's next run. What a repository imports of go-tools as a library - `versions`, `provenance` - is `go.mod`'s. Each release is also tagged `v0..`, a module version, so Dependabot's `gomod` updates