Skip to content

Commit

Permalink
Prefer File.getCanonicalFile over Path.normalize
Browse files Browse the repository at this point in the history
The former normalizes Windows 8.3 paths to long paths, whereas the
latter doesn't.
  • Loading branch information
alexarchambault committed May 23, 2021
1 parent 53c33bb commit 8f64cf9
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions os/src/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,14 @@ object Path {
throw PathError.AbsolutePathOutsideRoot
}

val normalized = f.normalize()
fromNIO(f)
}

private def fromNIO(p: java.nio.file.Path): Path = {
// On Windows, File.getCanonicalFile normalizes 8.3 paths as long paths,
// whereas Path.normalize doesn't. So we use the former here.
require(p.isAbsolute, s"$p is not an absolute path")
val normalized = p.toFile.getCanonicalFile.toPath
new Path(normalized)
}

Expand Down Expand Up @@ -437,8 +444,8 @@ class Path private[os](val wrapped: java.nio.file.Path)

def /(chunk: PathChunk): ThisType = {
if (chunk.ups > wrapped.getNameCount) throw PathError.AbsolutePathOutsideRoot
val resolved = wrapped.resolve(chunk.toString).normalize()
new Path(resolved)
val resolved = wrapped.resolve(chunk.toString)
Path.fromNIO(resolved)
}
override def toString = wrapped.toString

Expand Down

0 comments on commit 8f64cf9

Please sign in to comment.