1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package fix implements the ``go fix'' command.
11 "cmd/go/internal/load"
12 "cmd/go/internal/modload"
14 "cmd/go/internal/work"
21 var CmdFix
= &base
.Command
{
22 UsageLine
: "go fix [-fix list] [packages]",
23 Short
: "update packages to use new APIs",
25 Fix runs the Go fix command on the packages named by the import paths.
27 The -fix flag sets a comma-separated list of fixes to run.
28 The default is all known fixes.
29 (Its value is passed to 'go tool fix -r'.)
31 For more about fix, see 'go doc cmd/fix'.
32 For more about specifying packages, see 'go help packages'.
34 To run fix with other options, run 'go tool fix'.
36 See also: go fmt, go vet.
40 var fixes
= CmdFix
.Flag
.String("fix", "", "comma-separated list of fixes to apply")
43 work
.AddBuildFlags(CmdFix
, work
.DefaultBuildFlags
)
44 CmdFix
.Run
= runFix
// fix cycle
47 func runFix(ctx context
.Context
, cmd
*base
.Command
, args
[]string) {
48 pkgs
:= load
.PackagesAndErrors(ctx
, load
.PackageOpts
{}, args
)
50 for _
, pkg
:= range pkgs
{
52 base
.Errorf("%v", pkg
.Error
)
61 for _
, pkg
:= range pkgs
{
62 if modload
.Enabled() && pkg
.Module
!= nil && !pkg
.Module
.Main
{
64 fmt
.Fprintf(os
.Stderr
, "go: not fixing packages in dependency modules\n")
69 // Use pkg.gofiles instead of pkg.Dir so that
70 // the command only applies to this package,
71 // not to packages in subdirectories.
72 files
:= base
.RelPaths(pkg
.InternalAllGoFiles())
74 if pkg
.Module
!= nil {
75 goVersion
= "go" + pkg
.Module
.GoVersion
76 } else if pkg
.Standard
{
77 goVersion
= build
.Default
.ReleaseTags
[len(build
.Default
.ReleaseTags
)-1]
81 fixArg
= []string{"-r=" + *fixes
}
83 base
.Run(str
.StringList(cfg
.BuildToolexec
, base
.Tool("fix"), "-go="+goVersion
, fixArg
, files
))