Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverage data does not survive remote cache push-pull #421

Open
vlfig opened this issue Feb 10, 2022 · 4 comments
Open

coverage data does not survive remote cache push-pull #421

vlfig opened this issue Feb 10, 2022 · 4 comments

Comments

@vlfig
Copy link

vlfig commented Feb 10, 2022

I'm a bit unsure where to post this but here goes.

coverageDataDir + "/scoverage-data/" doesn't get cached on pushRemoteCache. This breaks tests on instrumented code.

Example script below:

  1. clean
  2. test with coverage - fine
  3. clean
  4. compile tests with coverage
  5. push cache
  6. clean
  7. pull cache
  8. run tests with coverage - error FileNotFoundException
sbt:my-module> clean
[success] Total time: 0 s, completed 10 Feb 2022, 12:08:39
sbt:my-module> ;coverage ;test
[snip]
[info] Wrote instrumentation file [/home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data/scoverage.coverage]
[info] Will write measurement data to [/home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data]
[snip]
[info] All tests passed.
sbt:my-module> clean
[success] Total time: 0 s, completed 10 Feb 2022, 12:09:06
sbt:my-module> ;coverage ;Test/compile
[snip]
[info] Wrote instrumentation file [/home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data/scoverage.coverage]
[info] Will write measurement data to [/home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data]
[snip]
sbt:my-module> pushRemoteCache
[warn] multiple main classes detected: run 'show discoveredMainClasses' to see the list
[info]  published my-module_2.13 to file:/home/user/projects/acme/my-project/sbt-cache/com/acme/my-module_2.13/0.0.0-305a5886860cd5d4/my-module_2.13-0.0.0-305a5886860cd5d4-cached-compile.jar
[info]  published my-module_2.13 to file:/home/user/projects/acme/my-project/sbt-cache/com/acme/my-module_2.13/0.0.0-840291a3ca4253fe/my-module_2.13-0.0.0-840291a3ca4253fe-cached-test.jar
[success] Total time: 0 s, completed 10 Feb 2022, 12:09:19
sbt:my-module> clean
[success] Total time: 0 s, completed 10 Feb 2022, 12:09:26
sbt:my-module> pullRemoteCache
[info] remote cache artifact extracted for com.acme:my-module:0.0.0-305a5886860cd5d4 Some(cached-compile)
[info] remote cache artifact extracted for com.acme:my-module:0.0.0-840291a3ca4253fe Some(cached-test)
[success] Total time: 0 s, completed 10 Feb 2022, 12:09:28
sbt:my-module> ;coverage ;test
[snip]
[info] > Exception: java.io.FileNotFoundException: /home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data/scoverage.measurements.b0dad2a4-06f7-44be-ab65-77ef8631ae71.15 (No such file or directory)
[info] > Exception: java.io.FileNotFoundException: /home/user/projects/acme/my-project/my-module/target/scala-2.13/scoverage-data/scoverage.measurements.b0dad2a4-06f7-44be-ab65-77ef8631ae71.15 (No such file or directory)
[snip]
[info] *** x TESTS FAILED ***

Maybe tweak ThisBuild / Compile / pushRemoteCacheConfiguration / remoteCacheArtifacts?

Thanks!

@strygul
Copy link

strygul commented Nov 18, 2022

@vlfig were you able to find a workaround for this by a chance?

@vlfig
Copy link
Author

vlfig commented Jan 19, 2023

@vlfig were you able to find a workaround for this by a chance?

No, I was not. We're just not using remote cache.

@danslapman
Copy link

For those who need it to work - it's possible to add an AutoPlugin as a workaround

import sbt.*
import sbt.Keys.*
import sbt.internal.RemoteCache
import sbt.internal.remotecache.CustomRemoteCacheArtifact
import sbt.io.IO
import scoverage.ScoverageSbtPlugin
import scoverage.ScoverageKeys._

object ScoverageCachePlugin extends AutoPlugin {
  val packCoverage = taskKey[File]("packCoverage")

  override def projectSettings: Seq[Def.Setting[_]] = Seq(
    Test / packCoverage := {
      val root = coverageDataDir.value / "scoverage-data"
      val output = coverageDataDir.value / "coverage.jar"
      val files = IO.listFiles(root)
      val z = IO.zip(files.map(f => f -> IO.relativizeFile(root, f).getOrElse(f).toPath.toString), output, None)

      output
    },
    Test / pushRemoteCache / pushRemoteCacheConfiguration / remoteCacheArtifacts += CustomRemoteCacheArtifact(
      Artifact(moduleName.value, "cached-scoverage"),
      Test / packCoverage,
      coverageDataDir.value / "scoverage-data",
      true
    )
  )

  override def trigger = allRequirements

  override def requires = ScoverageSbtPlugin
}

This code has a little downside (an extra artifact is added even when coverage is not enabled), but just because it's impossible to get project settings from autoplugin

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

No branches or pull requests

4 participants