Skip to content

cmd/gofmt: symlink has extra bytes from old content after executing the go fmt command #79735

@bozaro

Description

@bozaro

Go version

go version go1.26.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='x86_64-linux-gnu-gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='x86_64-linux-gnu-g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/bozar/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/bozar/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build209784865=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/bozar/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/bozar/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/lib/go-1.26'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/bozar/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/lib/go-1.26/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.26.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

Currenty go fmt does not truncate the symlinked file, as it compares the size of the recorded data not with the real file size, but with the symlink reference path length.

A script to reproduce the issue:

#!/bin/bash -e

rm -f issue*.go

cat <<EOF > issue-original.go
package main;

import "fmt"

func main() {
	fmt.Println(  "Hello, world!!!"  )
}
EOF

# Go fmt regular file (expected content)
cp issue-original.go issue-expected.go
go fmt issue-expected.go

# Go fmt symlink file
ln -s issue-original.go issue-symlink.go
go fmt issue-symlink.go

# Expected same file content
diff -c issue-expected.go issue-symlink.go

What did you see happen?

Expected same content on issue-symlink.go and issue-expected.go files.

What did you expect to see?

issue-symlink.go has extra bytes at end of files:

issue-expected.go
issue-symlink.go
*** issue-expected.go	2026-05-28 13:37:52.450203694 +0300
--- issue-symlink.go	2026-05-28 13:37:52.479204129 +0300
***************
*** 5,7 ****
--- 5,9 ----
  func main() {
  	fmt.Println("Hello, world!!!")
  }
+  )
+ }

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions