Skip to content

Commit

Permalink
Add an error about Scala 2.12 not supported for toolkits
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Dec 13, 2023
1 parent 295de6d commit 6539323
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 144 deletions.
4 changes: 3 additions & 1 deletion build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,9 @@ object ci extends Module {

def checkScalaVersions() = T.command {
website.checkMainScalaVersions(os.pwd / "website" / "docs" / "reference" / "scala-versions.md")
website.checkScalaJsVersions(os.pwd / "website" / "docs" / "guides" / "advanced" / "scala-js.md")
website.checkScalaJsVersions(
os.pwd / "website" / "docs" / "guides" / "advanced" / "scala-js.md"
)
}
}

Expand Down
24 changes: 24 additions & 0 deletions modules/build/src/test/scala/scala/build/tests/BuildTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -951,4 +951,28 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
expect(maybeBuild.exists(_.success))
}
}

for (dirValue <- Seq("default", "typelevel:default"))
test(s"error when toolkit $dirValue is used with Scala 2.12") {
val testInputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using toolkit $dirValue
|
|val n = 2
|println(s"n=$$n")
|""".stripMargin
)

val scala212Options = baseOptions.copy(
scalaOptions = baseOptions.scalaOptions.copy(
scalaVersion = Some(MaybeScalaVersion(Constants.defaultScala212Version)),
scalaBinaryVersion = None
),
scriptOptions = ScriptOptions(Some(true))
)

testInputs.withBuild(scala212Options, buildThreads, bloopConfigOpt) { (_, _, maybeBuild) =>
expect(maybeBuild.left.exists(_.message.startsWith("Toolkits do not support Scala 2.12")))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ object Repl extends ScalaCommand[ReplOptions] {
logger.message("Dry run, not running REPL.")
else {
val depClassPathArgs: Seq[String] =
if replArtifacts.depsClassPath.nonEmpty && !replArtifacts.replMainClass.startsWith("ammonite") then
if replArtifacts.depsClassPath.nonEmpty && !replArtifacts.replMainClass.startsWith(
"ammonite"
)
then
Seq(
"-classpath",
replArtifacts.depsClassPath.map(_.toString).mkString(File.pathSeparator)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package scala.build.errors

import scala.build.Position

final class ToolkitVersionError(msg: String, positions: Seq[Position])
extends BuildException(msg, positions)
Original file line number Diff line number Diff line change
Expand Up @@ -1908,134 +1908,135 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("actionable diagnostics on deprecated using directives") {
val inputs = TestInputs(
os.rel / "test.sc" ->
"""//> using toolkit latest
|//> using test.toolkit typelevel:latest
|
|//> using lib org.typelevel::cats-core:2.6.1
|
|object Test extends App {
| println("Hello")
|}
|""".stripMargin
)

withBsp(inputs, Seq(".", "--actions=false")) { (root, localClient, remoteServer) =>
async {
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
val target = {
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
expect(targets.length == 2)
extractMainTargets(targets)
}
if (!actualScalaVersion.startsWith("2.12"))
test("actionable diagnostics on deprecated using directives") {
val inputs = TestInputs(
os.rel / "test.sc" ->
"""//> using toolkit latest
|//> using test.toolkit typelevel:latest
|
|//> using lib org.typelevel::cats-core:2.6.1
|
|object Test extends App {
| println("Hello")
|}
|""".stripMargin
)

val targetUri = TestUtil.normalizeUri(target.getUri)
checkTargetUri(root, targetUri)
withBsp(inputs, Seq(".", "--actions=false")) { (root, localClient, remoteServer) =>
async {
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
val target = {
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
expect(targets.length == 2)
extractMainTargets(targets)
}

val targets = List(target).asJava
val targetUri = TestUtil.normalizeUri(target.getUri)
checkTargetUri(root, targetUri)

val compileResp = await {
remoteServer
.buildTargetCompile(new b.CompileParams(targets))
.asScala
}
expect(compileResp.getStatusCode == b.StatusCode.OK)
val targets = List(target).asJava

val diagnosticsParams = {
val diagnostics = localClient.diagnostics()
.filter(_.getReset == false)
expect(diagnostics.size == 3)
val params = diagnostics.head
expect(params.getBuildTarget.getUri == targetUri)
expect(
TestUtil.normalizeUri(params.getTextDocument.getUri) ==
TestUtil.normalizeUri((root / "test.sc").toNIO.toUri.toASCIIString)
)
diagnostics
}
val compileResp = await {
remoteServer
.buildTargetCompile(new b.CompileParams(targets))
.asScala
}
expect(compileResp.getStatusCode == b.StatusCode.OK)

val diagnostics = diagnosticsParams.flatMap(_.getDiagnostics.asScala)
.sortBy(_.getRange().getEnd().getCharacter())
val diagnosticsParams = {
val diagnostics = localClient.diagnostics()
.filter(_.getReset == false)
expect(diagnostics.size == 3)
val params = diagnostics.head
expect(params.getBuildTarget.getUri == targetUri)
expect(
TestUtil.normalizeUri(params.getTextDocument.getUri) ==
TestUtil.normalizeUri((root / "test.sc").toNIO.toUri.toASCIIString)
)
diagnostics
}

{
checkDiagnostic(
diagnostic = diagnostics.apply(0),
expectedMessage =
"Using `latest` is deprecated, use `default` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 0,
expectedStartCharacter = 10,
expectedEndLine = 0,
expectedEndCharacter = 24
)
val diagnostics = diagnosticsParams.flatMap(_.getDiagnostics.asScala)
.sortBy(_.getRange().getEnd().getCharacter())

{
checkDiagnostic(
diagnostic = diagnostics.apply(0),
expectedMessage =
"Using `latest` is deprecated, use `default` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 0,
expectedStartCharacter = 10,
expectedEndLine = 0,
expectedEndCharacter = 24
)

checkScalaAction(
diagnostic = diagnostics.apply(0),
expectedActionsSize = 1,
expectedTitle = "Change to: toolkit default",
expectedChanges = 1,
expectedStartLine = 0,
expectedStartCharacter = 10,
expectedEndLine = 0,
expectedEndCharacter = 24,
expectedNewText = "toolkit default"
)
}
checkScalaAction(
diagnostic = diagnostics.apply(0),
expectedActionsSize = 1,
expectedTitle = "Change to: toolkit default",
expectedChanges = 1,
expectedStartLine = 0,
expectedStartCharacter = 10,
expectedEndLine = 0,
expectedEndCharacter = 24,
expectedNewText = "toolkit default"
)
}

{
checkDiagnostic(
diagnostic = diagnostics.apply(1),
expectedMessage =
"Using `latest` is deprecated, use `default` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 1,
expectedStartCharacter = 10,
expectedEndLine = 1,
expectedEndCharacter = 39
)
{
checkDiagnostic(
diagnostic = diagnostics.apply(1),
expectedMessage =
"Using `latest` is deprecated, use `default` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 1,
expectedStartCharacter = 10,
expectedEndLine = 1,
expectedEndCharacter = 39
)

checkScalaAction(
diagnostic = diagnostics.apply(1),
expectedActionsSize = 1,
expectedTitle = "Change to: test.toolkit typelevel:default",
expectedChanges = 1,
expectedStartLine = 1,
expectedStartCharacter = 10,
expectedEndLine = 1,
expectedEndCharacter = 39,
expectedNewText = "test.toolkit typelevel:default"
)
}
checkScalaAction(
diagnostic = diagnostics.apply(1),
expectedActionsSize = 1,
expectedTitle = "Change to: test.toolkit typelevel:default",
expectedChanges = 1,
expectedStartLine = 1,
expectedStartCharacter = 10,
expectedEndLine = 1,
expectedEndCharacter = 39,
expectedNewText = "test.toolkit typelevel:default"
)
}

{
checkDiagnostic(
diagnostic = diagnostics.apply(2),
expectedMessage =
"Using `lib` is deprecated, use `dep` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 3,
expectedStartCharacter = 10,
expectedEndLine = 3,
expectedEndCharacter = 44
)
{
checkDiagnostic(
diagnostic = diagnostics.apply(2),
expectedMessage =
"Using `lib` is deprecated, use `dep` instead",
expectedSeverity = b.DiagnosticSeverity.WARNING,
expectedStartLine = 3,
expectedStartCharacter = 10,
expectedEndLine = 3,
expectedEndCharacter = 44
)

checkScalaAction(
diagnostic = diagnostics.apply(2),
expectedActionsSize = 1,
expectedTitle = "Change to: dep org.typelevel::cats-core:2.6.1",
expectedChanges = 1,
expectedStartLine = 3,
expectedStartCharacter = 10,
expectedEndLine = 3,
expectedEndCharacter = 44,
expectedNewText = "dep org.typelevel::cats-core:2.6.1"
)
checkScalaAction(
diagnostic = diagnostics.apply(2),
expectedActionsSize = 1,
expectedTitle = "Change to: dep org.typelevel::cats-core:2.6.1",
expectedChanges = 1,
expectedStartLine = 3,
expectedStartCharacter = 10,
expectedEndLine = 3,
expectedEndCharacter = 44,
expectedNewText = "dep org.typelevel::cats-core:2.6.1"
)
}
}
}
}
}

private def checkIfBloopProjectIsInitialised(
root: os.Path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2166,13 +2166,14 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("toolkit default works") {
test("toolkit default") {
val inputs = TestInputs(
os.rel / "Main.scala" ->
"""//> using toolkit default
|//> using toolkit typelevel:default
|
|import cats.effect.*
|import cats.effect.IOApp
|import cats.effect.IO
|
|object Hello extends IOApp.Simple {
| def run = IO.println(os.pwd)
Expand All @@ -2181,8 +2182,16 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
)

inputs.fromRoot { root =>
val result = os.proc(TestUtil.cli, extraOptions, ".").call(cwd = root, check = true)
expect(result.out.trim() == root.toString)
val result =
os.proc(TestUtil.cli, extraOptions, ".").call(cwd = root, stderr = os.Pipe, check = false)
if (actualScalaVersion.startsWith("2.12")) {
expect(result.exitCode == 1)
expect(result.err.trim().contains("Toolkits do not support Scala 2.12"))
}
else {
expect(result.exitCode == 0)
expect(result.out.trim() == root.toString)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,27 +733,29 @@ abstract class TestTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("toolkit") {
successfulTestInputs(s"//> using toolkit ${Constants.toolkitVersion}").fromRoot { root =>
val output = os.proc(TestUtil.cli, "test", extraOptions, ".").call(cwd = root).out.text()
expect(output.contains("Hello from tests"))
if (!actualScalaVersion.startsWith("2.12")) {
test("toolkit") {
successfulTestInputs(s"//> using toolkit ${Constants.toolkitVersion}").fromRoot { root =>
val output = os.proc(TestUtil.cli, "test", extraOptions, ".").call(cwd = root).out.text()
expect(output.contains("Hello from tests"))
}
}
}

test("toolkit from command line") {
successfulTestInputs("").fromRoot { root =>
val output =
os.proc(
TestUtil.cli,
"test",
extraOptions,
".",
"--toolkit",
Constants.toolkitVersion
).call(cwd =
root
).out.text()
expect(output.contains("Hello from tests"))
test("toolkit from command line") {
successfulTestInputs("").fromRoot { root =>
val output =
os.proc(
TestUtil.cli,
"test",
extraOptions,
".",
"--toolkit",
Constants.toolkitVersion
).call(cwd =
root
).out.text()
expect(output.contains("Hello from tests"))
}
}
}
}
Loading

0 comments on commit 6539323

Please sign in to comment.