Skip to content

Commit b5fd2eb

Browse files
committed
internal/version: drop manual VCS version code
Go 1.24 started stamping local builds from a VCS clone with proper version information for the main module. Before the changes: $ go install $ gofumpt -version v0.8.1-0.20250831000946-c75c2d2e49fb (go1.26-devel_8bcda6c79d 2025-08-29 23:57:12 -0700) And after the changes in a dirty state: $ go install $ gofumpt -version v0.8.1-0.20250831000946-c75c2d2e49fb+dirty (go1.26-devel_8bcda6c79d 2025-08-29 23:57:12 -0700)
1 parent c75c2d2 commit b5fd2eb

File tree

2 files changed

+7
-67
lines changed

2 files changed

+7
-67
lines changed

internal/version/version.go

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,14 @@
44
package version
55

66
import (
7-
"encoding/json"
87
"fmt"
98
"os"
109
"runtime"
1110
"runtime/debug"
12-
"time"
13-
14-
"golang.org/x/mod/module"
1511
)
1612

17-
// Note that this is not a main package, so a "var version" will not work with
18-
// our go-cross script which uses -ldflags=main.version=xxx.
19-
2013
const ourModulePath = "mvdan.cc/gofumpt"
2114

22-
const fallbackVersion = "(devel)" // to match the default from runtime/debug
23-
2415
func findModule(info *debug.BuildInfo, modulePath string) *debug.Module {
2516
if info.Main.Path == modulePath {
2617
return &info.Main
@@ -36,57 +27,18 @@ func findModule(info *debug.BuildInfo, modulePath string) *debug.Module {
3627
func gofumptVersion() string {
3728
info, ok := debug.ReadBuildInfo()
3829
if !ok {
39-
return fallbackVersion // no build info available
30+
return "(no build info)"
4031
}
4132
// Note that gofumpt may be used as a library via the format package,
4233
// so we cannot assume it is the main module in the build.
4334
mod := findModule(info, ourModulePath)
4435
if mod == nil {
45-
return fallbackVersion // not found?
36+
return "(module not found)"
4637
}
4738
if mod.Replace != nil {
4839
mod = mod.Replace
4940
}
50-
51-
// If we found a meaningful version, we are done.
52-
// If gofumpt is not the main module, stop as well,
53-
// as VCS info is only for the main module.
54-
if mod.Version != "(devel)" || mod != &info.Main {
55-
return mod.Version
56-
}
57-
58-
// Fall back to trying to use VCS information.
59-
// Until https://github.com/golang/go/issues/50603 is implemented,
60-
// manually construct something like a pseudo-version.
61-
// TODO: remove when this code is dead, hopefully in Go 1.20.
62-
63-
// For the tests, as we don't want the VCS information to change over time.
64-
if v := os.Getenv("GARBLE_TEST_BUILDSETTINGS"); v != "" {
65-
var extra []debug.BuildSetting
66-
if err := json.Unmarshal([]byte(v), &extra); err != nil {
67-
panic(err)
68-
}
69-
info.Settings = append(info.Settings, extra...)
70-
}
71-
72-
var vcsTime time.Time
73-
var vcsRevision string
74-
for _, setting := range info.Settings {
75-
switch setting.Key {
76-
case "vcs.time":
77-
// If the format is invalid, we'll print a zero timestamp.
78-
vcsTime, _ = time.Parse(time.RFC3339Nano, setting.Value)
79-
case "vcs.revision":
80-
vcsRevision = setting.Value
81-
if len(vcsRevision) > 12 {
82-
vcsRevision = vcsRevision[:12]
83-
}
84-
}
85-
}
86-
if vcsRevision != "" {
87-
return module.PseudoVersion("", "", vcsTime, vcsRevision)
88-
}
89-
return fallbackVersion
41+
return mod.Version
9042
}
9143

9244
func goVersion() string {

testdata/script/diagnose.txtar

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
# Note that `go test` binaries don't get stamped with versions,
2+
# so the local tests below end up with a "devel" gofumpt version.
13
env GO_VERSION_TEST=go1.18.29
24

3-
# First, test a local build of gofumpt resulting from 'git clone'.
4-
# Its version will be inferred from VCS, but since we want a stable test,
5-
# we mock the VCS information. Note that test binaries do not have VCS info.
6-
# Data obtained from a real build while developing.
7-
env GARBLE_TEST_BUILDSETTINGS='[{"Key":"vcs","Value":"git"},{"Key":"vcs.revision","Value":"8dda8068d9f339047fc1777b688afb66a0a0db17"},{"Key":"vcs.time","Value":"2022-07-27T15:58:40Z"},{"Key":"vcs.modified","Value":"true"}]'
85
exec gofumpt foo.go
96
cmp stdout foo.go.golden
107

@@ -26,11 +23,6 @@ exec gofumpt -d foo.go.golden
2623
exec gofumpt -d -extra foo.go.golden-extra
2724
! stdout .
2825

29-
# A local build without VCS information will result in a missing version.
30-
env GARBLE_TEST_BUILDSETTINGS='[]'
31-
exec gofumpt foo.go
32-
cmp stdout foo.go.golden-devel
33-
3426
[short] stop 'the rest of this test builds gofumpt binaries'
3527

3628
# We want a published version of gofumpt on the public module proxies,
@@ -80,19 +72,15 @@ package p
8072
-- foo.go.golden --
8173
package p
8274

83-
//gofumpt:diagnose version: v0.0.0-20220727155840-8dda8068d9f3 (go1.18.29) flags: -lang=go1.16 -modpath=test
84-
-- foo.go.golden-devel --
85-
package p
86-
8775
//gofumpt:diagnose version: (devel) (go1.18.29) flags: -lang=go1.16 -modpath=test
8876
-- foo.go.golden-extra --
8977
package p
9078

91-
//gofumpt:diagnose version: v0.0.0-20220727155840-8dda8068d9f3 (go1.18.29) flags: -lang=go1.16 -modpath=test -extra
79+
//gofumpt:diagnose version: (devel) (go1.18.29) flags: -lang=go1.16 -modpath=test -extra
9280
-- foo.go.golden-lang --
9381
package p
9482

95-
//gofumpt:diagnose version: v0.0.0-20220727155840-8dda8068d9f3 (go1.18.29) flags: -lang=go1 -modpath=test
83+
//gofumpt:diagnose version: (devel) (go1.18.29) flags: -lang=go1 -modpath=test
9684
-- foo.go.golden-released --
9785
package p
9886

0 commit comments

Comments
 (0)