Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't use different version for native CLI #4571

Closed
EnviousSwan opened this issue Nov 20, 2024 · 15 comments · Fixed by #4573
Closed

Can't use different version for native CLI #4571

EnviousSwan opened this issue Nov 20, 2024 · 15 comments · Fixed by #4573

Comments

@EnviousSwan
Copy link

This issue is observable in the following configuration

  • latest version of CLI and previous version in .scalafmt.conf

Configuration (required)

version = 3.8.3
runner.dialect = scala3
...
rewrite.scala3.convertToNewSyntax = true
rewrite.scala3.removeOptionalBraces = false

Command-line parameters (required)

When I run scalafmt via CLI like this: scalafmt --diff-branch origin/master

Steps

Given a GitHub workflow

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: coursier/setup-action@v1
      with:
        jvm: temurin:21
        apps: scalafmt
    - run: scalafmt --diff-branch origin/master

Problem

When running scalafmt when a PR is raised via

Run scalafmt --diff-branch origin/master
  scalafmt --diff-branch origin/master
  shell: /usr/bin/bash -e {0}
  env:
    JAVA_HOME: /home/runner/.cache/coursier/arc/https/github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.5%252B11/OpenJDK21U-jdk_x64_linux_hotspot_21.0.5_11.tar.gz/jdk-21.0.5+11
    COURSIER_BIN_DIR: /home/runner/cs/bin

I get the following error

Exception in thread "main" java.lang.Exception: Can't use different version for native CLI
	at org.scalafmt.interfaces.Scalafmt$.create(Scalafmt.scala:142)
	at org.scalafmt.interfaces.Scalafmt.create(Scalafmt.scala)
	at org.scalafmt.cli.ScalafmtDynamicRunner$.run(ScalafmtDynamicRunner.scala:21)
	at org.scalafmt.cli.Cli$.runWithRunner(Cli.scala:134)
	at org.scalafmt.cli.Cli$.run(Cli.scala:61)
	at org.scalafmt.cli.Cli$.mainWithOptions(Cli.scala:35)
	at org.scalafmt.cli.Cli$.main(Cli.scala:18)
	at org.scalafmt.cli.Cli.main(Cli.scala)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at coursier.bootstrap.launcher.a.a(Unknown Source)
	at coursier.bootstrap.launcher.Launcher.main(Unknown Source)

Expectation

I expect scalafmt to format the code or just do nothing

Run scalafmt --diff-branch origin/master
  scalafmt --diff-branch origin/master
  shell: /usr/bin/bash -e {0}
  env:
    JAVA_HOME: /home/runner/.cache/coursier/arc/https/github.com/adoptium/temurin21-binaries/releases/download/jdk-21.0.5%252B11/OpenJDK21U-jdk_x64_linux_hotspot_21.0.5_11.tar.gz/jdk-21.0.5+11
    COURSIER_BIN_DIR: /home/runner/cs/bin
Reformatting...

Workaround

I've found that by setting scalafmt version in .scalafmt.conf to the latest 3.4.2-RC2 the issue doesn't happen anymore

Notes

My assumption is that coursier pulls the latest scalafmt-cli as it's configured to latest.release

This only started happening yesterday, probably after 3.8.4-RC2 release, because there were no problems when 3.8.4-RC1 was the latest.

The same issue happens when running scala-steward from GitHub Actions when it tries to reformat changes

 2024-11-20 09:05:57,595 INFO  Found 1 update:
    io.github.embeddedkafka:(embedded-kafka, embedded-kafka_2.13) : 3.8.1 -> 3.9.0
  2024-11-20 09:05:57,599 INFO  Process update io.github.embeddedkafka:(embedded-kafka, embedded-kafka_2.13) : 3.8.1 -> 3.9.0
  2024-11-20 09:05:57,833 INFO  Create branch update/embedded-kafka-3.9.0
  2024-11-20 09:05:58,814 WARN  Reformatting changed files failed in .
  org.scalasteward.core.io.process$ProcessFailedException: '"SBT_OPTS=-Xmx2048m -Xss8m -XX:MaxMetaspaceSize=512m" scalafmt --non-interactive --mode changed' exited with code 1.
  Error: Exception in thread "main" java.lang.Exception: Can't use different version for native CLI
  	at org.scalafmt.interfaces.Scalafmt$.create(Scalafmt.scala:142)
  	at org.scalafmt.interfaces.Scalafmt.create(Scalafmt.scala)
@EnviousSwan
Copy link
Author

EnviousSwan commented Nov 20, 2024

Can confirm this happens on my machine when it's 3.8.3 or 3.8.4-RC1 in .scalafmt.conf

scalafmt --version
✔ scalafmt 3.8.4-RC2      
                                                                                                                                                                                      
