Skip to content

Commit 41fa1a5

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 5a81505 commit 41fa1a5

4 files changed

Lines changed: 67 additions & 66 deletions

File tree

src/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import analysis from './analysis.js'
77
import fs from 'node:fs'
88
import { getCustom } from "./tools.js";
99
import { resolveBatchMetadata, resolveContinueOnError } from './batch_opts.js'
10+
import { discoverGoWorkspaceModules } from './providers/golang_gomodules.js'
1011
import {
11-
discoverGoWorkspaceModules,
1212
discoverWorkspaceCrates,
1313
discoverWorkspacePackages,
1414
filterManifestPathsByDiscoveryIgnore,
@@ -100,7 +100,7 @@ export {
100100
* @param {any} valueToBePrinted - The value to log.
101101
* @private
102102
*/
103-
function logOptionsAndEnvironmentsVariables(alongsideText,valueToBePrinted) {
103+
function logOptionsAndEnvironmentsVariables(alongsideText, valueToBePrinted) {
104104
if (process.env["TRUSTIFY_DA_DEBUG"] === "true") {
105105
console.log(`${alongsideText}: ${valueToBePrinted} ${EOL}`)
106106
}
@@ -112,9 +112,9 @@ function logOptionsAndEnvironmentsVariables(alongsideText,valueToBePrinted) {
112112
*/
113113
function readAndPrintVersionFromPackageJson() {
114114
let dirName
115-
// new ESM way in nodeJS ( since node version 22 ) to bring module directory.
115+
// new ESM way in nodeJS ( since node version 22 ) to bring module directory.
116116
dirName = import.meta.dirname
117-
// old ESM way in nodeJS ( before node versions 22.00 to bring module directory)
117+
// old ESM way in nodeJS ( before node versions 22.00 to bring module directory)
118118
if (!dirName) {
119119
dirName = url.fileURLToPath(new URL('.', import.meta.url));
120120
}

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,8 +4,8 @@ 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 {
8-
discoverGoWorkspaceModules,
99
discoverWorkspaceCrates,
1010
discoverWorkspacePackages,
1111
filterManifestPathsByDiscoveryIgnore,
@@ -205,7 +205,7 @@ suite('discoverGoWorkspaceModules', () => {
205205
{ DiskPath: './module-b' },
206206
],
207207
}
208-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
208+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
209209
'../../src/tools.js': {
210210
getCustom: () => null,
211211
getCustomPath: () => 'go',
@@ -229,7 +229,7 @@ suite('discoverGoWorkspaceModules', () => {
229229
{ DiskPath: './libs/util' },
230230
],
231231
}
232-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
232+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
233233
'../../src/tools.js': {
234234
getCustom: () => null,
235235
getCustomPath: () => 'go',
@@ -248,7 +248,7 @@ suite('discoverGoWorkspaceModules', () => {
248248
Go: { Version: '1.22' },
249249
Use: [{ DiskPath: './mymod' }],
250250
}
251-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
251+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
252252
'../../src/tools.js': {
253253
getCustom: () => null,
254254
getCustomPath: () => 'go',
@@ -269,7 +269,7 @@ suite('discoverGoWorkspaceModules', () => {
269269
{ DiskPath: './nonexistent' },
270270
],
271271
}
272-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
272+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
273273
'../../src/tools.js': {
274274
getCustom: () => null,
275275
getCustomPath: () => 'go',
@@ -283,7 +283,7 @@ suite('discoverGoWorkspaceModules', () => {
283283

284284
test('returns empty when go command fails', async () => {
285285
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
286-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
286+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
287287
'../../src/tools.js': {
288288
getCustom: () => null,
289289
getCustomPath: () => 'go',
@@ -296,7 +296,7 @@ suite('discoverGoWorkspaceModules', () => {
296296

297297
test('returns empty when go output is invalid JSON', async () => {
298298
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
299-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
299+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
300300
'../../src/tools.js': {
301301
getCustom: () => null,
302302
getCustomPath: () => 'go',
@@ -310,7 +310,7 @@ suite('discoverGoWorkspaceModules', () => {
310310
test('returns empty when Use array is empty', async () => {
311311
const root = path.resolve('test/providers/tst_manifests/golang/go_workspace')
312312
const goWorkJson = { Go: { Version: '1.22' }, Use: [] }
313-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
313+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
314314
'../../src/tools.js': {
315315
getCustom: () => null,
316316
getCustomPath: () => 'go',
@@ -330,7 +330,7 @@ suite('discoverGoWorkspaceModules', () => {
330330
{ DiskPath: './libs/util' },
331331
],
332332
}
333-
const { discoverGoWorkspaceModules } = await esmock('../../src/workspace.js', {
333+
const { discoverGoWorkspaceModules } = await esmock('../../src/providers/golang_gomodules.js', {
334334
'../../src/tools.js': {
335335
getCustom: () => null,
336336
getCustomPath: () => 'go',

0 commit comments

Comments
 (0)