diff --git a/io/src/main/scala/sbt/io/IO.scala b/io/src/main/scala/sbt/io/IO.scala index bc1245ce..932148b0 100644 --- a/io/src/main/scala/sbt/io/IO.scala +++ b/io/src/main/scala/sbt/io/IO.scala @@ -608,8 +608,8 @@ object IO { * If `file` or `base` are not absolute, they are first resolved against the current working directory. */ def relativize(base: File, file: File): Option[String] = { - val basePath = base.toPath - val filePath = file.toPath + val basePath = (if (base.isAbsolute) base else base.getCanonicalFile).toPath + val filePath = (if (file.isAbsolute) file else file.getCanonicalFile).toPath if ((filePath startsWith basePath) || (filePath.normalize() startsWith basePath.normalize())) { val relativePath = catching(classOf[IllegalArgumentException]) opt (basePath relativize filePath) relativePath map (_.toString) diff --git a/io/src/test/scala/sbt/io/IOSpec.scala b/io/src/test/scala/sbt/io/IOSpec.scala index 00ed1a44..11621eb7 100644 --- a/io/src/test/scala/sbt/io/IOSpec.scala +++ b/io/src/test/scala/sbt/io/IOSpec.scala @@ -36,6 +36,12 @@ class IOSpec extends FlatSpec with Matchers { IO.relativize(base, file3) shouldBe Some(".git") } + it should "relativize relative paths" in { + val base = new File(".").getCanonicalFile + val file = new File("build.sbt") + IO.relativize(base, file) shouldBe Some("build.sbt") + } + "toURI" should "make URI" in { val u = IO.toURI(file("/etc/hosts")) assert(u.toString == "file:///etc/hosts")