diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinJvmModuleBuildTarget.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinJvmModuleBuildTarget.kt index 1a4d8910bb2..928585e070f 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinJvmModuleBuildTarget.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/jps/targets/KotlinJvmModuleBuildTarget.kt @@ -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) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/fileUtils.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/fileUtils.kt index d337b5fe532..61d062a72ac 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/fileUtils.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/utils/fileUtils.kt @@ -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.toPathsArray(): Array = 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()