Skip to content

[2.x] feat: Use sbt script in BSP config instead of hardcoded Java path#8920

Merged
eed3si9n merged 1 commit intosbt:developfrom
bittoby:fix/7245-bsp-use-java-from-path
Mar 20, 2026
Merged

[2.x] feat: Use sbt script in BSP config instead of hardcoded Java path#8920
eed3si9n merged 1 commit intosbt:developfrom
bittoby:fix/7245-bsp-use-java-from-path

Conversation

@bittoby
Copy link
Copy Markdown
Contributor

@bittoby bittoby commented Mar 18, 2026

Problem

sbt bspConfig writes the absolute path of the current Java binary into .bsp/sbt.json. When the user switches Java versions (via sdkman, cs java, etc.) or removes that JDK, the IDE fails to start the sbt BSP server because the hardcoded path is stale or gone.

Solution

When an sbt launcher script is available (via sbt.script system property or PATH lookup), generate:

"argv": ["/path/to/sbt", "bsp"]

instead of the old direct java invocation. The sbt script resolves Java from PATH/JAVA_HOME at runtime, so switching Java versions no longer breaks BSP.

Falls back to the previous direct java invocation when no sbt script is found (e.g. when sbt is launched from an IDE without the script on PATH).

Changes

  • BuildServerConnection.scala - prefer sbt script as argv[0], fall back to direct java
  • Main.scala, NetworkClient.scala - recognize "bsp" (without dash) as a BSP command
  • sbt (bash) - handle bsp command: bypass native client, pass -bsp to Boot
  • sbt.bat - handle bsp command: bypass native client
  • sbtw/Main.scala - handle bsp in residual args: bypass native client

Test plan

  • Unit tests added in BuildServerConnectionSpec (fallback argv structure, sbtScriptInPath)
  • Integration test updated in BspConfigTest (accepts both sbt-script and java-direct formats)
  • protocolProj/test - 10/10 passed
  • commandProj/test - 42/42 passed
  • scalafmtCheck - all passed
  • Manual test: publishLocalBin, created test project, ran bspConfig, confirmed .bsp/sbt.json uses sbt script path
  • Manual test: sbt bsp starts and exits cleanly (exit code 0)
  • Manual test: fallback path (no sbt script) still produces direct java argv

Closes: #7245

Generated-by: Claude Opus 4.6

@bittoby
Copy link
Copy Markdown
Contributor Author

bittoby commented Mar 18, 2026

@eed3si9n Please review this PR. I'd appreciate any feedbacks.

@eed3si9n eed3si9n changed the title [2.x] fix: Use sbt script in BSP config instead of hardcoded Java path (#7245) [2.x] feat: Use sbt script in BSP config instead of hardcoded Java path Mar 20, 2026
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@eed3si9n eed3si9n merged commit be305eb into sbt:develop Mar 20, 2026
15 checks passed
@bittoby bittoby deleted the fix/7245-bsp-use-java-from-path branch March 20, 2026 00:58
eed3si9n pushed a commit to eed3si9n/sbt that referenced this pull request Mar 23, 2026
…#8920)

**Problem**

sbt bspConfig writes the absolute path of the current Java binary into .bsp/sbt.json. When the user switches Java versions (via sdkman, cs java, etc.) or removes that JDK, the IDE fails to start the sbt BSP server because the hardcoded path is stale or gone.

**Solution**

When an sbt launcher script is available (via `sbt.script` system property or PATH lookup), generate:

"argv": ["/path/to/sbt", "bsp"]
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 this pull request may close these issues.

Should use Java from path when starting sbt via BSP config

2 participants