Skip to content
enforce banned dependency imports in Go code
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
testdata/pkgs
.gitignore
.goreleaser.yaml
LICENSE
Makefile
README.md
main.go
main_test.go

README.md

bandep

Bandep is a tool meant to be used in the CI and/or git commit hooks to prevent users from adding banned deps.

Use Cases

  • you have multiple packages with the same name in GOPATH (maybe even the same methods) and your editor sometimes import the wrong import in some files (example)
  • you have explicit rules in your codebase regarding which packages can import which packages

Usage

A basic usage would be to ban some deps in the current project, recursively:

bandep --pkg ./... --ban foo/bar,github.com/foo/bar

Or, for example, on DigitalOcean, they forbid the exp package to be imported from anywhere but the exp package itself:

go list -f '{{ .Dir }}' -e ./... | grep -v do/exp | while read -r pkg; do
  bandep --pkg $pkg --ban do/exp/foo
done

Composing it with shell (and go list), you can do basically anything you need.

You can, of course, add it to your git pre-commit hook. Check out this example.

Install

go get github.com/caarlos0/bandep

or

brew install caarlos0/tap/bandep

Or download one from the releases tab and install manually.

How it works

It is quite simple, really: we scan the dirs into an AST, and use that AST to check if a banned import is being used.

You can’t perform that action at this time.