feat(golang): update go command completions for modern toolchain

Refresh the golang plugin completion definitions to better match current
Go command usage.

Update shared build flags from `go help build`, replacing obsolete
toolchain-era descriptions and adding modern flags such as `-C`, `-msan`,
`-asan`, `-cover`, `-covermode`, `-coverpkg`, `-asmflags`, `-buildmode`,
`-buildvcs`, `-json`, `-linkshared`, `-mod`, `-modcacherw`, `-modfile`,
`-overlay`, `-pgo`, `-pkgdir`, `-trimpath`, and `-toolexec`. Add value
completion for enum-like flags including `-buildmode`, `-buildvcs`,
`-compiler`, and `-mod`.

Update `go get` completion for its current module-focused behavior by
adding `-t`, `-u`, `-u=patch`, `-tool`, and package argument completion
while continuing to reuse the shared build flags.

Add `go version` argument completion for `-m`, `-v`, and file operands.

Add top-level `go work` support, including completion for `edit`, `init`,
`sync`, `use`, `vendor`, and `help`. Add command-specific flags and
operands for workspace editing, module directory arguments, recursive use,
and vendor output options.

Also remove duplicate `-v` definitions from `go build` and `go install`
now that `-v` is part of the shared build flags.
This commit is contained in:
刘睿华 2026-07-09 10:14:36 +08:00 committed by GitHub
commit 035c76561f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -81,6 +81,7 @@ _go() {
'tool[run specified go tool]' 'tool[run specified go tool]'
'version[print Go version]' 'version[print Go version]'
'vet[run go tool vet on packages]' 'vet[run go tool vet on packages]'
'work[workspace maintenance]'
) )
if (( CURRENT == 2 )); then if (( CURRENT == 2 )); then
# explain go commands # explain go commands
@ -88,19 +89,38 @@ _go() {
return return
fi fi
build_flags=( build_flags=(
'-a[force reinstallation of packages that are already up to date]' '-C[change to dir before running the command]:directory:_path_files -/'
'-a[force rebuilding of packages that are already up-to-date]'
'-n[print the commands but do not run them]' '-n[print the commands but do not run them]'
'-p[number of parallel builds]:number' '-p[number of parallel builds]:number'
'-race[enable data race detection]' '-race[enable data race detection]'
'-x[print the commands]' '-msan[enable interoperation with memory sanitizer]'
'-asan[enable interoperation with address sanitizer]'
'-cover[enable code coverage instrumentation]'
'-covermode[set the mode for coverage analysis]:mode:(set count atomic)'
'-coverpkg[apply coverage analysis to matching packages]:pattern'
'-v[print the names of packages as they are compiled]'
'-work[print temporary directory name and keep it]' '-work[print temporary directory name and keep it]'
'-ccflags[flags for 5c/6c/8c]:flags' '-x[print the commands]'
'-gcflags[flags for 5g/6g/8g]:flags' '-asmflags[arguments to pass on each go tool asm invocation]:flags'
'-ldflags[flags for 5l/6l/8l]:flags' '-buildmode[build mode to use]:mode:(archive c-archive c-shared default shared exe pie plugin)'
'-gccgoflags[flags for gccgo]:flags' '-buildvcs[stamp binaries with version control information]:mode:(true false auto)'
'-compiler[name of compiler to use]:name' '-compiler[name of compiler to use]:name:(gccgo gc)'
'-installsuffix[suffix to add to package directory]:suffix' '-gccgoflags[arguments to pass on each gccgo compiler/linker invocation]:flags'
'-gcflags[arguments to pass on each go tool compile invocation]:flags'
'-installsuffix[suffix to use in the package installation directory]:suffix'
'-json[emit build output in JSON suitable for automated processing]'
'-ldflags[arguments to pass on each go tool link invocation]:flags'
'-linkshared[build code that will be linked against shared libraries]'
'-mod[module download mode to use]:mode:(readonly vendor mod)'
'-modcacherw[leave newly-created directories in the module cache read-write]'
'-modfile[read and write an alternate go.mod file]:file:_files -g "*.mod"'
'-overlay[read a JSON config file that provides an overlay for build operations]:file:_files -g "*.json"'
'-pgo[specify the file path of a profile for profile-guided optimization]:file:_files'
'-pkgdir[install and load all packages from dir]:directory:_path_files -/'
'-tags[list of build tags to consider satisfied]:tags' '-tags[list of build tags to consider satisfied]:tags'
'-trimpath[remove all file system paths from the resulting executable]'
'-toolexec[program to use to invoke toolchain programs]:command'
) )
case ${words[2]} in case ${words[2]} in
@ -125,17 +145,20 @@ _go() {
;; ;;
install) install)
_arguments -s -w : ${build_flags[@]} \ _arguments -s -w : ${build_flags[@]} \
"-v[show package names]" \
'*:importpaths:__go_packages' '*:importpaths:__go_packages'
;; ;;
get) get)
_arguments -s -w : \ _arguments -s -w : \
${build_flags[@]} ${build_flags[@]} \
"-t[consider modules needed to build tests of packages]" \
"-u[update modules providing dependencies to newer minor or patch releases]" \
"-u=[update modules providing dependencies to patch releases]:mode:(patch)" \
"-tool[add a matching tool line to go.mod for each listed package]" \
"*:importpaths:__go_packages"
;; ;;
build) build)
_arguments -s -w : \ _arguments -s -w : \
${build_flags[@]} \ ${build_flags[@]} \
"-v[show package names]" \
"-o[output file]:file:_files" \ "-o[output file]:file:_files" \
"*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }" "*:args:{ _alternative ':importpaths:__go_packages' ':files:_path_files -g \"*.go\"' }"
;; ;;
@ -238,6 +261,59 @@ _go() {
;; ;;
esac esac
;; ;;
work)
local -a work_commands
work_commands+=(
'edit[edit go.work from tools or scripts]'
'init[initialize workspace file]'
'sync[sync workspace build list to modules]'
'use[add modules to workspace file]'
'vendor[make vendored copy of dependencies]'
)
if (( CURRENT == 3 )); then
_values 'go work commands' ${work_commands[@]} "help[display help]"
return
fi
case ${words[3]} in
help)
_values 'go work commands' ${work_commands[@]}
;;
edit)
_arguments -s -w : \
"-fmt[reformat the go.work file]" \
"-godebug[=key=value add a godebug key=value line]:key=value" \
"-dropgodebug[=key drop an existing godebug line]:key" \
"-use[=path add a use directive]:directory:_path_files -/" \
"-dropuse[=path drop a use directive]:directory:_path_files -/" \
"-replace[=old{@v}=new{@v} add a replacement of the given module path and version pair]:name" \
"-dropreplace[=old{@v} drop a replacement of the given module path and version pair]:name" \
"-go[={version} set the expected Go language version]:number" \
"-toolchain[=name set the Go toolchain to use]:name" \
"-print[print the final go.work in its text format]" \
"-json[print the final go.work file in JSON format]" \
"1:go.work file:_files -g 'go.work'"
;;
init)
_arguments -s -w : \
"*:module directories:_path_files -/"
;;
sync)
;;
use)
_arguments -s -w : \
"-r[search recursively for modules in the argument directories]" \
"*:module directories:_path_files -/"
;;
vendor)
_arguments -s -w : \
"-e[attempt to proceed despite errors encountered while loading packages]" \
"-v[print the names of vendored modules and packages]" \
"-o[create the vendor directory at the given path]:output directory:_path_files -/"
;;
esac
;;
help) help)
_values "${commands[@]}" \ _values "${commands[@]}" \
'environment[show Go environment variables available]' \ 'environment[show Go environment variables available]' \
@ -247,6 +323,12 @@ _go() {
'testflag[description of testing flags]' \ 'testflag[description of testing flags]' \
'testfunc[description of testing functions]' 'testfunc[description of testing functions]'
;; ;;
version)
_arguments -s -w : \
"-m[print module version information]" \
"-v[report unrecognized files]" \
"*:file:_files"
;;
run) run)
_arguments -s -w : \ _arguments -s -w : \
${build_flags[@]} \ ${build_flags[@]} \