From 7221788fc4f9bb2cfabbbb9e3a3782751d2751ff Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Fri, 11 Oct 2024 14:02:51 +0200 Subject: [PATCH] Fix JavaModule.artifactTypes (#3719) Follow-up of https://github.com/com-lihaoyi/mill/pull/3703 that added `JavaModule.artifactTypes`. `JavaModule` performs two dependency resolutions: one for `compileClasspath`, the other for `runClasspath`. https://github.com/com-lihaoyi/mill/pull/3703 used `artifactTypes` in the `runClasspath` one, but not in the `compileClasspath` one. The PR here fixes that, and adds unit tests for both paths. This addresses the issues found around https://github.com/com-lihaoyi/mill/pull/3696#issuecomment-2405891608. --- .../src/DocAnnotationsTests.scala | 2 +- scalalib/src/mill/scalalib/JavaModule.scala | 5 +++- .../resources/pomArtifactType/.placeholder | 0 .../src/mill/scalalib/ResolveDepsTests.scala | 29 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 scalalib/test/resources/pomArtifactType/.placeholder diff --git a/integration/feature/docannotations/src/DocAnnotationsTests.scala b/integration/feature/docannotations/src/DocAnnotationsTests.scala index 02d27311844..4ef2900e86c 100644 --- a/integration/feature/docannotations/src/DocAnnotationsTests.scala +++ b/integration/feature/docannotations/src/DocAnnotationsTests.scala @@ -93,7 +93,7 @@ object DocAnnotationsTests extends UtestIntegrationTestSuite { assert( globMatches( - """core.ivyDepsTree(JavaModule.scala:893) + """core.ivyDepsTree(JavaModule.scala:896) | Command to print the transitive dependency tree to STDOUT. | | --inverse Invert the tree representation, so that the root is on the bottom val diff --git a/scalalib/src/mill/scalalib/JavaModule.scala b/scalalib/src/mill/scalalib/JavaModule.scala index f72bc67c743..4d92101bcb4 100644 --- a/scalalib/src/mill/scalalib/JavaModule.scala +++ b/scalalib/src/mill/scalalib/JavaModule.scala @@ -545,7 +545,10 @@ trait JavaModule * Resolved dependencies based on [[transitiveIvyDeps]] and [[transitiveCompileIvyDeps]]. */ def resolvedIvyDeps: T[Agg[PathRef]] = Task { - defaultResolver().resolveDeps(transitiveCompileIvyDeps() ++ transitiveIvyDeps()) + defaultResolver().resolveDeps( + transitiveCompileIvyDeps() ++ transitiveIvyDeps(), + artifactTypes = Some(artifactTypes()) + ) } /** diff --git a/scalalib/test/resources/pomArtifactType/.placeholder b/scalalib/test/resources/pomArtifactType/.placeholder new file mode 100644 index 00000000000..e69de29bb2d diff --git a/scalalib/test/src/mill/scalalib/ResolveDepsTests.scala b/scalalib/test/src/mill/scalalib/ResolveDepsTests.scala index cd55d1b14b5..a54a2d023f9 100644 --- a/scalalib/test/src/mill/scalalib/ResolveDepsTests.scala +++ b/scalalib/test/src/mill/scalalib/ResolveDepsTests.scala @@ -4,6 +4,7 @@ import coursier.maven.MavenRepository import mill.api.Result.{Failure, Success} import mill.api.{PathRef, Result} import mill.api.Loose.Agg +import mill.testkit.{UnitTester, TestBaseModule} import utest._ object ResolveDepsTests extends TestSuite { @@ -28,6 +29,21 @@ object ResolveDepsTests extends TestSuite { assert(upickle.default.read[Dep](upickle.default.write(dep)) == dep) } } + + object TestCase extends TestBaseModule { + object pomStuff extends JavaModule { + def ivyDeps = Agg( + // Dependency whose packaging is "pom", as it's meant to be used + // as a "parent dependency" by other dependencies, rather than be pulled + // as we do here. We do it anyway, to check that pulling the "pom" artifact + // type brings that dependency POM file in the class path. We need a dependency + // that has a "pom" packaging for that. + ivy"org.apache.hadoop:hadoop-yarn-server:3.4.0" + ) + def artifactTypes = super.artifactTypes() + coursier.Type("pom") + } + } + val tests = Tests { test("resolveValidDeps") { val deps = Agg(ivy"com.lihaoyi::pprint:0.5.3") @@ -95,5 +111,18 @@ object ResolveDepsTests extends TestSuite { val Failure(errMsg, _) = evalDeps(deps) assert(errMsg.contains("fake")) } + + test("pomArtifactType") { + val sources = os.Path(sys.env("MILL_TEST_RESOURCE_DIR")) / "pomArtifactType" + UnitTester(TestCase, sourceRoot = sources).scoped { eval => + val Right(compileResult) = eval(TestCase.pomStuff.compileClasspath) + val compileCp = compileResult.value.toSeq.map(_.path) + assert(compileCp.exists(_.lastOpt.contains("hadoop-yarn-server-3.4.0.pom"))) + + val Right(runResult) = eval(TestCase.pomStuff.runClasspath) + val runCp = runResult.value.toSeq.map(_.path) + assert(runCp.exists(_.lastOpt.contains("hadoop-yarn-server-3.4.0.pom"))) + } + } } }