Skip to content

Commit cb39a5f

Browse files
adonovangopherbot
authored andcommitted
gopls/internal/golang: Format generated files
This CL reverts the effect of CL 365295 (to address issue golang/go#49555), which caused Format to fail on generated files. We believe this was a mistake: Go developers have plenty of reasons to temporarily edit generated files, for example to quickly experiment with changes without having to rewrite the code generator, or to add a log.Print statement while debugging. If a client asks gopls to format a file or organize its imports, gopls should do that, even if the file contains a "generated..." comment. + test Updates golang/go#49555 Fixes golang/go#73959 Change-Id: I2d02d5b2611b1599068df8fc267453773c66e027 Reviewed-on: https://go-review.googlesource.com/c/tools/+/678815 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e43ca0c commit cb39a5f

File tree

3 files changed

+27
-38
lines changed

3 files changed

+27
-38
lines changed

gopls/internal/golang/format.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ func Format(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) ([]pr
4040
return nil, err
4141
}
4242

43-
// Generated files shouldn't be edited. So, don't format them.
44-
if ast.IsGenerated(pgf.File) {
45-
return nil, fmt.Errorf("can't format %q: file is generated", fh.URI().Path())
46-
}
47-
4843
// Even if this file has parse errors, it might still be possible to format it.
4944
// Using format.Node on an AST with errors may result in code being modified.
5045
// Attempt to format the source of this file instead.

gopls/internal/test/integration/misc/formatting_test.go

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -268,39 +268,6 @@ func main() {
268268
}
269269
}
270270

271-
func TestFormattingOfGeneratedFile_Issue49555(t *testing.T) {
272-
const input = `
273-
-- main.go --
274-
// Code generated by generator.go. DO NOT EDIT.
275-
276-
package main
277-
278-
import "fmt"
279-
280-
func main() {
281-
282-
283-
284-
285-
fmt.Print("hello")
286-
}
287-
`
288-
289-
Run(t, input, func(t *testing.T, env *Env) {
290-
wantErrSuffix := "file is generated"
291-
292-
env.OpenFile("main.go")
293-
err := env.Editor.FormatBuffer(env.Ctx, "main.go")
294-
if err == nil {
295-
t.Fatal("expected error, got nil")
296-
}
297-
// Check only the suffix because an error contains a dynamic path to main.go
298-
if !strings.HasSuffix(err.Error(), wantErrSuffix) {
299-
t.Fatalf("unexpected error %q, want suffix %q", err.Error(), wantErrSuffix)
300-
}
301-
})
302-
}
303-
304271
func TestGofumptFormatting(t *testing.T) {
305272
// Exercise some gofumpt formatting rules:
306273
// - No empty lines following an assignment operator
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
This test checks that formatting includes generated files too
2+
(reversing https://go.dev/cl/365295 to address issue #49555).
3+
4+
See https://github.com/golang/go/issues/73959.
5+
6+
-- flags --
7+
-ignore_extra_diags
8+
9+
-- go.mod --
10+
module example.com
11+
go 1.21
12+
13+
-- a/a.go --
14+
// Code generated by me. DO NOT EDIT.
15+
16+
package a; func main() { fmt.Println("hello") }
17+
18+
//@format(out)
19+
20+
-- @out --
21+
// Code generated by me. DO NOT EDIT.
22+
23+
package a
24+
25+
func main() { fmt.Println("hello") }
26+
27+
//@format(out)

0 commit comments

Comments
 (0)