Skip to content

Commit e64cf7c

Browse files
authored
Merge pull request #60 from anilkumarmyla/debug_logging
Add debug logging to demystify inner workings
2 parents 1c7eebd + c6eed44 commit e64cf7c

File tree

6 files changed

+54
-11
lines changed

6 files changed

+54
-11
lines changed

Diff for: README.md

+29
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,35 @@ unusedCompileDependenciesFilter -= moduleFilter("org.scalaz", "scalaz")
115115
Note: If you're filtering things out because you think the plugin is returning
116116
false-positive results, please open a GitHub issue.
117117

118+
## Debugging
119+
120+
You can pass `-debug` flag to sbt or set logLevel to debug to understand how the plugin computes compile depndencies
121+
122+
```
123+
sbt:example> set logLevel := Level.Debug
124+
sbt:example> unusedCompileDependencies
125+
...
126+
[debug] Source to library relations:
127+
[debug] sbt-explicit-dependencies/example/src/main/scala/foo/MyCaseClass.scala -> /Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/typelevel/cats-effect_2.12/0.10.1/cats-effect_2.12-0.10.1.jar
128+
[debug] sbt-explicit-dependencies/example/src/main/scala/foo/Example.scala -> /Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/typelevel/cats-core_2.12/1.2.0/cats-core_2.12-1.2.0.jar
129+
...
130+
[debug] Library dependencies:
131+
[debug] /Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/typelevel/cats-effect_2.12/0.10.1/cats-effect_2.12-0.10.1.jar
132+
[debug] /Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/typelevel/cats-core_2.12/1.2.0/cats-core_2.12-1.2.0.jar
133+
...
134+
[debug] jarFile: cats-effect_2.12-0.10.1.jar -> "org.typelevel" %% "cats-effect" % "0.10.1"
135+
[debug] jarFile: cats-core_2.12-1.2.0.jar -> "org.typelevel" %% "cats-core" % "1.2.0"
136+
...
137+
[debug] Compile depends on:
138+
[debug] "org.typelevel" %% "cats-effect" % "0.10.1"
139+
[debug] "org.typelevel" %% "cats-core" % "1.2.0"
140+
...
141+
[debug] Declared dependencies:
142+
[debug] "org.http4s" %% "http4s-blaze-server" % "0.18.16"
143+
[debug] "org.http4s" %% "http4s-circe" % "0.18.16"
144+
...
145+
```
146+
118147
## Example project
119148

120149
There is an example sbt project in the `example` folder so you can see the

