CLI: improve path relativization for compiler messages

On Windows, the absolute file for the root of the drive is `"C:\"`,
which differs from the absolute file for any other directory, which
doesn't end with `\`. This resulted in incorrect trimming of the first
character of the path name in `descendantRelativeTo`.

Also, do not use canonicalPath because there is no point in expanding
symbolic links here.

 #KT-40979 Fixed
This commit is contained in:
Alexander Udalov
2021-04-19 13:07:01 +02:00
committed by teamcityserver
parent f63dac26e6
commit c898805ac5
2 changed files with 81 additions and 11 deletions
@@ -37,14 +37,9 @@ fun File.withReplacedExtensionOrNull(oldExt: String, newExt: String): File? {
* If this file does not belong to the [base] directory, it is returned unchanged.
*/
fun File.descendantRelativeTo(base: File): File {
val prefix = base.canonicalPath
val answer = this.canonicalPath
return if (answer.startsWith(prefix)) {
val prefixSize = prefix.length
if (answer.length > prefixSize) {
File(answer.substring(prefixSize + 1))
} else File("")
} else {
this
}
}
assert(base.isAbsolute) { "$base" }
assert(base.isDirectory) { "$base" }
val cwd = base.normalize()
val filePath = this.absoluteFile.normalize()
return if (filePath.startsWith(cwd)) filePath.relativeTo(cwd) else this
}