Skip to content

Commit

Permalink
WIP! GoMod: Fix an issue with the replace directive
Browse files Browse the repository at this point in the history
When applying this fix `go mod why fails` I believe it runs into
golang/go#26904

Signed-off-by: Frank Viernau <[email protected]>
  • Loading branch information
fviernau committed Jul 22, 2022
1 parent 0546d45 commit d881a9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion analyzer/src/funTest/assets/projects/synthetic/gomod/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/atomtree/go-spew v1.1.0 // indirect
github.com/google/uuid v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -19,3 +19,4 @@ require (
)

replace github.com/stretchr/testify => github.com/stretchr/testify v1.7.2
replace github.com/atomtree/go-spew v1.1.0 => github.com/davecgh/go-spew v1.1.0
18 changes: 7 additions & 11 deletions analyzer/src/main/kotlin/managers/GoMod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ class GoMod(
* Return the module graph output from `go mod graph` with non-vendor dependencies removed.
*/
private fun getModuleGraph(projectDir: File, moduleInfoForModuleName: Map<String, ModuleInfo>): Graph {
fun moduleInfo(moduleName: String): ModuleInfo = moduleInfoForModuleName.getValue(moduleName)

fun parseModuleEntry(entry: String): Identifier =
entry.substringBefore('@').let { moduleName ->
moduleInfo(moduleName).toId()
}
fun parseModuleEntry(entry: String): String = entry.substringBefore('@')

var graph = Graph()

Expand All @@ -163,15 +158,16 @@ class GoMod(
val columns = line.split(' ')
require(columns.size == 2) { "Expected exactly one occurrence of ' ' on any non-blank line." }

val parent = parseModuleEntry(columns[0])
val child = parseModuleEntry(columns[1])
// The module info is null if and only if the module has been replaced via the 'replace' directive.
val parent = moduleInfoForModuleName[parseModuleEntry(columns[0])] ?: return@forEach
val child = moduleInfoForModuleName[parseModuleEntry(columns[1])] ?: return@forEach

if (moduleInfo(parent.name).main && moduleInfo(child.name).indirect) {
log.debug { "Module '${child.name}' is an indirect dependency of '${parent.name}. Skip adding edge." }
if (parent.main && child.indirect) {
log.debug { "Module '${parent.path}' is an indirect dependency of '${child.path}. Skip adding edge." }
return@forEach
}

graph.addEdge(parent, child)
graph.addEdge(parent.toId(), child.toId())
}

val vendorModules = getVendorModules(graph, projectDir)
Expand Down

0 comments on commit d881a9d

Please sign in to comment.