8000 Actually delete temp files created by partest for Wasm. · scala-js/scala-js@67fa195 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67fa195

Browse files
committed
Actually delete temp files created by partest for Wasm.
It turns out `deleteOnExit()` does not do anything for non-empty directories. So we must recursively delete our temporary output directory ourselves.
1 parent 70a4164 commit 67fa195

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

partest/src/main/scala/scala/tools/nsc/MainGenericRunner.scala

Lines changed: 58 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import com.google.common.jimfs.Jimfs
2828

2929
import scala.tools.partest.scalajs.ScalaJSPartestOptions._
3030

31+
import java.io.IOException
3132
import java.net.URL
3233
import java.nio.file._
34+
import java.nio.file.attribute.BasicFileAttributes
3335

3436
import scala.io.Source
3537
import scala.concurrent.Await
@@ -112,51 +114,56 @@ class MainGenericRunner {
112114
/* The Wasm output needs to read other files in the same directory,
113115
* with predictable names. Therefore, we need to use real files.
114116
*/
115-
val tempDir = Files.createTempDirectory("tmp-scalajs-partest")
116-
tempDir.toFile().deleteOnExit()
117-
tempDir
117+
Files.createTempDirectory("tmp-scalajs-partest")
118118
}
119+
try {
120+
val sjsCode = {
121+
val cache = StandardImpl.irFileCache().newCache
122+
val result = PathIRContainer
123+
.fromClasspath(command.settings.classpathURLs.map(urlToPath _))
124+
.map(_._1)
125+
.flatMap(cache.cached _)
126+
.flatMap(linker.link(_, moduleInitializers, PathOutputDirectory(dir), logger))
119127

120-
val sjsCode = {
121-
val cache = StandardImpl.irFileCache().newCache
122-
val result = PathIRContainer
123-
.fromClasspath(command.settings.classpathURLs.map(urlToPath _))
124-
.map(_._1)
125-
.flatMap(cache.cached _)
126-
.flatMap(linker.link(_, moduleInitializers, PathOutputDirectory(dir), logger))
127-
128-
val report = Await.result(result, Duration.Inf)
128+
val report = Await.result(result, Duration.Inf)
129129

130-
if (report.publicModules.size != 1)
131-
throw new AssertionError(s"got other than 1 module: $report")
130+
if (report.publicModules.size != 1)
131+
throw new AssertionError(s"got other than 1 module: $report")
132132

133-
dir.resolve(report.publicModules.head.jsFileName)
134-
}
133+
dir.resolve(report.publicModules.head.jsFileName)
134+
}
135135

136-
val jsEnvConfig: NodeJSEnv.Config = if (!useWasm) {
137-
NodeJSEnv.Config()
138-
} else {
139-
NodeJSEnv.Config().withArgs(List(
140-
"--experimental-wasm-exnref",
141-
"--experimental-wasm-imported-strings", // for JS string builtins
142-
/* Force using the Turboshaft infrastructure for the optimizing compiler.
143-
* It appears to be more stable for the Wasm that we throw at it.
144-
* See also the use of this flag in Build.scala.
145-
*/
146-
"--turboshaft-wasm"
147-
))
148-
}
136+
val jsEnvConfig: NodeJSEnv.Config = if (!useWasm) {
137+
NodeJSEnv.Config()
138+
} else {
139+
NodeJSEnv.Config().withArgs(List(
140+
"--experimental-wasm-exnref",
141+
"--experimental-wasm-imported-strings", // for JS string builtins
142+
/* Force using the Turboshaft infrastructure for the optimizing compiler.
143+
* It appears to be more stable for the Wasm that we throw at it.
144+
* See also the use of this flag in Build.scala.
145+
*/
146+
"--turboshaft-wasm"
147+
))
148+
}
149149

150-
val input =
151-
if (useESModule) Input.ESModule(sjsCode) :: Nil
152-
else Input.Script(sjsCode) :: Nil
153-
val config = RunConfig().withLogger(logger)
150+
val input =
151+
if (useESModule) Input.ESModule(sjsCode) :: Nil
152+
else Input.Script(sjsCode) :: Nil
153+
val config = RunConfig().withLogger(logger)
154154

155-
val run = new NodeJSEnv(jsEnvConfig).start(input, config)
156-
try {
157-
Await.result(run.future, Duration.Inf)
155+
val run = new NodeJSEnv(jsEnvConfig).start(input, config)
156+
try {
157+
Await.result(run.future, Duration.Inf)
158+
} finally {
159+
run.close()
160+
}
158161
} finally {
159-
run.close()
162+
/* If using Wasm, we created actual files that we must delete.
163+
* For JS, we use an in-memory file system, so there is no point.
164+
*/
165+
if (useWasm)
166+
recursivelyDeleteDir(dir)
160167
}
161168

162169
true
@@ -169,6 +176,20 @@ class MainGenericRunner {
169176
case e: java.net.URISyntaxException => Paths.get(url.getPath())
170177
}
171178
}
179+
180+
private def recursivelyDeleteDir(directory: Path): Unit = {
181+
Files.walkFileTree(directory, new SimpleFileVisitor[Path] {
182+
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
183+
Files.delete(file)
184+
FileVisitResult.CONTINUE
185+
}
186+
187+
override def postVisitDirectory(dir: Path, exc: IOException): FileVisitResult = {
188+
Files.delete(dir)
189+
FileVisitResult.CONTINUE
190+
}
191+
})
192+
}
172193
}
173194

174195
object MainGenericRunner extends MainGenericRunner {

0 commit comments

Comments
 (0)
0