Skip to content

Commit 43ad3a7

Browse files
committed
refactor(GoMod): Use option -json with go list to get module names
The `-json` option allows specifying a comma separated list of property names to include in the output. The `go list` command has a mechanism for skipping certain processing steps which are not needed for computing the values corresponding to those specified property names, see [^1],[^2] and [^3]. Such a mechanism is not in place for the `-f` option. Migrate from `-f` to `-json` to benefit from above mechanism, removing potential sources of issues. It could be that this allows to drop the parameter `-buildvcs=false`, which is left for future investigation. The primary reason for switching to `-json` is to extend the mentioned mechanism [^4] in `go list` to conditionally skip computing embed files in an analog way which would fix [^5]. [^1]: https://github.com/golang/go/blob/0cd309e12818f988693bf8e4d9f1453331dcf9f2/src/cmd/go/internal/load/pkg.go#L2768-L2776 [^2]: https://github.com/golang/go/blob/0cd309e12818f988693bf8e4d9f1453331dcf9f2/src/cmd/go/internal/list/list.go#L605-L606 [^3]: golang/go#29666 [^4]: golang/go#49534 (comment) [^5]: #5560 Signed-off-by: Frank Viernau <[email protected]>
1 parent 50e4c24 commit 43ad3a7

File tree

1 file changed

+10
-11
lines changed
  • analyzer/src/main/kotlin/managers

1 file changed

+10
-11
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ class GoMod(
213213
val main: Boolean = false
214214
)
215215

216+
private data class DepInfo(
217+
@JsonProperty("Module")
218+
val module: ModuleInfo? = null
219+
)
220+
216221
private fun ModuleInfo.toId(): Identifier =
217222
Identifier(
218223
type = managerName.takeIf { version.isBlank() } ?: "Go",
@@ -273,20 +278,14 @@ class GoMod(
273278
* Return the module names of all transitive main module dependencies. This excludes test-only dependencies.
274279
*/
275280
private fun getTransitiveMainModuleDependencies(projectDir: File): Set<String> {
276-
val result = mutableSetOf<String>()
277-
278281
// See https://pkg.go.dev/text/template for the format syntax.
279-
val list = run(
280-
"list", "-deps", "-f", "{{with .Module}}{{.Path}} {{.Version}}{{end}}", "-buildvcs=false", "./...",
281-
workingDir = projectDir
282-
)
282+
val list = run("list", "-deps", "-json=Module", "-buildvcs=false", "./...", workingDir = projectDir)
283283

284-
list.stdout.lines().forEach { line ->
285-
val columns = line.splitOnWhitespace()
286-
if (columns.size in 1..2) result += columns.first()
284+
return jsonMapper.createParser(list.stdout).use { parser ->
285+
jsonMapper.readValues<DepInfo>(parser).readAll()
286+
}.mapNotNullTo(mutableSetOf()) { depInfo ->
287+
depInfo.module?.path
287288
}
288-
289-
return result
290289
}
291290

292291
private fun ModuleInfo.toPackage(): Package {

0 commit comments

Comments
 (0)