Diff for: src/main/scala-sbt-0.13/explicitdeps/package.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ package object explicitdeps {
66

77
val defaultModuleFilter: ModuleFilter = sbt.DependencyFilter.moduleFilter()
88

9-
def getAllLibraryDeps(analysis: Analysis): Set[java.io.File] =
10-
analysis.relations.allBinaryDeps.toSet
9+
def getAllLibraryDeps(analysis: Analysis, log: sbt.util.Logger): Set[java.io.File] = {
10+
log.debug(
11+
s"Source to library relations:\n${analysis.relations.binaryDep.all.map(r => s" ${r._1} -> ${r._2}").mkString("\n")}"
12+
)
13+
val allLibraryDeps = analysis.relations.allBinaryDeps.toSet
14+
log.debug(s"Library dependencies:\n${allLibraryDeps.mkString(" ", "\n ", "")}")
15+
allLibraryDeps
16+
}
1117

1218
implicit class NodeSeqOps(nodeSeq: scala.xml.NodeSeq) {
1319

Diff for: src/main/scala-sbt-1.0/explicitdeps/package.scala

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ package object explicitdeps {
66

77
val defaultModuleFilter: ModuleFilter = sbt.librarymanagement.DependencyFilter.moduleFilter()
88

9-
def getAllLibraryDeps(analysis: Analysis): Set[java.io.File] =
10-
analysis.relations.allLibraryDeps.toSet
9+
def getAllLibraryDeps(analysis: Analysis, log: sbt.util.Logger): Set[java.io.File] = {
10+
log.debug(
11+
s"Source to library relations:\n${analysis.relations.libraryDep.all.map(r => s" ${r._1} -> ${r._2}").mkString("\n")}"
12+
)
13+
val allLibraryDeps = analysis.relations.allLibraryDeps.toSet
14+
log.debug(s"Library dependencies:\n${allLibraryDeps.mkString(" ", "\n ", "")}")
15+
allLibraryDeps
16+
}
1117

1218
}

Diff for: src/main/scala/explicitdeps/BoringStuff.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ object BoringStuff {
2020
val dependencyFromIvyCache = findIvyFileInIvyCache(jarFile).flatMap(parseIvyFile(scalaBinaryVersion, log))
2121
val dependencyFromIvyLocal = findIvyFileInIvyLocal(jarFile).flatMap(parseIvyFile(scalaBinaryVersion, log))
2222

23-
dependencyFromPom.orElse(dependencyFromIvyCache).orElse(dependencyFromIvyLocal)
23+
val dependencyOpt = dependencyFromPom.orElse(dependencyFromIvyCache).orElse(dependencyFromIvyLocal)
24+
log.debug(s"jarFile: ${jarFile.getName} -> ${dependencyOpt.mkString}")
25+
dependencyOpt
2426
}
2527

2628
private def findPomFile(jarFile: File): Option[File] = {

Diff for: src/main/scala/explicitdeps/ExplicitDepsPlugin.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ object ExplicitDepsPlugin extends AutoPlugin {
3636
)
3737

3838
lazy val undeclaredCompileDependenciesTask = Def.task {
39+
val log = streams.value.log
3940
val projectName = name.value
40-
val allLibraryDeps = getAllLibraryDeps(compile.in(Compile).value.asInstanceOf[Analysis])
41+
val allLibraryDeps = getAllLibraryDeps(compile.in(Compile).value.asInstanceOf[Analysis], log)
4142
val libraryDeps = libraryDependencies.value
4243
val scalaBinaryVer = scalaBinaryVersion.value
4344
val filter = undeclaredCompileDependenciesFilter.value
44-
val log = streams.value.log
4545

4646
Logic.getUndeclaredCompileDependencies(
4747
projectName,
@@ -60,12 +60,12 @@ object ExplicitDepsPlugin extends AutoPlugin {
6060
}
6161

6262
lazy val unusedCompileDependenciesTask = Def.task {
63+
val log = streams.value.log
6364
val projectName = name.value
64-
val allLibraryDeps = getAllLibraryDeps(compile.in(Compile).value.asInstanceOf[Analysis])
65+
val allLibraryDeps = getAllLibraryDeps(compile.in(Compile).value.asInstanceOf[Analysis], log)
6566
val libraryDeps = libraryDependencies.value
6667
val scalaBinaryVer = scalaBinaryVersion.value
6768
val filter = unusedCompileDependenciesFilter.value
68-
val log = streams.value.log
6969

7070
Logic.getUnusedCompileDependencies(
7171
projectName,

Diff for: src/main/scala/explicitdeps/Logic.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ object Logic {
7070

7171
val compileDependencies = compileDependencyJarFiles
7272
.flatMap(BoringStuff.jarFileToDependency(scalaBinaryVersion, log))
73-
log.debug(s"Compile depends on:\n${compileDependencies.mkString("\n")}")
73+
log.debug(s"Compile depends on:\n${compileDependencies.mkString(" ", "\n ", "")}")
7474

7575
compileDependencies
7676
}
@@ -82,7 +82,7 @@ object Logic {
8282

8383
val declaredCompileDependencies = compileConfigLibraryDependencies
8484
.map(moduleId => Dependency(moduleId.organization, moduleId.name, moduleId.revision, moduleId.crossVersion.isInstanceOf[Binary]))
85-
log.debug(s"Declared dependencies:\n${declaredCompileDependencies.mkString("\n")}")
85+
log.debug(s"Declared dependencies:\n${declaredCompileDependencies.mkString(" ", "\n ", "")}")
8686

8787
declaredCompileDependencies.toSet
8888
}

0 commit comments

Comments
 (0)