Skip to content

gomod.star

Read and raise the go directive in go.mod files.

load("@fleet//gomod.star", "modules", "go_version", "set_go_version", "requires", "outdated", "set_work_go_version")
Function What it does
modules Lists the repo's Go modules.
go_version Reads the version in a go.mod's go directive.
set_go_version Raises the go directive in every go.mod, never lowering it.
requires Reads every module a go.mod requires, in single-line and block form.
outdated Finds the modules that require a dependency below a version.
set_work_go_version Raises the go directive in go.work, never lowering it.

modules

modules(repo)

Lists the repo's Go modules.

Arguments

Name Description
repo The repo passed to change(repo).

Returns: The sorted paths of every go.mod outside vendor and testdata directories.

Example

modules(repo)
# returns ["go.mod", "tools/go.mod"]

go.mod

module acme/api

tools/go.mod

module acme/tools

vendor/x/go.mod

module x

Source

go_version

go_version(repo, path = "go.mod")

Reads the version in a go.mod's go directive.

Arguments

Name Description
repo The repo passed to change(repo).
path The go.mod to read.

Returns: The version, such as 1.22.1, or None when the file has no go directive or does not exist.

Example

go_version(repo)
# returns "1.22.1"

go.mod

module acme/api

go 1.22.1

Source

set_go_version

set_go_version(repo, version, paths = None)

Raises the go directive in every go.mod, never lowering it.

A go.mod without a directive gets one after its module line. A toolchain line the new version covers is removed; a newer one is kept.

Arguments

Name Description
repo The repo passed to change(repo).
version The Go version, such as 1.27.3.
paths The go.mod files to change; every module by default.

Returns: The list of paths that changed.

Example

set_go_version(repo, "1.27.3")
# returns ["go.mod"]

go.mod

module acme/api

go 1.22.1

toolchain go1.22.5

tools/go.mod

module acme/tools

go 1.28

go.mod

module acme/api

go 1.27.3

Used in: bump-go-version, make-go-version

Source

requires

requires(repo, path = "go.mod")

Reads every module a go.mod requires, in single-line and block form.

Arguments

Name Description
repo The repo passed to change(repo).
path The go.mod to read.

Returns: A dict of module path to version, in file order; empty when the file does not exist.

Example

requires(repo)
# returns {"golang.org/x/net": "v0.20.0", "github.com/spf13/cobra": "v1.8.0"}

go.mod

module acme/api

require (
    golang.org/x/net v0.20.0 // indirect
    github.com/spf13/cobra v1.8.0
)

Source

outdated

outdated(repo, module, version)

Finds the modules that require a dependency below a version.

Use it to pick where a run step should run go get module@version.

Arguments

Name Description
repo The repo passed to change(repo).
module The dependency's module path.
version The version it should be at least, such as v0.33.0.

Returns: The directories of the matching go.mod files; the repo root is ..

Example

outdated(repo, "golang.org/x/net", "v0.33.0")
# returns ["."]

go.mod

module acme/api

require golang.org/x/net v0.20.0

tools/go.mod

module acme/tools

require golang.org/x/net v0.40.0

Used in: go-module-bump

Source

set_work_go_version

set_work_go_version(repo, version)

Raises the go directive in go.work, never lowering it.

Arguments

Name Description
repo The repo passed to change(repo).
version The Go version, such as 1.27.3.

Returns: True when the file changed; False when the repo has no go.work.

Example

set_work_go_version(repo, "1.27.3")
# returns True

go.work

go 1.22

use (
    ./api
    ./worker
)

go.work

go 1.27.3

use (
    ./api
    ./worker
)

Source