Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kortschak opened this issue Feb 9, 2022 · 3 comments
Open

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

kortschak opened this issue Feb 9, 2022 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@kortschak
Copy link
Contributor

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.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 9, 2022
@gopherbot gopherbot added this to the Unreleased milestone Feb 9, 2022
@cherrymui cherrymui added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 11, 2022
@cherrymui
Copy link
Member

cc @heschi @bradfitz

@heschi
Copy link
Contributor

heschi commented Feb 11, 2022

Yikes. A delightful combination of line directives, which I only half understand the implementation of, and comment manipulation in the AST, which is broken.

I'm not going to have the time to investigate this any time soon. Given that the file doesn't need any changes, there may be an easy fix to just echo out the incoming bytes rather than doing whatever formatting it's doing. That might break regrouping though. Dunno.

@kortschak
Copy link
Contributor Author

It's worth noting that neither gofmt nor gofumpt have this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

4 participants