Skip to content

[WIP] Add sloth support#4348

Draft
Gedochao wants to merge 9 commits into
VirtusLab:mainfrom
Gedochao:feature/lazyvalgrade
Draft

[WIP] Add sloth support#4348
Gedochao wants to merge 9 commits into
VirtusLab:mainfrom
Gedochao:feature/lazyvalgrade

Conversation

@Gedochao

@Gedochao Gedochao commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Adds Sloth support behind --sloth / //> using sloth
https://github.com/VirtusLab/sloth

Scala 3, up till 3.7.x series, has been using the (now terminally deprecated) sun.misc.Unsafe API. This in turn results in some ugly warnings on JDK 24+ (and likely errors in the future).

WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by scala.runtime.LazyVals$ 
(file:(...)/maven2/org/scala-lang/scala3-library_3/3.7.4/scala3-library_3-3.7.4.jar)
WARNING: Please consider reporting this to the maintainers of class scala.runtime.LazyVals$
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release

Now, while Scala 3.8+ uses a new implementation, and Scala 3.3 allows for the new implementation on JDK 9+ behind the -Yfuture-lazy-vals compiler flag, the problem is not entirely solved. Even if your app is on the latest Scala, it likely still depends on libraries built with Scala 3.3 (or some other <3.8 version). That in turn brings the problem back. And since it is not widespread to suffix Scala 3 libraries with the minor, as we did in Scala 2 days, it is not entirely easy to spot which libraries will pose a problem.

Sloth is a bytecode patcher, which is capable of patching all past lazy val implementations (including the pre-3.3 one) up to the current one. Kudos to @lbialy for developing it.

Behind the --sloth flag, this PR allows to post-process and patch the bytecode of your app/artifacts to silently get rid of the problem.
The --sloth-agent flag runs Sloth as a Java agent instead, allowing it to i.e. run in the JVM your tests live.
More details on how it works and what it does at https://github.com/VirtusLab/sloth

This feature is brought to the following sub-commands:

  • run
  • compile
  • doc
  • fix
  • package
  • publish
  • repl
  • run (including the REPL artifacts themselves for Scala <3.8)
  • test

Checklist

  • tested the solution locally and it works
  • ran the code formatter (scala-cli fmt .)
  • ran scalafix (./mill -i __.fix)
  • ran reference docs auto-generation (./mill -i 'generate-reference-doc[]'.run)

How much have your relied on LLM-based tools in this contribution?

extensively, Cursor + Claude

How was the solution tested?

./mill -i integration.test.jvm '*sun.misc.Unsafe*'

@Gedochao

Copy link
Copy Markdown
Contributor Author

@lbialy how close are we to publishing a stable version of lazyvalgrade to Maven Central?

@Gedochao
Gedochao force-pushed the feature/lazyvalgrade branch from a8e0c42 to 52da2af Compare June 30, 2026 08:38
@SethTisue

Copy link
Copy Markdown
Contributor

Why is it called "lazyvalgrade", what does "grade" mean here?

@Gedochao

Gedochao commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@SethTisue I believe that's a pun on "lazy val upgrade".
We've already been discussing releasing it under a different name, coming very soon 😅

@Gedochao
Gedochao force-pushed the feature/lazyvalgrade branch from 52da2af to 330d1d0 Compare July 2, 2026 14:03
@Gedochao Gedochao changed the title [WIP] Add lazyvalgrade support [WIP] Add sloth support Jul 2, 2026
@Gedochao

Gedochao commented Jul 2, 2026

Copy link
Copy Markdown
Contributor Author

https://central.sonatype.com/search?q=org.virtuslab+sloth
Sloth artifacts are up!

Comment thread website/docs/reference/cli-options.md
@tgodzik

tgodzik commented Jul 3, 2026

Copy link
Copy Markdown
Member

Do we now have adopt a sloth in a zoo? And have sloth plushies?

@Gedochao

Gedochao commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

We should. 🦥

@Gedochao
Gedochao force-pushed the feature/lazyvalgrade branch 2 times, most recently from bbac081 to 15645e2 Compare July 14, 2026 10:19
@Gedochao
Gedochao force-pushed the feature/lazyvalgrade branch from b243a04 to 714f8a5 Compare July 21, 2026 10:11
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.

4 participants