Skip to content

Commit

Permalink
refactor(GoMod): Use option -json with go list to get module names
Browse files Browse the repository at this point in the history
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 <frank_viernau@epam.com>
  • Loading branch information
fviernau committed Feb 14, 2023
1 parent 87792a3 commit 78bf96a
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions analyzer/src/main/kotlin/managers/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78bf96a

Please sign in to comment.