Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check all dependency jars before marking unused #967

Merged
merged 6 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,23 @@ class JdepsMerger {
}

if (reportUnusedDeps != "off") {
val unusedLabels = dependencyMap.values
.filter { it.kind == Deps.Dependency.Kind.UNUSED }
.mapNotNull { readJarOwnerFromManifest(Paths.get(it.path)).label }
val kindMap = mutableMapOf<String, Deps.Dependency.Kind>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mauriciogg is there any test coverage for this behavior in JdepsMergerTest that can be added?


// A target might produce multiple jars (Android produces _resources.jar)
// so we need to make sure wedon't mart the dependency as unused
// unless all of the jars are unused.
dependencyMap.values.forEach {
val label = readJarOwnerFromManifest(Paths.get(it.path)).label
if (label != null &&
kindMap.getOrDefault(label, Deps.Dependency.Kind.UNUSED) >= it.kind
) {
kindMap.put(label, it.kind)
}
}

val unusedLabels = kindMap.entries
.filter { it.value == Deps.Dependency.Kind.UNUSED }
.map { it.key }
.filter { it != label }

if (unusedLabels.isNotEmpty()) {
Expand Down
128 changes: 109 additions & 19 deletions src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/JdepsMergerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class JdepsMergerTest {

private fun jdeps(
path: String,
buildDeps: (Deps.Dependencies.Builder.() -> Deps.Dependencies.Builder)
buildDeps: (Deps.Dependencies.Builder.() -> Deps.Dependencies.Builder),
): Path {
val jdepsPath = Files.createDirectories(wrkDir.resolve("jdeps")).resolve(path)

Expand All @@ -40,12 +40,12 @@ class JdepsMergerTest {
return Files.createDirectories(wrkDir.resolve("out")).resolve(name)
}

fun ktJvmLibrary(label: String): String {
val path = Files.createDirectories(wrkDir.resolve("out")).resolve("lib$label.jar")
fun ktJvmLibrary(label: String, suffix: String = ""): String {
val path = Files.createDirectories(wrkDir.resolve("out")).resolve("lib${label}$suffix.jar")
JarCreator(
path = path,
normalize = true,
verbose = false
verbose = false,
).also {
it.setJarOwner(label, "kt_jvm_library")
it.execute()
Expand All @@ -63,7 +63,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.EXPLICIT
path = "/path/to/kt_dep.jar"
build()
}
},
)
}

Expand All @@ -73,7 +73,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.EXPLICIT
path = "/path/to/java_dep.jar"
build()
}
},
)
}

Expand All @@ -89,7 +89,7 @@ class JdepsMergerTest {
input(javaJdeps)
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "off")
}
},
)
}
}
Expand All @@ -100,7 +100,7 @@ class JdepsMergerTest {
assertThat(depsProto.ruleLabel).isEqualTo("//foo/bar:baz")
assertThat(depsProto.dependencyList.map { it.path }).containsExactly(
"/path/to/kt_dep.jar",
"/path/to/java_dep.jar"
"/path/to/java_dep.jar",
)
}

Expand All @@ -114,7 +114,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.UNUSED
path = "/path/to/shared_dep.jar"
build()
}
},
)
}

Expand All @@ -124,7 +124,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.EXPLICIT
path = "/path/to/shared_dep.jar"
build()
}
},
)
}

Expand All @@ -140,7 +140,7 @@ class JdepsMergerTest {
input(javaJdeps)
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "off")
}
},
)
}
}
Expand All @@ -163,7 +163,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.UNUSED
path = unusedKotlinDep
build()
}
},
)
}

Expand All @@ -173,7 +173,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.EXPLICIT
path = ktJvmLibrary("java_dep")
build()
}
},
)
}

