VirtusLab / scala-cli

Scala CLI is a command-line tool to interact with the Scala language. It lets you compile, run, test, and package your Scala code (and more!)
https://scala-cli.virtuslab.org
Apache License 2.0
527 stars 124 forks source link

`-Djavax.net.ssl.trustStore` certs not properly passed when downloading deps in BSP #2963

Open jiramares opened 2 weeks ago

jiramares commented 2 weeks ago

Versions Scala CLI version: 1.3.2 Scala version (default): 3.4.2

IntelliJ IDEA 2024.1.3 (Ultimate Edition) Build #IU-241.17890.1, built on June 4, 2024

Description We used company artifactory to resolve/cache artefacts. Therefor I have to set up environment varianble COURSIER_REPOSITORIES to handle it right. Because we are using self signed certificate on server there is problem with resolving artefacts directly using coursier (upgrades scala-cli, scala or java).

I realized that there is config repositories.default that seems to me to fulfil the same thing but only for scala-cli (not system wide). So I started using it. It works well but breaks the integration with IDE.

After starting using repositories.default instead of COURSIER_REPOSITORIES I can't build in IDEA via BSP. In BSP window there has been no tree under the root of project. The problem is with our artifactory server with self signed certificate because using maven central causes everything works fine.

To Reproduce

  1. scala-cli config repositories.default ivy2local https://our-server/with-self-signed-certificate/repo --power
  2. create directory test
  3. in it create file test.sc with content
    
    //> using scala 3.3

println(sum(2, 3))

def sum(a: Int, b: Int) = a + b


4. in the directory `test` run `scala-cli setup-ide test.sc`
5. open IDEA and File / New / Project from Existing Sources - choose folder test and in following dialog choose model BSP

In BSP window there is no tree and println is marked as uknown symbol.

**Expected behaviour**
There should be dependencies in BSP window and println shoul be known.
jiramares commented 2 weeks ago

And now it is not working with artifact proxy with self-signed certificate via https at all. Neither COURSIER_REPOSITORIES environment variable nor repositories.default scala-cli config. I don't know what to do :-(

Gedochao commented 2 weeks ago

I don't know what to do :-(

Hey, I'll try to tackle this soon (likely won't be this week, however). I'd suspect something is wrong with the config sub-command's repositories.* settings, but COURSIER_REPOSITORIES is known and proven to be reliable... 😕
Can't tell you much until I take a closer look.

jiramares commented 2 weeks ago

Hi @Gedochao, I have been a little more investigating and it looks that problem is with https protocol with artifact proxy (not with its setting via COURSIER_REPOSITORIES or repositories.default). I have to restart computer between tests because I can't reset the environment well (killing all java processes have not been enough). scala-cli works great with http and https too (for https I have to use -Djavax.net.ssl.trustStore). But the IDEA is not able to resolve using https at all (not possible to validate server certificate - I have imported it into IDEA's store - it looks like idea is using coursier under the hood too but I can't specify -Djavax.net.ssl.trustStore). To resolve dependencies is job of IDEA or bloop?

jiramares commented 1 week ago

From my point of view it directs to coursier that is run under the hood to resolve artifacts. But when run via scala-cli I can add -Djavax.net.ssl.trustStore but when run in IDEA it is not possible and therfore not working.

Gedochao commented 1 week ago

To resolve dependencies is job of IDEA or bloop?

Definitely Scala CLI/Bloop, depending on which dependencies. Both use Coursier under the hood. However, I can't be sure what IDEA does in the background.

From my point of view it directs to coursier that is run under the hood to resolve artifacts. But when run via scala-cli I can add -Djavax.net.ssl.trustStore but when run in IDEA it is not possible and therfore not working.

@jiramares Can you raise a corresponding ticket on the JetBrains issue tracker? (https://youtrack.jetbrains.com/issues) So that we can establish this works as expected on the IDEA side of things.

jiramares commented 1 week ago

I have already raised one an the result is - it is not IDEA problem (https://youtrack.jetbrains.com/issue/SCL-22728/scala-cli-project-not-working)](https://youtrack.jetbrains.com/issue/SCL-22728/scala-cli-project-not-working)

jiramares commented 1 week ago

But .. who starts the bloop server, from mine point of view it is started by scala-cli and IDEA in calling it via BSP? Therefore if the bloop is responsible for resolving why it is run okay through scala-cli and not running via IDEA? I am confused....

Gedochao commented 1 week ago

One extra question - have you tried running it in Metals? I just want to confirm this isn't IDEA specific, since from what I understand it works when run outside of IDEA. I'd theorise maybe some coursier instance ran inside of IntelliJ is using a JVM without the cert flag.

I hope we won't end up bouncing this back and forth...

jiramares commented 1 week ago

No, I haven't ... be honest don't know how to make it. Just install Metals into VSC and run scala-cli setup-id . and open it in VSC? Or it is necessary to do something more?

Gedochao commented 1 week ago

No, I haven't ... be honest don't know how to make it. Just install Metals into VSC and run scala-cli setup-id . and open it in VSC? Or it is necessary to do something more?

Exactly this. plus start the Metals server and check if everything works.

jiramares commented 1 week ago

I try to make it but I am sorry not to test it now ... hope to make it this week. j

jiramares commented 5 days ago

I have installed metals into VSC and have same result as in IDEA:

024.06.28 15:33:28 INFO  BSP server: [error]   download error: Caught javax.net.ssl.SSLHandshakeException (PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target) while downloading https://ids/artifactory/repo/org/typelevel/cats-core_3/2.12.0/cats-core_3-2.12.0.pom

Looks like that the -Djavax.net.ssl.trustStore have to be somehow passed to bloop too...

tgodzik commented 2 days ago

@jiramares do you use cats within your project?

jiramares commented 2 days ago

Yes, because of dylemma.xml-spac

tgodzik commented 1 day ago

My guess is that we are not adding the certs when downloading sources somehow, but it seems unexpected :thinking: