Skip to content

Commit 90be2ec

Browse files
fviernauporsche-rbieniek
authored andcommitted
GoMod: Distinguish main module dependencies 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 these 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 b4ecd22 commit 90be2ec

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

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

+14
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

+11
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

+33-1
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(
@@ -201,6 +213,26 @@ class GoMod(
201213
return graph.nodes().filterTo(mutableSetOf()) { it.name in vendorModuleNames }
202214
}
203215

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

0 commit comments

Comments
 (0)