0

Self-teaching Scala and SBT from some online resources. One of the difficulties is decoupling different concepts of the toolchain, IDE (trying intellij) and the learning the language. Why is SBT showing a different scalaVersion from whatever version the system says I have? And is it a concern in how my code is compiled? Here is the console output.

della@dell-xps ~/s/l/sbt-testing> scala --version
Scala code runner version 3.2.2 -- Copyright 2002-2023, LAMP/EPFL
della@dell-xps ~/s/l/sbt-testing> scalac --version
Scala compiler version 3.2.2 -- Copyright 2002-2023, LAMP/EPFL
della@dell-xps ~/s/l/sbt-testing> sbt
[info] welcome to sbt 1.8.2 (Ubuntu Java 11.0.17)
[info] loading project definition from /home/della/supply-chain-dev/learning_projects/sbt-testing/project
[info] set current project to sbt-testing (in build file:/home/della/supply-chain-dev/learning_projects/sbt-testing/)
[info] sbt server started at local:///home/della/.sbt/1.0/server/0b33882a8fc31214bc13/sock
[info] started sbt server
sbt:sbt-testing> scalaVersion
[info] 2.12.17
sbt:sbt-testing> ^D
della@dell-xps ~/s/l/sbt-testing> sbt --version
sbt version in this project: 1.8.2
sbt script version: 1.8.2

How to handle this? I read somewhere that scala 2 and 3 are not compatible, so how does it impact my code which is compatible with scala 3 but not with 2? Also, how to remove all traces of scala 2 from my system and force sbt to use the system scala version?

If important, the OS is Ubuntu 22.04.

Della
  • 1,264
  • 2
  • 15
  • 32
  • 1
    How did you install Scala and sbt? – Joachim Sauer Feb 22 '23 at 11:08
  • I do not remember the exact step, but the version 3.* involved Coursier in some form. Also, before that I installed somehow else, but that was 2.* which I purged. Now, coursier lists the following applications. amm coursier cs sbt sbtn scala scala-cli scalac scalafmt – Della Feb 22 '23 at 11:14
  • Also, does intellij install its own scala as well somehow to complicate things, or does it use the coursier scala? I remember in intellij, in some places it shows 2.*. – Della Feb 22 '23 at 11:15
  • 1
    https://docs.scala-lang.org/tutorials/FAQ/index.html#i-want-scala-213-or-some-other-version-why-does-sbt-say-its-using-scala-212 – Seth Tisue Feb 23 '23 at 03:37

1 Answers1

4

Normally, version of Scala installed in the system is irrelevant. There can be no Scala installed at all and you still will be able to run Scala with sbt if just Java and sbt are installed.

When you start repl (command scala), you run the Scala installed in the system. Regarding IntelliJ, you can look at File -> Project Structure.

In sbt you specify Scala version (scalaVersion := ...) in the build file (build.sbt). This is the version of Scala to be used for building the project. If sbt needs it will download necessary version of Scala.

Different subprojects of your project can be built/cross-built with different versions of Scala.

sbt:sbt-testing> scalaVersion
[info] 2.12.17

If you don't specify Scala version in build.sbt, by default sbt uses Scala version it's built with. It's some 2.12.x Scala.

Also, how to remove all traces of scala 2 from my system

Only you can know how to rollback completely those steps you did to install it. You can try to run which scala, which scalac to look where it's located. You can delete the directory. Also you can look at PATH and SCALA_HOME variables (echo $PATH, echo $SCALA_HOME) and remove Scala parts.

force sbt to use the system scala version?

You shouldn't do this. You can specify any Scala version you need in the build file. Also when you clonning/downloading somebody else's project it should be run with Scala version specified there.

In principle you can do this

https://www.scala-sbt.org/1.x/docs/Local-Scala.html

https://www.scala-sbt.org/1.x/docs/Configuring-Scala.html

https://www.scala-sbt.org/1.x/docs/Howto-Scala.html

I read somewhere that scala 2 and 3 are not compatible

Yeah, Scala 2 and Scala 3 are different. For example type T = List[Option[A]] forSome {type A} compiles in Scala 2 but not Scala 3, type T = [A] =>> [B] =>> (A, B) compiles in Scala 3 but not Scala 2.

http://dotty.epfl.ch/docs/reference/other-new-features/index.html

http://dotty.epfl.ch/docs/reference/changed-features/index.html

http://dotty.epfl.ch/docs/reference/dropped-features/index.html

But there is also Scala 2.13 - Scala 3 interop

https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html

You can see example of a project using both Scala 2.13 and Scala 3 in Scala Spark Encoders.product[X] (where X is a case class) keeps giving me "No TypeTag available for X" error


Which version of SBT ? scala2.13.3

About sbt and how it runs: SBT gives java.lang.NullPointerException when trying to run spark

How can I check the SBT version?

Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66