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

Add troubleshooting sectipon to README.md #411

Merged
merged 1 commit into from
Sep 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ Flags:
Create a test `rules.go` file:

```go
// +build ignore
//go:build ruleguard
// +build ruleguard

package gorules

Expand Down Expand Up @@ -163,6 +164,55 @@ A rule definition always starts with [`Match(patterns...)`](https://godoc.org/gi

There can be additional calls in between these two. For example, a [`Where(cond)`](https://godoc.org/github.com/quasilyte/go-ruleguard/dsl#Matcher.Where) call applies constraints to a match to decide whether its accepted or rejected. So even if there is a match for a pattern, it won't produce a report message unless it satisfies a `Where()` condition.

## Troubleshooting

For ruleguard to work the `dsl` package must be available at _runtime_. If it's not, you are going to see an error like:

```
$ ruleguard -rules rules.go .
ruleguard: load rules: parse rules file: 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")
```

This is fixed by adding the dsl package to the module:

```
$ ruleguard-test go get github.com/quasilyte/go-ruleguard/dsl
go: downloading github.com/quasilyte/go-ruleguard v0.3.18
go: downloading github.com/quasilyte/go-ruleguard/dsl v0.3.21
go: added github.com/quasilyte/go-ruleguard/dsl v0.3.21
$ ruleguard-test ruleguard -rules rules.go .
.../test.go:6:5: boolExprSimplify: suggestion: 1 == 0 (rules.go:9)
```

If you have followed past advise of using a build constraint in the rules.go file, like this:

```
$ ruleguard-test head -4 rules.go
//go:build ignore
// +build ignore

package gorules
```

you are going to notice that `go.mod` file lists the dsl as an indirect dependency:

```
$ grep dsl go.mod
require github.com/quasilyte/go-ruleguard/dsl v0.3.21 // indirect
```

If you run `go mod tidy` now, you are going to notice the the dsl package dissapears from the `go.mod` file:

```
$ go mod tidy
$ grep dsl go.mod
$
```

This is because `go mod tidy` behaves as if all the build constaints are in effect, _with the exception of `ignore`_. [This is documented in the go website](https://go.dev/ref/mod#go-mod-tidy).

This is fixed by using a different build constraint, like `ruleguard` or `rules`.

## Documentation

* [Ruleguard by example](https://go-ruleguard.github.io/by-example/) tour
Expand Down
3 changes: 2 additions & 1 deletion _docs/rules.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build ignore
//go:build ruleguard
// +build ruleguard

package gorules

Expand Down