Skip to content

Commit bf1705d

Browse files
committed
use custom removeRecursively function, to please scala2 compiler...
For some reason, calling `os.remove.all(this)`: ```scala class TempPath private[os] (wrapped: java.nio.file.Path) extends Path(wrapped) with AutoCloseable { override def close(): Unit = os.remove.all(this) } ``` fails in Scala 2, with rather obscure compiler errors: ``` [error] Unwanted cyclic dependency [info] symbol: object all [info] symbol: value up [info] More dependencies at lines 394 394 71 87 71 152 152 87 [info] /home/mp/Projects/os-lib/os/src-jvm/package.scala:13:34: [info] def resource(implicit resRoot: ResourceRoot = Thread.currentThread().getContextClassLoader) = { [info] ^ [info] symbol: trait ResourceRoot [info] More dependencies at lines 14 14 [info] /home/mp/Projects/os-lib/os/src-jvm/ResourcePath.scala:19:49: [info] extends BasePathImpl with ReadablePath with SegmentedPath { [info] ^ [info] symbol: trait SegmentedPath [info] More dependencies at lines 44 36 18 31 44 18 18 31 [error] 6 errors found ```
1 parent bd4291d commit bf1705d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

os/src/Path.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package os
22

33
import java.net.URI
44
import java.nio.file.Paths
5+
import java.nio.file.Files
56

67
import collection.JavaConverters._
78
import scala.language.implicitConversions
@@ -281,7 +282,7 @@ class RelPath private[os] (segments0: Array[String], val ups: Int)
281282
case _ => false
282283
}
283284

284-
def toNIO = java.nio.file.Paths.get(toString)
285+
def toNIO = Paths.get(toString)
285286

286287
def asSubPath = {
287288
require(ups == 0)
@@ -342,7 +343,7 @@ class SubPath private[os] (val segments0: Array[String])
342343
case _ => false
343344
}
344345

345-
def toNIO = java.nio.file.Paths.get(toString)
346+
def toNIO = Paths.get(toString)
346347

347348
def resolveFrom(base: os.Path) = base / this
348349
}
@@ -448,7 +449,7 @@ trait ReadablePath {
448449
class Path private[os] (val wrapped: java.nio.file.Path)
449450
extends FilePath with ReadablePath with BasePathImpl {
450451
def toSource: SeekableSource =
451-
new SeekableSource.ChannelSource(java.nio.file.Files.newByteChannel(wrapped))
452+
new SeekableSource.ChannelSource(Files.newByteChannel(wrapped))
452453

453454
require(wrapped.isAbsolute, s"$wrapped is not an absolute path")
454455
def segments: Iterator[String] = wrapped.iterator().asScala.map(_.toString)
@@ -495,12 +496,23 @@ class Path private[os] (val wrapped: java.nio.file.Path)
495496

496497
def resolveFrom(base: os.Path) = this
497498

498-
def getInputStream = java.nio.file.Files.newInputStream(wrapped)
499+
def getInputStream = Files.newInputStream(wrapped)
499500
}
500501

501502
class TempPath private[os] (wrapped: java.nio.file.Path)
502503
extends Path(wrapped) with AutoCloseable {
503-
override def close(): Unit = os.remove.all(this)
504+
505+
override def close(): Unit = deleteRecursively(wrapped)
506+
507+
/** Wouldn't it be nice if we could just call `os.remove.all(this)`?
508+
* For some reason, Scala 2 throws a rather obscure `[error] Unwanted cyclic dependency`
509+
*/
510+
private def deleteRecursively(ioPath: java.nio.file.Path): Unit = {
511+
if (Files.isDirectory(ioPath)) {
512+
Files.list(ioPath).forEach(deleteRecursively)
513+
}
514+
Files.deleteIfExists(ioPath)
515+
}
504516
}
505517

506518
sealed trait PathConvertible[T] {

0 commit comments

Comments
 (0)