Skip to content

Commit 09597e1

Browse files
Strum355claude
andcommitted
feat(workspace): add Go workspace (go.work) discovery
Add discoverGoWorkspaceModules() for discovering go.mod manifest paths in Go multi-module workspaces via `go work edit -json`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 543aef4 commit 09597e1

13 files changed

Lines changed: 260 additions & 2 deletions

File tree

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import fs from 'node:fs'
88
import { getCustom } from "./tools.js";
99
import { resolveBatchMetadata, resolveContinueOnError } from './batch_opts.js'
1010
import {
11+
discoverGoWorkspaceModules,
1112
discoverGradleSubprojects,
1213
discoverMavenModules,
1314
discoverWorkspaceCrates,
@@ -25,6 +26,7 @@ export { getProjectLicense, findLicenseFilePath, identifyLicense, getLicenseDeta
2526

2627
export default { componentAnalysis, stackAnalysis, stackAnalysisBatch, imageAnalysis, validateToken, generateSbom }
2728
export {
29+
discoverGoWorkspaceModules,
2830
discoverGradleSubprojects,
2931
discoverMavenModules,
3032
discoverWorkspacePackages,
@@ -323,7 +325,7 @@ async function generateOneSbom(manifestPath, workspaceOpts) {
323325
*
324326
* @param {string} root - Resolved workspace root
325327
* @param {Options} opts
326-
* @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'maven' | 'gradle' | 'unknown', manifestPaths: string[] }>}
328+
* @returns {Promise<{ ecosystem: 'javascript' | 'cargo' | 'maven' | 'gradle' | 'gomodules' | 'unknown', manifestPaths: string[] }>}
327329
* @private
328330
*/
329331
async function detectWorkspaceManifests(root, opts) {
@@ -352,6 +354,13 @@ async function detectWorkspaceManifests(root, opts) {
352354
}
353355
}
354356

357+
if (fs.existsSync(path.join(root, 'go.work'))) {
358+
const manifestPaths = await discoverGoWorkspaceModules(root, opts)
359+
if (manifestPaths.length > 0) {
360+
return { ecosystem: 'gomodules', manifestPaths }
361+
}
362+
}
363+
355364
const hasJsLock = fs.existsSync(path.join(root, 'pnpm-lock.yaml'))
356365
|| fs.existsSync(path.join(root, 'yarn.lock'))
357366
|| fs.existsSync(path.join(root, 'package-lock.json'))
@@ -510,7 +519,7 @@ async function stackAnalysisBatch(workspaceRoot, html = false, opts = {}) {
510519
}
511520

512521
if (manifestPaths.length === 0) {
513-
throw new Error(`No workspace manifests found at ${root}. Ensure Cargo.toml+Cargo.lock or package.json+lock file exist.`)
522+
throw new Error(`No workspace manifests found at ${root}. Ensure a supported workspace root exists (Cargo.toml+Cargo.lock, pom.xml, settings.gradle[.kts], go.work, or package.json+lock file).`)
514523
}
515524

516525
const workspaceOpts = { ...opts, TRUSTIFY_DA_WORKSPACE_DIR: root }

src/workspace.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,56 @@ export async function discoverWorkspaceCrates(workspaceRoot, opts = {}) {
538538
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts)
539539
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns)
540540
}
541+
542+
/**
543+
* Discover all go.mod manifest paths in a Go workspace.
544+
* Uses `go work edit -json` to get workspace members.
545+
*
546+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain go.work)
547+
* @param {import('./index.js').Options} [opts={}]
548+
* @returns {Promise<string[]>} Paths to go.mod files (absolute)
549+
*/
550+
export async function discoverGoWorkspaceModules(workspaceRoot, opts = {}) {
551+
const root = path.resolve(workspaceRoot)
552+
const goWork = path.join(root, 'go.work')
553+
554+
if (!fs.existsSync(goWork)) {
555+
return []
556+
}
557+
558+
const goBin = getCustomPath('go', opts)
559+
let output
560+
try {
561+
output = invokeCommand(goBin, ['work', 'edit', '-json', goWork], { cwd: root })
562+
} catch {
563+
return []
564+
}
565+
566+
let workspace
567+
try {
568+
workspace = JSON.parse(output.toString().trim())
569+
} catch {
570+
return []
571+
}
572+
573+
const useEntries = workspace.Use || []
574+
if (useEntries.length === 0) {
575+
return []
576+
}
577+
578+
const manifestPaths = []
579+
for (const entry of useEntries) {
580+
const diskPath = entry.DiskPath
581+
if (!diskPath) {
582+
continue
583+
}
584+
const moduleDir = path.resolve(root, diskPath)
585+
const goMod = path.join(moduleDir, 'go.mod')
586+
if (fs.existsSync(goMod)) {
587+
manifestPaths.push(goMod)
588+
}
589+
}
590+
591+
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts)
592+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns)
593+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.22
2+
3+
use (
4+
./module-a
5+
./module-b
6+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/module-a
2+
3+
go 1.22
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/module-b
2+
3+
go 1.22
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/existing
2+
3+
go 1.22
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.22
2+
3+
use (
4+
./existing
5+
./nonexistent
6+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.22
2+
3+
use (
4+
./libs/core
5+
./libs/util
6+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/libs/core
2+
3+
go 1.22
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example.com/libs/util
2+
3+
go 1.22

0 commit comments

Comments
 (0)