check_go_version¶
rhiza_hooks.check_go_version
¶
Check that the Go version is consistent across project files.
A Go project states its version in up to three places:
go.mod— thegodirective, the minimum language version the module requires;go.mod— the optionaltoolchaindirective, the toolchain thegocommand switches to for this module;.go-version— the toolchain pin honoured by goenv andactions/setup-go.
The hook enforces the three relationships between them: the toolchain
directive may not be below the go directive (the go command itself rejects
that), .go-version may not be below the go directive (the pinned
toolchain could not build the module), and .go-version must name the same
version as the toolchain directive when both are present.
Values that are not dotted-numeric (toolchain default, toolchain local)
carry no version number, so they are accepted without comparison. A leading
go prefix (go1.22.5) is stripped before parsing.
check_version_consistency(repo_root)
¶
Check Go version consistency across project files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
Path
|
Root directory of the repository. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of error messages (empty if consistent, or if the repository |
list[str]
|
declares no Go versions at all). |
Source code in rhiza_hooks/check_go_version.py
get_go_mod_directives(repo_root)
¶
Read the go and toolchain directives from the repository's go.mod.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
Path
|
Root directory of the repository. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Mapping of directive name to value; empty when |
dict[str, str]
|
unreadable. |
Source code in rhiza_hooks/check_go_version.py
get_go_version_file(repo_root)
¶
Read the toolchain pin from .go-version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_root
|
Path
|
Root directory of the repository. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The normalized version string, or None if the file is missing, |
str | None
|
unreadable, or empty. |
Source code in rhiza_hooks/check_go_version.py
main(argv=None)
¶
Main entry point for the hook.
Source code in rhiza_hooks/check_go_version.py
parse_go_mod(text)
¶
Extract the go and toolchain directives from go.mod text.
Parenthesised blocks (require ( … )) are skipped so their contents
can never be mistaken for a top-level directive. A directive repeated at top
level — which the go command rejects anyway — keeps its last occurrence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Full contents of a |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Mapping of directive name to its normalized value, containing only the |
dict[str, str]
|
directives actually present. |