internal/worker: enforce go.mod dependency download for args.init - Previously, go.mod dependencies were not fully downloaded when args.init was true. This change serves to ensure that go.mod dependencies are downloaded if the file exists. Otherwise, it will be created with `go mod init && go mod tidy`. Change-Id: Idd631f5a05dd394d8f39bfc1ac766f181e822a83 Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/705935 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com> Auto-Submit: Ethan Lee <ethanalee@google.com>
diff --git a/internal/worker/scan.go b/internal/worker/scan.go index babae13..0bc5e8b 100644 --- a/internal/worker/scan.go +++ b/internal/worker/scan.go
@@ -250,22 +250,32 @@ // prepareModule prepares a module for scanning, and takes other actions that increase // the chance that package loading will succeed. func prepareModule(ctx context.Context, args prepareModuleArgs) error { - if args.noDeps || args.init { - log.Infof(ctx, "downloading %s@%s to %s", args.modulePath, args.version, args.dir) - if err := modules.Download(ctx, args.modulePath, args.version, args.dir, args.proxyClient); err != nil { - return err - } - hasGoMod := fileExists(filepath.Join(args.dir, "go.mod")) - if !hasGoMod && args.init { - return goModInit(ctx, args.modulePath, args.version, args.dir, args.modulePath, args.insecure) - } - return nil - } - opts := &goCommandOptions{ dir: args.dir, insecure: args.insecure, } + if args.noDeps || args.init { + log.Infof(ctx, "downloading %s@%s to %s", args.modulePath, args.version, args.dir) + if err := modules.Download(ctx, args.modulePath, args.version, args.dir, args.proxyClient); err != nil { + log.Warnf(ctx, "`modules.Download` error: %v", err) + return err + } + + if args.noDeps { + log.Infof(ctx, "Skipping downloading modules for %s@%s since args.dir is %t", args.modulePath, args.version, args.noDeps) + return nil + } + + hasGoMod := fileExists(filepath.Join(args.dir, "go.mod")) + if hasGoMod { + return runGoCommand(ctx, args.modulePath, args.version, opts, "mod", "download") + } else { + if err := goModInit(ctx, args.modulePath, args.version, args.dir, args.modulePath, args.insecure); err != nil { + return err + } + return goModTidy(ctx, args.modulePath, args.version, args.dir, args.insecure) + } + } log.Infof(ctx, "prepareModule %s@%s to %s", args.modulePath, args.version, args.dir) if err := runGoCommand(ctx, args.modulePath, args.version, opts, "mod", "init", "an.empty/module"); err != nil {