Skip to content

Commit

Permalink
GoMod: Distinguish main module depencencies from developmenti-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 "dependencies" which contains only the main module
dependencies and compute it from the "vendor" scope by filtering out all
non-main module dependencies determined by [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 17, 2022
1 parent cea7f66 commit cc47bee
Show file tree
Hide file tree
Showing 3 changed files with 57 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: "dependencies"
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: "dependencies"
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
33 changes: 32 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 = "dependencies",
dependencies = graph.subgraph(dependenciesScopePackageIds).toPackageReferenceForest(projectId)
),
Scope(
name = "vendor",
dependencies = graph.toPackageReferenceForest(projectId)
)
)

return listOf(
Expand Down Expand Up @@ -199,6 +211,25 @@ 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 cc47bee

Please sign in to comment.