Skip to content

Commit aefe0b7

Browse files
committed
internal/lsp: set correct directness when adding new requires
When adding a require, we should add an `// indirect` comment if that's what go mod tidy would do. It's possible I should split Add out from Update and Remove, but this was quick and easy and I'm not too worried about it for now. Also minimize the test that covered this case, which was way more complicated than it needed to be AFAICT. Fixes golang/go#38914. Change-Id: I89c44f8573873227c4c9e637d1d31d8c1a6530aa Reviewed-on: https://go-review.googlesource.com/c/tools/+/267578 Trust: Heschi Kreinick <heschi@google.com> Run-TryBot: Heschi Kreinick <heschi@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent 330dc7d commit aefe0b7

File tree

4 files changed

+19
-42
lines changed

4 files changed

+19
-42
lines changed

gopls/internal/regtest/modfile_test.go

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ func main() {
324324
func TestBadlyVersionedModule(t *testing.T) {
325325
testenv.NeedsGo1Point(t, 14)
326326

327-
const badModule = `
328-
-- example.com/blah/@v/list --
329-
v1.0.0
327+
const proxy = `
330328
-- example.com/blah/@v/v1.0.0.mod --
331329
module example.com
332330
@@ -335,55 +333,33 @@ go 1.12
335333
package blah
336334
337335
const Name = "Blah"
338-
-- example.com/blah@v1.0.0/blah_test.go --
339-
package blah_test
340-
341-
import (
342-
"testing"
343-
)
344-
345-
func TestBlah(t *testing.T) {}
346-
347-
-- example.com/blah/v2/@v/list --
348-
v2.0.0
349336
-- example.com/blah/v2/@v/v2.0.0.mod --
350337
module example.com
351338
352339
go 1.12
353340
-- example.com/blah/v2@v2.0.0/blah.go --
354341
package blah
355342
356-
const Name = "Blah"
357-
-- example.com/blah/v2@v2.0.0/blah_test.go --
358-
package blah_test
359-
360-
import (
361-
"testing"
362-
363-
"example.com/blah"
364-
)
343+
import "example.com/blah"
365344
366-
func TestBlah(t *testing.T) {}
345+
var _ = blah.Name
346+
const Name = "Blah"
367347
`
368-
const pkg = `
348+
const files = `
369349
-- go.mod --
370350
module mod.com
371351
372352
go 1.12
373353
374-
require (
375-
example.com/blah/v2 v2.0.0
376-
)
354+
require example.com/blah/v2 v2.0.0
377355
-- main.go --
378356
package main
379357
380358
import "example.com/blah/v2"
381359
382-
func main() {
383-
println(blah.Name)
384-
}
360+
var _ = blah.Name
385361
`
386-
runner.Run(t, pkg, func(t *testing.T, env *Env) {
362+
withOptions(WithProxyFiles(proxy)).run(t, files, func(t *testing.T, env *Env) {
387363
env.OpenFile("main.go")
388364
env.OpenFile("go.mod")
389365
var d protocol.PublishDiagnosticsParams
@@ -399,7 +375,7 @@ func main() {
399375
go 1.12
400376
401377
require (
402-
example.com/blah v1.0.0
378+
example.com/blah v1.0.0 // indirect
403379
example.com/blah/v2 v2.0.0
404380
)
405381
`
@@ -409,7 +385,7 @@ require (
409385
if got := env.ReadWorkspaceFile("go.mod"); got != want {
410386
t.Fatalf("suggested fixes failed:\n%s", tests.Diff(want, got))
411387
}
412-
}, WithProxyFiles(badModule))
388+
})
413389
}
414390

415391
// Reproduces golang/go#38232.

internal/lsp/cache/mod_tidy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func unusedError(m *protocol.ColumnMapper, req *modfile.Require, computeEdits di
353353
if err != nil {
354354
return source.Error{}, err
355355
}
356-
args, err := source.MarshalArgs(m.URI, []string{req.Mod.Path + "@none"})
356+
args, err := source.MarshalArgs(m.URI, false, []string{req.Mod.Path + "@none"})
357357
if err != nil {
358358
return source.Error{}, err
359359
}
@@ -425,7 +425,7 @@ func missingModuleError(snapshot source.Snapshot, pm *source.ParsedModule, req *
425425
return source.Error{}, err
426426
}
427427
}
428-
args, err := source.MarshalArgs(pm.Mapper.URI, []string{req.Mod.Path + "@" + req.Mod.Version})
428+
args, err := source.MarshalArgs(pm.Mapper.URI, !req.Indirect, []string{req.Mod.Path + "@" + req.Mod.Version})
429429
if err != nil {
430430
return source.Error{}, err
431431
}

internal/lsp/command.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,14 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
200200
case source.CommandAddDependency, source.CommandUpgradeDependency, source.CommandRemoveDependency:
201201
var uri protocol.DocumentURI
202202
var goCmdArgs []string
203-
if err := source.UnmarshalArgs(args, &uri, &goCmdArgs); err != nil {
203+
var addRequire bool
204+
if err := source.UnmarshalArgs(args, &uri, &addRequire, &goCmdArgs); err != nil {
204205
return err
205206
}
206-
if command == source.CommandAddDependency {
207+
if addRequire {
207208
// Using go get to create a new dependency results in an
208-
// `// indirect` comment we don't want. The only way to avoid it is
209-
// to add the require as direct first. Then we can use go get to
209+
// `// indirect` comment we may not want. The only way to avoid it
210+
// is to add the require as direct first. Then we can use go get to
210211
// update go.sum and tidy up.
211212
if err := s.directGoModCommand(ctx, uri, "mod", append([]string{"edit", "-require"}, goCmdArgs...)...); err != nil {
212213
return err

internal/lsp/mod/code_lens.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func upgradeLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHa
5252
if err != nil {
5353
return nil, err
5454
}
55-
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), []string{dep})
55+
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), false, []string{dep})
5656
if err != nil {
5757
return nil, err
5858
}
@@ -69,7 +69,7 @@ func upgradeLens(ctx context.Context, snapshot source.Snapshot, fh source.FileHa
6969
// If there is at least 1 upgrade, add "Upgrade all dependencies" to
7070
// the module statement.
7171
if len(allUpgrades) > 0 {
72-
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), append([]string{"-u"}, allUpgrades...))
72+
upgradeDepArgs, err := source.MarshalArgs(fh.URI(), false, append([]string{"-u"}, allUpgrades...))
7373
if err != nil {
7474
return nil, err
7575
}

0 commit comments

Comments
 (0)