internal/worker: addSource information to results

This fixes 2 issues:

(1) Source information was not added if, e.g., the analyzer exited with
a non-zero exit code.

(2) Diagnostic URLs were not created for analysis runs not using
/tmp/modules to store code.

Change-Id: Ib273e3d3174bc6aaff1620bb8f9f0dd120a3df95
Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/708377
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ethan Lee <ethanalee@google.com>
diff --git a/internal/worker/analysis.go b/internal/worker/analysis.go
index ef78440..fa6cbf5 100644
--- a/internal/worker/analysis.go
+++ b/internal/worker/analysis.go
@@ -203,7 +203,10 @@
 		defer derrors.Cleanup(&err, func() error { return os.RemoveAll(modDir) })
 
 		jsonTree, err := s.scanInternal(ctx, req, localBinaryPath, modDir)
+		row.Diagnostics = analysis.JSONTreeToDiagnostics(jsonTree)
+		err = errors.Join(err, addSource(ctx, row.Diagnostics, 1))
 		if err != nil {
+			log.Warnf(ctx, "doScan or addSource failed: %v", err)
 			return err
 		}
 		hasGoMod = fileExists(filepath.Join(modDir, "go.mod")) // for precise error breakdown
@@ -213,8 +216,7 @@
 		}
 		row.Version = info.Version
 		row.CommitTime = info.Time
-		row.Diagnostics = analysis.JSONTreeToDiagnostics(jsonTree)
-		return addSource(ctx, row.Diagnostics, 1)
+		return nil
 	})
 	if err != nil {
 		// The errors are classified as to explicitly make a distinction
@@ -337,6 +339,14 @@
 			// position.
 			continue
 		}
+		if d.Position == "-" {
+			// Entries without a file position
+			continue
+		}
+		if strings.HasPrefix(d.Position, "/root/.cache/go-build/") {
+			// TODO: how do we read contents of go-build cache?
+			continue
+		}
 
 		file, line, _, err := parsePosition(d.Position)
 		if err != nil {
@@ -385,8 +395,7 @@
 // sourceURL creates a URL showing the code corresponding to
 // position pos and highlighting line.
 func sourceURL(pos string, line int) (string, error) {
-	// Trim /tmp/modules/ from the position string.
-	relPos := strings.TrimPrefix(pos, modulesDir+"/")
+	relPos := strings.TrimPrefix(strings.TrimPrefix(pos, "/root/go/pkg/mod/"), modulesDir+"/")
 	if relPos == pos {
 		return "", errors.New("unexpected prefix")
 	}