Skip to content

Add watchedFiles support to MBT script importers - #8669

Draft
zielinsky wants to merge 7 commits into
scalameta:main-v2from
zielinsky:mbt-watched-files
Draft

Add watchedFiles support to MBT script importers#8669
zielinsky wants to merge 7 commits into
scalameta:main-v2from
zielinsky:mbt-watched-files

Conversation

@zielinsky

@zielinsky zielinsky commented Jul 10, 2026

Copy link
Copy Markdown
Member

Extracted from #8360

Screen.Recording.2026-07-14.at.11.26.35.mov

Summary by CodeRabbit

  • New Features

    • Build-tool imports now track and refresh relevant watched files more precisely.
    • Changes to explicitly watched files can trigger automatic project re-imports.
    • Script-based MBT builds now treat an explicit watched file list as authoritative, with glob-only entries ignored.
  • Bug Fixes

    • Improved watched-file change handling across Maven, Gradle, and Bazel projects to ensure the correct reimport behavior.
  • Tests

    • Added/updated coverage for watched-file caching, matching, and script importer behavior.

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 861b9c90-4cd0-4aba-b236-1f947222b2b3

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

MBT watched-file classification now carries explicit watched paths from imported builds into script importers. Successful imports refresh LSP file watchers, and watched-file changes trigger reimports through a digest-bypassing path.

Changes

MBT watched-file tracking

Layer / File(s) Summary
Watched-file contract and build data
metals/src/main/scala/scala/meta/internal/metals/mbt/..., metals/src/main/scala/scala/meta/internal/builds/MavenBuildTool.scala
Importer predicates are renamed to isWatchedFile; MbtBuild stores and merges watched paths, while MbtGlobMatcher centralizes pattern handling.
Script watched-file cache
metals/src/main/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporter.scala, tests/unit/src/test/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporterSuite.scala
ScriptMbtImporter caches explicit watched paths, always watches its own script, filters glob entries, and adds matching tests.
Import and reimport wiring
metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala, metals/src/main/scala/scala/meta/internal/metals/ProjectMetalsLspService.scala, metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImport.scala
Successful MBT imports refresh LSP watched-file registrations; watched-file changes use digest-bypassing reimports.

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

Sequence Diagram(s)

sequenceDiagram
  participant ProjectMetalsLspService
  participant ScriptMbtImporter
  participant ConnectionProvider
  participant MbtImport
  participant LanguageClient
  ProjectMetalsLspService->>ScriptMbtImporter: isWatchedFile(changedPath)
  ProjectMetalsLspService->>ConnectionProvider: runMbtReimportIgnoringDigest(importers)
  ConnectionProvider->>MbtImport: runIgnoringDigest(importers)
  MbtImport-->>ConnectionProvider: return import status
  ConnectionProvider->>ScriptMbtImporter: updateWatchedFiles(importers)
  ConnectionProvider->>LanguageClient: refresh didChangeWatchedFiles registrations
Loading

Possibly related PRs

Suggested reviewers: tgodzik, tgodzik

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding watchedFiles support to MBT script importers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

@zielinsky
zielinsky force-pushed the mbt-watched-files branch from b73e03f to 0944b60 Compare July 10, 2026 13:48

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala (1)

1072-1097: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

switchBspServer() still skips the MBT watched-file refresh. This path runs mbtImport.runUnconditionally(...) directly, but updateMbtWatchedFiles(...) is only called in the other MBT import flows. Script importers switched here will keep stale watched-file caches until another full reimport runs.

🤖 Prompt for 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.

In `@metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala`
around lines 1072 - 1097, Update the MBT import path in switchBspServer(),
specifically the ConnectRequest branch that calls
mbtImport.runUnconditionally(...), to invoke updateMbtWatchedFiles(...) after a
successful import and before connect(request, progress), matching the other MBT
import flows. Preserve the existing failure handling and ensure watched files
are refreshed for script importers during server switching.
🤖 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
`@metals/src/main/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporter.scala`:
- Around line 78-89: Normalize watched-file patterns when caching them in
updateWatchedFiles, matching the normalization used by isWatchedFile: convert
backslashes to forward slashes and remove any leading "./" segments before
storing them in ScriptMbtImporter.watchedFilesCache. Keep the lookup comparison
consistent so patterns from mbtBuild.getWatchedFiles match normalized relative
paths, including the corresponding logic around the additional referenced
section.

---

Outside diff comments:
In `@metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala`:
- Around line 1072-1097: Update the MBT import path in switchBspServer(),
specifically the ConnectRequest branch that calls
mbtImport.runUnconditionally(...), to invoke updateMbtWatchedFiles(...) after a
successful import and before connect(request, progress), matching the other MBT
import flows. Preserve the existing failure handling and ensure watched files
are refreshed for script importers during server switching.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: cbc9f753-6d2c-400c-bda8-3751077971ca

📥 Commits

Reviewing files that changed from the base of the PR and between 748710d and b73e03f.

📒 Files selected for processing (11)
  • metals/src/main/scala/scala/meta/internal/builds/MavenBuildTool.scala
  • metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/ProjectMetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtBuild.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtBuildSupport.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/GradleMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImportProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporter.scala
  • tests/unit/src/main/scala/tests/MbtJsonBuilder.scala
  • tests/unit/src/test/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporterSuite.scala

@zielinsky
zielinsky force-pushed the mbt-watched-files branch 2 times, most recently from 3db6bb7 to 5fcf8e0 Compare July 10, 2026 14:22
@zielinsky
zielinsky force-pushed the mbt-watched-files branch from 5fcf8e0 to 98fd9a4 Compare July 13, 2026 13:38
@zielinsky

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 14, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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: 2

🤖 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 `@metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala`:
- Around line 287-298: Update refreshMbtWatchedFiles to normalize watcher glob
paths for LSP compatibility: convert folder.toString() backslashes to forward
slashes and remove any leading "./" from each raw pattern before concatenating
it with the root. Keep watcher registration limited to non-empty patterns and
continue passing the normalized paths to FileSystemWatcher.

In
`@metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImport.scala`:
- Around line 98-125: Update runIgnoringDigest to append the same
completion-based digest status update used by runIfApproved: mark the current
digest Installed when the import succeeds and Rejected when it fails or is
declined. Preserve the existing user prompt and result handling, while ensuring
the digest state is restored after every completion.
🪄 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: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3bedc82c-14b0-4a61-8114-1fa54c73c23a

📥 Commits

Reviewing files that changed from the base of the PR and between 5fcf8e0 and 0e16572.

📒 Files selected for processing (11)
  • metals/src/main/scala/scala/meta/internal/builds/MavenBuildTool.scala
  • metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/ProjectMetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtBuild.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtGlobMatcher.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/GradleMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImport.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImportProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporter.scala
  • tests/unit/src/test/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporterSuite.scala
🚧 Files skipped from review as they are similar to previous changes (9)
  • metals/src/main/scala/scala/meta/internal/metals/ProjectMetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/builds/MavenBuildTool.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtGlobMatcher.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/MbtImportProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/GradleMbtImporter.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtBuild.scala
  • tests/unit/src/test/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporterSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/ScriptMbtImporter.scala

Comment thread metals/src/main/scala/scala/meta/internal/metals/ConnectionProvider.scala Outdated
@zielinsky
zielinsky force-pushed the mbt-watched-files branch 3 times, most recently from ea633d2 to 816a8bc Compare August 6, 2026 20:21
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.

1 participant