Skip to content

Commit 13c5a8e

Browse files
Strum355claude
andcommitted
refactor: move Go discovery logic from workspace.js to golang_gomodules.js
Move discoverGoWorkspaceModules to its provider file so workspace.js only retains generic scaffolding. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 7f95f29 commit 13c5a8e

4 files changed

Lines changed: 64 additions & 63 deletions

File tree

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { getCustom } from "./tools.js";
99
import { resolveBatchMetadata, resolveContinueOnError } from './batch_opts.js'
1010
import { discoverMavenModules } from './providers/java_maven.js'
1111
import { discoverGradleSubprojects } from './providers/java_gradle.js'
12+
import { discoverGoWorkspaceModules } from './providers/golang_gomodules.js'
1213
import {
13-
discoverGoWorkspaceModules,
1414
discoverWorkspaceCrates,
1515
discoverWorkspacePackages,
1616
filterManifestPathsByDiscoveryIgnore,

src/providers/golang_gomodules.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PackageURL } from 'packageurl-js'
66
import { readLicenseFile } from '../license/license_utils.js'
77
import Sbom from '../sbom.js'
88
import { getCustom, getCustomPath, invokeCommand } from "../tools.js";
9+
import { filterManifestPathsByDiscoveryIgnore, resolveWorkspaceDiscoveryIgnore } from '../workspace.js'
910

1011
import { getParser, getRequireQuery } from './gomod_parser.js'
1112

@@ -427,3 +428,56 @@ function getLineSeparatorGolang() {
427428
let reg = /\n|\r\n/
428429
return reg
429430
}
431+
432+
/**
433+
* Discover all go.mod manifest paths in a Go workspace.
434+
* Uses `go work edit -json` to get workspace members.
435+
*
436+
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain go.work)
437+
* @param {import('../index.js').Options} [opts={}]
438+
* @returns {Promise<string[]>} Paths to go.mod files (absolute)
439+
*/
440+
export async function discoverGoWorkspaceModules(workspaceRoot, opts = {}) {
441+
const root = path.resolve(workspaceRoot)
442+
const goWork = path.join(root, 'go.work')
443+
444+
if (!fs.existsSync(goWork)) {
445+
return []
446+
}
447+
448+
const goBin = getCustomPath('go', opts)
449+
let output
450+
try {
451+
output = invokeCommand(goBin, ['work', 'edit', '-json', goWork], { cwd: root })
452+
} catch {
453+
return []
454+
}
455+
456+
let workspace
457+
try {
458+
workspace = JSON.parse(output.toString().trim())
459+
} catch {
460+
return []
461+
}
462+
463+
const useEntries = workspace.Use || []
464+
if (useEntries.length === 0) {
465+
return []
466+
}
467+
468+
const manifestPaths = []
469+
for (const entry of useEntries) {
470+
const diskPath = entry.DiskPath
471+
if (!diskPath) {
472+
continue
473+
}
474+
const moduleDir = path.resolve(root, diskPath)
475+
const goMod = path.join(moduleDir, 'go.mod')
476+
if (fs.existsSync(goMod)) {
477+
manifestPaths.push(goMod)
478+
}
479+
}
480+
481+
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts)
482+
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns)
483+
}

