From 2fd34692bd3d57e6d4468f2c077e134ba70bc3c6 Mon Sep 17 00:00:00 2001 From: Piotr Chabelski Date: Wed, 11 Oct 2023 11:34:02 +0200 Subject: [PATCH] Ensure build resolution is kept when packaging assemblies with provided dependencies (#2457) --- .../preprocessing/directives/Packaging.scala | 12 ++-- .../integration/PackageTestDefinitions.scala | 66 +++++++++++++++++++ 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/modules/directives/src/main/scala/scala/build/preprocessing/directives/Packaging.scala b/modules/directives/src/main/scala/scala/build/preprocessing/directives/Packaging.scala index d76856f21d..8204e30bf6 100644 --- a/modules/directives/src/main/scala/scala/build/preprocessing/directives/Packaging.scala +++ b/modules/directives/src/main/scala/scala/build/preprocessing/directives/Packaging.scala @@ -11,15 +11,8 @@ import scala.build.errors.{ MalformedInputError, ModuleFormatError } +import scala.build.options._ import scala.build.options.packaging.{DockerOptions, NativeImageOptions} -import scala.build.options.{ - BuildOptions, - JavaOpt, - PackageOptions, - PackageType, - PostBuildOptions, - ShadowingSeq -} import scala.build.{Logger, Positioned, options} import scala.cli.commands.SpecificationLevel @@ -100,6 +93,9 @@ final case class Packaging( } BuildOptions( + internal = InternalOptions( + keepResolution = provided0.nonEmpty || packageTypeOpt.contains(PackageType.Spark) + ), notForBloopOptions = PostBuildOptions( packageOptions = PackageOptions( packageTypeOpt = packageTypeOpt, diff --git a/modules/integration/src/test/scala/scala/cli/integration/PackageTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/PackageTestDefinitions.scala index b089205487..224d97a518 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/PackageTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/PackageTestDefinitions.scala @@ -1057,4 +1057,70 @@ abstract class PackageTestDefinitions(val scalaVersionOpt: Option[String]) expect(outputAssembly == root.toString) } } + + if (actualScalaVersion.startsWith("2")) { + test("resolution is kept for assemblies with provided spark deps (packaging.provided)") { + val msg = "Hello" + val inputPath = os.rel / "Hello.scala" + TestInputs( + inputPath -> + s"""//> using lib org.apache.spark::spark-sql:3.3.2 + |//> using lib org.apache.spark::spark-hive:3.3.2 + |//> using lib org.apache.spark::spark-sql-kafka-0-10:3.3.2 + |//> using packaging.packageType assembly + |//> using packaging.provided org.apache.spark::spark-sql + |//> using packaging.provided org.apache.spark::spark-hive + | + |object Main extends App { + | println("$msg") + |} + |""".stripMargin + ).fromRoot { root => + val outputJarPath = root / "Hello.jar" + val res = os.proc( + TestUtil.cli, + "--power", + "package", + inputPath, + "-o", + outputJarPath, + extraOptions + ).call(cwd = root, stderr = os.Pipe) + expect(os.isFile(outputJarPath)) + expect(res.err.trim().contains(s"Wrote $outputJarPath")) + } + } + + test( + "resolution is kept for assemblies with provided spark deps (packaging.packageType spark)" + ) { + val msg = "Hello" + val inputPath = os.rel / "Hello.scala" + TestInputs( + inputPath -> + s"""//> using lib org.apache.spark::spark-sql:3.3.2 + |//> using lib org.apache.spark::spark-hive:3.3.2 + |//> using lib org.apache.spark::spark-sql-kafka-0-10:3.3.2 + |//> using packaging.packageType spark + | + |object Main extends App { + | println("$msg") + |} + |""".stripMargin + ).fromRoot { root => + val outputJarPath = root / "Hello.jar" + val res = os.proc( + TestUtil.cli, + "--power", + "package", + inputPath, + "-o", + outputJarPath, + extraOptions + ).call(cwd = root, stderr = os.Pipe) + expect(os.isFile(outputJarPath)) + expect(res.err.trim().contains(s"Wrote $outputJarPath")) + } + } + } }