Gerrit Bot has uploaded this change for review.
Feature: allow ignoring unused modules for go mod tidy
Fixes https://github.com/golang/go/issues/51262
This change modifies `go mod tidy` to allow ignoring unused imports by specifying an end-of-line comment containing `ignore-unused`.
Example:
```
module mod.com
go 1.14
require example.com v1.0.0
require ignored.com v1.0.0 // ignore-unused
```
In this example, `go mod tidy` will remove `example.com` and will keep `ignored.com`.
Change-Id: Ia05f9a9284c5cf428f07eef166ecce311cb967af
GitHub-Last-Rev: dd598ff65c0f2c12f35216e1453c98404d86bc4c
GitHub-Pull-Request: golang/tools#374
---
M gopls/internal/regtest/modfile/modfile_test.go
M internal/lsp/cache/mod_tidy.go
2 files changed, 44 insertions(+), 0 deletions(-)
diff --git a/gopls/internal/regtest/modfile/modfile_test.go b/gopls/internal/regtest/modfile/modfile_test.go
index 05b7ade..1514515 100644
--- a/gopls/internal/regtest/modfile/modfile_test.go
+++ b/gopls/internal/regtest/modfile/modfile_test.go
@@ -293,15 +293,21 @@
-- examp...@v1.0.0/x.go --
package pkg
const X = 1
+-- ignor...@v1.0.0/x.go --
+package pkg
+const X = 1
`
const files = `
-- a/go.mod --
module mod.com
go 1.14
require example.com v1.0.0
+require ignored.com v1.0.0 // ignore-unused
-- a/go.sum --
example.com v1.0.0 h1:38O7j5rEBajXk+Q5wzLbRN7KqMkSgEiN9NqcM1O2bBM=
example.com v1.0.0/go.mod h1:vUsPMGpx9ZXXzECCOsOmYCW7npJTwuA16yl89n3Mgls=
+ignored.com v1.0.0 h1:38O7j5rEBajXk+Q5wzLbRN7KqMkSgEiN9NqcM1O2bBM=
+ignored.com v1.0.0/go.mod h1:6AkkVrgFovS0vF13BIgXOmRbcvDiZd8SbcOLkLuqxhw=
-- a/main.go --
package main
func main() {}
@@ -310,6 +316,8 @@
const want = `module mod.com
go 1.14
+
+require ignored.com v1.0.0 // ignore-unused
`
RunMultiple{
diff --git a/internal/lsp/cache/mod_tidy.go b/internal/lsp/cache/mod_tidy.go
index f6c74c5..acd5853 100644
--- a/internal/lsp/cache/mod_tidy.go
+++ b/internal/lsp/cache/mod_tidy.go
@@ -313,6 +313,9 @@
// Finally, add errors for any unused dependencies.
onlyDiagnostic := len(diagnostics) == 0 && len(unused) == 1
for _, req := range unused {
+ if shouldIgnoreUnused(req) {
+ continue
+ }
srcErr, err := unusedDiagnostic(pm.Mapper, req, onlyDiagnostic)
if err != nil {
return nil, err
@@ -322,6 +325,15 @@
return diagnostics, nil
}
+func shouldIgnoreUnused(req *modfile.Require) bool {
+ for _, comment := range req.Syntax.Comments.Suffix {
+ if strings.Contains(comment.Token, "ignore-unused") {
+ return true
+ }
+ }
+ return false
+}
+
// unusedDiagnostic returns a source.Diagnostic for an unused require.
func unusedDiagnostic(m *protocol.ColumnMapper, req *modfile.Require, onlyDiagnostic bool) (*source.Diagnostic, error) {
rng, err := rangeFromPositions(m, req.Syntax.Start, req.Syntax.End)
To view, visit change 390254. To unsubscribe, or for help writing mail filters, visit settings.
Congratulations on opening your first change. Thank you for your contribution!
Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.
Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.
Attention is currently required from: Robert Findley, Hyang-Ah Hana Kim.
1 comment:
Patchset:
I think we need agreement that this is the right approach.
If it is the right approach, it needs to be documented. But let's first agree that this is the way to go. That should be discussed on the issue. Thanks.
To view, visit change 390254. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Hyang-Ah Hana Kim.
1 comment:
Patchset:
I think we need agreement that this is the right approach. […]
Yes, I think it would be cleaner to allow configuring the verbosity of modfile diagnostics in gopls, rather than annotating imports with special comments intended only for gopls.
To view, visit change 390254. To unsubscribe, or for help writing mail filters, visit settings.