From 93cc6411ea9649957be37fdb20194d0b49839fc5 Mon Sep 17 00:00:00 2001 From: Frank Viernau Date: Tue, 14 Feb 2023 15:19:04 +0100 Subject: [PATCH] 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] and [^2]. 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 in `go list` to conditionally skip computing embed files in an analog way which would fix [^4]. [^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]: https://github.com/golang/go/issues/49534#issuecomment-1184587164 [^4]: https://github.com/oss-review-toolkit/ort/issues/5560 Signed-off-by: Frank Viernau --- analyzer/src/main/kotlin/managers/GoMod.kt | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/analyzer/src/main/kotlin/managers/GoMod.kt b/analyzer/src/main/kotlin/managers/GoMod.kt index 50bb8b785eda7..72c39d6c61206 100644 --- a/analyzer/src/main/kotlin/managers/GoMod.kt +++ b/analyzer/src/main/kotlin/managers/GoMod.kt @@ -288,13 +288,26 @@ class GoMod( // See https://pkg.go.dev/text/template for the format syntax. val list = run( - "list", "-deps", "-f", "{{with .Module}}{{.Path}} {{.Version}}{{end}}", "-buildvcs=false", "./...", + "list", "-deps", "-json=Module", "-buildvcs=false", "./...", workingDir = projectDir ) - list.stdout.lines().forEach { line -> - val columns = line.splitOnWhitespace() - if (columns.size in 1..2) result += columns.first() + data class DepInfo( + @JsonProperty("Module") + val module: ModuleInfo? = null + ) + + list.stdout.byteInputStream().use { inputStream -> + JsonFactory().createParser(inputStream).apply { + codec = ObjectMapper() + nextToken() + + while (hasCurrentToken()) { + val moduleInfo = jsonMapper.readValue(this, DepInfo::class.java).module + if (moduleInfo != null) result += moduleInfo.path + nextToken() + } + } } return result