# Go

Build and test the Go modules of a repository alongside everything else.

The Go plugin lets Axle see the Go modules in a repository, so they build and test in the same run as everything else, in dependency order, from the cache when nothing changed. It's written in Go and runs with `go run`, so every repository it serves already has what it needs.

## Add it

```sh
axle add go
```

This writes it into the root `axle.toml`:

```toml
[plugins.go]
source = "npm:@axle-rs/plugin-go@0.1.0"
hash = "sha256-…"
```

It needs `go` 1.22 or newer on the `PATH`. The first time it starts, Go compiles it, which takes a second or two, and keeps it in Go's build cache; it uses only Go's standard library, so nothing is downloaded.

A repository with no JavaScript needs no `package.json`: the `axle.toml` at its root, the one that lists the plugin, is what Axle finds it by.

## What it finds

Each `go.mod` with a `module` line is a project, named after its module path, such as `example.com/shop/api`, and `axle list` shows them with the kind `plugin`. Files Git ignores are skipped, and so are folders named `vendor`, `testdata`, `node_modules` and `.git`, a module at the root of the repository and a folder that is already a package.

A module depends on another module of the repository when it `require`s its path and Go builds it from the repository rather than downloading it: when a `replace` in its `go.mod`, or in the `go.work` it belongs to, points at the other module's folder, or when the same `go.work` `use`s both. Its tasks then wait for that module's. A module belongs to the nearest `go.work` above it, if that file `use`s it, just as `go` decides.

## Versions and the cache

The plugin reads the versions each module uses from its `go.sum`, every module whose code Go checked, leaving out those it only read a `go.mod` of, and from the `go.work.sum` of its workspace. Axle hashes them like a JavaScript lockfile, so a new version of a dependency misses the cache of the modules that use it and no others. `go.sum` and `go.work.sum` themselves aren't among the files every task reads, and `--affected` counts a change to them against every module.

Each task reads its module's files, the files of the modules it depends on, and the `go.work` of its workspace. `GOFLAGS`, `GOOS`, `GOARCH`, `GOAMD64`, `GOARM`, `GOARM64`, `GOEXPERIMENT`, `GOTOOLCHAIN`, `GOWORK` and the `CGO_*` variables change a task's hash, and tasks also get `GOPATH`, `GOROOT`, `GOCACHE`, `GOMODCACHE`, `GOENV` and Go's proxy and checksum settings.

Axle caches each task's result and output, and no files: `go build ./...` checks that everything compiles without writing a program, and Go keeps what it compiled and the results of tests in its own cache, outside the repository. A task from the cache doesn't run Go at all.

The plugin's answers are kept until a `go.mod`, `go.sum`, `go.work`, `go.work.sum` or `.gitignore` changes.

## Tasks

Each module gets two tasks, run in its folder:

| Task    | Command          |
| ------- | ---------------- |
| `build` | `go build ./...` |
| `test`  | `go test ./...`  |

`tasks` in the plugin's settings adds tasks or changes them for every module. A task is the `go` command and its arguments, which the plugin runs after `go`, a table with that command in `run` and any [task settings](https://axlers.dev/docs/reference/configuration.md#tasks), or `false` to leave it out:

```toml
[plugins.go]
source = "npm:@axle-rs/plugin-go@0.1.0"
hash = "sha256-…"
tasks = { test = "test -race ./...", vet = "vet ./...", build = { cache = false } }
```

A project's own `axle.toml` and the root one change them as they would any task, and win over the plugin.

The tasks don't run in [batches](https://axlers.dev/docs/reference/plugins.md#batch-mode). Go's build cache is shared by every `go` command and safe to use from many at once, so one `go test` for several modules would save nothing over one for each.
