@@ -286,15 +286,28 @@ jobs:
286286 with :
287287 go-version : ' stable'
288288
289- - name : Vendor Go workspace dependencies
289+ - name : Prepare Go workspace for BlackDuck scanning
290290 if : ${{ hashFiles('go.work') != '' }}
291291 run : |
292- go work vendor
293- # Tell all subsequent Go commands (including those run by Detect) to use the vendor directory
294- echo "GOFLAGS=-mod=vendor" >> "$GITHUB_ENV"
295- # Calculate the detector search depth needed to find go.mod files in workspace module subdirs
296- GO_WORK_DEPTH=$(grep -E '^\s*use\s+\.' go.work | awk '{print $2}' | tr -d '"' | awk -F'/' '{print NF-1}' | sort -n | tail -1)
292+ # Extract all relative module paths from go.work.
293+ # grep -oE handles both single-line (use ./path) and block (use (\n ./path\n)) syntax
294+ # because it matches any './' sequence anywhere in the file.
295+ GO_WORK_DEPTH=$(grep -oE '\./[^[:space:]"/)]+' go.work \
296+ | awk -F'/' '{print NF-1}' \
297+ | sort -rn | head -1)
298+ # Default to 1 if all modules sit at root or grep returned nothing
299+ [[ -z "$GO_WORK_DEPTH" || "$GO_WORK_DEPTH" -le 0 ]] && GO_WORK_DEPTH=1
297300 echo "GO_WORK_DETECTOR_DEPTH=${GO_WORK_DEPTH}" >> "$GITHUB_ENV"
301+ echo "Go workspace detector search depth: ${GO_WORK_DEPTH}"
302+ # Vendor all workspace dependencies (requires Go 1.22+).
303+ # If this fails (e.g. private module network issue) Detect will still run
304+ # with the correct search depth and resolve modules via the Go toolchain.
305+ if go work vendor; then
306+ echo "GOFLAGS=-mod=vendor" >> "$GITHUB_ENV"
307+ echo "Successfully vendored Go workspace dependencies"
308+ else
309+ echo "go work vendor did not complete; Detect will resolve modules via Go toolchain"
310+ fi
298311
299312 - name : Construct BlackDuck detect arguments
300313 id : detect-args
@@ -320,8 +333,9 @@ jobs:
320333 DETECT_ARGS="${DETECT_ARGS} --detect.blackduck.scan.mode=RAPID"
321334 fi
322335
323- # If a Go workspace was vendored, set detector search depth so Detect finds go.mod in module subdirs
324- if [[ -f "go.work" && -d "vendor" && -n "${{ env.GO_WORK_DETECTOR_DEPTH }}" ]]; then
336+ # If repo uses a Go workspace, increase detector search depth so Detect finds
337+ # go.mod files inside module subdirectories (default depth 0 = root only = only Git found)
338+ if [[ -f "go.work" ]]; then
325339 DETECT_ARGS="${DETECT_ARGS} --detect.detector.search.depth=${{ env.GO_WORK_DETECTOR_DEPTH }}"
326340 fi
327341
0 commit comments