From 3d374247cac8403c301ca2ddf56ea2be4da07e7d Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Mon, 18 May 2020 16:54:19 +0200 Subject: [PATCH] Rely on coursier.Resolve to handle exclusions It applies it to root dependencies too, in particular. --- .../CoursierDependencyResolution.scala | 22 ++++++------- .../sbtcoursiershared/InputsTasks.scala | 14 +------- .../sbtcoursier/ResolutionTasks.scala | 13 +++++++- .../{ => a}/src/main/scala/Main.scala | 0 .../b/src/main/scala/Main.scala | 30 +++++++++++++++++ .../shared-1/exclude-dependencies/build.sbt | 33 ++++++++++++++----- .../shared-1/exclude-dependencies/test | 8 +++-- 7 files changed, 84 insertions(+), 36 deletions(-) rename modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/{ => a}/src/main/scala/Main.scala (100%) create mode 100644 modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/b/src/main/scala/Main.scala diff --git a/modules/lm-coursier/src/main/scala/lmcoursier/CoursierDependencyResolution.scala b/modules/lm-coursier/src/main/scala/lmcoursier/CoursierDependencyResolution.scala index 3d9d2cf6f..82b00d189 100644 --- a/modules/lm-coursier/src/main/scala/lmcoursier/CoursierDependencyResolution.scala +++ b/modules/lm-coursier/src/main/scala/lmcoursier/CoursierDependencyResolution.scala @@ -25,14 +25,6 @@ class CoursierDependencyResolution(conf: CoursierConfiguration) extends Dependen * sbt-coursier, that was moved to this module. */ - private lazy val excludeDependencies = conf - .excludeDependencies - .map { - case (strOrg, strName) => - (lmcoursier.definitions.Organization(strOrg), lmcoursier.definitions.ModuleName(strName)) - } - .toSet - def moduleDescriptor(moduleSetting: ModuleDescriptorConfiguration): ModuleDescriptor = CoursierModuleDescriptor(moduleSetting, conf) @@ -125,8 +117,7 @@ class CoursierDependencyResolution(conf: CoursierConfiguration) extends Dependen } .map { case (config, dep) => - val dep0 = dep.withExclusions(dep.exclusions ++ excludeDependencies) - (ToCoursier.configuration(config), ToCoursier.dependency(dep0)) + (ToCoursier.configuration(config), ToCoursier.dependency(dep)) } val orderedConfigs = Inputs.orderedConfigurations(Inputs.configExtendsSeq(module0.configurations)) @@ -145,6 +136,14 @@ class CoursierDependencyResolution(conf: CoursierConfiguration) extends Dependen .withCredentials(conf.credentials.map(ToCoursier.credentials)) .withFollowHttpToHttpsRedirections(conf.followHttpToHttpsRedirections.getOrElse(true)) + val excludeDependencies = conf + .excludeDependencies + .map { + case (strOrg, strName) => + (coursier.Organization(strOrg), coursier.ModuleName(strName)) + } + .toSet + val resolutionParams = ResolutionParams( dependencies = dependencies, fallbackDependencies = conf.fallbackDependencies, @@ -164,7 +163,8 @@ class CoursierDependencyResolution(conf: CoursierConfiguration) extends Dependen .withProfiles(conf.mavenProfiles.toSet) .withForceVersion(conf.forceVersions.map { case (k, v) => (ToCoursier.module(k), v) }.toMap) .withTypelevel(typelevel) - .withReconciliation(ToCoursier.reconciliation(conf.reconciliation)), + .withReconciliation(ToCoursier.reconciliation(conf.reconciliation)) + .withExclusions(excludeDependencies), strictOpt = conf.strict.map(ToCoursier.strict), missingOk = conf.missingOk, ) diff --git a/modules/sbt-coursier-shared/src/main/scala/coursier/sbtcoursiershared/InputsTasks.scala b/modules/sbt-coursier-shared/src/main/scala/coursier/sbtcoursiershared/InputsTasks.scala index 0561db1cc..97d77d458 100644 --- a/modules/sbt-coursier-shared/src/main/scala/coursier/sbtcoursiershared/InputsTasks.scala +++ b/modules/sbt-coursier-shared/src/main/scala/coursier/sbtcoursiershared/InputsTasks.scala @@ -28,32 +28,21 @@ object InputsTasks { private def coursierProject0( projId: ModuleID, dependencies: Seq[ModuleID], - excludeDeps: Seq[InclExclRule], configurations: Seq[sbt.librarymanagement.Configuration], sv: String, sbv: String, log: Logger ): Project = { - val exclusions0 = Inputs.exclusions(excludeDeps, sv, sbv, log) - val configMap = Inputs.configExtendsSeq(configurations).toMap - val proj = FromSbt.project( + FromSbt.project( projId, dependencies, configMap, sv, sbv ) - - proj.withDependencies( - proj.dependencies.map { - case (config, dep) => - val dep0 = dep.withExclusions(dep.exclusions ++ exclusions0) - (config, dep0) - } - ) } private[sbtcoursiershared] def coursierProjectTask: Def.Initialize[sbt.Task[Project]] = @@ -68,7 +57,6 @@ object InputsTasks { coursierProject0( projectID.in(projectRef).get(state), allDependenciesTask.value, - actualExcludeDependencies.in(projectRef).get(state), // should projectID.configurations be used instead? ivyConfigurations.in(projectRef).get(state), scalaVersion.in(projectRef).get(state), diff --git a/modules/sbt-coursier/src/main/scala/coursier/sbtcoursier/ResolutionTasks.scala b/modules/sbt-coursier/src/main/scala/coursier/sbtcoursier/ResolutionTasks.scala index 463bea98d..ee3166fe0 100644 --- a/modules/sbt-coursier/src/main/scala/coursier/sbtcoursier/ResolutionTasks.scala +++ b/modules/sbt-coursier/src/main/scala/coursier/sbtcoursier/ResolutionTasks.scala @@ -114,6 +114,16 @@ object ResolutionTasks { .map(_.foldLeft[ProjectCache](Map.empty)(_ ++ _)) .getOrElse(Map.empty) + val excludeDeps = Inputs.exclusions( + coursier.sbtcoursiershared.InputsTasks.actualExcludeDependencies.value, + sv, + sbv, + log + ).map { + case (org, name) => + (Organization(org.value), ModuleName(name.value)) + } + val mainRepositories = resolvers .flatMap { resolver => Resolvers.repository( @@ -154,7 +164,8 @@ object ResolutionTasks { .withProfiles(userEnabledProfiles) .withForceVersion(userForceVersions.map { case (k, v) => (ToCoursier.module(k), v) }.toMap) .withTypelevel(typelevel) - .addReconciliation(versionReconciliations0: _*), + .addReconciliation(versionReconciliations0: _*) + .withExclusions(excludeDeps), strictOpt = strictOpt, missingOk = missingOk, ), diff --git a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/src/main/scala/Main.scala b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/a/src/main/scala/Main.scala similarity index 100% rename from modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/src/main/scala/Main.scala rename to modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/a/src/main/scala/Main.scala diff --git a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/b/src/main/scala/Main.scala b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/b/src/main/scala/Main.scala new file mode 100644 index 000000000..89960e16b --- /dev/null +++ b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/b/src/main/scala/Main.scala @@ -0,0 +1,30 @@ +import java.io.File +import java.nio.file.Files + +import scala.util.Try + +object Main extends App { + + def classFound(clsName: String) = Try( + Thread.currentThread() + .getContextClassLoader() + .loadClass(clsName) + ).toOption.nonEmpty + + val shapelessFound = classFound("shapeless.HList") + val argonautFound = classFound("argonaut.Json") + val argonautShapelessFound = classFound("argonaut.derive.MkEncodeJson") + + assert( + argonautShapelessFound, + "Expected to find class from argonaut-shapeless" + ) + assert( + !shapelessFound, + "Expected not to find classes from shapeless" + ) + assert( + !argonautFound, + "Expected not to find classes from argonaut" + ) +} diff --git a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/build.sbt b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/build.sbt index 0ac1493d5..7888e23ca 100644 --- a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/build.sbt +++ b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/build.sbt @@ -1,11 +1,26 @@ -scalaVersion := "2.12.8" +lazy val a = project + .settings( + organization := "io.get-coursier.test", + name := "sbt-coursier-exclude-dependencies", + version := "0.1.0-SNAPSHOT", + scalaVersion := "2.12.8", + libraryDependencies += "com.github.alexarchambault" %% "argonaut-shapeless_6.2" % "1.2.0-M11", + excludeDependencies += sbt.ExclusionRule("com.chuusai", "shapeless_2.12"), + excludeDependencies += "io.argonaut" %% "argonaut" + ) -organization := "io.get-coursier.test" -name := "sbt-coursier-exclude-dependencies" -version := "0.1.0-SNAPSHOT" - -libraryDependencies += "com.github.alexarchambault" %% "argonaut-shapeless_6.2" % "1.2.0-M11" - -excludeDependencies += sbt.ExclusionRule("com.chuusai", "shapeless_2.12") -excludeDependencies += "io.argonaut" %% "argonaut" +lazy val b = project + .settings( + organization := "io.get-coursier.test", + name := "sbt-coursier-exclude-dependencies-2", + version := "0.1.0-SNAPSHOT", + scalaVersion := "2.12.8", + libraryDependencies ++= Seq( + "com.github.alexarchambault" %% "argonaut-shapeless_6.2" % "1.2.0-M11", + "com.chuusai" %% "shapeless" % "2.3.3", + "io.argonaut" %% "argonaut" % "6.2.3" + ), + excludeDependencies += sbt.ExclusionRule("com.chuusai", "shapeless_2.12"), + excludeDependencies += "io.argonaut" %% "argonaut" + ) diff --git a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/test b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/test index e3e7dcee8..9eee79a6c 100644 --- a/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/test +++ b/modules/sbt-coursier/src/sbt-test/shared-1/exclude-dependencies/test @@ -1,3 +1,7 @@ -> run -> publishLocal +> a/run +> a/publishLocal $ exec java -jar coursier launch io.get-coursier.test:sbt-coursier-exclude-dependencies_2.12:0.1.0-SNAPSHOT +> b/run +> b/publishLocal +# doesn't pass yet (missing exclusions of root deps in ivy.xml?) +# $ exec java -jar coursier launch io.get-coursier.test:sbt-coursier-exclude-dependencies-2_2.12:0.1.0-SNAPSHOT