Skip to content

[2.x] fix: Reject java.io.File as cached task output type#8766

Merged
eed3si9n merged 5 commits intosbt:developfrom
speedcoder430:fix/reject-file-cache-output
Feb 21, 2026
Merged

[2.x] fix: Reject java.io.File as cached task output type#8766
eed3si9n merged 5 commits intosbt:developfrom
speedcoder430:fix/reject-file-cache-output

Conversation

@speedcoder430
Copy link
Copy Markdown
Contributor

@speedcoder430 speedcoder430 commented Feb 19, 2026

Problem

Fixes #8762

When a task returns java.io.File (e.g. assembly / assemblyOutputPath), the result gets cached by ActionCache. Since File serializes as an absolute path string, the cached value becomes stale when scalaVersion changes via ++ 2.13.18! — the path still contains the old Scala version directory.

Reproduction (from #8762)

// build.sbt
scalaVersion := "2.12.21"
val assemblyOutputPath = taskKey[File]("")
assembly / target := crossTarget.value
assembly / assemblyOutputPath := (assembly / target).value / (assembly / assemblyJarName).value
sbt> print assembly / assemblyOutputPath
.../scala-2.12.21/.../foo.jar
sbt> ++ 2.13.18!
sbt> print assembly / assemblyOutputPath
.../scala-2.12.21/.../foo.jar   // BUG: should be scala-2.13.18

Solution

In Cont.scala's callActionCache method, detect when the task's return type A contains java.io.File at compile time using containsFileType[A]. When it does, skip wrapping the task body with ActionCache.cache(...), while still processing any Def.declareOutput(...) calls via letOutput. This way:

  • taskKey[File] tasks are never cached (since File isn't content-addressable)
  • Def.declareOutput(...) continues to work correctly for File tasks
  • No user-facing changes required — existing builds just work

Tests

  • cache/skip-file-cache scripted test: Defines a taskKey[File] that depends on scalaVersion, switches versions, and asserts the returned path updates.
  • Verified actions/task-map (uses Def.declareOutput with taskKey[File]) continues to pass.
  • Ran full CI job 2 suite locally: actions/* cache/* tests/* compiler-project/* etc. — all pass.

@speedcoder430 speedcoder430 force-pushed the fix/reject-file-cache-output branch 2 times, most recently from 378ea96 to f3d9792 Compare February 20, 2026 17:11
@eed3si9n
Copy link
Copy Markdown
Member

Hi, thanks for the contribution! Please check out our Contributor's Guildeline and:

  1. Make sure you can reproduce the problem and the fix manually
  2. Include tests for the change
  3. Follow our AI assisted contributions guideline (please disclose AI usages in the PR description, if any)
  4. Multiple contributors might work on this issue in parallel
  5. Generally we recommend getting CI to pass on a forked repo before sending a pull request (pull requests that do not pass tests will be closed after a few days)
  6. Sign Scala CLA https://contribute.akka.io/contribute/cla/scala

@speedcoder430
Copy link
Copy Markdown
Contributor Author

Hi, thanks for the contribution! Please check out our Contributor's Guildeline and:

  1. Make sure you can reproduce the problem and the fix manually
  2. Include tests for the change
  3. Follow our AI assisted contributions guideline (please disclose AI usages in the PR description, if any)
  4. Multiple contributors might work on this issue in parallel
  5. Generally we recommend getting CI to pass on a forked repo before sending a pull request (pull requests that do not pass tests will be closed after a few days)
  6. Sign Scala CLA https://contribute.akka.io/contribute/cla/scala

Thanks for your detailed feedback.

@speedcoder430 speedcoder430 changed the title Fix/reject file cache output [2.x] fix: Auto-skip caching for tasks with java.io.File output type Feb 20, 2026
@speedcoder430 speedcoder430 force-pushed the fix/reject-file-cache-output branch from f3d9792 to e7161a0 Compare February 20, 2026 18:29
Change the behavior when a cached task's output type contains
java.io.File: instead of silently skipping the cache, throw a
compile-time error with a message recommending xsbti.VirtualFileRef,
xsbti.HashedVirtualFileRef, or xsbti.VirtualFile, and linking to the
caching documentation.

Internal sbt tasks that return File types are wrapped with
Def.uncached to opt out of caching.

Fixes sbt#8762

Generated-by: GitHub Copilot (Claude)
@speedcoder430 speedcoder430 force-pushed the fix/reject-file-cache-output branch from e7161a0 to 890bb27 Compare February 20, 2026 21:56
((ThisBuild / baseDirectory).value / "target" / "sona-bundle" / "bundle.zip").toPath()
)
.toFile(),
sonaBundle := Def.uncached(
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.

As a general strategy, I think we should put @trainsient on Keys.scala instead of putting Def.uncached(...) in Defaults.scala since this would force the build users to also put Def.uncached(...) when they rewire the task.

Use @transient on File-returning keys in Keys.scala instead of
wrapping tasks with Def.uncached in Defaults.scala. This ensures
build users who rewire these tasks also get caching skipped
automatically.

- Add @transient to 19 File-returning taskKey definitions in Keys.scala
- Revert Def.uncached wrappers from Defaults.scala
- Error at compile time when File is used as cached task output type
- Update error message to recommend @transient and link to docs
- Update scripted tests to use @transient approach

Fixes sbt#8762

Generated-by: GitHub Copilot (Claude Opus 4.6)
@eed3si9n eed3si9n changed the title [2.x] fix: Auto-skip caching for tasks with java.io.File output type [2.x] fix: Reject java.io.File as cached task output type Feb 21, 2026
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

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

Thanks!

@eed3si9n eed3si9n merged commit a7d5f45 into sbt:develop Feb 21, 2026
15 checks passed
@eed3si9n eed3si9n mentioned this pull request Mar 17, 2026
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.x] java.io.File yields incorrect cache

2 participants