scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.72k stars 1.04k forks source link

binary distribution: package information about jars required for scalac classpath in a separate file #20413

Open unkarjedy opened 1 month ago

unkarjedy commented 1 month ago

Context:

In IntelliJ IDEA, you can use a plain IntelliJ project without build system SBT/Maven/Gradle/BSP. In Scala Plugin we have a concept of "Scala SDK" which simply represents the scala compiler (classpath) and scala version. In case the user already has Scala "installed" (whatever that means) IntelliJ has some logic to detect it. We have multiple kinds of detectors for Brew, SDKMAN, Coursier, Maven, Ivy.

We also have a "System" detector which can detect Scala SDK from the unzipped binary Scala distribution (see Assets at github) To construct Scala Compiler classpath, we read some jars from lib directory.

In Scala2 the code of constructing the classpath was quite simple. We just search for a fixed set of compiler jars: scala-compiler-2.X.Y.jar, scala-library-2.X.Y.jar, scala-reflect-2.X.Y.jar:

image

Issue

In Scala3 things get different. Scala 3 compiler classpath has more dependencies on more libraries. In general, each minor/major Scala 3 version can have dependencies on different libraries with different versions. We can't just include all jars from lib folder. We need an explicit list of compiler classpath dependencies because lib folder contains a lot of other jar files, not required for the compiler. AFAIU most of them are needed for scaladoc, not scalac.

image

With Maven/Coursier/Ivy detectors, we can live with it because we can resolve transitive dependencies for scala3-compiler_3 and get the classpath. However, with System detector we can't do it. Three years ago I provided a hardcoded solution for Scala 3.0.0 (see the code) It worked up to Scala 3.3.1 but shot back in 3.3.2 & 3.4.x (see https://youtrack.jetbrains.com/issue/SCL-22510).

Proposal

I think the only reliable solution is that along with the jar files, scala compiler publishes the classpath required for scalac. For consistency, it could also publish the classpath for Scaladoc. For example, it could be just two files:

Those files could contain a list of jar names or even jar name patterns on every line. Then in IntelliJ we could simply read that file content. If you have better ideas on the file names/format, we can discuss it.

Notes

Note that the information about scalac/scaladoc classpath is already known to the code which does the binary distribution packaging. It's already present in bin/common, bin/common.bat, bin/scaladoc, bin/scaladoc.bat. However, it would be a too fragile solution to rely on those files as their structure can change and our parsing logic can break.

unkarjedy commented 1 month ago

Note that the information about scalac/scaladoc classpath is already known to the code which does the binary distribution packaging. It's already present in bin/common, bin/common.bat, bin/scaladoc, bin/scaladoc.bat

I also notice that the classpath used in those scripts has one suspicious extra jar scala3-staging_3*.jar It's not a transitive dependency of scala3-compiler_3, also it's not reported by SBT (show scalaInstance).

What is that jar? Is it indeed required for scalac classpath?

unkarjedy commented 1 month ago

Related links:

bishabosha commented 1 month ago

we were proposing to heavily change the lib directory currently in https://github.com/scala/scala3/pull/20351 (e.g. removing it or perhaps converting the contents to alias links) - so would be good to know if its actually used by other tooling - it seems it is - it would be good to work together on this

unkarjedy commented 1 month ago

@bishabosha Could you please point to the particular place in the discussion mentioning "potential removing of lib folder"?

or perhaps converting the contents to alias links

Alias links to where?

it would be good to work together on this

Is there anything else we can provide from our side?

bishabosha commented 1 month ago

Is there anything else we can provide from our side?

I updated the PR description of https://github.com/scala/scala3/pull/20351. otherwise you can download the zip from 3.5.0-RC1 release

bishabosha commented 1 month ago

I also notice that the classpath used in those scripts has one suspicious extra jar scala3-staging_3*.jar It's not a transitive dependency of scala3-compiler_3, also it's not reported by SBT (show scalaInstance).

What is that jar? Is it indeed required for scalac classpath?

there is also a scala3-tasty-inspector_3 jar to go alongside scala3-staging_3 - these are not normally on the classpath for scala/scalac, but are added to the classpath if the user passes the -with-compiler argument to the launcher. (Typically the user would need to add explicit libraryDependencies to use them in sbt/mill)

bishabosha commented 2 weeks ago

@unkarjedy what do you think of the change in https://github.com/scala/scala3/pull/20631 which would add files which provide the classpath?

unkarjedy commented 2 weeks ago

@bishabosha Looks good. Do you already have some nightly/rc build to try it our on our side?

bishabosha commented 2 weeks ago

Do you already have some nightly/rc build to try it our on our side?

@unkarjedy no but you can build it yourself with sbt dist/packArchive. I hope that this will be coming in 3.5.0-RC3 but that is a long time