Skip to content

Commit a9b5a7f

Browse files
committed
Work around for native filesystem warning on windows
1 parent e6887e1 commit a9b5a7f

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -668,24 +668,32 @@ class KotlinCompilation {
668668
4. Run javac with Java sources and the compiled Kotlin classes
669669
*/
670670

671-
// step 1 and 2: generate stubs and run annotation processors
672-
try {
673-
val exitCode = stubsAndApt()
674-
if (exitCode != ExitCode.OK) {
675-
val messages = internalMessageBuffer.readUtf8()
676-
searchSystemOutForKnownErrors(messages)
677-
return Result(exitCode, classesDir, messages)
671+
/* Work around for warning that sometimes happens:
672+
"Failed to initialize native filesystem for Windows
673+
java.lang.RuntimeException: Could not find installation home path.
674+
Please make sure bin/idea.properties is present in the installation directory"
675+
See: https://github.com/arturbosch/detekt/issues/630
676+
*/
677+
withSystemProperty("idea.use.native.fs.for.win", "false") {
678+
// step 1 and 2: generate stubs and run annotation processors
679+
try {
680+
val exitCode = stubsAndApt()
681+
if (exitCode != ExitCode.OK) {
682+
val messages = internalMessageBuffer.readUtf8()
683+
searchSystemOutForKnownErrors(messages)
684+
return Result(exitCode, classesDir, messages)
685+
}
686+
} finally {
687+
KaptComponentRegistrar.threadLocalParameters.remove()
678688
}
679-
} finally {
680-
KaptComponentRegistrar.threadLocalParameters.remove()
681-
}
682689

683-
// step 3: compile Kotlin files
684-
compileKotlin().let { exitCode ->
685-
if(exitCode != ExitCode.OK) {
686-
val messages = internalMessageBuffer.readUtf8()
687-
searchSystemOutForKnownErrors(messages)
688-
return Result(exitCode, classesDir, messages)
690+
// step 3: compile Kotlin files
691+
compileKotlin().let { exitCode ->
692+
if(exitCode != ExitCode.OK) {
693+
val messages = internalMessageBuffer.readUtf8()
694+
searchSystemOutForKnownErrors(messages)
695+
return Result(exitCode, classesDir, messages)
696+
}
689697
}
690698
}
691699

@@ -790,4 +798,3 @@ private fun getHostClasspaths(): List<File> {
790798

791799
return (classpaths + modules).distinctBy(File::getAbsolutePath)
792800
}
793-

src/main/kotlin/com/tschuchort/compiletesting/Utils.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,26 @@ internal fun URLClassLoader.addUrl(url: URL) {
4444
val addUrlMethod = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java)
4545
addUrlMethod.isAccessible = true
4646
addUrlMethod.invoke(this, url)
47+
}
48+
49+
internal inline fun <T> withSystemProperty(key: String, value: String, f: () -> T): T
50+
= withSystemProperties(mapOf(key to value), f)
51+
52+
53+
internal inline fun <T> withSystemProperties(properties: Map<String, String>, f: () -> T): T {
54+
val previousProperties = mutableMapOf<String, String?>()
55+
56+
for ((key, value) in properties) {
57+
previousProperties[key] = System.getProperty(key)
58+
System.setProperty(key, value)
59+
}
60+
61+
try {
62+
return f()
63+
} finally {
64+
for ((key, value) in previousProperties) {
65+
if (value != null)
66+
System.setProperty(key, value)
67+
}
68+
}
4769
}

0 commit comments

Comments
 (0)