Skip to content

Commit

Permalink
fix(cargo): Treat projects outside the analyer root as packages
Browse files Browse the repository at this point in the history
E.g. when analyzing a single Cargo project in a sub-directory with a
`cargo.toml` file that points to "path dependencies" in a parent
directory, these dependencies should not be seen as projects but as
packages. This restores the behavior from before 7522a0c.

Resolves #8571.

Signed-off-by: Sebastian Schuberth <[email protected]>
  • Loading branch information
sschuberth committed Apr 25, 2024
1 parent 3d0510d commit 1955e0f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions plugins/package-managers/cargo/src/main/kotlin/Cargo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,18 @@ class Cargo(
)

val nonProjectPackages = packageById.values.mapNotNullTo(mutableSetOf()) { cargoPkg ->
cargoPkg.takeUnless { it.isProject() }?.toPackage(hashes)
cargoPkg.takeUnless { it.isProject(workingDir) }?.toPackage(hashes)
}

return listOf(ProjectAnalyzerResult(project, nonProjectPackages))
}
}

private fun CargoMetadata.Package.isProject() = source == null
private fun CargoMetadata.Package.isProject(root: File? = null): Boolean {
val path = id.substringAfter("path+file://", "").removeSuffix(")").ifEmpty { null }
val isWithinRoot = root != null && path != null && File(path).startsWith(root)
return source == null && isWithinRoot
}

private fun CargoMetadata.Package.toPackage(hashes: Map<String, String>): Package {
val declaredLicenses = parseDeclaredLicenses()
Expand Down

0 comments on commit 1955e0f

Please sign in to comment.