scalafmt                                                                                                                                 
✔ Exception in thread "main" java.lang.Exception: Can't use different version for native CLI
        at org.scalafmt.interfaces.Scalafmt$.create(Scalafmt.scala:142)
        at org.scalafmt.interfaces.Scalafmt.create(Scalafmt.scala)
        at org.scalafmt.cli.ScalafmtDynamicRunner$.run(ScalafmtDynamicRunner.scala:21)
        at org.scalafmt.cli.Cli$.runWithRunner(Cli.scala:134)
        at org.scalafmt.cli.Cli$.run(Cli.scala:61)
        at org.scalafmt.cli.Cli$.mainWithOptions(Cli.scala:35)
        at org.scalafmt.cli.Cli$.main(Cli.scala:18)
        at org.scalafmt.cli.Cli.main(Cli.scala)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at coursier.bootstrap.launcher.a.a(Unknown Source)
        at coursier.bootstrap.launcher.Launcher.main(Unknown Source)

@kitbellew
Copy link
Collaborator

@EnviousSwan why are you submitting this issue here, and not to the maintainer of coursier/setup-action@v1?

judging by the error message, that setup-action downloaded a native binary rather than a JVM, and native does not allow the version line in .scalafmt.conf to be different from the actual version of the binary.

@kitbellew kitbellew reopened this Nov 20, 2024
@tgodzik
Copy link
Contributor

tgodzik commented Nov 20, 2024

Looks like it's not actually native image but a bootstrapped jar, so should work.

@kitbellew
Copy link
Collaborator

it appears that @jchyb 's #4325 is not fully ready. let me see what i can do to fix, without a full-blown revert.

@kitbellew
Copy link
Collaborator

@alexarchambault is it possible that coursier downloads a native version, instead of jvm, if one is available, like for 3.8.4-RC2?

judging by the error message ("Can't use different version for native CLI"), it comes from the NativePlatform build, and not the JVMPlatform version.

I just downloaded two versions, both of them produce this "Can't use" error:

  • coursier bootstrap org.scalameta:scalafmt-cli_2.13:3.8.4-RC2 -r sonatype:snapshots --main org.scalafmt.cli.Cli --standalone
  • coursier bootstrap org.scalameta:scalafmt-cli_2.13:3.8.4-RC2 -r sonatype:snapshots --main org.scalafmt.cli.Cli

Yet I added a #4572 test, and both JVM and Native version seem to load the right interface.

@tgodzik @jchyb any other ideas?

@jchyb
Copy link
Contributor

jchyb commented Nov 20, 2024

It's weird, I tried to run the locally released assembly version and that works fine, but launching the RC from coursier indeed reproduces the error. I have a suspicion about the crossVersion := CrossVersion.disabled (perhaps it leads set to ignore the native and jvm directory split?) line in the build.sbt, but not sure how to test removing it

@tgodzik
Copy link
Contributor

tgodzik commented Nov 20, 2024

On the other hand 3.8.4-RC1 works and it also has native artifacts released.

@tgodzik
Copy link
Contributor

tgodzik commented Nov 20, 2024

Could it be an issue that it's under java?

@tgodzik
Copy link
Contributor

tgodzik commented Nov 20, 2024

Based on the published sources it looks like the wrong files were used in RC2. RC2 source jars contain the native scala files, which RC1 contains java files in interfaces.

This should be reproducible locally when publishing interfaces

@tgodzik
Copy link
Contributor

tgodzik commented Nov 20, 2024

Looks like both interfaces and interfacesNative publish the same jars, which is why this is random.

tgodzik added a commit to tgodzik/scalafmt that referenced this issue Nov 20, 2024
Fixes scalameta#4571

crossVersion actually is set differently if you use `crossProject` so if you disable it you get no suffix in all platforms.
kitbellew pushed a commit that referenced this issue Nov 20, 2024
Fixes #4571

crossVersion actually is set differently if you use `crossProject` so if you disable it you get no suffix in all platforms.
@joan38
Copy link
Contributor

joan38 commented Nov 20, 2024

What action can we take to unblock scala-steward-org/scala-steward#3481 ?

@kitbellew
Copy link
Collaborator

What action can we take to unblock scala-steward-org/scala-steward#3481 ?

use an older formatter version, i think.

@joan38
Copy link
Contributor

joan38 commented Nov 20, 2024

Arf I think our test is actually trying to upgrade to the latest. So we are blocked until a release with the fix is out.

@kitbellew
Copy link
Collaborator

Arf I think our test is actually trying to upgrade to the latest. So we are blocked until a release with the fix is out.

in that case, give us please a couple of days.

@joan38
Copy link
Contributor

joan38 commented Nov 21, 2024

I was able to cap the version upgrade. No rush guys.
Thanks for the awesome work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants