Skip to content

Commit 09c65fc

Browse files
committed
rework modules download mode option
1 parent b693037 commit 09c65fc

File tree

5 files changed

+54
-14
lines changed

5 files changed

+54
-14
lines changed

.golangci.example.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ run:
3636
- ".*\\.my\\.go$"
3737
- lib/bad.go
3838

39+
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
40+
# If invoked with -mod=readonly, the go command is disallowed from the implicit
41+
# automatic updating of go.mod described above. Instead, it fails when any changes
42+
# to go.mod are needed. This setting is most useful to check that go.mod does
43+
# not need updates, such as in a continuous integration and testing system.
44+
# If invoked with -mod=vendor, the go command assumes that the vendor
45+
# directory holds the correct copies of dependencies and ignores
46+
# the dependency descriptions in go.mod.
47+
modules-download-mode: readonly|release|vendor
48+
3949

4050
# output configuration options
4151
output:

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ Flags:
432432
--print-linter-name Print linter name in issue line (default true)
433433
--issues-exit-code int Exit code when issues were found (default 1)
434434
--build-tags strings Build tags
435-
--mod string module download mode to use: readonly or vendor (passed to go list)
436435
--deadline duration Deadline for total work (default 1m0s)
437436
--tests Analyze tests (*_test.go) (default true)
438437
--print-resources-usage Print avg and max memory usage of golangci-lint and total time
@@ -552,6 +551,16 @@ run:
552551
- ".*\\.my\\.go$"
553552
- lib/bad.go
554553
554+
# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
555+
# If invoked with -mod=readonly, the go command is disallowed from the implicit
556+
# automatic updating of go.mod described above. Instead, it fails when any changes
557+
# to go.mod are needed. This setting is most useful to check that go.mod does
558+
# not need updates, such as in a continuous integration and testing system.
559+
# If invoked with -mod=vendor, the go command assumes that the vendor
560+
# directory holds the correct copies of dependencies and ignores
561+
# the dependency descriptions in go.mod.
562+
modules-download-mode: readonly|release|vendor
563+
555564
556565
# output configuration options
557566
output:

pkg/commands/run.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager) {
6363
fs.IntVar(&rc.ExitCodeIfIssuesFound, "issues-exit-code",
6464
exitcodes.IssuesFound, wh("Exit code when issues were found"))
6565
fs.StringSliceVar(&rc.BuildTags, "build-tags", nil, wh("Build tags"))
66-
fs.StringVar(&rc.Mod, "mod", "", wh("module download mode to use: readonly or vendor (passed to go list)"))
6766
fs.DurationVar(&rc.Deadline, "deadline", time.Minute, wh("Deadline for total work"))
6867
fs.BoolVar(&rc.AnalyzeTests, "tests", true, wh("Analyze tests (*_test.go)"))
6968
fs.BoolVar(&rc.PrintResourcesUsage, "print-resources-usage", false,

pkg/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ type Run struct {
109109

110110
Args []string
111111

112-
BuildTags []string `mapstructure:"build-tags"`
113-
Mod string `mapstructure:"mod"`
112+
BuildTags []string `mapstructure:"build-tags"`
113+
ModulesDownloadMode string `mapstructure:"modules-download-mode"`
114114

115115
ExitCodeIfIssuesFound int `mapstructure:"issues-exit-code"`
116116
AnalyzeTests bool `mapstructure:"tests"`

pkg/lint/load.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,23 +193,45 @@ func (cl ContextLoader) buildArgs() []string {
193193
return retArgs
194194
}
195195

196+
func (cl ContextLoader) makeBuildFlags() ([]string, error) {
197+
var buildFlags []string
198+
199+
if len(cl.cfg.Run.BuildTags) != 0 {
200+
// go help build
201+
buildFlags = append(buildFlags, "-tags", strings.Join(cl.cfg.Run.BuildTags, " "))
202+
}
203+
204+
mod := cl.cfg.Run.ModulesDownloadMode
205+
if mod != "" {
206+
// go help modules
207+
allowedMods := []string{"release", "readonly", "vendor"}
208+
var ok bool
209+
for _, am := range allowedMods {
210+
if am == mod {
211+
ok = true
212+
break
213+
}
214+
}
215+
if !ok {
216+
return nil, fmt.Errorf("invalid modules download path %s, only (%s) allowed", mod, strings.Join(allowedMods, "|"))
217+
}
218+
219+
buildFlags = append(buildFlags, fmt.Sprintf("-mod=%s", cl.cfg.Run.ModulesDownloadMode))
220+
}
221+
222+
return buildFlags, nil
223+
}
224+
196225
func (cl ContextLoader) loadPackages(ctx context.Context, loadMode packages.LoadMode) ([]*packages.Package, error) {
197226
defer func(startedAt time.Time) {
198227
cl.log.Infof("Go packages loading at mode %s took %s", stringifyLoadMode(loadMode), time.Since(startedAt))
199228
}(time.Now())
200229

201230
cl.prepareBuildContext()
202231

203-
var buildFlags []string
204-
205-
if len(cl.cfg.Run.BuildTags) != 0 {
206-
// go help build
207-
buildFlags = []string{"-tags", strings.Join(cl.cfg.Run.BuildTags, " ")}
208-
}
209-
210-
if cl.cfg.Run.Mod != "" {
211-
// go help module
212-
buildFlags = append(buildFlags, fmt.Sprintf("-mod=%s", cl.cfg.Run.Mod))
232+
buildFlags, err := cl.makeBuildFlags()
233+
if err != nil {
234+
return nil, errors.Wrap(err, "failed to make build flags for go list")
213235
}
214236

215237
conf := &packages.Config{

0 commit comments

Comments
 (0)