Skip to content

Commit 742e0a9

Browse files
qmuntalgopherbot
authored andcommitted
cmd/go: support shared libraries in 'go version' on Windows
This change modifies 'go version' to support shared windows libraries. Updates #48187 Change-Id: I2e8436b8df84fe76677106fa9ca02dcd1fb90e77 Reviewed-on: https://go-review.googlesource.com/c/go/+/391855 Reviewed-by: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Quim Muntal <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Auto-Submit: Bryan Mills <[email protected]>
1 parent 388fbf2 commit 742e0a9

File tree

4 files changed

+65
-24
lines changed

4 files changed

+65
-24
lines changed

src/cmd/go/alldocs.go

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/version/version.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import (
2222
var CmdVersion = &base.Command{
2323
UsageLine: "go version [-m] [-v] [file ...]",
2424
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.
2626
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.
2928
3029
If no files are named on the command line, go version prints its own
3130
version information.
@@ -35,7 +34,7 @@ looking for recognized Go binaries and reporting their versions.
3534
By default, go version does not report unrecognized files found
3635
during a directory scan. The -v flag causes it to report unrecognized files.
3736
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
3938
module version information, when available. In the output, the module
4039
information consists of multiple lines following the version line, each
4140
indented by a leading tab character.
@@ -92,7 +91,7 @@ func runVersion(ctx context.Context, cmd *base.Command, args []string) {
9291
}
9392
}
9493

95-
// scanDir scans a directory for executables to run scanFile on.
94+
// scanDir scans a directory for binary to run scanFile on.
9695
func scanDir(dir string) {
9796
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
9897
if d.Type().IsRegular() || d.Type()&fs.ModeSymlink != 0 {
@@ -109,18 +108,24 @@ func scanDir(dir string) {
109108
})
110109
}
111110

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.")
116122
}
117-
return info.Mode().IsRegular() && info.Mode()&0111 != 0
118123
}
119124

120125
// scanFile scans file to try to report the Go and module versions.
121126
// If mustPrint is true, scanFile will report any error reading file.
122127
// 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.
124129
func scanFile(file string, info fs.FileInfo, mustPrint bool) {
125130
if info.Mode()&fs.ModeSymlink != 0 {
126131
// Accept file symlinks only.
@@ -134,20 +139,20 @@ func scanFile(file string, info fs.FileInfo, mustPrint bool) {
134139
info = i
135140
}
136141

137-
if !isExe(file, info) {
138-
if mustPrint {
139-
fmt.Fprintf(os.Stderr, "%s: not executable file\n", file)
140-
}
141-
return
142-
}
143-
144142
bi, err := buildinfo.ReadFile(file)
145143
if err != nil {
146144
if mustPrint {
147145
if pathErr := (*os.PathError)(nil); errors.As(err, &pathErr) && filepath.Clean(pathErr.Path) == filepath.Clean(file) {
148146
fmt.Fprintf(os.Stderr, "%v\n", file)
149147
} 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+
}
151156
}
152157
}
153158
return

src/cmd/go/testdata/script/version.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ stderr 'with arguments'
99
! go version -v
1010
stderr 'with arguments'
1111

12+
# Check that 'go version' succeed even when it does not contain Go build info.
13+
# It should print an error if the file has a known Go binary extension.
14+
#
15+
go version empty.txt
16+
! stdout .
17+
! stderr .
18+
go version empty.exe
19+
stderr 'could not read Go build info'
20+
go version empty.so
21+
stderr 'could not read Go build info'
22+
go version empty.dll
23+
stderr 'could not read Go build info'
24+
1225
# Neither of the two flags above should be an issue via GOFLAGS.
1326
env GOFLAGS='-m -v'
1427
go version
@@ -71,4 +84,9 @@ module m
7184

7285
-- empty.go --
7386
package main
74-
func main(){}
87+
func main(){}
88+
89+
-- empty.txt --
90+
-- empty.exe --
91+
-- empty.so --
92+
-- empty.dll --
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[short] skip
2+
[!buildmode:c-shared] stop
3+
4+
env GO111MODULE=on
5+
6+
go get rsc.io/fortune
7+
go build -buildmode=c-shared -o external.so rsc.io/fortune
8+
go version external.so
9+
stdout '^external.so: .+'
10+
go version -m external.so
11+
stdout '^\tpath\trsc.io/fortune'
12+
stdout '^\tmod\trsc.io/fortune\tv1.0.0'
13+
14+
-- go.mod --
15+
module m
16+
17+
-- empty.go --
18+
package main
19+
func main(){}

0 commit comments

Comments
 (0)