Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksandr Vyshniak <[email protected]>
  • Loading branch information
molekyla committed Jul 5, 2024
1 parent 3f24991 commit ae8b656
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ The credentials can be supplied through these methods, with the following preced
3. By creating the file `~/.sbt/.credentials` in your home directory.
4. By creating the file `~/.ivy2/.credentials` in your home directory.

### Resolving issues
If your project packaging failed you can examine plugin debug logs using commands:
```shell
sbt package
sbt last
```
This sequence of commands will print all the logs generated during packaging to the console

## Contributors
- Evgenii Kuznetcov (https://github.com/simpadjo)
- Oleksandr Vyshniak (https://github.com/molekyla)
Expand Down
10 changes: 7 additions & 3 deletions plugin/src/main/scala/com/here/bom/Bom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ object Bom {
)
.withUpdateOptions((update / updateOptions).value)
loadCredentials(logger)
logger.debug(f"Ivy configuration used for the BOM dependency resolution: $ivyConfig")
val depRes = new DependencyResolutionProxy(IvyDependencyResolution(ivyConfig))
val ivyHome = (update / Keys.ivyPaths).value.ivyHome.get
IvyPomLocator.tweakIvyHome(logger)
Expand All @@ -68,19 +69,22 @@ object Bom {
}

private def loadCredentials(logger: Logger): Unit = {
val filePaths: Seq[File] = Seq(
val credentialFiles = Seq(
Option(sys.props("sbt.boot.credentials"))
.filter(s => s != null && s.nonEmpty)
.map(new File(_)),
sys.env.get("SBT_CREDENTIALS").map(new File(_)),
Some(Path.userHome / ".ivy2" / ".credentials"),
Some(Path.userHome / ".sbt" / ".credentials")
).flatten
)
logger.debug(s"Credentials load order: $credentialFiles")
val filePaths: Seq[File] = credentialFiles.flatten
filePaths.find(_.exists()) match {
case Some(file) =>
logger.info(s"Loading credentials from file $file")
logger.debug(s"Loading credentials from file $file")
Credentials.loadCredentials(file) match {
case Right(d) =>
logger.debug(s"Credentials loaded from file $file")
CredentialsStore.INSTANCE.addCredentials(d.realm, d.host, d.userName, d.passwd)
case Left(error) => logger.warn(s"Failed to load credentials: $error")
}
Expand Down
10 changes: 9 additions & 1 deletion plugin/src/main/scala/com/here/bom/internal/BomReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
.getPomFile(moduleId)
.getOrElse(sys.error(s"Failed to resolve ${moduleId}"))
val url = pomFile.asURL
logger.debug(f"Reading pom file $url")
new PomReader(url, new URLResource(url))
}

Expand Down Expand Up @@ -146,6 +147,8 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
.groupBy(e => (e._1.group, e._1.name))
.mapValues(chooseBestVersion)

logger.debug(s"Effective resolved versions: $effectiveVersions")

new Bom {
override def version(dependency: OrganizationArtifactName): String = {
val normalized = NormalizedArtifact.fromModule(dependency % "whatever", scalaBinaryVersion)
Expand All @@ -170,6 +173,7 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
priority: Priority
): List[(ResolvedBom, Priority)] = {
val chain = buildParentsChain(module)
logger.debug(f"Resolved parents chain for the module: $module: $chain")
val rootProps = new Props()
rootProps.put("scala.compat.version", scalaBinaryVersion)
val rootPriority = priority + chain.size - 1
Expand All @@ -191,7 +195,9 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
}
}

attachInheritedProps(chain, Nil, rootProps, rootPriority)
val tuples = attachInheritedProps(chain, Nil, rootProps, rootPriority)
logger.debug(f"Inherited properties: $rootProps")
tuples
}

private def mergeProperties(reader: PomReader, props: Props): Props = {
Expand Down Expand Up @@ -234,6 +240,8 @@ class BomReader(pomLocator: IvyPomLocator, logger: Logger, scalaBinaryVersion: S
evalOrFail(reader.getParentVersion)
)

logger.debug(f"Module $module has parent $parent")

go(parent, (current, reader) :: acc)
} else {
(current, reader) :: acc
Expand Down
10 changes: 9 additions & 1 deletion plugin/src/main/scala/com/here/bom/internal/IvyPomLocator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ class IvyPomLocator(resolver: DependencyResolutionProxy, ivyHome: File, logger:
s"/cache/${moduleId.group}/${moduleId.name}/ivy-${moduleId.version}.xml.original"
)
if (localIvyPomLocation.exists()) {
logger.debug(f"Found local pom file: $localIvyPomLocation")
Some(localIvyPomLocation)
} else {
val ivyPropsFile = new File(
ivyHome,
s"/cache/${moduleId.group}/${moduleId.name}/ivydata-${moduleId.version}.properties"
)
logger.debug(f"Trying to load local pom file: $ivyPropsFile")
referencedPomLocation(ivyPropsFile)
}
}
Expand All @@ -128,11 +130,17 @@ class IvyPomLocator(resolver: DependencyResolutionProxy, ivyHome: File, logger:

val locationKey =
properties.keys().asScala.find(key => key.asInstanceOf[String].endsWith(".location"))
if (locationKey.isEmpty) {
logger.warn(f"No *.location entry in the file $ivyPropertyFile")
}
locationKey.map(k => {
val f = new File(properties.getProperty(k.asInstanceOf[String]))
require(f.exists(), s"File $f doesn't exist, is artifact cache corrupted?")
f
})
} else None
} else {
logger.debug(f"File: $ivyPropertyFile not exists")
None
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {

"BomReader" should "assemble the BOM correctly" in {
val pomLocatorMock = mock[IvyPomLocator]
val loggerMock = mock[Logger]
val loggerMock = stub[Logger]
val scalaBinaryVersion = "2.12"

val bomReader = new BomReader(pomLocatorMock, loggerMock, scalaBinaryVersion)
Expand All @@ -50,7 +50,7 @@ class BomReaderSpec extends AnyFlatSpec with Matchers with MockFactory {

"BomReader" should "assemble the BOM correctly with parent" in {
val pomLocatorMock = mock[IvyPomLocator]
val loggerMock = mock[Logger]
val loggerMock = stub[Logger]
val scalaBinaryVersion = "2.12"

val bomReader = new BomReader(pomLocatorMock, loggerMock, scalaBinaryVersion)
Expand Down

0 comments on commit ae8b656

Please sign in to comment.