• Stars
    star
    229
  • Rank 174,666 (Top 4 %)
  • Language
    Go
  • License
    Other
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Report unwanted import path and declaration usages

faillint

Faillint is a simple Go linter that fails when a specific set of import paths or exported path's functions, constant, vars or types are used. It's meant to be used in CI/CD environments to catch rules you want to enforce in your projects.

As an example, you could enforce the usage of github.com/pkg/errors instead of the errors package. To prevent the usage of the errors package, you can configure faillint to fail whenever someone imports the errors package in this case. To make sure fmt.Errorf is not used for creating errors as well, you can configure to fail on such single function of fmt as well.

faillint

Install

go install github.com/fatih/faillint@latest

Example

Assume we have the following file:

package a

import (
        "errors"
)

func foo() error {
        return errors.New("bar!")
}

Let's run faillint to check if errors import is used and report it:

$ faillint -paths "errors=github.com/pkg/errors" a.go
a.go:4:2: package "errors" shouldn't be imported, suggested: "github.com/pkg/errors"

Usage

faillint works on a file, directory or a Go package:

$ faillint -paths "errors,fmt.{Errorf}" foo.go # pass a file
$ faillint -paths "errors,fmt.{Errorf}" ./...  # recursively analyze all files
$ faillint -paths "errors,fmt.{Errorf}" github.com/fatih/gomodifytags # or pass a package

By default, faillint will not check any import paths. You need to explicitly define it with the -paths flag, which is comma-separated list. Some examples are:

# Fail if the errors package is used.
-paths "errors"

# Fail if the old context package is imported.
-paths "golang.org/x/net/context"

# Fail both on stdlib log and errors package to enforce other internal libraries.
-paths "log,errors"

# Fail if any of Print, Printf of Println function were used from fmt library.
-paths "fmt.{Print,Printf,Println}"

# Fail if the package is imported including sub paths starting with
  "golang.org/x/net/". In example: `golang.org/x/net/context`, 
  `golang.org/x/net/nettest`, .nettest`, ...
-paths "golang.org/x/net/..."

If you have a preferred import path to suggest, append the suggestion after a = character:

# Fail if the errors package is used and suggest to use github.com/pkg/errors.
-paths "errors=github.com/pkg/errors"

# Fail for the old context import path and suggest to use the stdlib context.
-paths "golang.org/x/net/context=context"

# Fail both on stdlib log and errors package to enforce other libraries.
-paths "log=go.uber.org/zap,errors=github.com/pkg/errors"

# Fail on fmt.Errorf and suggest the Errorf function from github.compkg/errors instead.
-paths "fmt.{Errorf}=github.com/pkg/errors.{Errorf}"

Ignoring problems

If you want to ignore a problem reported by faillint you can add a lint directive based on staticcheck's design.

Line-based lint directives

Line-based lint directives can be applied to imports or functions you want to tolerate. The format is,

//lint:ignore faillint reason

For example,

package a

import (
        //lint:ignore faillint Whatever your reason is.
        "errors"
        "fmt" //lint:ignore faillint Whatever your reason is.
)

func foo() error {
        //lint:ignore faillint Whatever your reason is.
        return errors.New("bar!")
}

File-based lint directives

File-based lint directives can be applied to ignore faillint problems in a whole file. The format is,

//lint:file-ignore faillint reason

This may be placed anywhere in the file but conventionally it should be placed at, or near, the top of the file.

For example,

//lint:file-ignore faillint This file should be ignored by faillint.

package a

import (
        "errors"
)

func foo() error {
        return errors.New("bar!")
}

The need for this tool?

Most of these checks should be probably detected during the review cycle. But it's totally normal to accidentally import them (we're all humans in the end).

Second, tools like goimports favors certain packages. As an example going forward if you decided to use github.com/pkg/errors in you project, and write errors.New() in a new file, goimports will automatically import the errors package (and not github.com/pkg/errors). The code will perfectly compile. faillint would be able to detect and report it to you.

Credits

This tool is built on top of the excellent go/analysis package that makes it easy to write custom analyzers in Go. If you're interested in writing a tool, check out my Using go/analysis to write a custom linter blog post.

Part of the code is modified and based on astutil.UsesImport

More Repositories

1

vim-go

Go development plugin for Vim
Vim Script
15,577
star
2

color

Color package for Go (golang)
Go
6,536
star
3

structs

Utilities for Go structs
Go
3,811
star
4

vim-go-tutorial

Tutorial for vim-go
Vim Script
2,122
star
5

gomodifytags

Go tool to modify struct field tags
Go
1,995
star
6

pool

Connection pool for Go's net.Conn interface
Go
1,331
star
7

subvim

Vim customized to be like SublimeText
C++
1,122
star
8

dotfiles

My personal dotfiles
Lua
792
star
9

set

Set data structure for Go
Go
657
star
10

structtag

Parse and modify Go struct field tags
Go
568
star
11

errwrap

Go tool to wrap and fix errors with the new %w verb directive
Go
366
star
12

semgroup

Like errgroup/waitgroup, but only runs a maximum of tasks at any time.
Go
280
star
13

hclfmt

Format and prettify HCL files
Go
227
star
14

motion

Navigation and insight in Go
Go
180
star
15

astrewrite

Go tool to walk & rewrite AST
Go
166
star
16

camelcase

Split a camelcase word into a slice of words in Go
Go
158
star
17

starhook

Manage & Analyze repositories at scale
Go
93
star
18

vim-hclfmt

Vim plugin for hclfmt
Vim Script
73
star
19

stopwatch

Stopwatch functionality for Go
Go
69
star
20

images

Images is a tool for managing machine images from multiple providers
Go
68
star
21

addlint

An example linter written with go/analysis for tutorial purposes
Go
53
star
22

gb-example

Example gb project with dependencies and CI integration
Go
47
star
23

hcl

HCL Parser and Printer in Go
Go
44
star
24

templatectl

Simple templating CLI
Go
42
star
25

twirpdemo

An example repository of using the Twirp RPC framework with Go
Go
32
star
26

talks

My personal talk slides
Go
24
star
27

unexport

Unexport notused exported identifiers in Go
Go
22
star
28

kodla-talk-2022

Code and slides for Kodla 2022
Go
20
star
29

flags

Flag parsing in Go
Go
18
star
30

vim-nginx

Nginx runtime files for Vim
Vim Script
17
star
31

dvb-t2

Software implementation of DVB-T2
Objective-C
16
star
32

sicp

My personal notes, solutions, thoughts, etc.. about SICP
16
star
33

amqp-examples

Examples to show basic amqp commands in different languages
Go
15
star
34

testmod

Testing Go modules
AMPL
11
star
35

RailsDashboard.kdapp

An easy way to learn, test and deploy Rails
CoffeeScript
7
star
36

cafetiere

An iOS app to make beautiful Coffee
Objective-C
6
star
37

koding-wiki

Koding framework docs to build KD Apps
6
star
38

blog.arsln.org-backup

Fatih Arslan's Personal Blog
CSS
4
star
39

docker-ubuntu-go

Docker image for Go and Ubuntu
Shell
4
star
40

sinerji

A gui written in PyQt4 that uses Avahi as backend for Synergy
Python
1
star
41

snippets

Snippets, code examples, etc..
C
1
star
42

pisi-vim

A vim plugin for pisi packaging
Vim Script
1
star