Skip to content

Commit 14cfddc

Browse files
committed
GoMod: Distinguish main module depencencies from development-only ones
The produced "vendor" scope, which is the only scope contained in the result, contains all modules needed for building and testing the main module. Introduce the scope "main" containing the dependencies of the main module only. Determine the main module dependencies by utilizing [1], see also the discussion around [2]. [1] `go list -deps -f '{{with .Module}}{{.Path}} {{.Version}}{{end}}' ./...` [2] golang/go#26955 (comment) Signed-off-by: Frank Viernau <[email protected]>
1 parent cea7f66 commit 14cfddc

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

analyzer/src/funTest/assets/projects/synthetic/gomod-expected-output.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@ project:
1616
path: "<REPLACE_PATH>"
1717
homepage_url: ""
1818
scopes:
19+
- name: "main"
20+
dependencies:
21+
- id: "GoMod::github.com/fatih/color:v1.13.0"
22+
linkage: "PROJECT_STATIC"
23+
- id: "GoMod::github.com/google/uuid:v1.0.0"
24+
linkage: "PROJECT_STATIC"
25+
- id: "GoMod::github.com/mattn/go-colorable:v0.1.12"
26+
linkage: "PROJECT_STATIC"
27+
- id: "GoMod::github.com/mattn/go-isatty:v0.0.14"
28+
linkage: "PROJECT_STATIC"
29+
- id: "GoMod::github.com/pborman/uuid:v1.2.1"
30+
linkage: "PROJECT_STATIC"
31+
- id: "GoMod::golang.org/x/sys:v0.0.0-20220610221304-9f5ed59c137d"
32+
linkage: "PROJECT_STATIC"
1933
- name: "vendor"
2034
dependencies:
2135
- id: "GoMod::github.com/davecgh/go-spew:v1.1.0"

analyzer/src/funTest/assets/projects/synthetic/gomod-subpkg-expected-output.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ project:
1616
path: "<REPLACE_PATH>"
1717
homepage_url: ""
1818
scopes:
19+
- name: "main"
20+
dependencies:
21+
- id: "GoMod::github.com/fatih/color:v1.7.0"
22+
linkage: "PROJECT_STATIC"
23+
- id: "GoMod::github.com/mattn/go-colorable:v0.1.4"
24+
linkage: "PROJECT_STATIC"
25+
- id: "GoMod::github.com/mattn/go-isatty:v0.0.10"
26+
linkage: "PROJECT_STATIC"
27+
dependencies:
28+
- id: "GoMod::golang.org/x/sys:v0.0.0-20191008105621-543471e840be"
29+
linkage: "PROJECT_STATIC"
1930
- name: "vendor"
2031
dependencies:
2132
- id: "GoMod::github.com/fatih/color:v1.7.0"

analyzer/src/main/kotlin/managers/GoMod.kt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,20 @@ class GoMod(
9696
val packageIds = graph.nodes() - projectId
9797
val packages = packageIds.mapTo(sortedSetOf()) { createPackage(it) }
9898
val projectVcs = processProjectVcs(projectDir)
99+
100+
val dependenciesScopePackageIds = getTransitiveMainModuleDependencies(projectDir).let { moduleNames ->
101+
graph.nodes().filterTo(mutableSetOf()) { it.name in moduleNames }
102+
}
103+
99104
val scopes = sortedSetOf(
100-
Scope(name = "vendor", dependencies = graph.toPackageReferenceForest(projectId))
105+
Scope(
106+
name = "main",
107+
dependencies = graph.subgraph(dependenciesScopePackageIds).toPackageReferenceForest(projectId)
108+
),
109+
Scope(
110+
name = "vendor",
111+
dependencies = graph.toPackageReferenceForest(projectId)
112+
)
101113
)
102114

103115
return listOf(
@@ -199,6 +211,25 @@ class GoMod(
199211
return graph.nodes().filterTo(mutableSetOf()) { it.name in vendorModuleNames }
200212
}
201213

214+
/**
215+
* Return the module names of all transitive main module dependencies. This excludes test-only dependencies.
216+
*/
217+
private fun getTransitiveMainModuleDependencies(projectDir: File): Set<String> {
218+
val result = mutableSetOf<String>()
219+
220+
val list = run("list", "-deps", "-f", "{{with .Module}}{{.Path}} {{.Version}}{{end}}", "./...",
221+
workingDir = projectDir)
222+
223+
list.stdout.lines().forEach { line ->
224+
val columns = line.split(' ')
225+
if (columns.size != 2) return@forEach
226+
227+
result += columns[0]
228+
}
229+
230+
return result
231+
}
232+
202233
private fun createPackage(id: Identifier): Package {
203234
val vcsInfo = id.toVcsInfo().takeUnless { it.type == VcsType.UNKNOWN }.orEmpty()
204235

0 commit comments

Comments
 (0)