Skip to content

Commit 42984c4

Browse files
committed
internal/lsp/regtest: run one quick fix at a time in TestUnknownRevision
There are two possible quick fixes for a missing go.sum entry, and the regression tests always run all available fixes. That never made sense, but I never got around to fixing it because it didn't cause a problem. Now that it turns out to be the cause of the problem described in CL 315152, fix it and roll that CL back. Change-Id: I49430429a99a412f43bd11b30afe8903db99a694 Reviewed-on: https://go-review.googlesource.com/c/tools/+/315910 Trust: Heschi Kreinick <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 19b1717 commit 42984c4

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

gopls/internal/regtest/modfile/modfile_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,6 @@ func TestUnknownRevision(t *testing.T) {
519519
-- a/go.mod --
520520
module mod.com
521521
522-
go 1.16
523-
524522
require (
525523
example.com v1.2.2
526524
)
@@ -556,8 +554,12 @@ func main() {
556554
ReadDiagnostics("a/go.mod", &d),
557555
),
558556
)
559-
env.ApplyQuickFixes("a/go.mod", d.Diagnostics)
560-
env.SaveBuffer("a/go.mod") // Save to trigger diagnostics.
557+
qfs := env.GetQuickFixes("a/go.mod", d.Diagnostics)
558+
if len(qfs) == 0 {
559+
t.Fatalf("got 0 code actions to fix %v, wanted at least 1", d.Diagnostics)
560+
}
561+
env.ApplyCodeAction(qfs[0]) // Arbitrarily pick a single fix to apply. Applying all of them seems to cause trouble in this particular test.
562+
env.SaveBuffer("a/go.mod") // Save to trigger diagnostics.
561563
env.Await(
562564
EmptyDiagnostics("a/go.mod"),
563565
env.DiagnosticAtRegexp("a/main.go", "x = "),
@@ -569,8 +571,6 @@ func main() {
569571
-- a/go.mod --
570572
module mod.com
571573
572-
go 1.16
573-
574574
require (
575575
example.com v1.2.3
576576
)

internal/lsp/fake/editor.go

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,13 @@ func (e *Editor) Symbol(ctx context.Context, query string) ([]SymbolInformation,
754754

755755
// OrganizeImports requests and performs the source.organizeImports codeAction.
756756
func (e *Editor) OrganizeImports(ctx context.Context, path string) error {
757-
_, err := e.codeAction(ctx, path, nil, nil, protocol.SourceOrganizeImports)
757+
_, err := e.applyCodeActions(ctx, path, nil, nil, protocol.SourceOrganizeImports)
758758
return err
759759
}
760760

761761
// RefactorRewrite requests and performs the source.refactorRewrite codeAction.
762762
func (e *Editor) RefactorRewrite(ctx context.Context, path string, rng *protocol.Range) error {
763-
applied, err := e.codeAction(ctx, path, rng, nil, protocol.RefactorRewrite)
763+
applied, err := e.applyCodeActions(ctx, path, rng, nil, protocol.RefactorRewrite)
764764
if applied == 0 {
765765
return errors.Errorf("no refactorings were applied")
766766
}
@@ -769,19 +769,46 @@ func (e *Editor) RefactorRewrite(ctx context.Context, path string, rng *protocol
769769

770770
// ApplyQuickFixes requests and performs the quickfix codeAction.
771771
func (e *Editor) ApplyQuickFixes(ctx context.Context, path string, rng *protocol.Range, diagnostics []protocol.Diagnostic) error {
772-
applied, err := e.codeAction(ctx, path, rng, diagnostics, protocol.QuickFix, protocol.SourceFixAll)
772+
applied, err := e.applyCodeActions(ctx, path, rng, diagnostics, protocol.SourceFixAll, protocol.QuickFix)
773773
if applied == 0 {
774774
return errors.Errorf("no quick fixes were applied")
775775
}
776776
return err
777777
}
778778

779+
// ApplyCodeAction applies the given code action.
780+
func (e *Editor) ApplyCodeAction(ctx context.Context, action protocol.CodeAction) error {
781+
for _, change := range action.Edit.DocumentChanges {
782+
path := e.sandbox.Workdir.URIToPath(change.TextDocument.URI)
783+
if int32(e.buffers[path].version) != change.TextDocument.Version {
784+
// Skip edits for old versions.
785+
continue
786+
}
787+
edits := convertEdits(change.Edits)
788+
if err := e.EditBuffer(ctx, path, edits); err != nil {
789+
return errors.Errorf("editing buffer %q: %w", path, err)
790+
}
791+
}
792+
// Execute any commands. The specification says that commands are
793+
// executed after edits are applied.
794+
if action.Command != nil {
795+
if _, err := e.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
796+
Command: action.Command.Command,
797+
Arguments: action.Command.Arguments,
798+
}); err != nil {
799+
return err
800+
}
801+
}
802+
// Some commands may edit files on disk.
803+
return e.sandbox.Workdir.CheckForFileChanges(ctx)
804+
}
805+
779806
// GetQuickFixes returns the available quick fix code actions.
780807
func (e *Editor) GetQuickFixes(ctx context.Context, path string, rng *protocol.Range, diagnostics []protocol.Diagnostic) ([]protocol.CodeAction, error) {
781808
return e.getCodeActions(ctx, path, rng, diagnostics, protocol.QuickFix, protocol.SourceFixAll)
782809
}
783810

784-
func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Range, diagnostics []protocol.Diagnostic, only ...protocol.CodeActionKind) (int, error) {
811+
func (e *Editor) applyCodeActions(ctx context.Context, path string, rng *protocol.Range, diagnostics []protocol.Diagnostic, only ...protocol.CodeActionKind) (int, error) {
785812
actions, err := e.getCodeActions(ctx, path, rng, diagnostics, only...)
786813
if err != nil {
787814
return 0, err
@@ -802,29 +829,7 @@ func (e *Editor) codeAction(ctx context.Context, path string, rng *protocol.Rang
802829
continue
803830
}
804831
applied++
805-
for _, change := range action.Edit.DocumentChanges {
806-
path := e.sandbox.Workdir.URIToPath(change.TextDocument.URI)
807-
if int32(e.buffers[path].version) != change.TextDocument.Version {
808-
// Skip edits for old versions.
809-
continue
810-
}
811-
edits := convertEdits(change.Edits)
812-
if err := e.EditBuffer(ctx, path, edits); err != nil {
813-
return 0, errors.Errorf("editing buffer %q: %w", path, err)
814-
}
815-
}
816-
// Execute any commands. The specification says that commands are
817-
// executed after edits are applied.
818-
if action.Command != nil {
819-
if _, err := e.ExecuteCommand(ctx, &protocol.ExecuteCommandParams{
820-
Command: action.Command.Command,
821-
Arguments: action.Command.Arguments,
822-
}); err != nil {
823-
return 0, err
824-
}
825-
}
826-
// Some commands may edit files on disk.
827-
if err := e.sandbox.Workdir.CheckForFileChanges(ctx); err != nil {
832+
if err := e.ApplyCodeAction(ctx, action); err != nil {
828833
return 0, err
829834
}
830835
}

internal/lsp/regtest/wrappers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
201201
}
202202
}
203203

204+
// ApplyCodeAction applies the given code action.
205+
func (e *Env) ApplyCodeAction(action protocol.CodeAction) {
206+
e.T.Helper()
207+
if err := e.Editor.ApplyCodeAction(e.Ctx, action); err != nil {
208+
e.T.Fatal(err)
209+
}
210+
}
211+
204212
// GetQuickFixes returns the available quick fix code actions.
205213
func (e *Env) GetQuickFixes(path string, diagnostics []protocol.Diagnostic) []protocol.CodeAction {
206214
e.T.Helper()

0 commit comments

Comments
 (0)