Fix creation of temp file in case of bad parent dir for Gradle

newTmpFile can fail, if you pass non-existent path as temp parent directory or java.io.tmpdir is empty or invalid. Now we try to create non-existent dirs and provide more info if it also fails. Also fix error throwing in JPS part(46c0c4f9)

#KT-51374 Fixed
This commit is contained in:
Aleksei.Cherepanov
2022-02-24 13:13:52 +03:00
committed by Space
parent 4022918ea2
commit ee756638a0
2 changed files with 17 additions and 12 deletions
@@ -269,15 +269,12 @@ class KotlinJvmModuleBuildTarget(kotlinContext: KotlinCompileContext, jpsModuleB
} catch (e: NoSuchFileException) {
val parentDir = File(e.file).parentFile
if (parentDir != null && !parentDir.exists()) {
try {
parentDir.mkdirs()
} catch (e: IOException) {
val message: String?
if (dir == null) {
if (!parentDir.mkdirs()) {
val message = if (dir == null) {
val tmpPath = System.getProperty("java.io.tmpdir", null).trim().ifEmpty { null }
message = "java.io.tmpdir is set to $tmpPath and it does not exist. Attempt to create it failed with exception"
"java.io.tmpdir is set to $tmpPath and it does not exist. Attempt to create it failed with exception"
} else {
message = "kotlin.jps.dir.for.module.files is set to $dir and it does not exist. " +
"kotlin.jps.dir.for.module.files is set to $dir and it does not exist. " +
"Attempt to create it failed with exception"
}
throwException(e, dir, message)
@@ -7,9 +7,9 @@ package org.jetbrains.kotlin.gradle.utils
import org.gradle.api.Project
import java.io.File
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*
internal fun File.isJavaFile() =
extension.equals("java", ignoreCase = true)
@@ -29,10 +29,18 @@ internal fun File.relativeToRoot(project: Project): String =
internal fun Iterable<File>.toPathsArray(): Array<String> =
map { it.canonicalPath }.toTypedArray()
internal fun newTmpFile(prefix: String, suffix: String? = null, directory: File? = null, deleteOnExit: Boolean = true): File =
(if (directory == null) Files.createTempFile(prefix, suffix) else Files.createTempFile(directory.toPath(), prefix, suffix))
.toFile()
.apply { if (deleteOnExit) deleteOnExit() }
internal fun newTmpFile(prefix: String, suffix: String? = null, directory: File? = null, deleteOnExit: Boolean = true): File {
val tempDir = directory
?: System.getProperty("java.io.tmpdir", null)?.trim()?.ifEmpty { null }?.let { Paths.get(it) }?.toFile()
?: throw IOException("Temporary directory not specified")
if (tempDir.isFile) throw IOException("Temp folder $tempDir is not a directory")
if (!tempDir.isDirectory) {
if (!tempDir.mkdirs()) throw IOException("Could not create temp directory $tempDir")
}
return Files.createTempFile(tempDir.toPath(), prefix, suffix).toFile().apply { if (deleteOnExit) deleteOnExit() }
}
internal fun File.isParentOf(childCandidate: File, strict: Boolean = false): Boolean {
val parentPath = Paths.get(this.absolutePath).normalize()