Skip to content

Commit 5ea1a5e

Browse files
authored
fix: properly handle go.mod files with no dependencies (#245)
1 parent 969dc24 commit 5ea1a5e

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

src/providers/golang_gomodules.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,22 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
258258
}
259259
let manifestDir = path.dirname(manifest)
260260
try {
261-
var goGraphOutput = invokeCommand(goBin, ['mod', 'graph'], {cwd: manifestDir}).toString()
261+
var goGraphOutput = invokeCommand(goBin, ['mod', 'graph'], {cwd: manifestDir}).toString().trim()
262262
} catch(error) {
263263
throw new Error('failed to invoke go binary for module graph', {cause: error})
264264
}
265+
266+
try {
267+
var goModEditOutput = JSON.parse(invokeCommand(goBin, ["mod", "edit", "-json"], {cwd: manifestDir}).toString().trim())
268+
} catch(error) {
269+
throw new Error('failed to determine root module name', {cause: error})
270+
}
271+
265272
let ignoredDeps = getIgnoredDeps(manifest);
266273
let allIgnoredDeps = ignoredDeps.map((dep) => dep.toString())
267274
let sbom = new Sbom();
268-
let rows = goGraphOutput.split(getLineSeparatorGolang()).filter(line => {
269-
return !line.includes(' go@');
270-
});
271-
let root = getParentVertexFromEdge(rows[0])
275+
let rows = goGraphOutput.split(getLineSeparatorGolang()).filter(line => !line.includes(' go@'));
276+
let root = getParentVertexFromEdge(goModEditOutput['Module']['Path'])
272277
let matchManifestVersions = getCustom("MATCH_MANIFEST_VERSIONS", "false", opts);
273278
if(matchManifestVersions === "true") {
274279
performManifestVersionsCheck(root, rows, manifest)
@@ -278,7 +283,7 @@ function getSBOM(manifest, opts = {}, includeTransitive) {
278283
sbom.addRoot(mainModule)
279284
const exhortGoMvsLogicEnabled = getCustom("EXHORT_GO_MVS_LOGIC_ENABLED", "false", opts)
280285
if(includeTransitive && exhortGoMvsLogicEnabled === "true") {
281-
rows = getFinalPackagesVersionsForModule(rows,manifest,goBin)
286+
rows = getFinalPackagesVersionsForModule(rows, manifest, goBin)
282287
}
283288
if (includeTransitive) {
284289
let currentParent = ""

test/providers/golang_gomodules.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ suite('testing the golang-go-modules data provider', () => {
2323
"go_mod_no_ignore",
2424
"go_mod_with_ignore",
2525
"go_mod_test_ignore",
26-
"go_mod_with_all_ignore"
26+
"go_mod_with_all_ignore",
27+
"go_mod_empty"
2728
].forEach(testCase => {
2829
let scenario = testCase.replace('go_mod_', '').replaceAll('_', ' ')
2930
test(`verify go.mod sbom provided for stack analysis with scenario ${scenario}`, () => {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"bomFormat": "CycloneDX",
3+
"specVersion": "1.4",
4+
"version": 1,
5+
"metadata": {
6+
"timestamp": "2023-08-07T00:00:00.000Z",
7+
"component": {
8+
"group": "github.com/sample",
9+
"name": "empty-module",
10+
"version": "v0.0.0",
11+
"purl": "pkg:golang/github.com/sample/[email protected]",
12+
"type": "application",
13+
"bom-ref": "pkg:golang/github.com/sample/[email protected]"
14+
}
15+
},
16+
"components": [
17+
{
18+
"group": "github.com/sample",
19+
"name": "empty-module",
20+
"version": "v0.0.0",
21+
"purl": "pkg:golang/github.com/sample/[email protected]",
22+
"type": "application",
23+
"bom-ref": "pkg:golang/github.com/sample/[email protected]"
24+
}
25+
],
26+
"dependencies": []
27+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"bomFormat": "CycloneDX",
3+
"specVersion": "1.4",
4+
"version": 1,
5+
"metadata": {
6+
"timestamp": "2023-08-07T00:00:00.000Z",
7+
"component": {
8+
"group": "github.com/sample",
9+
"name": "empty-module",
10+
"version": "v0.0.0",
11+
"purl": "pkg:golang/github.com/sample/[email protected]",
12+
"type": "application",
13+
"bom-ref": "pkg:golang/github.com/sample/[email protected]"
14+
}
15+
},
16+
"components": [
17+
{
18+
"group": "github.com/sample",
19+
"name": "empty-module",
20+
"version": "v0.0.0",
21+
"purl": "pkg:golang/github.com/sample/[email protected]",
22+
"type": "application",
23+
"bom-ref": "pkg:golang/github.com/sample/[email protected]"
24+
},
25+
{
26+
"name": "go",
27+
"version": "1.24",
28+
"purl": "pkg:golang/[email protected]",
29+
"type": "library",
30+
"bom-ref": "pkg:golang/[email protected]"
31+
},
32+
{
33+
"name": "toolchain",
34+
"version": "go1.24",
35+
"purl": "pkg:golang/[email protected]",
36+
"type": "library",
37+
"bom-ref": "pkg:golang/[email protected]"
38+
}
39+
],
40+
"dependencies": [
41+
{
42+
"ref": "pkg:golang/[email protected]",
43+
"dependsOn": [
44+
"pkg:golang/[email protected]"
45+
]
46+
},
47+
{
48+
"ref": "pkg:golang/[email protected]",
49+
"dependsOn": []
50+
}
51+
]
52+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/sample/empty-module
2+
3+
go 1.24

0 commit comments

Comments
 (0)