Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gocritic: ruleguard init error could not import github.com/quasilyte/go-ruleguard/dsl #1152

Open
sebastien-rosset opened this issue Nov 5, 2021 · 1 comment
Labels
bug Something isn't working ruleguard

Comments

@sebastien-rosset
Copy link
Contributor

sebastien-rosset commented Nov 5, 2021

Symptom

When running ruleguard from golangci-lint, ruleguard is unable to import the github.com/quasilyte/go-ruleguard/dsl package. An error is returned.

Steps to reproduce

  1. Install golangci-lint version 1.43.0
  2. Execute golangci-lint -c golangci-lint.yaml run with gocritic/ruleguard enabled.
  3. Notice the following error message is printed. None of the ruleguard rules are executed.
WARN [runner] Can't run linter goanalysis_metalinter: gocritic:
ruleguard init error: typechecker error: gorules/logging-rules.go:7:2:
could not import github.com/quasilyte/go-ruleguard/dsl
(can't find import: "github.com/quasilyte/go-ruleguard/dsl")

Environment

golangci-lint --version
golangci-lint has version 1.43.0 built from 861262b7 on 2021-11-03T11:57:46Z

golangci.yaml config:

linters:
  enable:
    - gocritic
linters-settings:
 gocritic:
    enabled-checks:
      - ruleguard
    settings:
      ruleguard:
        failOn: dsl,import
        rules: "${configDir}/gorules/myrule.go"

${configDir}/gorules/myrule.go:

// go:build ruleguard
package ruleguard

import "github.com/quasilyte/go-ruleguard/dsl"

func StringsSimplify(m dsl.Matcher) {
	// Some issues have simple fixes that can be expressed as
	// a replacement pattern. Rules can use Suggest() function
	// to add a quickfix action for such issues.
	m.Match(`strings.Replace($s, $old, $new, -1)`).
		Report(`this Replace call can be simplified`).
		Suggest(`strings.ReplaceAll($s, $old, $new)`)
}

Workaround

Execute the following command. This installs the required dependency in the go cache.
go get github.com/quasilyte/go-ruleguard/dsl

Additional Information

It turns out that two days prior to installing golangci-lint, I cleaned up the go cache by running go clean -modcache. This had the effect of removing github.com/quasilyte/go-ruleguard/dsl from the cache. The same problem would probably occur when doing a fresh installation of golangci-lint, because the ruleguard dependency would not be installed.

I'm not sure if this is a regression.

Proposed changes

Should gocritic automatically execute go get github.com/quasilyte/go-ruleguard/dsl? It does not seem right that the user has to manually install ruleguard. Also, the error is quite obscure, it's not obvious what needs to be done to workaround the problem.

michalnicp pushed a commit to fleetdm/fleet that referenced this issue Jul 12, 2022
Related to go-critic/go-critic#1152
Need to have github.com/quasilyte/go-ruleguard/dsl
michalnicp added a commit to fleetdm/fleet that referenced this issue Jul 19, 2022
* add upgrade tests

* fix lint issues

go.mod

* remove req.cnf

* revert unrelated changes

* make version configurable in test

* fix golangci-lint ruleguard issue

Related to go-critic/go-critic#1152
Need to have github.com/quasilyte/go-ruleguard/dsl

* fix lint issues

* fix

* clean up docker-compose.yml

* fix http request

* add readme

* fix lint issues

* address feedback

* fix

* add platform

* address feedback

* run go fmt
@cristaloleg
Copy link
Member

I think this is already solved with in a newer version. Can we resolve this? CC: @quasilyte

@cristaloleg cristaloleg added bug Something isn't working ruleguard labels Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ruleguard
Projects
None yet
Development

No branches or pull requests

2 participants