Expand All @@ -189,7 +189,7 @@ class JdepsMergerTest {
flag(JdepsMergerFlags.TARGET_LABEL, "//foo/bar:baz")
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "warn")
}
},
)
}
}
Expand All @@ -200,7 +200,7 @@ class JdepsMergerTest {
assertThat(
depsProto.dependencyList
.filter { it.kind == Dependency.Kind.UNUSED }
.map { it.path }
.map { it.path },
).containsExactly(unusedKotlinDep)
}

Expand All @@ -215,7 +215,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.UNUSED
path = unusedKotlinDep
build()
}
},
)
}

Expand All @@ -225,7 +225,7 @@ class JdepsMergerTest {
kind = Dependency.Kind.EXPLICIT
path = ktJvmLibrary("java_dep")
build()
}
},
)
}

Expand All @@ -242,7 +242,7 @@ class JdepsMergerTest {
flag(JdepsMergerFlags.TARGET_LABEL, "//foo/bar:baz")
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "error")
}
},
)
}
}
Expand All @@ -253,10 +253,100 @@ class JdepsMergerTest {
assertThat(
depsProto.dependencyList
.filter { it.kind == Dependency.Kind.UNUSED }
.map { it.path }
.map { it.path },
).containsExactly(unusedKotlinDep)
}

@Test
fun `unused deps multiple jars for label`() {
val merger = DaggerJdepsMergerTestComponent.builder().build().jdepsMerger()

val unusedKotlinDepA = ktJvmLibrary("kotlin_dep", "_a")
val unusedKotlinDepB = ktJvmLibrary("kotlin_dep", "_b")
val kotlinJdeps = jdeps("kt.jdeps") {
addDependency(
with(Dependency.newBuilder()) {
kind = Dependency.Kind.UNUSED
path = unusedKotlinDepA
build()
},
)
addDependency(
with(Dependency.newBuilder()) {
kind = Dependency.Kind.UNUSED
path = unusedKotlinDepB
build()
},
)
}

val mergedJdeps = out("merged.jdeps")

val result = WorkerContext.run {
doTask("jdepsmerge") { taskCtx ->
MergeJdeps(merger = merger).invoke(
taskCtx,
args {
input(kotlinJdeps)
flag(JdepsMergerFlags.TARGET_LABEL, "//foo/bar:baz")
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "warn")
},
)
}
}
assertThat(result.status).isEqualTo(SUCCESS)
assertThat(result.log.out.toString()).contains("'remove deps kotlin_dep' //foo/bar:baz")
}

@Test
fun `used deps multiple jars for label`() {
val merger = DaggerJdepsMergerTestComponent.builder().build().jdepsMerger()

val unusedKotlinDep = ktJvmLibrary("kotlin_dep", "_a")
val usedKotlinDep = ktJvmLibrary("kotlin_dep", "_b")
val kotlinJdeps = jdeps("kt.jdeps") {
addDependency(
with(Dependency.newBuilder()) {
kind = Dependency.Kind.UNUSED
path = unusedKotlinDep
build()
},
)
addDependency(
with(Dependency.newBuilder()) {
kind = Dependency.Kind.EXPLICIT
path = usedKotlinDep
build()
},
)
}

val mergedJdeps = out("merged.jdeps")

val result = WorkerContext.run {
doTask("jdepsmerge") { taskCtx ->
MergeJdeps(merger = merger).invoke(
taskCtx,
args {
input(kotlinJdeps)
flag(JdepsMergerFlags.TARGET_LABEL, "//foo/bar:baz")
flag(JdepsMergerFlags.OUTPUT, mergedJdeps)
flag(JdepsMergerFlags.REPORT_UNUSED_DEPS, "warn")
},
)
}
}
assertThat(result.status).isEqualTo(SUCCESS)

val depsProto = depsProto(mergedJdeps)
assertThat(
depsProto.dependencyList
.filter { it.kind == Dependency.Kind.EXPLICIT }
.map { it.path },
).containsExactly(usedKotlinDep)
}

private fun depsProto(mergedJdeps: Path) =
Deps.Dependencies.parseFrom(BufferedInputStream(Files.newInputStream(mergedJdeps)))

Expand Down