go-ruleguard: go1.21: load rules: parse rules file: typechecker error: ...: could not import

Hello,

I know it’s a bit early but I started to work on go1.21 for golangci-lint. https://github.com/golangci/golangci-lint/pull/3922

And there is an issue with go-ruleguard. I run the tests of go-ruleguard and there is the same problem:

ruleguard init error, skip ./rules.go: typechecker error: ./rules.go:6:8: could not import github.com/quasilyte/go-ruleguard/dsl (can't find import: "github.com/quasilyte/go-ruleguard/dsl")

Currently, I don’t know the root cause but I want to share it with you, I think will you see the problem faster than me.

Maybe it’s related to the new package initialization order, I don’t know.

I already created an issue on go-critic.

go test
$ go test ./...                             
?       github.com/quasilyte/go-ruleguard       [no test files]
?       github.com/quasilyte/go-ruleguard/analyzer/testanalyzer [no test files]
?       github.com/quasilyte/go-ruleguard/cmd/ruleguard [no test files]
?       github.com/quasilyte/go-ruleguard/internal/golist       [no test files]
?       github.com/quasilyte/go-ruleguard/internal/xtypes       [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/goutil      [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/ir  [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/irprint     [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/profiling   [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/quasigo/internal/evaltest   [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/quasigo/stdlib/qfmt [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/quasigo/stdlib/qstrconv     [no test files]
?       github.com/quasilyte/go-ruleguard/ruleguard/quasigo/stdlib/qstrings     [no test files]
--- FAIL: TestAnalyzer (4.78s)
    --- FAIL: TestAnalyzer/gocritic (0.38s)
        analysistest.go:295: error analyzing ruleguard@gocritic: load rules: parse rules file: typechecker error: ./testdata/src/gocritic/rules.go:6:8: could not import github.com/quasilyte/go-ruleguard/dsl (can't find import: "github.com/quasilyte/go-ruleguard/dsl")
...

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22 (21 by maintainers)

Commits related to this issue

Most upvoted comments

Maybe someone can reopen this issue.

go env output changed in go1.21 to be single-quoted and escape single quotes as '\'' in https://go-review.googlesource.com/c/go/+/493535

parseGoEnv() in https://github.com/quasilyte/go-ruleguard/blob/master/internal/goenv/goenv.go has to handle both double-quoted and single-quoted formats now, something like:

	} else {
		// Line format is: `$name="$value"` before go1.21
		// Line format is: `$name='$value'` in go1.21+
		for _, l := range lines {
			parts := strings.Split(strings.TrimSpace(l), "=")
			if len(parts) != 2 {
				continue
			}
			val := parts[1]
			switch {
			case val == `""` || val == `''`:
				val = ""
			case len(val) > 2 && val[0] == '"' && val[len(val)-1] == '"':
				// go < 1.21 before https://github.com/golang/go/commit/f379e78951a405e7e99a60fb231eeedbf976c108
				var err error
				val, err = strconv.Unquote(parts[1])
				if err != nil {
					continue
				}
			case len(val) > 2 && val[0] == '\'' && val[len(val)-1] == '\'':
				// go >= 1.21 after https://github.com/golang/go/commit/f379e78951a405e7e99a60fb231eeedbf976c108
				// trim leading/trailing single-quotes
				val = val[1 : len(val)-1]
				// replace internally escaped single-quotes
				val = strings.ReplaceAll(val, `'\''`, `'`)
			default:
				val = ""
			}
			vars[parts[0]] = val
		}
	}

The rc4 is here, the GA is closer than ever.

@quasilyte can I help you?

see my PR #451

hrmm… my fix resolved the issue I was seeing finding stdlib imports… the error you’re seeing looks to be in resolving non-stdlib imports

there may be more issues to resolve here, though

I try your suggestion (I will remove it after) inside my PR https://github.com/quasilyte/go-ruleguard/pull/451 And it works!