Skip to content

m-ocean-it/errgroup-ctx-lint

Repository files navigation

errgroup-ctx-lint

About

This linter catches cases when, within an error-group goroutine, a non-errgroup context is passed to a function or method, while there is a context, specifically attached to the said errgroup. In most cases, you want that specific context to be passed to functions invoked within the errgroup's Go methods.

eg, egCtx := errgroup.WithContext(ctx)

eg.Go(func() error {
	return doSmth(ctx) // want `errgroup callback should probably not reference outer context "ctx", use the errgroup-derived context "egCtx"`
})

eg.Go(func() error {
	return doSmth(egCtx) // Correctly uses the context returned by "errgroup.WithContext"
})

eg.TryTo(func() error {
	return doSmth(ctx) // want `errgroup callback should probably not reference outer context "ctx", use the errgroup-derived context "egCtx"`
})

A lot more cases are covered in the examples.go file!

Golangci-lint plugin guide

Read the official guide.

Prepare a .custom-gcl.yml file:

version: v2.11.4
plugins:
  - module: "github.com/m-ocean-it/errgroup-ctx-lint"
    import: "github.com/m-ocean-it/errgroup-ctx-lint"
    version: latest

Run

golangci-lint custom -v

A custom binary of golangci-lint would appear at path ./custom-gcl.

Prepare a .golangci.yml config file (or amend the existing one):

version: "2"

linters:
  default: none
  enable:
    - errgroupctx
  settings:
    custom:
      errgroupctx:
        type: "module"
        settings:
          errgroup_package_paths:
            # Specify alternative errgroup packages here, if needed, like so:
            # - golang.org/x/sync/errgroup
            # - errgroup1
            # - foobar/errgroup2

Run the resulted binary like the original golangci-lint:

./custom-gcl run ./...

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors