Skip to content

Scoverage does not close source files after reading #371

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

Closed
lefou opened this issue Sep 15, 2021 · 0 comments · Fixed by #372
Closed

Scoverage does not close source files after reading #371

lefou opened this issue Sep 15, 2021 · 0 comments · Fixed by #372

Comments

@lefou
Copy link
Contributor

lefou commented Sep 15, 2021

I have the issue that our mill test suite fails in subsequent scoverage plugin tests on Windows. Between each run we clean the complete work directory, but we don't drop the JVM which runs the scalac-scoverage-plugin. As a result we can't clean our workdir because the source files, which were processed are still locked. Windows locks files as long as one process keeps it open. So my suspicion is, that somewhere in scoverage, the Scala source files were opened but not properly closed.
We get this message:

java.nio.file.FileSystemException: D:\a\mill\mill\target\worksources\mill\contrib\scoverage\HelloWorldTests#HelloWorld\core\src\Greet.scala: The process cannot access the file because it is being used by another process.

    sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
    sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
    java.nio.file.Files.delete(Files.java:1126)
    os.remove$.apply(FileOps.scala:285)
    os.remove$.apply(FileOps.scala:284)
    geny.Generator.$anonfun$foreach$1(Generator.scala:50)
    geny.Generator$Mapped.$anonfun$generate$4(Generator.scala:283)
    os.walk$stream$$anon$2$$anon$3.visitFile(ListOps.scala:249)
    os.walk$stream$$anon$2$$anon$3.visitFile(ListOps.scala:230)
    java.nio.file.Files.walkFileTree(Files.java:2670)
    os.walk$stream$$anon$2.generate(ListOps.scala:230)
    geny.Generator$Mapped.generate(Generator.scala:283)
    geny.Generator.foreach(Generator.scala:49)
    geny.Generator.foreach$(Generator.scala:49)
    geny.Generator$Mapped.foreach(Generator.scala:281)
    os.remove$all$.apply(FileOps.scala:294)
    mill.contrib.scoverage.HelloWorldTests.workspaceTest(HelloWorldTests.scala:67)
    mill.contrib.scoverage.HelloWorldTests.workspaceTest$(HelloWorldTests.scala:62)
    mill.contrib.scoverage.HelloWorldTests_2_12$.workspaceTest(HelloWorldTests.scala:191)
    mill.contrib.scoverage.HelloWorldTests.$anonfun$tests$30(HelloWorldTests.scala:74)

Of course the stack trace belongs to the point, where we try to delete the file. But as it seems to be already opened (and locked) it fails.

This can't be reproduced on non-Windows machines

After a chat on Gitter @ckipp01 probably located the suspected issue at:

private def source(mfile: MeasuredFile): String = {
val src = sourceEncoding match {
case Some(enc) => Source.fromFile(mfile.source, enc)
case None => Source.fromFile(mfile.source)
}
src.mkString
}

A naive fix could look like this:

@@ -79,7 +78,8 @@ class CodeGrid(mFile: MeasuredFile, sourceEncoding: Option[String]) {
       case Some(enc) => Source.fromFile(mfile.source, enc)
       case None      => Source.fromFile(mfile.source)
     }
-    src.mkString
+    try src.mkString
+    finally src.close()
   }

   private def spanStart(status: StatementStatus): String =
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 a pull request may close this issue.

1 participant