internal/analysis: add param for skip initialization of non-modules

This is useful for, say, gomodstat analysis that explicitly analyzes
go.mod files.

Change-Id: I0a7216fae6a1bbd196bbf3c7c0e0dc654cbede7a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/546595
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Zvonimir Pavlinovic <zpavlinovic@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/analysis/analysis.go b/internal/analysis/analysis.go
index fcbbf67..4deb9a6 100644
--- a/internal/analysis/analysis.go
+++ b/internal/analysis/analysis.go
@@ -36,6 +36,7 @@
 	Insecure      bool   // if true, run outside sandbox
 	Serve         bool   // serve results back to client instead of writing them to BigQuery
 	JobID         string // ID of job, if non-empty
+	SkipInit      bool   // if true, do not initialize non-module Go projects
 }
 
 type EnqueueParams struct {
@@ -46,6 +47,7 @@
 	File     string // path to file containing modules; if missing, use DB
 	Suffix   string // appended to task queue IDs to generate unique tasks
 	User     string // user initiating enqueue
+	SkipInit bool   // if true, do not initialize non-module Go projects
 }
 
 // Request implements queue.Task so it can be put on a TaskQueue.
diff --git a/internal/worker/analysis.go b/internal/worker/analysis.go
index 4ee0b73..b127beb 100644
--- a/internal/worker/analysis.go
+++ b/internal/worker/analysis.go
@@ -242,8 +242,7 @@
 }
 
 func (s *analysisServer) scanInternal(ctx context.Context, req *analysis.ScanRequest, binaryPath, moduleDir string) (jt analysis.JSONTree, err error) {
-	const init = true
-	if err := prepareModule(ctx, req.Module, req.Version, moduleDir, s.proxyClient, req.Insecure, init); err != nil {
+	if err := prepareModule(ctx, req.Module, req.Version, moduleDir, s.proxyClient, req.Insecure, !req.SkipInit); err != nil {
 		return nil, err
 	}
 	var sbox *sandbox.Sandbox
@@ -475,6 +474,7 @@
 				ImportedBy:    mod.ImportedBy,
 				Insecure:      params.Insecure,
 				JobID:         jobID,
+				SkipInit:      params.SkipInit,
 			},
 		})
 	}