Source file src/cmd/go/main.go

     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.
     4  
     5  //go:generate go test cmd/go -v -run=^TestDocsUpToDate$ -fixdocs
     6  
     7  package main
     8  
     9  import (
    10  	"context"
    11  	"flag"
    12  	"fmt"
    13  	"internal/buildcfg"
    14  	"log"
    15  	"os"
    16  	"path/filepath"
    17  	rtrace "runtime/trace"
    18  	"slices"
    19  	"strings"
    20  
    21  	"cmd/go/internal/base"
    22  	"cmd/go/internal/bug"
    23  	"cmd/go/internal/cfg"
    24  	"cmd/go/internal/clean"
    25  	"cmd/go/internal/doc"
    26  	"cmd/go/internal/envcmd"
    27  	"cmd/go/internal/fix"
    28  	"cmd/go/internal/fmtcmd"
    29  	"cmd/go/internal/generate"
    30  	"cmd/go/internal/help"
    31  	"cmd/go/internal/list"
    32  	"cmd/go/internal/modcmd"
    33  	"cmd/go/internal/modfetch"
    34  	"cmd/go/internal/modget"
    35  	"cmd/go/internal/modload"
    36  	"cmd/go/internal/run"
    37  	"cmd/go/internal/telemetrycmd"
    38  	"cmd/go/internal/telemetrystats"
    39  	"cmd/go/internal/test"
    40  	"cmd/go/internal/tool"
    41  	"cmd/go/internal/toolchain"
    42  	"cmd/go/internal/trace"
    43  	"cmd/go/internal/version"
    44  	"cmd/go/internal/vet"
    45  	"cmd/go/internal/work"
    46  	"cmd/go/internal/workcmd"
    47  	"cmd/internal/telemetry"
    48  	"cmd/internal/telemetry/counter"
    49  )
    50  
    51  func init() {
    52  	base.Go.Commands = []*base.Command{
    53  		bug.CmdBug,
    54  		work.CmdBuild,
    55  		clean.CmdClean,
    56  		doc.CmdDoc,
    57  		envcmd.CmdEnv,
    58  		fix.CmdFix,
    59  		fmtcmd.CmdFmt,
    60  		generate.CmdGenerate,
    61  		modget.CmdGet,
    62  		work.CmdInstall,
    63  		list.CmdList,
    64  		modcmd.CmdMod,
    65  		workcmd.CmdWork,
    66  		run.CmdRun,
    67  		telemetrycmd.CmdTelemetry,
    68  		test.CmdTest,
    69  		tool.CmdTool,
    70  		version.CmdVersion,
    71  		vet.CmdVet,
    72  
    73  		help.HelpBuildConstraint,
    74  		help.HelpBuildmode,
    75  		help.HelpC,
    76  		help.HelpCache,
    77  		help.HelpEnvironment,
    78  		help.HelpFileType,
    79  		modload.HelpGoMod,
    80  		help.HelpGopath,
    81  		modfetch.HelpGoproxy,
    82  		help.HelpImportPath,
    83  		modload.HelpModules,
    84  		modfetch.HelpModuleAuth,
    85  		help.HelpPackages,
    86  		modfetch.HelpPrivate,
    87  		test.HelpTestflag,
    88  		test.HelpTestfunc,
    89  		modget.HelpVCS,
    90  	}
    91  }
    92  
    93  var _ = go11tag
    94  
    95  var counterErrorsGOPATHEntryRelative = counter.New("go/errors:gopath-entry-relative")
    96  
    97  func main() {
    98  	log.SetFlags(0)
    99  	telemetry.MaybeChild() // Run in child mode if this is the telemetry sidecar child process.
   100  	cmdIsGoTelemetryOff := cmdIsGoTelemetryOff()
   101  	if !cmdIsGoTelemetryOff {
   102  		counter.Open() // Open the telemetry counter file so counters can be written to it.
   103  	}
   104  	handleChdirFlag()
   105  	toolchain.Select()
   106  
   107  	if !cmdIsGoTelemetryOff {
   108  		telemetry.MaybeParent() // Run the upload process. Opening the counter file is idempotent.
   109  	}
   110  	flag.Usage = base.Usage
   111  	flag.Parse()
   112  	counter.Inc("go/invocations")
   113  	counter.CountFlags("go/flag:", *flag.CommandLine)
   114  
   115  	args := flag.Args()
   116  	if len(args) < 1 {
   117  		base.Usage()
   118  	}
   119  
   120  	cfg.CmdName = args[0] // for error messages
   121  	if args[0] == "help" {
   122  		counter.Inc("go/subcommand:" + strings.Join(append([]string{"help"}, args[1:]...), "-"))
   123  		help.Help(os.Stdout, args[1:])
   124  		return
   125  	}
   126  
   127  	if cfg.GOROOT == "" {
   128  		fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: 'go' binary is trimmed and GOROOT is not set\n")
   129  		os.Exit(2)
   130  	}
   131  	if fi, err := os.Stat(cfg.GOROOT); err != nil || !fi.IsDir() {
   132  		fmt.Fprintf(os.Stderr, "go: cannot find GOROOT directory: %v\n", cfg.GOROOT)
   133  		os.Exit(2)
   134  	}
   135  	switch strings.ToLower(cfg.GOROOT) {
   136  	case "/usr/local/go": // Location recommended for installation on Linux and Darwin and used by Mac installer.
   137  		counter.Inc("go/goroot:usr-local-go")
   138  	case "/usr/lib/go": // A typical location used by Linux package managers.
   139  		counter.Inc("go/goroot:usr-lib-go")
   140  	case "/usr/lib/golang": // Another typical location used by Linux package managers.
   141  		counter.Inc("go/goroot:usr-lib-golang")
   142  	case `c:\program files\go`: // Location used by Windows installer.
   143  		counter.Inc("go/goroot:program-files-go")
   144  	case `c:\program files (x86)\go`: // Location used by 386 Windows installer on amd64 platform.
   145  		counter.Inc("go/goroot:program-files-x86-go")
   146  	default:
   147  		counter.Inc("go/goroot:other")
   148  	}
   149  
   150  	// Diagnose common mistake: GOPATH==GOROOT.
   151  	// This setting is equivalent to not setting GOPATH at all,
   152  	// which is not what most people want when they do it.
   153  	if gopath := cfg.BuildContext.GOPATH; filepath.Clean(gopath) == filepath.Clean(cfg.GOROOT) {
   154  		fmt.Fprintf(os.Stderr, "warning: both GOPATH and GOROOT are the same directory (%s); see https://go.dev/wiki/InstallTroubleshooting\n", gopath)
   155  	} else {
   156  		for _, p := range filepath.SplitList(gopath) {
   157  			// Some GOPATHs have empty directory elements - ignore them.
   158  			// See issue 21928 for details.
   159  			if p == "" {
   160  				continue
   161  			}
   162  			// Note: using HasPrefix instead of Contains because a ~ can appear
   163  			// in the middle of directory elements, such as /tmp/git-1.8.2~rc3
   164  			// or C:\PROGRA~1. Only ~ as a path prefix has meaning to the shell.
   165  			if strings.HasPrefix(p, "~") {
   166  				fmt.Fprintf(os.Stderr, "go: GOPATH entry cannot start with shell metacharacter '~': %q\n", p)
   167  				os.Exit(2)
   168  			}
   169  			if !filepath.IsAbs(p) {
   170  				if cfg.Getenv("GOPATH") == "" {
   171  					// We inferred $GOPATH from $HOME and did a bad job at it.
   172  					// Instead of dying, uninfer it.
   173  					cfg.BuildContext.GOPATH = ""
   174  				} else {
   175  					counterErrorsGOPATHEntryRelative.Inc()
   176  					fmt.Fprintf(os.Stderr, "go: GOPATH entry is relative; must be absolute path: %q.\nFor more details see: 'go help gopath'\n", p)
   177  					os.Exit(2)
   178  				}
   179  			}
   180  		}
   181  	}
   182  
   183  	cmd, used := lookupCmd(args)
   184  	cfg.CmdName = strings.Join(args[:used], " ")
   185  	if len(cmd.Commands) > 0 {
   186  		if used >= len(args) {
   187  			help.PrintUsage(os.Stderr, cmd)
   188  			base.SetExitStatus(2)
   189  			base.Exit()
   190  		}
   191  		if args[used] == "help" {
   192  			// Accept 'go mod help' and 'go mod help foo' for 'go help mod' and 'go help mod foo'.
   193  			counter.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "-" + strings.Join(args[used:], "-"))
   194  			help.Help(os.Stdout, append(slices.Clip(args[:used]), args[used+1:]...))
   195  			base.Exit()
   196  		}
   197  		helpArg := ""
   198  		if used > 0 {
   199  			helpArg += " " + strings.Join(args[:used], " ")
   200  		}
   201  		cmdName := cfg.CmdName
   202  		if cmdName == "" {
   203  			cmdName = args[0]
   204  		}
   205  		counter.Inc("go/subcommand:unknown")
   206  		fmt.Fprintf(os.Stderr, "go %s: unknown command\nRun 'go help%s' for usage.\n", cmdName, helpArg)
   207  		base.SetExitStatus(2)
   208  		base.Exit()
   209  	}
   210  	// Increment a subcommand counter for the subcommand we're running.
   211  	// Don't increment the counter for the tool subcommand here: we'll
   212  	// increment in the tool subcommand's Run function because we need
   213  	// to do the flag processing in invoke first.
   214  	if cfg.CmdName != "tool" {
   215  		counter.Inc("go/subcommand:" + strings.ReplaceAll(cfg.CmdName, " ", "-"))
   216  	}
   217  	telemetrystats.Increment()
   218  	invoke(cmd, args[used-1:])
   219  	base.Exit()
   220  }
   221  
   222  // cmdIsGoTelemeteryOff reports whether the command is "go telemetry off". This
   223  // is used to decide whether to disable the opening of counter files. See #69269.
   224  func cmdIsGoTelemetryOff() bool {
   225  	restArgs := os.Args[1:]
   226  	// skipChdirFlag skips the -C flag, which is the only flag that can appear
   227  	// in a valid 'go telemetry off' command, and which hasn't been processed
   228  	// yet. We need to determine if the command is 'go telemetry off' before we open
   229  	// the counter file, but we want to process -C after we open counters so that
   230  	// we can increment the flag counter for it.
   231  	skipChdirFlag := func() {
   232  		if len(restArgs) == 0 {
   233  			return
   234  		}
   235  		switch a := restArgs[0]; {
   236  		case a == "-C", a == "--C":
   237  			if len(restArgs) < 2 {
   238  				restArgs = nil
   239  				return
   240  			}
   241  			restArgs = restArgs[2:]
   242  
   243  		case strings.HasPrefix(a, "-C="), strings.HasPrefix(a, "--C="):
   244  			restArgs = restArgs[1:]
   245  		}
   246  	}
   247  	skipChdirFlag()
   248  	cmd, used := lookupCmd(restArgs)
   249  	if cmd != telemetrycmd.CmdTelemetry {
   250  		return false
   251  	}
   252  	restArgs = restArgs[used:]
   253  	skipChdirFlag()
   254  	return len(restArgs) == 1 && restArgs[0] == "off"
   255  }
   256  
   257  // lookupCmd interprets the initial elements of args
   258  // to find a command to run (cmd.Runnable() == true)
   259  // or else a command group that ran out of arguments
   260  // or had an unknown subcommand (len(cmd.Commands) > 0).
   261  // It returns that command and the number of elements of args
   262  // that it took to arrive at that command.
   263  func lookupCmd(args []string) (cmd *base.Command, used int) {
   264  	cmd = base.Go
   265  	for used < len(args) {
   266  		c := cmd.Lookup(args[used])
   267  		if c == nil {
   268  			break
   269  		}
   270  		if c.Runnable() {
   271  			cmd = c
   272  			used++
   273  			break
   274  		}
   275  		if len(c.Commands) > 0 {
   276  			cmd = c
   277  			used++
   278  			if used >= len(args) || args[0] == "help" {
   279  				break
   280  			}
   281  			continue
   282  		}
   283  		// len(c.Commands) == 0 && !c.Runnable() => help text; stop at "help"
   284  		break
   285  	}
   286  	return cmd, used
   287  }
   288  
   289  func invoke(cmd *base.Command, args []string) {
   290  	// 'go env' handles checking the build config
   291  	if cmd != envcmd.CmdEnv {
   292  		buildcfg.Check()
   293  		if cfg.ExperimentErr != nil {
   294  			base.Fatal(cfg.ExperimentErr)
   295  		}
   296  	}
   297  
   298  	// Set environment (GOOS, GOARCH, etc) explicitly.
   299  	// In theory all the commands we invoke should have
   300  	// the same default computation of these as we do,
   301  	// but in practice there might be skew
   302  	// This makes sure we all agree.
   303  	cfg.OrigEnv = toolchain.FilterEnv(os.Environ())
   304  	cfg.CmdEnv = envcmd.MkEnv()
   305  	for _, env := range cfg.CmdEnv {
   306  		if os.Getenv(env.Name) != env.Value {
   307  			os.Setenv(env.Name, env.Value)
   308  		}
   309  	}
   310  
   311  	cmd.Flag.Usage = func() { cmd.Usage() }
   312  	if cmd.CustomFlags {
   313  		args = args[1:]
   314  	} else {
   315  		base.SetFromGOFLAGS(&cmd.Flag)
   316  		cmd.Flag.Parse(args[1:])
   317  		flagCounterPrefix := "go/" + strings.ReplaceAll(cfg.CmdName, " ", "-") + "/flag"
   318  		counter.CountFlags(flagCounterPrefix+":", cmd.Flag)
   319  		counter.CountFlagValue(flagCounterPrefix+"/", cmd.Flag, "buildmode")
   320  		args = cmd.Flag.Args()
   321  	}
   322  
   323  	if cfg.DebugRuntimeTrace != "" {
   324  		f, err := os.Create(cfg.DebugRuntimeTrace)
   325  		if err != nil {
   326  			base.Fatalf("creating trace file: %v", err)
   327  		}
   328  		if err := rtrace.Start(f); err != nil {
   329  			base.Fatalf("starting event trace: %v", err)
   330  		}
   331  		defer func() {
   332  			rtrace.Stop()
   333  			f.Close()
   334  		}()
   335  	}
   336  
   337  	ctx := maybeStartTrace(context.Background())
   338  	ctx, span := trace.StartSpan(ctx, fmt.Sprint("Running ", cmd.Name(), " command"))
   339  	cmd.Run(ctx, cmd, args)
   340  	span.Done()
   341  }
   342  
   343  func init() {
   344  	base.Usage = mainUsage
   345  }
   346  
   347  func mainUsage() {
   348  	help.PrintUsage(os.Stderr, base.Go)
   349  	os.Exit(2)
   350  }
   351  
   352  func maybeStartTrace(pctx context.Context) context.Context {
   353  	if cfg.DebugTrace == "" {
   354  		return pctx
   355  	}
   356  
   357  	ctx, close, err := trace.Start(pctx, cfg.DebugTrace)
   358  	if err != nil {
   359  		base.Fatalf("failed to start trace: %v", err)
   360  	}
   361  	base.AtExit(func() {
   362  		if err := close(); err != nil {
   363  			base.Fatalf("failed to stop trace: %v", err)
   364  		}
   365  	})
   366  
   367  	return ctx
   368  }
   369  
   370  // handleChdirFlag handles the -C flag before doing anything else.
   371  // The -C flag must be the first flag on the command line, to make it easy to find
   372  // even with commands that have custom flag parsing.
   373  // handleChdirFlag handles the flag by chdir'ing to the directory
   374  // and then removing that flag from the command line entirely.
   375  //
   376  // We have to handle the -C flag this way for two reasons:
   377  //
   378  //  1. Toolchain selection needs to be in the right directory to look for go.mod and go.work.
   379  //
   380  //  2. A toolchain switch later on reinvokes the new go command with the same arguments.
   381  //     The parent toolchain has already done the chdir; the child must not try to do it again.
   382  func handleChdirFlag() {
   383  	_, used := lookupCmd(os.Args[1:])
   384  	used++ // because of [1:]
   385  	if used >= len(os.Args) {
   386  		return
   387  	}
   388  
   389  	var dir string
   390  	switch a := os.Args[used]; {
   391  	default:
   392  		return
   393  
   394  	case a == "-C", a == "--C":
   395  		if used+1 >= len(os.Args) {
   396  			return
   397  		}
   398  		dir = os.Args[used+1]
   399  		os.Args = slices.Delete(os.Args, used, used+2)
   400  
   401  	case strings.HasPrefix(a, "-C="), strings.HasPrefix(a, "--C="):
   402  		_, dir, _ = strings.Cut(a, "=")
   403  		os.Args = slices.Delete(os.Args, used, used+1)
   404  	}
   405  	counter.Inc("go/flag:C")
   406  
   407  	if err := os.Chdir(dir); err != nil {
   408  		base.Fatalf("go: %v", err)
   409  	}
   410  }
   411  

View as plain text