Skip to content

x/tools/cmd/goimports: line directive can cause file corruption #51119

@kortschak

Description

@kortschak

What version of Go are you using (go version)?

$ go version
go version go1.17.6 linux/amd64

goimports version v0.1.9..

Does this issue reproduce with the latest release?

Yes, also on playground.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/user/bin"
GOCACHE="/home/user/.cache/go-build"
GOENV="/home/user/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/user/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/user"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/user/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/user/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.6"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/user/thinking/linedirective/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build229616347=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Run goimports on the following program.

package main

import (
	"fmt"
	"os"
)

//line :4

// Comment
func main() {
	fmt.Fprintln(os.Stderr, "Hello world!")
}

What did you expect to see?

No change.

What did you see instead?

package main

import (
	"fmt"
	"os" //line :4
	// Comment
)

func main() {
	fmt.Fprintln(os.Stderr, "Hello world!")
}

This can be reproduced here by clicking the format button.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions