Skip to content

improvement: Save semanticdb to disk under setting - #8762

Merged
tgodzik merged 1 commit into
scalameta:main-v2from
tgodzik:add-caching-mechanismt
Aug 10, 2026
Merged

improvement: Save semanticdb to disk under setting#8762
tgodzik merged 1 commit into
scalameta:main-v2from
tgodzik:add-caching-mechanismt

Conversation

@tgodzik

@tgodzik tgodzik commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Also allow to specify the maximum cache size for the text document cache.

Summary by CodeRabbit

  • New Features

    • Added configurable MBT settings for importing generated sources and enabling SemanticDB caching.
    • Added a persistent, size-limited cache to improve MBT reference lookups across sessions.
    • Added configuration support for customizing the SemanticDB cache size.
  • Bug Fixes

    • Existing configuration formats remain supported, ensuring a smooth transition to the nested MBT settings.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 668a0633-e21f-44d8-bbe6-79b007ae4b9d

📥 Commits

Reviewing files that changed from the base of the PR and between 2c372ca and 9a880fb.

📒 Files selected for processing (7)
  • metals/src/main/scala/scala/meta/internal/metals/Configs.scala
  • metals/src/main/scala/scala/meta/internal/metals/Directories.scala
  • metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtReferenceProvider.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/mbt/MbtReferenceSpec.scala
🚧 Files skipped from review as they are similar to previous changes (7)
  • metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/Configs.scala
  • tests/unit/src/test/scala/tests/mbt/MbtReferenceSpec.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtReferenceProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/Directories.scala

📝 Walkthrough

Walkthrough

The change adds nested MBT configuration, legacy parsing support, and configurable SemanticDB caching. MbtReferenceProvider now uses an in-memory LRU cache with optional persistent storage. MBT tests verify configuration parsing and generated cache files.

Changes

MBT configuration and SemanticDB caching

Layer / File(s) Summary
MBT configuration model and parsing
metals/src/main/scala/scala/meta/internal/metals/Configs.scala, metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala, tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
MbtConfig defines defaults and optional-value handling. UserConfiguration serializes nested mbt settings and parses both nested and legacy flat keys.
Persistent SemanticDB cache
metals/src/main/scala/scala/meta/internal/metals/Directories.scala, metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala, metals/src/main/scala/scala/meta/internal/metals/mbt/MbtReferenceProvider.scala
MbtReferenceProvider receives workspace configuration, maintains a synchronized LRU cache, and loads or writes MD5-validated SemanticDB documents under .metals/semanticdb-cache.
MBT importing and cache validation
metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtImporter.scala, tests/unit/src/test/scala/tests/mbt/MbtReferenceSpec.scala
Bazel generated-source importing reads MbtConfig. Integration tests verify MBT references and persisted .semanticdb files.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MetalsLspService
  participant MbtReferenceProvider
  participant SemanticDBCache
  participant SemanticDB
  MetalsLspService->>MbtReferenceProvider: initialize with workspace and user configuration
  MbtReferenceProvider->>SemanticDBCache: check memory and persistent cache
  SemanticDBCache-->>MbtReferenceProvider: return valid document or miss
  MbtReferenceProvider->>SemanticDB: index on cache miss
  SemanticDB-->>MbtReferenceProvider: return SemanticDB document
  MbtReferenceProvider->>SemanticDBCache: store document in memory and on disk
Loading

Possibly related PRs

Suggested reviewers: zielinsky

🚥 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 clearly describes the main change: saving SemanticDB data to disk under a configuration setting.
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.

@tgodzik
tgodzik force-pushed the add-caching-mechanismt branch 3 times, most recently from d2a3109 to 2c372ca Compare August 4, 2026 13:40
@tgodzik
tgodzik marked this pull request as ready for review August 4, 2026 13:43

@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/mbt/MbtReferenceProvider.scala`:
- Around line 721-733: The loadFromDisk method only validates cached
TextDocument entries using MD5 of source text, but doesn't account for changes
in build metadata (classpath, compiler options, source roots). When the build
context changes without text changes, stale SemanticDB is reused. Update the
loadFromDisk method to additionally validate a build-context fingerprint
alongside the existing md5 check at line 732. Compute the build-context
fingerprint from current MBT metadata and include it in both the document
storage step (around line 777) and the validation condition in loadFromDisk, so
cache entries are invalidated when build context changes.
- Around line 708-711: Update the cache management around removeEldestEntry so
changes to maxCacheSize are enforced immediately: under the cache lock, remove
eldest entries until the map size is at or below the current configured maximum
before the next cache access, including when the maximum decreases. Preserve the
existing one-entry eviction behavior for normal insertions.
🪄 Autofix

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: d5e7cb9d-8ad0-44a5-88b5-5f7ee4437967

📥 Commits

Reviewing files that changed from the base of the PR and between d70c1d7 and 2c372ca.

📒 Files selected for processing (8)
  • metals/src/main/scala/scala/meta/internal/metals/Configs.scala
  • metals/src/main/scala/scala/meta/internal/metals/Directories.scala
  • metals/src/main/scala/scala/meta/internal/metals/MetalsLspService.scala
  • metals/src/main/scala/scala/meta/internal/metals/UserConfiguration.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/MbtReferenceProvider.scala
  • metals/src/main/scala/scala/meta/internal/metals/mbt/importer/BazelMbtImporter.scala
  • tests/unit/src/test/scala/tests/UserConfigurationSuite.scala
  • tests/unit/src/test/scala/tests/mbt/MbtReferenceSpec.scala

@zielinsky zielinsky left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM, two nitpicks from me

Comment thread tests/unit/src/test/scala/tests/mbt/MbtReferenceSpec.scala Outdated
@tgodzik
tgodzik force-pushed the add-caching-mechanismt branch from 2c372ca to f03b15d Compare August 10, 2026 13:00
Also allow to specify the maximum cache size for the text document cache.
@tgodzik
tgodzik force-pushed the add-caching-mechanismt branch from f03b15d to 9a880fb Compare August 10, 2026 13:01
@tgodzik
tgodzik merged commit 25bf9dc into scalameta:main-v2 Aug 10, 2026
18 checks passed
@tgodzik
tgodzik deleted the add-caching-mechanismt branch August 10, 2026 14:54
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.

2 participants