Skip to content

Commit 1a2a672

Browse files
authored
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
1 parent e2d717b commit 1a2a672

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

pkg/commands/executor.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,15 @@ func (e *Executor) acquireFileLock() bool {
218218
lockFile := filepath.Join(os.TempDir(), "golangci-lint.lock")
219219
e.debugf("Locking on file %s...", lockFile)
220220
f := flock.New(lockFile)
221-
const totalTimeout = 5 * time.Second
222221
const retryDelay = time.Second
223-
ctx, finish := context.WithTimeout(context.Background(), totalTimeout)
224-
defer finish()
225222

223+
ctx := context.Background()
224+
if !e.cfg.Run.AllowSerialRunners {
225+
const totalTimeout = 5 * time.Second
226+
var cancel context.CancelFunc
227+
ctx, cancel = context.WithTimeout(ctx, totalTimeout)
228+
defer cancel()
229+
}
226230
if ok, _ := f.TryLockContext(ctx, retryDelay); !ok {
227231
return false
228232
}

pkg/commands/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
111111
const allowParallelDesc = "Allow multiple parallel golangci-lint instances running. " +
112112
"If false (default) - golangci-lint acquires file lock on start."
113113
fs.BoolVar(&rc.AllowParallelRunners, "allow-parallel-runners", false, wh(allowParallelDesc))
114+
const allowSerialDesc = "Allow multiple golangci-lint instances running, but serialize them around a lock. " +
115+
"If false (default) - golangci-lint exits with an error if it fails to acquire file lock on start."
116+
fs.BoolVar(&rc.AllowSerialRunners, "allow-serial-runners", false, wh(allowSerialDesc))
114117

115118
// Linters settings config
116119
lsc := &cfg.LintersSettings

pkg/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ type Run struct {
152152
UseDefaultSkipDirs bool `mapstructure:"skip-dirs-use-default"`
153153

154154
AllowParallelRunners bool `mapstructure:"allow-parallel-runners"`
155+
AllowSerialRunners bool `mapstructure:"allow-serial-runners"`
155156
}
156157

157158
type LintersSettings struct {

0 commit comments

Comments
 (0)