Allow for serializing multiple golangci-lint invocations (#1302)

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
This commit is contained in:
Romanos 2020-08-28 07:06:28 +01:00 committed by GitHub
parent e2d717b873
commit 1a2a672644
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -218,11 +218,15 @@ func (e *Executor) acquireFileLock() bool {
lockFile := filepath.Join(os.TempDir(), "golangci-lint.lock") lockFile := filepath.Join(os.TempDir(), "golangci-lint.lock")
e.debugf("Locking on file %s...", lockFile) e.debugf("Locking on file %s...", lockFile)
f := flock.New(lockFile) f := flock.New(lockFile)
const totalTimeout = 5 * time.Second
const retryDelay = 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 { if ok, _ := f.TryLockContext(ctx, retryDelay); !ok {
return false return false
} }

View File

@ -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. " + const allowParallelDesc = "Allow multiple parallel golangci-lint instances running. " +
"If false (default) - golangci-lint acquires file lock on start." "If false (default) - golangci-lint acquires file lock on start."
fs.BoolVar(&rc.AllowParallelRunners, "allow-parallel-runners", false, wh(allowParallelDesc)) 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 // Linters settings config
lsc := &cfg.LintersSettings lsc := &cfg.LintersSettings

View File

@ -152,6 +152,7 @@ type Run struct {
UseDefaultSkipDirs bool `mapstructure:"skip-dirs-use-default"` UseDefaultSkipDirs bool `mapstructure:"skip-dirs-use-default"`
AllowParallelRunners bool `mapstructure:"allow-parallel-runners"` AllowParallelRunners bool `mapstructure:"allow-parallel-runners"`
AllowSerialRunners bool `mapstructure:"allow-serial-runners"`
} }
type LintersSettings struct { type LintersSettings struct {