Skip to content

Commit

Permalink
BugFix - Export issues when full scala version is not used in directi…
Browse files Browse the repository at this point in the history
…ves (#3032)

* Fixed resolving scala version when only major/minor part is provided in directives

* Added tests for export with different scala versions

* Fixed issue with export using the scala version resolved from BuildOptions as per review

* scalafmt ran

* Update modules/integration/src/test/scala/scala/cli/integration/ExportCommonTestDefinitions.scala

Co-authored-by: Piotr Chabelski <[email protected]>

* Update modules/cli/src/main/scala/scala/cli/exportCmd/MillProjectDescriptor.scala

Co-authored-by: Piotr Chabelski <[email protected]>

* Update modules/cli/src/main/scala/scala/cli/exportCmd/SbtProjectDescriptor.scala

Co-authored-by: Piotr Chabelski <[email protected]>

* Update modules/integration/src/test/scala/scala/cli/integration/ExportTestProjects.scala

Co-authored-by: Piotr Chabelski <[email protected]>

* fixed errors after review comments

---------

Co-authored-by: Piotr Chabelski <[email protected]>
  • Loading branch information
yadavan88 and Gedochao authored Jul 18, 2024
1 parent e1c5c9c commit e936d1c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ final case class MillProjectDescriptor(
options.classPathOptions.allExtraDependencies.toSeq
.forall(_.value.nameAttributes == NoAttributes)

val sv = options.scalaOptions.scalaVersion
.flatMap(_.versionOpt) // FIXME If versionOpt is empty, the project is pure Java
.getOrElse(ScalaCli.getDefaultScalaVersion)
val sv = options.scalaParams.toOption.flatten.map(_.scalaVersion).getOrElse(
ScalaCli.getDefaultScalaVersion // FIXME account for pure Java projects, where Scala version isn't defined
)

if (pureJava)
MillProject()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,11 @@ final case class SbtProjectDescriptor(
private def scalaVersionSettings(options: BuildOptions): SbtProject = {

val scalaVerSetting = {
val sv = options.scalaOptions.scalaVersion
.flatMap(_.versionOpt) // FIXME If versionOpt is empty, the project is pure Java
.getOrElse(ScalaCli.getDefaultScalaVersion)

val sv = options.scalaParams.toOption.flatten.map(_.scalaVersion).getOrElse(
ScalaCli.getDefaultScalaVersion // FIXME account for pure Java projects, where Scala version isn't defined
)

s"""scalaVersion := "$sv""""
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ trait ExportCommonTestDefinitions { _: ScalaCliSuite & TestScalaVersionArgs =>
expect(output.contains("Hello"))
}

protected def scalaVersionTest(scalaVersion: String): Unit =
prepareTestInputs(ExportTestProjects.scalaVersionTest(scalaVersion)).fromRoot {
root =>
exportCommand(".").call(cwd = root, stdout = os.Inherit)
val res = buildToolCommand(root, runMainArgs*)
.call(cwd = root / outputDir)
val output = res.out.text(Charset.defaultCharset())
expect(output.contains("Hello"))
}

def extraSourceFromDirectiveWithExtraDependency(inputs: String*): Unit =
prepareTestInputs(
ExportTestProjects.extraSourceFromDirectiveWithExtraDependency(actualScalaVersion)
Expand All @@ -86,6 +96,8 @@ trait ExportCommonTestDefinitions { _: ScalaCliSuite & TestScalaVersionArgs =>
}
}

private val scalaVersionsInDir: Seq[String] = Seq("2.12", "2.13", "2", "3", "3.lts")

if (runExportTests) {
test("compile-time only for jsoniter macros") {
compileOnlyTest()
Expand All @@ -105,5 +117,11 @@ trait ExportCommonTestDefinitions { _: ScalaCliSuite & TestScalaVersionArgs =>
test("extra source passed both via directive and from command line") {
extraSourceFromDirectiveWithExtraDependency(".")
}
scalaVersionsInDir.foreach { scalaV =>
test(s"check export for project with scala version in directive as $scalaV") {
scalaVersionTest(scalaV)
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,14 @@ object ExportTestProjects {
|}
|""".stripMargin
)

def scalaVersionTest(scalaVersion: String): TestInputs =
TestInputs(
os.rel / "Hello.scala" ->
s"""//> using scala $scalaVersion
|object Main extends App {
| println("Hello")
|}
|""".stripMargin
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.build.options
import dependency.AnyDependency

import scala.build.Positioned
import scala.build.internal.Constants

final case class ScalaOptions(
scalaVersion: Option[MaybeScalaVersion] = None,
Expand Down

0 comments on commit e936d1c

Please sign in to comment.