Skip to content

bugfix: skip Scala CLI auto-start outside workspace folders - #8737

Open
russwyte wants to merge 9 commits into
scalameta:mainfrom
russwyte:bugfix/no-scala-cli-outside-workspace
Open

bugfix: skip Scala CLI auto-start outside workspace folders#8737
russwyte wants to merge 9 commits into
scalameta:mainfrom
russwyte:bugfix/no-scala-cli-outside-workspace

Conversation

@russwyte

@russwyte russwyte commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes #8736: fallback no longer auto-starts Scala CLI (or runs presentation-compiler load) when textDocument/didOpen targets a path outside the LSP workspace folder(s).
  • Short-circuits out-of-workspace didOpen in FallbackMetalsLspService, clears stale diagnostics, and gates auto-start via ScalaCliAutoStart.
  • WorkspaceLspService passes both Scala and non-Scala folder roots into the check. Empty folder list keeps legacy auto-start (standalone / no-folder sessions). Manual metals.scala-cli-start is unchanged.

Motivation

Some editors (notably Cursor) can send didOpen for Scala files outside the configured workspace. Metals treated those as orphans and started a Scala CLI BSP per file, flooding Problems and spawning many processes even with no open tabs.

Test plan

  • unit/testOnly tests.ScalaCliAutoStartSuite (membership policy: in/out, siblings, empty folders, non-Scala/Java, scripts, worksheets/sbt, multi-folder, prefix lookalikes, lexical symlink cases)
  • unit/testOnly tests.OutOfWorkspaceScalaCliLspSuite (LSP: outside/sibling skip; empty folders still auto-start; outside .sc / non-Scala / repeated outsiders)
  • Reviewers: confirm manual Scala CLI start still works for intentional out-of-workspace files (ScalaCliSuite / StartScalaCliServer)

Editor agents can open Scala files outside the LSP workspace, which
previously made FallbackMetalsLspService spawn Scala CLI BSP for each
orphan. Gate auto-start on workspace folder membership; manual start is
unchanged. Fixes scalameta#8736.
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Scala CLI fallback auto-start now checks Scala file type and workspace-folder containment. Workspace folders are supplied dynamically to the fallback service, which skips out-of-workspace startup while retaining in-workspace orphan behavior. Unit and LSP tests cover these decisions.

Changes

Workspace-aware Scala CLI auto-start

Layer / File(s) Summary
Scala CLI auto-start policy and unit coverage
metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCliAutoStart.scala, tests/unit/src/test/scala/tests/ScalaCliAutoStartSuite.scala
Adds workspace-aware auto-start decisions requiring Scala file types and workspace containment, while preserving empty-workspace behavior and covering nested folders, siblings, scripts, non-Scala files, and symlinks.
Fallback service integration
metals/src/main/scala/scala/meta/internal/metals/FallbackMetalsLspService.scala, metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala
Aggregates workspace folders, passes them to the fallback service, clears diagnostics for out-of-workspace documents, and gates Scala CLI startup for eligible paths.
LSP regression coverage and CI registration
tests/unit/src/test/scala/tests/OutOfWorkspaceScalaCliLspSuite.scala, project/TestGroups.scala
Tests out-of-workspace, sibling, script, multiple-file, non-Scala, and empty-workspace didOpen behavior, and adds the suites to CI test sharding.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant LSPClient
  participant WorkspaceLspService
  participant FallbackMetalsLspService
  participant ScalaCliAutoStart
  LSPClient->>WorkspaceLspService: didOpen document
  WorkspaceLspService->>FallbackMetalsLspService: handle document path
  FallbackMetalsLspService->>ScalaCliAutoStart: check workspace and file type
  alt outside workspace
    ScalaCliAutoStart-->>FallbackMetalsLspService: skip startup
    FallbackMetalsLspService-->>LSPClient: clear diagnostics
  else eligible path
    ScalaCliAutoStart-->>FallbackMetalsLspService: allow startup
    FallbackMetalsLspService->>FallbackMetalsLspService: start/import Scala CLI and run load()
  end
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes implement the requested workspace-folder gating for out-of-workspace didOpen paths and preserve in-workspace and empty-workspace behavior.
Out of Scope Changes check ✅ Passed The added helper, tests, and test-group wiring all support the workspace auto-start fix and do not introduce unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: preventing Scala CLI auto-start outside workspace folders.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@tests/unit/src/test/scala/tests/OutOfWorkspaceScalaCliLspSuite.scala`:
- Around line 43-52: The fixed 500 ms delays do not synchronize assertions with
fallback import completion. In
tests/unit/src/test/scala/tests/OutOfWorkspaceScalaCliLspSuite.scala#L43-L52 and
`#L81-L87`, replace each Thread.sleep-based wait with awaiting the fallback-import
task or the existing test-harness idle hook before performing the Scala CLI
server/path assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 3da690e7-235c-4cb8-8c22-d6ddfe3afcf7

📥 Commits

Reviewing files that changed from the base of the PR and between 0116337 and 547856b.

📒 Files selected for processing (5)
  • metals/src/main/scala/scala/meta/internal/metals/FallbackMetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/WorkspaceLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCliAutoStart.scala
  • tests/unit/src/test/scala/tests/OutOfWorkspaceScalaCliLspSuite.scala
  • tests/unit/src/test/scala/tests/ScalaCliAutoStartSuite.scala

Comment thread tests/unit/src/test/scala/tests/OutOfWorkspaceScalaCliLspSuite.scala Outdated
russwyte added 2 commits July 26, 2026 09:10
Skipping Scala CLI was not enough: presentation-compiler load still
published Problems for Cursor-restored sibling files. Short-circuit
didOpen, clear diagnostics, and skip import/load for paths outside
workspace folders.
@russwyte

Copy link
Copy Markdown
Contributor Author

Opened a parallel port to main-v2 for the 2.0 milestones: https://github.com/scalameta/metals/pull/$(gh pr list --repo scalameta/metals --head russwyte:bugfix/no-scala-cli-outside-workspace-v2 --json number --jq '.[0].number')

@russwyte

Copy link
Copy Markdown
Contributor Author

Parallel port to main-v2 for the 2.0 milestones: #8738

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.

Ignore / don't auto-start Scala CLI for URIs outside workspace folders

1 participant