//
// go version [-m] [-v] [file ...]
//
-// Version prints the build information for Go executables.
+// Version prints the build information for Go binary files.
//
-// Go version reports the Go version used to build each of the named
-// executable files.
+// Go version reports the Go version used to build each of the named files.
//
// If no files are named on the command line, go version prints its own
// version information.
// By default, go version does not report unrecognized files found
// during a directory scan. The -v flag causes it to report unrecognized files.
//
-// The -m flag causes go version to print each executable's embedded
+// The -m flag causes go version to print each file's embedded
// module version information, when available. In the output, the module
// information consists of multiple lines following the version line, each
// indented by a leading tab character.
var CmdVersion = &base.Command{
UsageLine: "go version [-m] [-v] [file ...]",
Short: "print Go version",
- Long: `Version prints the build information for Go executables.
+ Long: `Version prints the build information for Go binary files.
-Go version reports the Go version used to build each of the named
-executable files.
+Go version reports the Go version used to build each of the named files.
If no files are named on the command line, go version prints its own
version information.
By default, go version does not report unrecognized files found
during a directory scan. The -v flag causes it to report unrecognized files.
-The -m flag causes go version to print each executable's embedded
+The -m flag causes go version to print each file's embedded
module version information, when available. In the output, the module
information consists of multiple lines following the version line, each
indented by a leading tab character.
}
}
-// scanDir scans a directory for executables to run scanFile on.
+// scanDir scans a directory for binary to run scanFile on.
func scanDir(dir string) {
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if d.Type().IsRegular() || d.Type()&fs.ModeSymlink != 0 {
})
}
-// isExe reports whether the file should be considered executable.
-func isExe(file string, info fs.FileInfo) bool {
- if runtime.GOOS == "windows" {
- return strings.HasSuffix(strings.ToLower(file), ".exe")
+// isGoBinaryCandidate reports whether the file is a candidate to be a Go binary.
+func isGoBinaryCandidate(file string, info fs.FileInfo) bool {
+ if info.Mode().IsRegular() && info.Mode()&0111 != 0 {
+ return true
+ }
+ name := strings.ToLower(file)
+ switch filepath.Ext(name) {
+ case ".so", ".exe", ".dll":
+ return true
+ default:
+ return strings.Contains(name, ".so.")
}
- return info.Mode().IsRegular() && info.Mode()&0111 != 0
}
// scanFile scans file to try to report the Go and module versions.
// If mustPrint is true, scanFile will report any error reading file.
// Otherwise (mustPrint is false, because scanFile is being called
-// by scanDir) scanFile prints nothing for non-Go executables.
+// by scanDir) scanFile prints nothing for non-Go binaries.
func scanFile(file string, info fs.FileInfo, mustPrint bool) {
if info.Mode()&fs.ModeSymlink != 0 {
// Accept file symlinks only.
info = i
}
- if !isExe(file, info) {
- if mustPrint {
- fmt.Fprintf(os.Stderr, "%s: not executable file\n", file)
- }
- return
- }
-
bi, err := buildinfo.ReadFile(file)
if err != nil {
if mustPrint {
if pathErr := (*os.PathError)(nil); errors.As(err, &pathErr) && filepath.Clean(pathErr.Path) == filepath.Clean(file) {
fmt.Fprintf(os.Stderr, "%v\n", file)
} else {
- fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
+
+ // Skip errors for non-Go binaries.
+ // buildinfo.ReadFile errors are not fine-grained enough
+ // to know if the file is a Go binary or not,
+ // so try to infer it from the file mode and extension.
+ if isGoBinaryCandidate(file, info) {
+ fmt.Fprintf(os.Stderr, "%s: %v\n", file, err)
+ }
}
}
return