Skip to content

Commit

Permalink
GoMod: Distinguish main module dependencies from development-only ones
Browse files Browse the repository at this point in the history
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 <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Jun 20, 2022
1 parent c2b17a9 commit d6c0006
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ project:
path: "<REPLACE_PATH>"
homepage_url: ""
scopes:
- name: "main"
dependencies:
- id: "GoMod::github.com/fatih/color:v1.13.0"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/google/uuid:v1.0.0"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/mattn/go-colorable:v0.1.12"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/mattn/go-isatty:v0.0.14"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/pborman/uuid:v1.2.1"
linkage: "PROJECT_STATIC"
- id: "GoMod::golang.org/x/sys:v0.0.0-20220610221304-9f5ed59c137d"
linkage: "PROJECT_STATIC"
- name: "vendor"
dependencies:
- id: "GoMod::github.com/davecgh/go-spew:v1.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ project:
path: "<REPLACE_PATH>"
homepage_url: ""
scopes:
- name: "main"
dependencies:
- id: "GoMod::github.com/fatih/color:v1.7.0"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/mattn/go-colorable:v0.1.4"
linkage: "PROJECT_STATIC"
- id: "GoMod::github.com/mattn/go-isatty:v0.0.10"
linkage: "PROJECT_STATIC"
dependencies:
- id: "GoMod::golang.org/x/sys:v0.0.0-20191008105621-543471e840be"
linkage: "PROJECT_STATIC"
- name: "vendor"
dependencies:
- id: "GoMod::github.com/fatih/color:v1.7.0"
Expand Down
34 changes: 33 additions & 1 deletion analyzer/src/main/kotlin/managers/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,20 @@ class GoMod(
val packageIds = graph.nodes() - projectId
val packages = packageIds.mapTo(sortedSetOf()) { createPackage(it) }
val projectVcs = processProjectVcs(projectDir)

val dependenciesScopePackageIds = getTransitiveMainModuleDependencies(projectDir).let { moduleNames ->
graph.nodes().filterTo(mutableSetOf()) { it.name in moduleNames }
}

val scopes = sortedSetOf(
Scope(name = "vendor", dependencies = graph.toPackageReferenceForest(projectId))
Scope(
name = "main",
dependencies = graph.subgraph(dependenciesScopePackageIds).toPackageReferenceForest(projectId)
),
Scope(
name = "vendor",
dependencies = graph.toPackageReferenceForest(projectId)
)
)

return listOf(
Expand Down Expand Up @@ -199,6 +211,26 @@ class GoMod(
return graph.nodes().filterTo(mutableSetOf()) { it.name in vendorModuleNames }
}

/**
* Return the module names of all transitive main module dependencies. This excludes test-only dependencies.
*/
private fun getTransitiveMainModuleDependencies(projectDir: File): Set<String> {
val result = mutableSetOf<String>()

val list = run(
"list", "-deps", "-f", "{{with .Module}}{{.Path}} {{.Version}}{{end}}", "./...", workingDir = projectDir
)

list.stdout.lines().forEach { line ->
val columns = line.split(' ')
if (columns.size != 2) return@forEach

result += columns[0]
}

return result
}

private fun createPackage(id: Identifier): Package {
val vcsInfo = id.toVcsInfo().takeUnless { it.type == VcsType.UNKNOWN }.orEmpty()

Expand Down

0 comments on commit d6c0006

Please sign in to comment.