Skip to content

cmd/go: many subcommands take nonsensical build flags #62250

@aclements

Description

@aclements

What version of Go are you using (go version)?

$ go version
go version go1.21.0 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

go clean -n -gcflags=-S

What did you expect to see?

I expected the go command to reject the -gcflags flag because it makes no sense for that subcommand.

What did you see instead?

It quietly succeeded.

Many of the go subcommands are sensitive not just to which packages match the package pattern, but the specific set of source files selected for a build configuration. These call work.AddBuildFlags to register the flags that affect source selection, but this function also registers many flags that only affect actual compilation and linking.

  • The clean command is sensitive to source file selection because it finds all source files in the a package that have a correspondingly named binary in the directory (but are not part of package main) to delete the binaries.
  • The fix command uses work.AddBuildFlags, but appears not to be sensitive to any build flags, because it uses pkg.InternalAllGoFiles() to list source files, which should returns all source files regardless of source file selection.
  • The generate command only searches for go:generate directives in matched source files.

The full set of build flags makes sense for build, install, get, run, test, vet, and list because they all build source files (vet and list -export both invoke the toolchain to create up-to-date export files).

cc @bcmills

Metadata

Metadata

Assignees

No one assigned

    Labels

    GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions