From a03f0ad437ce686fafa209808850e1763fe6a959 Mon Sep 17 00:00:00 2001 From: andrea Date: Mon, 22 Oct 2018 14:57:44 +0100 Subject: [PATCH] Add ignore errors flag --- build.sbt | 1 + .../coursier/CoursierConfiguration.scala | 22 +++++++----- .../CoursierConfigurationFormats.scala | 4 ++- coursier/src/main/contraband/lm-coursier.json | 6 ++++ .../CoursierDependencyResolution.scala | 34 +++++++++++++------ 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/build.sbt b/build.sbt index 7e6a26ae9..3d14abf85 100644 --- a/build.sbt +++ b/build.sbt @@ -302,6 +302,7 @@ addCommandAlias("scriptedIvy", Seq( addCommandAlias("scriptedCoursier", Seq( "lmCore/publishLocal", + "lmIvy/publishLocal", "lmCoursier/publishLocal", "lmScriptedTest/clean", """set ThisBuild / scriptedTestLMImpl := "coursier"""", diff --git a/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfiguration.scala b/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfiguration.scala index 1864c4440..a892e1c75 100644 --- a/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfiguration.scala +++ b/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfiguration.scala @@ -10,22 +10,23 @@ final class CoursierConfiguration private ( val otherResolvers: Vector[sbt.librarymanagement.Resolver], val reorderResolvers: Boolean, val parallelDownloads: Int, - val maxIterations: Int) extends Serializable { + val maxIterations: Int, + val ignoreArtifactErrors: Boolean) extends Serializable { - private def this() = this(None, sbt.librarymanagement.Resolver.defaults, Vector.empty, true, 6, 100) + private def this() = this(None, sbt.librarymanagement.Resolver.defaults, Vector.empty, true, 6, 100, false) override def equals(o: Any): Boolean = o match { - case x: CoursierConfiguration => (this.log == x.log) && (this.resolvers == x.resolvers) && (this.otherResolvers == x.otherResolvers) && (this.reorderResolvers == x.reorderResolvers) && (this.parallelDownloads == x.parallelDownloads) && (this.maxIterations == x.maxIterations) + case x: CoursierConfiguration => (this.log == x.log) && (this.resolvers == x.resolvers) && (this.otherResolvers == x.otherResolvers) && (this.reorderResolvers == x.reorderResolvers) && (this.parallelDownloads == x.parallelDownloads) && (this.maxIterations == x.maxIterations) && (this.ignoreArtifactErrors == x.ignoreArtifactErrors) case _ => false } override def hashCode: Int = { - 37 * (37 * (37 * (37 * (37 * (37 * (37 * (17 + "sbt.librarymanagement.coursier.CoursierConfiguration".##) + log.##) + resolvers.##) + otherResolvers.##) + reorderResolvers.##) + parallelDownloads.##) + maxIterations.##) + 37 * (37 * (37 * (37 * (37 * (37 * (37 * (37 * (17 + "sbt.librarymanagement.coursier.CoursierConfiguration".##) + log.##) + resolvers.##) + otherResolvers.##) + reorderResolvers.##) + parallelDownloads.##) + maxIterations.##) + ignoreArtifactErrors.##) } override def toString: String = { - "CoursierConfiguration(" + log + ", " + resolvers + ", " + otherResolvers + ", " + reorderResolvers + ", " + parallelDownloads + ", " + maxIterations + ")" + "CoursierConfiguration(" + log + ", " + resolvers + ", " + otherResolvers + ", " + reorderResolvers + ", " + parallelDownloads + ", " + maxIterations + ", " + ignoreArtifactErrors + ")" } - private[this] def copy(log: Option[xsbti.Logger] = log, resolvers: Vector[sbt.librarymanagement.Resolver] = resolvers, otherResolvers: Vector[sbt.librarymanagement.Resolver] = otherResolvers, reorderResolvers: Boolean = reorderResolvers, parallelDownloads: Int = parallelDownloads, maxIterations: Int = maxIterations): CoursierConfiguration = { - new CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations) + private[this] def copy(log: Option[xsbti.Logger] = log, resolvers: Vector[sbt.librarymanagement.Resolver] = resolvers, otherResolvers: Vector[sbt.librarymanagement.Resolver] = otherResolvers, reorderResolvers: Boolean = reorderResolvers, parallelDownloads: Int = parallelDownloads, maxIterations: Int = maxIterations, ignoreArtifactErrors: Boolean = ignoreArtifactErrors): CoursierConfiguration = { + new CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations, ignoreArtifactErrors) } def withLog(log: Option[xsbti.Logger]): CoursierConfiguration = { copy(log = log) @@ -48,10 +49,13 @@ final class CoursierConfiguration private ( def withMaxIterations(maxIterations: Int): CoursierConfiguration = { copy(maxIterations = maxIterations) } + def withIgnoreArtifactErrors(ignoreArtifactErrors: Boolean): CoursierConfiguration = { + copy(ignoreArtifactErrors = ignoreArtifactErrors) + } } object CoursierConfiguration { def apply(): CoursierConfiguration = new CoursierConfiguration() - def apply(log: Option[xsbti.Logger], resolvers: Vector[sbt.librarymanagement.Resolver], otherResolvers: Vector[sbt.librarymanagement.Resolver], reorderResolvers: Boolean, parallelDownloads: Int, maxIterations: Int): CoursierConfiguration = new CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations) - def apply(log: xsbti.Logger, resolvers: Vector[sbt.librarymanagement.Resolver], otherResolvers: Vector[sbt.librarymanagement.Resolver], reorderResolvers: Boolean, parallelDownloads: Int, maxIterations: Int): CoursierConfiguration = new CoursierConfiguration(Option(log), resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations) + def apply(log: Option[xsbti.Logger], resolvers: Vector[sbt.librarymanagement.Resolver], otherResolvers: Vector[sbt.librarymanagement.Resolver], reorderResolvers: Boolean, parallelDownloads: Int, maxIterations: Int, ignoreArtifactErrors: Boolean): CoursierConfiguration = new CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations, ignoreArtifactErrors) + def apply(log: xsbti.Logger, resolvers: Vector[sbt.librarymanagement.Resolver], otherResolvers: Vector[sbt.librarymanagement.Resolver], reorderResolvers: Boolean, parallelDownloads: Int, maxIterations: Int, ignoreArtifactErrors: Boolean): CoursierConfiguration = new CoursierConfiguration(Option(log), resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations, ignoreArtifactErrors) } diff --git a/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfigurationFormats.scala b/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfigurationFormats.scala index ff8713f87..a2cba5218 100644 --- a/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfigurationFormats.scala +++ b/coursier/src/main/contraband-scala/sbt/librarymanagement/coursier/CoursierConfigurationFormats.scala @@ -17,8 +17,9 @@ implicit lazy val CoursierConfigurationFormat: JsonFormat[sbt.librarymanagement. val reorderResolvers = unbuilder.readField[Boolean]("reorderResolvers") val parallelDownloads = unbuilder.readField[Int]("parallelDownloads") val maxIterations = unbuilder.readField[Int]("maxIterations") + val ignoreArtifactErrors = unbuilder.readField[Boolean]("ignoreArtifactErrors") unbuilder.endObject() - sbt.librarymanagement.coursier.CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations) + sbt.librarymanagement.coursier.CoursierConfiguration(log, resolvers, otherResolvers, reorderResolvers, parallelDownloads, maxIterations, ignoreArtifactErrors) case None => deserializationError("Expected JsObject but found None") } @@ -31,6 +32,7 @@ implicit lazy val CoursierConfigurationFormat: JsonFormat[sbt.librarymanagement. builder.addField("reorderResolvers", obj.reorderResolvers) builder.addField("parallelDownloads", obj.parallelDownloads) builder.addField("maxIterations", obj.maxIterations) + builder.addField("ignoreArtifactErrors", obj.ignoreArtifactErrors) builder.endObject() } } diff --git a/coursier/src/main/contraband/lm-coursier.json b/coursier/src/main/contraband/lm-coursier.json index 927175607..6b8d69166 100644 --- a/coursier/src/main/contraband/lm-coursier.json +++ b/coursier/src/main/contraband/lm-coursier.json @@ -42,6 +42,12 @@ "type": "Int", "default": "100", "since": "0.0.1" + }, + { + "name": "ignoreArtifactErrors", + "type": "Boolean", + "default": "false", + "since": "0.0.1" } ] } diff --git a/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala b/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala index 3c8ccd2a4..2b26a1f3e 100644 --- a/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala +++ b/coursier/src/main/scala/sbt/librarymanagement/coursier/CoursierDependencyResolution.scala @@ -143,10 +143,12 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC val start = Resolution(dependencies) val authentication = None // TODO: get correct value val ivyConfiguration = ivyProperties // TODO: is it enough? + val repositories = reorderedResolvers.flatMap(r => FromSbt.repository(r, ivyConfiguration, log, authentication)) ++ Seq( Cache.ivy2Local, - Cache.ivy2Cache) + Cache.ivy2Cache + ) implicit val ec = pool @@ -160,17 +162,34 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC .run(fetch, coursierConfiguration.maxIterations) .unsafeRun() - if (resolution.isDone && resolution.errors.isEmpty && resolution.conflicts.isEmpty) { + def updateReport() = { val localArtifacts: Map[Artifact, Either[FileError, File]] = Gather[Task] .gather( resolution.artifacts.map { a => - Cache.file[Task](a, logger = Some(coursierLogger)).run.map((a, _)) + Cache + .file[Task](a, logger = Some(coursierLogger)) + .run + .map((a, _)) } ) .unsafeRun() .toMap toUpdateReport(resolution, localArtifacts, log) + } + + if (resolution.isDone && + resolution.errors.isEmpty && + resolution.conflicts.isEmpty) { + updateReport() + } else if (resolution.isDone && + (!resolution.errors.isEmpty && coursierConfiguration.ignoreArtifactErrors) + && resolution.conflicts.isEmpty) { + log.warn(s"""Failed to download artifacts: ${resolution.errors + .map(_._2) + .flatten + .mkString(", ")}""") + updateReport() } else { toSbtError(log, uwconfig, resolution) } @@ -232,17 +251,12 @@ private[sbt] class CoursierDependencyResolution(coursierConfiguration: CoursierC a -> err } - if (artifactErrors.nonEmpty) { - // TODO: handle error the correct sbt way - throw new RuntimeException(s"Could not download dependencies: $artifactErrors") - } - - // can be non empty only if ignoreArtifactErrors is true or some optional artifacts are not found val erroredArtifacts = artifactFilesOrErrors0.collect { case (a, Left(_)) => a }.toSet - val depsByConfig = resolution.dependencies.groupBy(_.configuration).mapValues(_.toSeq) + val depsByConfig = + resolution.dependencies.groupBy(_.configuration).mapValues(_.toSeq) val configurations = extractConfigurationTree