src/workspace.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -269,56 +269,3 @@ export async function discoverWorkspaceCrates(workspaceRoot, opts = {}) {
269269
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts)
270270
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns)
271271
}
272-
273-
/**
274-
* Discover all go.mod manifest paths in a Go workspace.
275-
* Uses `go work edit -json` to get workspace members.
276-
*
277-
* @param {string} workspaceRoot - Absolute or relative path to workspace root (must contain go.work)
278-
* @param {import('./index.js').Options} [opts={}]
279-
* @returns {Promise<string[]>} Paths to go.mod files (absolute)
280-
*/
281-
export async function discoverGoWorkspaceModules(workspaceRoot, opts = {}) {
282-
const root = path.resolve(workspaceRoot)
283-
const goWork = path.join(root, 'go.work')
284-
285-
if (!fs.existsSync(goWork)) {
286-
return []
287-
}
288-
289-
const goBin = getCustomPath('go', opts)
290-
let output
291-
try {
292-
output = invokeCommand(goBin, ['work', 'edit', '-json', goWork], { cwd: root })
293-
} catch {
294-
return []
295-
}
296-
297-
let workspace
298-
try {
299-
workspace = JSON.parse(output.toString().trim())
300-
} catch {
301-
return []
302-
}
303-
304-
const useEntries = workspace.Use || []
305-
if (useEntries.length === 0) {
306-
return []
307-
}
308-
309-
const manifestPaths = []
310-
for (const entry of useEntries) {
311-
const diskPath = entry.DiskPath
312-
if (!diskPath) {
313-
continue
314-
}
315-
const moduleDir = path.resolve(root, diskPath)
316-
const goMod = path.join(moduleDir, 'go.mod')
317-
if (fs.existsSync(goMod)) {
318-
manifestPaths.push(goMod)
319-
}
320-
}
321-
322-
const ignorePatterns = resolveWorkspaceDiscoveryIgnore(opts)
323-
return filterManifestPathsByDiscoveryIgnore(manifestPaths, root, ignorePatterns)
324-
}

test/providers/workspace.test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import path from 'node:path'
44
import { expect } from 'chai'
55
import esmock from 'esmock'
66

7+
import { discoverGoWorkspaceModules } from '../../src/providers/golang_gomodules.js'
78
import { discoverGradleSubprojects } from '../../src/providers/java_gradle.js'
89
import { discoverMavenModules } from '../../src/providers/java_maven.js'
910
import {
10-
discoverGoWorkspaceModules,
1111
discoverWorkspaceCrates,
1212
discoverWorkspacePackages,
1313
filterManifestPathsByDiscoveryIgnore,
@@ -339,7 +339,7 @@ suite('discoverGoWorkspaceModules', () => {
339339
{ DiskPath: './module-b' },
340340
],
341341
}
342-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
342+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
343343
'../../src/tools.js': {
344344
getCustom: () => null,
345345
getCustomPath: () => 'go',
@@ -363,7 +363,7 @@ suite('discoverGoWorkspaceModules', () => {
363363
{ DiskPath: './libs/util' },
364364
],
365365
}
366-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
366+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
367367
'../../src/tools.js': {
368368
getCustom: () => null,
369369
getCustomPath: () => 'go',
@@ -382,7 +382,7 @@ suite('discoverGoWorkspaceModules', () => {
382382
Go: { Version: '1.22' },
383383
Use: [{ DiskPath: './mymod' }],
384384
}
385-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
385+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
386386
'../../src/tools.js': {
387387
getCustom: () => null,
388388
getCustomPath: () => 'go',
@@ -403,7 +403,7 @@ suite('discoverGoWorkspaceModules', () => {
403403
{ DiskPath: './nonexistent' },
404404
],
405405
}
406-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
406+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
407407
'../../src/tools.js': {
408408
getCustom: () => null,
409409
getCustomPath: () => 'go',
@@ -417,7 +417,7 @@ suite('discoverGoWorkspaceModules', () => {
417417

418418
test('returns empty when go command fails', async () => {
419419
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
420-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
420+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
421421
'../../src/tools.js': {
422422
getCustom: () => null,
423423
getCustomPath: () => 'go',
@@ -430,7 +430,7 @@ suite('discoverGoWorkspaceModules', () => {
430430

431431
test('returns empty when go output is invalid JSON', async () => {
432432
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
433-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
433+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
434434
'../../src/tools.js': {
435435
getCustom: () => null,
436436
getCustomPath: () => 'go',
@@ -444,7 +444,7 @@ suite('discoverGoWorkspaceModules', () => {
444444
test('returns empty when Use array is empty', async () => {
445445
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
446446
const goWorkJson = { Go: { Version: '1.22' }, Use: [] }
447-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
447+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
448448
'../../src/tools.js': {
449449
getCustom: () => null,
450450
getCustomPath: () => 'go',
@@ -464,7 +464,7 @@ suite('discoverGoWorkspaceModules', () => {
464464
{ DiskPath: './libs/util' },
465465
],
466466
}
467-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
467+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
468468
'../../src/tools.js': {
469469
getCustom: () => null,
470470
getCustomPath: () => 'go',

0 commit comments

Comments
 (0)