[tools] Feature: allow ignoring unused modules for go mod tidy

565 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Mar 6, 2022, 1:07:07 PM3/6/22
to goph...@pubsubhelper.golang.org, Anna Rosenthal, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

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.

Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: Ia05f9a9284c5cf428f07eef166ecce311cb967af
Gerrit-Change-Number: 390254
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Anna Rosenthal <annaro...@github.com>
Gerrit-MessageType: newchange

Gopher Robot (Gerrit)

unread,
Mar 6, 2022, 1:07:31 PM3/6/22
to Gerrit Bot, Anna Rosenthal, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

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.

View Change

    To view, visit change 390254. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia05f9a9284c5cf428f07eef166ecce311cb967af
    Gerrit-Change-Number: 390254
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Anna Rosenthal <annaro...@github.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Sun, 06 Mar 2022 18:07:27 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Ian Lance Taylor (Gerrit)

    unread,
    Mar 7, 2022, 1:04:21 AM3/7/22
    to Gerrit Bot, Anna Rosenthal, goph...@pubsubhelper.golang.org, Hyang-Ah Hana Kim, Robert Findley, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Robert Findley, Hyang-Ah Hana Kim.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #1:

        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.

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia05f9a9284c5cf428f07eef166ecce311cb967af
    Gerrit-Change-Number: 390254
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-CC: Anna Rosenthal <annaro...@github.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Robert Findley <rfin...@google.com>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 07 Mar 2022 06:04:16 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Robert Findley (Gerrit)

    unread,
    Mar 7, 2022, 12:48:21 PM3/7/22
    to Gerrit Bot, Anna Rosenthal, goph...@pubsubhelper.golang.org, Ian Lance Taylor, Hyang-Ah Hana Kim, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Ian Lance Taylor, Hyang-Ah Hana Kim.

    View Change

    1 comment:

    • Patchset:

      • Patch Set #1:

        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.

    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: Ia05f9a9284c5cf428f07eef166ecce311cb967af
    Gerrit-Change-Number: 390254
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Reviewer: Robert Findley <rfin...@google.com>
    Gerrit-CC: Anna Rosenthal <annaro...@github.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
    Gerrit-Attention: Hyang-Ah Hana Kim <hya...@gmail.com>
    Gerrit-Comment-Date: Mon, 07 Mar 2022 17:48:17 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Ian Lance Taylor <ia...@golang.org>
    Gerrit-MessageType: comment
    Reply all
    Reply to author
    Forward
    0 new messages