Skip to content

Fix generated workflow varying with the Scala version under sbt 2 - #233

Merged
eed3si9n merged 1 commit into
sbt:mainfrom
nafg:fix-sbt2-cross-scala-target-aggregation
Sep 4, 2026
Merged

eed3si9n merged 1 commit into
sbt:mainfrom
nafg:fix-sbt2-cross-scala-target-aggregation

Conversation

@nafg

@nafg nafg commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

(ai generated)

Problem

Under sbt 2, a project's target is target/out/<platform>/scala-<scalaVersion>/<module> rather than sbt 1's version-independent <project>/target. GenerativePlugin feeds target.value straight into internalTargetAggregation, which is rendered into the Compress target directories step. In a build using plain crossScalaVersions, only the currently loaded Scala version's directory exists, so the generated ci.yml depends on which ++ sbt happened to be at when githubWorkflowGenerate ran:

generated at 2.13.10 -> run: tar cf targets.tar target/out/jvm/scala-2.13.10/root target/out/jvm/scala-2.13.10/sub project/target
generated at 2.12.17 -> run: tar cf targets.tar target/out/jvm/scala-2.12.17/root target/out/jvm/scala-2.12.17/sub project/target

The generated workflow runs sbt ++<scala> githubWorkflowCheck once per matrix row, so whichever version the committed file was generated at, every other row fails with "ci.yml does not contain contents that would have been generated by sbt-github-actions". There is no committed file that satisfies a multi-Scala matrix.

projectMatrix builds are not affected — every row is materialised as a real project, so all of the paths are enumerated regardless of ++. Only plain crossScalaVersions builds hit this. sbt 1 is not affected at all.

Fix

Aggregate the nearest ancestor of target that is not scoped to a Scala version — target/out/<platform> — instead of the version-scoped directory itself:

run: tar cf targets.tar target/out/jvm project/target

An sbt 1 <project>/target contains no scala-<scalaVersion> directory, so it is returned unchanged and sbt 1 output is byte-identical to before. internalTargetAggregation is now de-duplicated in pathStrs, since under sbt 2 every project of a build collapses onto the same aggregated path.

The artifact contract with the publish job is preserved: each matrix row still uploads a tarball rooted at the repository root, and githubWorkflowGeneratedDownloadSteps still just runs tar xf targets.tar per Scala version. Because each row only ever builds one Scala version, its tarball still contains only target/out/<platform>/scala-<that version>/…, so inflating all of them in the publish job reconstitutes the same tree as before.

Caveat worth a maintainer's opinion

Under sbt 2, githubWorkflowArtifactUpload := false on a subproject no longer excludes that subproject's output from the tarball, because all projects share target/out/<platform>. Excluding it would require naming the version-scoped directories again, which is exactly what causes the bug. The setting still works as before at the build level and under sbt 1. Happy to take this a different direction if you'd prefer something narrower.

Tests

  • src/sbt-test/sbtghactions/cross-scala-version-stability — new scripted test: generate at the default Scala version, ++ to the other crossScalaVersions entry, and githubWorkflowCheck again. It fails on main under sbt 2 and passes under sbt 1 and 2 with this change.
  • GenerativePluginSpec — three unit examples covering the new scalaVersionIndependentTarget helper, including that sbt 1 style targets are returned untouched.
  • The patchIfSbt2 helper tasks in the existing scripted tests are updated to the new expected sbt 2 path.

+test and +scripted are green (sbt 1.5.5/Scala 2.12.20 and sbt 2.0.0/Scala 3.8.4).

@eed3si9n

eed3si9n commented Sep 4, 2026

Copy link
Copy Markdown
Member

Thanks for the contribution!

Comment thread src/main/scala/sbtghactions/GenerativePlugin.scala
sbt 2 lays a project's target out as
`target/out/<platform>/scala-<scalaVersion>/<module>`, so with plain
`crossScalaVersions` only the currently loaded version's directory
exists. `internalTargetAggregation` picked that path up verbatim, which
made the generated "Compress target directories" step depend on the
`++` version the workflow happened to be generated at:

    at 2.13.10 -> tar cf targets.tar target/out/jvm/scala-2.13.10/root ... project/target
    at 2.12.17 -> tar cf targets.tar target/out/jvm/scala-2.12.17/root ... project/target

CI runs `githubWorkflowCheck` once per matrix row with a different `++`,
so no single committed ci.yml could satisfy more than one row.

Aggregate the nearest ancestor that is not scoped to a Scala version
instead, i.e. `target/out/<platform>`. Targets which are already
independent of the Scala version are returned unchanged: sbt 1's
`<project>/target`, and sbt 2's `target/out/<platform>/u/<module>`,
which is the layout used when `crossPaths` is `false`.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@nafg
nafg force-pushed the fix-sbt2-cross-scala-target-aggregation branch from 6184b86 to 9493953 Compare September 4, 2026 05:53

@eed3si9n eed3si9n 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.

Thanks!

@eed3si9n
eed3si9n merged commit 870ce57 into sbt:main Sep 4, 2026
7 checks passed
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