Skip to content

Migrate to BCR rules_scala and Bazel BSP#453

Closed
MrAMS wants to merge 3 commits intoThe-OpenROAD-Project:mainfrom
MrAMS:migrate-to-BCR-rules-scala
Closed

Migrate to BCR rules_scala and Bazel BSP#453
MrAMS wants to merge 3 commits intoThe-OpenROAD-Project:mainfrom
MrAMS:migrate-to-BCR-rules-scala

Conversation

@MrAMS
Copy link
Contributor

@MrAMS MrAMS commented Dec 17, 2025

Summary

This PR migrates the Scala toolchain from a custom implementation (~1500 lines) to the community-maintained BCR rules_scala 7.1.5, and replaces the deprecated Bloop IDE integration with modern Bazel BSP.

Changes

Scala Toolchain Migration:

  • Replace custom Scala toolchain with BCR rules_scala 7.1.5
  • Simplify Chisel wrappers to thin API layer over BCR rules
  • Add runtime workarounds for BCR Verilator compatibility
  • Preserve all existing Chisel wrapper APIs (chisel_library, chisel_binary, chisel_test)

IDE Support Modernization:

  • Remove Bloop integration (scala_bloop.bzl, tools/deploy.py, blooplib target)
  • Add Bazel BSP with .bazelproject configuration
  • Update README with BSP setup instructions for Metals
  • Remove obsolete CI Bloop check

Compatibility Fixes:

  • Use standard @rules_python for BSP compatibility
  • Fix fir_library to accept multi-file generators
  • Fix chisel_test to use @bazel-orfs labels for external dependencies

Benefits

  • Simpler codebase: 2244 lines removed, 613 lines added (-1631 net)
  • Better maintainability: Community-maintained rules with active development
  • Modern IDE support: Direct Bazel integration via BSP (no config export needed)
  • Easier upgrades: Can update rules_scala independently

Copy link
Collaborator

@oharboe oharboe left a comment

Choose a reason for hiding this comment

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

will test on private repository, but we need bloop before I can take delivery

@MrAMS MrAMS force-pushed the migrate-to-BCR-rules-scala branch from a1820c3 to bd98d24 Compare December 17, 2025 11:39
@MrAMS MrAMS changed the title Migrate to bcr rules scala Migrate to bcr rules scala and fix bloop Dec 17, 2025
@oharboe
Copy link
Collaborator

oharboe commented Dec 17, 2025

Fails:

$ bazelisk test sby/...
INFO: Invocation ID: de7f3ae9-2d0c-4da1-b8ad-0783d9424531
ERROR: /home/oyvind/bazel-orfs/sby/BUILD:21:9: in generator attribute of fir_library rule //sby:counter_fir: '//sby:generate_counter' must produce a single file. Since this rule was created by the macro 'sby_test', the error might have been caused by the macro implementation
ERROR: /home/oyvind/bazel-orfs/sby/BUILD:21:9: Analysis of target '//sby:counter_fir' (config: 4996c2f) failed
ERROR: Analysis of target '//sby:counter_test' failed; build aborted: Analysis failed
INFO: Elapsed time: 3.188s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: No test targets were found, yet testing was requested
FAILED: 
    Fetching repository @@+_repo_rules2+orfs_variable_metadata; starting
    Fetching repository @@+orfs_repositories+docker_orfs; starting
    Fetching module extension @@aspect_rules_js+//npm:extensions.bzl%npm; starting
    Fetching repository @@aspect_bazel_lib++toolchains+yq_linux_amd64; starting


