From ccec7355958be0172adfbceaa328c35d878dc533 Mon Sep 17 00:00:00 2001 From: Romanos Skiadas Date: Mon, 10 Aug 2020 17:43:26 +0100 Subject: [PATCH] Allow for serializing multiple golangci-lint invocations By default, golangci-lint fails after five seconds if another instance is running. It is possible to disable that, but the discussion around whether the cache is safe to use concurrently is not exactly full of confidence. Add a flag that allows golangci-lint to wait forever instead of failing. see #1301 --- pkg/commands/executor.go | 10 +++++++--- pkg/commands/run.go | 3 +++ pkg/config/config.go | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/commands/executor.go b/pkg/commands/executor.go index 9581acab21f2..0becc9900db6 100644 --- a/pkg/commands/executor.go +++ b/pkg/commands/executor.go @@ -218,11 +218,15 @@ func (e *Executor) acquireFileLock() bool { lockFile := filepath.Join(os.TempDir(), "golangci-lint.lock") e.debugf("Locking on file %s...", lockFile) f := flock.New(lockFile) - const totalTimeout = 5 * time.Second const retryDelay = time.Second - ctx, finish := context.WithTimeout(context.Background(), totalTimeout) - defer finish() + ctx := context.Background() + if !e.cfg.Run.AllowSerialRunners { + const totalTimeout = 5 * time.Second + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, totalTimeout) + defer cancel() + } if ok, _ := f.TryLockContext(ctx, retryDelay); !ok { return false } diff --git a/pkg/commands/run.go b/pkg/commands/run.go index 92f0b175f0df..62853f246665 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -111,6 +111,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is const allowParallelDesc = "Allow multiple parallel golangci-lint instances running. " + "If false (default) - golangci-lint acquires file lock on start." fs.BoolVar(&rc.AllowParallelRunners, "allow-parallel-runners", false, wh(allowParallelDesc)) + const allowSerialDesc = "Allow multiple golangci-lint instances running, but serialize them around a lock. " + + "If false (default) - golangci-lint exits with an error if it fails to acquire file lock on start." + fs.BoolVar(&rc.AllowSerialRunners, "allow-serial-runners", false, wh(allowSerialDesc)) // Linters settings config lsc := &cfg.LintersSettings diff --git a/pkg/config/config.go b/pkg/config/config.go index 22004bc17567..33b49435e20c 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -152,6 +152,7 @@ type Run struct { UseDefaultSkipDirs bool `mapstructure:"skip-dirs-use-default"` AllowParallelRunners bool `mapstructure:"allow-parallel-runners"` + AllowSerialRunners bool `mapstructure:"allow-serial-runners"` } type LintersSettings struct {