From 275a1b521c18784676d9df317507a84f75656dde Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Wed, 22 May 2024 16:00:39 +0000 Subject: [PATCH] [K/N][tests] Stop using shared Clang module cache to improve stability cinterop uses Clang and libclang. When getting `-fmodules` flag, Clang practices some caching, which is system-wide by default. Meaning, the cache directory can be used by other Clang invocations, even at the same time. Clang module cache machinery has sophisticated measures to avoid any problems with that, e.g. different compilation flags make it use different cache sub-directories, and there are locks in place to avoid data races. Still, sometimes we encounter problems seemingly triggered by re-using the same cache directory. This commit workarounds that by passing `-fmodules-cache-path=` flag to some of the relevant `cinterop` invocations in the test infrastructure. ^KT-68254 (cherry picked from commit 0044ca56b64f4a6844d66c653e4d600e669bf208) --- .../test/blackbox/NativeSimpleTestUtils.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/NativeSimpleTestUtils.kt b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/NativeSimpleTestUtils.kt index 1932a68a126c5..dd0798ff3c689 100644 --- a/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/NativeSimpleTestUtils.kt +++ b/native/native.tests/tests/org/jetbrains/kotlin/konan/test/blackbox/NativeSimpleTestUtils.kt @@ -143,17 +143,31 @@ internal fun AbstractNativeSimpleTest.cinteropToLibrary( outputDir: File, freeCompilerArgs: TestCompilerArgs ): TestCompilationResult { - val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, freeCompilerArgs) + val args = freeCompilerArgs + cinteropModulesCachePathArguments(freeCompilerArgs.cinteropArgs, outputDir) + val testCase: TestCase = generateCInteropTestCaseFromSingleDefFile(defFile, args) return CInteropCompilation( classLoader = testRunSettings.get(), targets = targets, - freeCompilerArgs = freeCompilerArgs, + freeCompilerArgs = args, defFile = testCase.modules.single().files.single().location, dependencies = emptyList(), expectedArtifact = getLibraryArtifact(testCase, outputDir) ).result } +private fun cinteropModulesCachePathArguments( + cinteropArgs: List, + outputDir: File, +) = if (cinteropArgs.contains("-fmodules") && cinteropArgs.none { it.startsWith(FMODULES_CACHE_PATH_EQ) }) { + // Don't reuse the system-wide module cache to make the test run more predictably. + // See e.g. https://youtrack.jetbrains.com/issue/KT-68254. + TestCInteropArgs("-compiler-option", "$FMODULES_CACHE_PATH_EQ$outputDir/modulesCachePath") +} else { + TestCompilerArgs.EMPTY +} + +private const val FMODULES_CACHE_PATH_EQ = "-fmodules-cache-path=" + internal class CompiledExecutable( val testCase: TestCase, val compilationResult: TestCompilationResult.Success