golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.92k stars 17.65k forks source link

x/tools/imports: Removes comments if it doesn't see "package" #8637

Open gopherbot opened 10 years ago

gopherbot commented 10 years ago
$ printf '// i am precious\n' >i.go && goimports -d i.go
diff i.go gofmt/i.go
--- /home/tv/tmp/gofmt524290255 2014-09-03 11:33:53.118042810 -0700
+++ /home/tv/tmp/gofmt219767778 2014-09-03 11:33:53.118042810 -0700
@@ -1 +1 @@
-// i am precious
+
$ gofmt -d i.go 
i.go:1:18: expected 'package', found 'EOF'

$ printf '// i am precious\nfunc f() {}' >i.go && goimports -d i.go
diff i.go gofmt/i.go
--- /home/tv/tmp/gofmt433476709 2014-09-03 11:36:09.002716626 -0700
+++ /home/tv/tmp/gofmt636561536 2014-09-03 11:36:09.002716626 -0700
@@ -1,2 +1 @@
-// i am precious
 func f() {}
\ No newline at end of file

I'm ok with getting an error about invalid Go, or having a still-invalid file that
preserves the comments. I am not ok with losing my input.

This is extra annoying when goimports is hooked into editor save, and I move a
documented function into a new file.
ianlancetaylor commented 10 years ago

Comment 1:

Labels changed: added repo-tools, release-none.