@@ -22,10 +22,9 @@ import (
22
22
var CmdVersion = & base.Command {
23
23
UsageLine : "go version [-m] [-v] [file ...]" ,
24
24
Short : "print Go version" ,
25
- Long : `Version prints the build information for Go executables .
25
+ Long : `Version prints the build information for Go binary files .
26
26
27
- Go version reports the Go version used to build each of the named
28
- executable files.
27
+ Go version reports the Go version used to build each of the named files.
29
28
30
29
If no files are named on the command line, go version prints its own
31
30
version information.
@@ -35,7 +34,7 @@ looking for recognized Go binaries and reporting their versions.
35
34
By default, go version does not report unrecognized files found
36
35
during a directory scan. The -v flag causes it to report unrecognized files.
37
36
38
- The -m flag causes go version to print each executable 's embedded
37
+ The -m flag causes go version to print each file 's embedded
39
38
module version information, when available. In the output, the module
40
39
information consists of multiple lines following the version line, each
41
40
indented by a leading tab character.
@@ -92,7 +91,7 @@ func runVersion(ctx context.Context, cmd *base.Command, args []string) {
92
91
}
93
92
}
94
93
95
- // scanDir scans a directory for executables to run scanFile on.
94
+ // scanDir scans a directory for binary to run scanFile on.
96
95
func scanDir (dir string ) {
97
96
filepath .WalkDir (dir , func (path string , d fs.DirEntry , err error ) error {
98
97
if d .Type ().IsRegular () || d .Type ()& fs .ModeSymlink != 0 {
@@ -109,18 +108,24 @@ func scanDir(dir string) {
109
108
})
110
109
}
111
110
112
- // isExe reports whether the file should be considered executable.
113
- func isExe (file string , info fs.FileInfo ) bool {
114
- if runtime .GOOS == "windows" {
115
- return strings .HasSuffix (strings .ToLower (file ), ".exe" )
111
+ // isGoBinaryCandidate reports whether the file is a candidate to be a Go binary.
112
+ func isGoBinaryCandidate (file string , info fs.FileInfo ) bool {
113
+ if info .Mode ().IsRegular () && info .Mode ()& 0111 != 0 {
114
+ return true
115
+ }
116
+ name := strings .ToLower (file )
117
+ switch filepath .Ext (name ) {
118
+ case ".so" , ".exe" , ".dll" :
119
+ return true
120
+ default :
121
+ return strings .Contains (name , ".so." )
116
122
}
117
- return info .Mode ().IsRegular () && info .Mode ()& 0111 != 0
118
123
}
119
124
120
125
// scanFile scans file to try to report the Go and module versions.
121
126
// If mustPrint is true, scanFile will report any error reading file.
122
127
// Otherwise (mustPrint is false, because scanFile is being called
123
- // by scanDir) scanFile prints nothing for non-Go executables .
128
+ // by scanDir) scanFile prints nothing for non-Go binaries .
124
129
func scanFile (file string , info fs.FileInfo , mustPrint bool ) {
125
130
if info .Mode ()& fs .ModeSymlink != 0 {
126
131
// Accept file symlinks only.
@@ -134,20 +139,20 @@ func scanFile(file string, info fs.FileInfo, mustPrint bool) {
134
139
info = i
135
140
}
136
141
137
- if ! isExe (file , info ) {
138
- if mustPrint {
139
- fmt .Fprintf (os .Stderr , "%s: not executable file\n " , file )
140
- }
141
- return
142
- }
143
-
144
142
bi , err := buildinfo .ReadFile (file )
145
143
if err != nil {
146
144
if mustPrint {
147
145
if pathErr := (* os .PathError )(nil ); errors .As (err , & pathErr ) && filepath .Clean (pathErr .Path ) == filepath .Clean (file ) {
148
146
fmt .Fprintf (os .Stderr , "%v\n " , file )
149
147
} else {
150
- fmt .Fprintf (os .Stderr , "%s: %v\n " , file , err )
148
+
149
+ // Skip errors for non-Go binaries.
150
+ // buildinfo.ReadFile errors are not fine-grained enough
151
+ // to know if the file is a Go binary or not,
152
+ // so try to infer it from the file mode and extension.
153
+ if isGoBinaryCandidate (file , info ) {
154
+ fmt .Fprintf (os .Stderr , "%s: %v\n " , file , err )
155
+ }
151
156
}
152
157
}
153
158
return
0 commit comments