This toolchain is adapted to work with Chisel and support vscode with bloop.
This directory contains Bazel rule wrappers for Chisel hardware design, built on top of [BCR rules_scala](https://github.com/bazelbuild/rules_scala).

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are so few users of bazel-orfs and Chisel, we don't need to have permanent record this migration, it is more than enough to have a pull request comment.

Better to have a longer term README.md that focuses on how it is after this migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

Copy link
Contributor Author

@MrAMS MrAMS Dec 17, 2025

Choose a reason for hiding this comment

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

I am thinking make these chisel rules to a new chisel_rules BCR package in the future.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes: they don't belong in bazel-orfs as such. bazel-orfs has become a staging ground for various and sundry needed for OpenROAD+Chisel work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will try to export these rules to a single rules_chisel in future.

@MrAMS MrAMS force-pushed the migrate-to-BCR-rules-scala branch 3 times, most recently from abc6ed2 to 6019831 Compare December 17, 2025 13:23
@MrAMS MrAMS changed the title Migrate to bcr rules scala and fix bloop Migrate to bcr rules scala and Bazel BSP Dec 17, 2025
@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 17, 2025

I've decided to migrate to the standard Bazel BSP (via bazel-bsp) instead of maintaining our own Bloop configuration rules locally.

While Bloop offers great performance, maintaining custom build rules to generate bloop.json files created significant maintenance overhead and technical debt. The modern Bazel BSP ecosystem has matured enough to provide a stable IDE experience without requiring us to manually manage the bridge between Bazel and the IDE. This aligns us better with the upstream community standards.

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 17, 2025

@oharboe Hi, CI all GREEN 🟢 , you can review now 🥳

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 17, 2025

I've decided to migrate to the standard Bazel BSP (via bazel-bsp) instead of maintaining our own Bloop configuration rules locally.

I have tested in zed-metals, it works smooth 💯
btw, recommand you to replace vscode to zed which is faster.

@MrAMS MrAMS requested a review from oharboe December 17, 2025 13:43
@oharboe
Copy link
Collaborator

oharboe commented Dec 17, 2025

What happens with bazelbsp and running bazel on the command line, are they fighting over the same server?

I.e. does it lock up?

The motivation for using bloop was the bazelbsp was blocking the command line bazel use and vice versa.

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 17, 2025

Yes, they do contend for the same Bazel server lock.

Since Bazel enforces a single-writer lock per output base to ensure consistency, running a build in the IDE (via bazel-bsp) will block CLI commands until it finishes, and vice-versa.

However, this is usually acceptable or even desired because they share the analysis cache and build artifacts. This means if I build a target in the IDE, a subsequent CLI build of the same target is instant (cached). If we separated them, we would double the memory usage and disk space, and lose the shared caching benefits.

@oharboe
Copy link
Collaborator

oharboe commented Dec 17, 2025

I think bloop creates a second copy and avoids the lockup and conflict, but there's a cost.

Will test.

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 17, 2025

We can configure the BSP server to run with its own output base. This would allow the IDE and CLI to run completely in parallel (separate Bazel servers), at the cost of higher RAM usage and no shared build cache. For now, sharing the server seems like the better trade-off for resource efficiency.

@oharboe
Copy link
Collaborator

oharboe commented Dec 17, 2025

Sounds good. We should document the both use cases, I think it is a matter of preferences as much as constraints

@oharboe
Copy link
Collaborator

oharboe commented Dec 17, 2025

run bazelisk mod tidy, re. failure in CI. Testing more locally though.

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

Closer...

I need to make this change

$ git diff
diff --git a/.bazelproject b/.bazelproject
index 6bf48fb..3210dc1 100644
--- a/.bazelproject
+++ b/.bazelproject
@@ -1,6 +1,6 @@
 targets:
-    //chisel:all
-    //sby:all
+    //chisel:codegenlib
+    //chisel:applicationlib
 
 allow_manual_targets_sync: false

But still not quite there...

image

UUhhh...

Suddenly it worked...

image

@MrAMS MrAMS force-pushed the migrate-to-BCR-rules-scala branch from 079d867 to 590a331 Compare December 18, 2025 14:11
@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

I am trying to rebase main, could you tell which one is right?

load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
# or
load("@bazel_orfs_rules_python//python:defs.bzl", "py_binary")
load("@bazel_orfs_rules_python//python:pip.bzl", "compile_pip_requirements")

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Oh I see pull/457, rules_python is right?

Replace custom Scala toolchain implementation with BCR rules_scala 7.1.5
while preserving Chisel wrapper API (chisel_library, chisel_binary, chisel_test).

Changes:
- Add rules_scala 7.1.5 dependency from Bazel Central Registry
- Configure Scala 2.13.17 toolchain via BCR extensions
- Rewrite Chisel wrappers to call BCR rules instead of custom implementation
- Remove ~1500 lines of custom Scala toolchain code
- Implement custom test wrapper with launcher script for environment variable expansion
- Temporarily disable scala_bloop (to be migrated later)

Benefits:
- Simpler codebase (1900 lines removed, 400 lines added)
- Standard community-maintained rules
- Easier upgrades and better long-term maintainability
@MrAMS MrAMS force-pushed the migrate-to-BCR-rules-scala branch from 590a331 to 8b4b904 Compare December 18, 2025 14:26
@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

Oh I see pull/457, rules_python is right?

Yes.

Replace deprecated Bloop integration with modern Bazel BSP for Metals IDE support.

Changes:
- Support SemanticDB
- Remove scala_bloop.bzl and tools/deploy.py (deprecated Bloop implementation)
- Remove blooplib target (used only for Bloop, caused CI failures)
- Remove CI Bloop check (replaced by Smoketests job)
- Add .bazelproject to configure BSP target selection
- Fix MODULE.bazel: use standard @rules_python for BSP compatibility
- Fix generate.bzl: remove allow_single_file from fir_library generator
- Fix chisel_test: use @bazel-orfs label for external dependency compatibility
- Update README with complete BSP setup instructions
- Add .bsp/ and .bazelbsp/ to .gitignore

Bazel BSP provides direct integration with Bazel, better accuracy, and active
community support.

Refs: http://scalameta.org/metals/docs/build-tools/bazel/
@MrAMS MrAMS force-pushed the migrate-to-BCR-rules-scala branch from 8b4b904 to 02c1976 Compare December 18, 2025 14:32
@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

CI should be passing, network issue.

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

I'm testing with a large downstream project. It is not perfect and sometimes it doesn't work for no apparent reason, but it does work.

I'm running some more tests and then I will take delivery and start learning and fixing and refining.

We're in considerable pain with bloop with no hope of improvement. Switching to bsp will mean some immediate relief, some new pain but a good diagnosis 😌

Thank you so much for all the help!

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Great to hear! Our downstream projects will both benefit from this PR merge. Adopting bazel-orfs has streamlined our lab's workflow, improving both collaboration and reproducibility. Thank you again for your guide and excellent open-source work!

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

Great to hear! Our downstream projects will both benefit from this PR merge. Adopting bazel-orfs has streamlined our lab's workflow, improving both collaboration and reproducibility. Thank you again for your excellent open-source work!

Can I ask where it is being used? I'm sure @tspyrou and @maliberty would be interested to hear!

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

😿

A few more nits...

What's going on here?

make: /home/oyvind/.cache/bazel/_bazel_oyvind/7bfbc4b14c23869c718b8542e5d1fe83/sandbox/processwrapper-sandbox/13979/execroot/_main/bazel-out/k8-opt/bin/xxx/Yyy_test_scala_inner.runfiles/_main/verilator: No such file or directory

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

@MrAMS Could you test on a downstream repository?

I think tests inside bazel-orfs are working but when chisel_test() is used from outside bazel-orfs, there are problems.

More mysterious verilator problems when running on downstream repostories:

python3: can't open file '/home/oyvind/.cache/bazel/_bazel_oyvind/7bfbc4b14c23869c718b8542e5d1fe83/execroot/_main/bazel-out/k8-opt/bin/hardware/aptos/XXX_test_launcher.sh.runfiles/_main/../verilator+/bin/verilator_includer': [Errno 2] No such file or directory

m

@MrAMS MrAMS changed the title Migrate to bcr rules scala and Bazel BSP Migrate to BCR rules_scala and Bazel BSP Dec 18, 2025
@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

I do not use chisel_test in our downstream repository. Problems about chisel_test is awful, you can refer to this issue bazel-central-registry/issues/6860.

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Can I ask where it is being used? I'm sure @tspyrou and @maliberty would be interested to hear!

We are a research group within the State Key Laboratory of Processors, Institute of Computing Technology, Chinese Academy of Sciences.

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

I do not use chisel_test in our downstream repository. Problems about chisel_test is awful, you can refer to this issue bazel-central-registry/issues/6860.

Ouch... Is there any hope of getting this to work?

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

I'll look into chisel_test later. Could you please provide a minimal reproducible example (MRE) to help me debug?

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Oh, maybe we can use my own old demo version of chisel_test which is ported from Google's CoralNPU. You can check the code here if that is ok for you?

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

Before we can merge this, we need to know that we can upgrade OpenROAD....

Could you create a pull equest alongside this one that works?

The-OpenROAD-Project/OpenROAD#9081

You can use the hash from a pull request in this repository when testing the pull request above with this pull request, no need to have it merged first.

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

Oh, maybe we can use my own old demo version of chisel_test which is ported from Google's CoralNPU. You can check the code here if that is ok for you?

Oohh... don't know that code, I was hoping not to introduce more stuff at this point and just have one more bugfix on what we have in this PR...

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Hi @oharboe check The-OpenROAD-Project/OpenROAD/pull/9081. This workflow requires approval from a maintainer.

@MrAMS
Copy link
Contributor Author

MrAMS commented Dec 18, 2025

Before we can merge this, we need to know that we can upgrade OpenROAD....

Allright, CI is passing. I’m heading to bed 💤. Handle the remain issues another day.

@oharboe
Copy link
Collaborator

oharboe commented Dec 18, 2025

merged here #463

@oharboe oharboe closed this Dec 18, 2025
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