Skip to content

Commit d9bacab

Browse files
committed
gopls/internal/server: improve "editing generated file" warning
The phrasing should be more informative and less prescriptive. Also, a test. Updates golang/go#49555 Fixes golang/go#73959 Change-Id: Ie8206b39d1b03c323690095552400c75bfeb9c5a Reviewed-on: https://go-review.googlesource.com/c/tools/+/678836 Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 1afeefa commit d9bacab

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

gopls/internal/server/text_synchronization.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,20 @@ func (s *server) warnAboutModifyingGeneratedFiles(ctx context.Context, uri proto
156156
return nil
157157
}
158158

159-
// Ideally, we should be able to specify that a generated file should
160-
// be opened as read-only. Tell the user that they should not be
161-
// editing a generated file.
159+
// Warn the user that they are editing a generated file, but
160+
// don't try to stop them: there are often good reasons to do
161+
// so, such as adding temporary logging, or evaluating changes
162+
// to the generated code without the trouble of modifying the
163+
// generator logic (see #73959).
162164
snapshot, release, err := s.session.SnapshotOf(ctx, uri)
163165
if err != nil {
164166
return err
165167
}
166168
isGenerated := golang.IsGenerated(ctx, snapshot, uri)
167169
release()
168-
169170
if isGenerated {
170-
msg := fmt.Sprintf("Do not edit this file! %s is a generated file.", uri.Path())
171+
msg := fmt.Sprintf("Warning: editing %s, a generated file.",
172+
filepath.Base(uri.Path()))
171173
showMessage(ctx, s.client, protocol.Warning, msg)
172174
}
173175
return nil

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,33 @@ package main
103103
env.RunGenerate("./")
104104
})
105105
}
106+
107+
func TestEditingGeneratedFileWarning(t *testing.T) {
108+
const src = `
109+
-- go.mod --
110+
module example.com
111+
go 1.21
112+
113+
-- a/a.go --
114+
// Code generated by me. DO NOT EDIT.
115+
116+
package a
117+
118+
var x = 1
119+
`
120+
Run(t, src, func(t *testing.T, env *Env) {
121+
env.OpenFile("a/a.go")
122+
env.RegexpReplace("a/a.go", "var", "const")
123+
collectMessages := env.Awaiter.ListenToShownMessages()
124+
env.Await(env.DoneWithChange())
125+
messages := collectMessages()
126+
127+
const want = "Warning: editing a.go, a generated file."
128+
if len(messages) != 1 || messages[0].Message != want {
129+
for _, message := range messages {
130+
t.Errorf("got message %q", message.Message)
131+
}
132+
t.Errorf("no %q warning", want)
133+
}
134+
})
135+
}

0 commit comments

Comments
 (0)