diff --git a/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartIrCache.kt b/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartIrCache.kt index ec0c4dc61e..cb9378ddd6 100644 --- a/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartIrCache.kt +++ b/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartIrCache.kt @@ -51,11 +51,14 @@ internal fun preparedClassStartupIr(case: CallsClassStartCase, scope: Path, outp val fingerprint = startupIrFingerprint(case) val cache = root.resolve(fingerprint) val lockPath = root.resolve("$fingerprint.lock") - FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE).use { channel -> + val ir = FileChannel.open(lockPath, StandardOpenOption.CREATE, StandardOpenOption.WRITE).use { channel -> channel.lock().use { - return prepareLockedStartupIr(cache = cache, scope = scope, output = output, fingerprint = fingerprint) + prepareLockedStartupIr(cache = cache, scope = scope, output = output, fingerprint = fingerprint) } } + val missing = case.requiredIrSources.filterNot { source -> Files.isRegularFile(ir.resolve("$source.json")) } + require(missing.isEmpty()) { "Frontend did not emit IR for required sources: ${missing.sorted()}" } + return ir } private fun prepareLockedStartupIr(cache: Path, scope: Path, output: Path, fingerprint: String): Path { diff --git a/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartSmoke.kt b/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartSmoke.kt index 31aad9c118..223f66c272 100644 --- a/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartSmoke.kt +++ b/usvm-ts-calls/src/main/kotlin/org/usvm/ts/calls/CallsClassStartSmoke.kt @@ -56,12 +56,14 @@ internal data class CallsClassStartCase( val acquisitionReceipt: String? = null, val acquisitionComplete: Boolean = false, val scenario: CallsClassLifecycleScenario? = null, + val requiredIrSources: Set = emptySet(), ) { init { require(classId.matches(Regex("[A-Za-z0-9_-]+"))) require(identity.isNotBlank() && className.isNotBlank()) require(projectRevision.matches(Regex("[0-9a-f]{40}"))) require(sourceFile in inputSha256) { "The full target source must be frozen" } + require(inputSha256.keys.containsAll(requiredIrSources)) { "Required IR sources must be frozen inputs" } inputSha256.forEach { (name, digest) -> val path = Path.of(name) require(!path.isAbsolute && path.normalize() == path && !path.startsWith("..")) diff --git a/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsRequiredIrSourcesTest.kt b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsRequiredIrSourcesTest.kt new file mode 100644 index 0000000000..39870e02d9 --- /dev/null +++ b/usvm-ts-calls/src/test/kotlin/org/usvm/ts/calls/CallsRequiredIrSourcesTest.kt @@ -0,0 +1,63 @@ +package org.usvm.ts.calls + +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import kotlin.test.Test +import kotlin.test.assertContains +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue + +class CallsRequiredIrSourcesTest { + @TempDir + lateinit var directory: Path + + @Test + fun `check declared source emission for new and cached IR`() { + val scope = Files.createDirectory(directory.resolve("source")) + Files.writeString(scope.resolve("Parent.ts"), "export class Parent { result() { return 1; } }") + val child = "import { Parent } from './Parent'; export class Child extends Parent {}" + Files.writeString(scope.resolve("Child.ts"), child) + val frozen = startupIrFiles(scope) + val oneRoot = """{"files":["Child.ts"]}""" + val base = CallsClassStartCase( + classId = "child", + identity = "fixture@Child", + projectRevision = "a".repeat(n = 40), + sourceRoot = scope.toString(), + sourceFile = "Child.ts", + className = "Child", + inputSha256 = frozen, + frontendConfig = oneRoot, + ) + Files.writeString(scope.resolve("tsconfig.json"), oneRoot) + val first = Files.createDirectory(directory.resolve("entry-only")) + val everything = frozen.keys + val required = base.copy(requiredIrSources = everything) + + val ir = preparedClassStartupIr(case = base, scope = scope, output = first) + + assertTrue(Files.isRegularFile(ir.resolve("Child.ts.json"))) + assertEquals(startupIrFingerprint(base), startupIrFingerprint(required)) + val cached = Files.createDirectory(directory.resolve("cached")) + first.resolve("ir-cache").toFile().copyRecursively(cached.resolve("ir-cache").toFile()) + val failure = assertFailsWith { + preparedClassStartupIr(case = required, scope = scope, output = cached) + } + assertContains(failure.message.orEmpty(), "Parent.ts") + assertTrue(Files.isRegularFile(cached.resolve("frontend-identity.json"))) + + val bothRoots = """{"files":["Child.ts","Parent.ts"]}""" + Files.writeString(scope.resolve("tsconfig.json"), bothRoots) + val full = Files.createDirectory(directory.resolve("both")) + val fullIr = preparedClassStartupIr( + case = required.copy(frontendConfig = bothRoots), + scope = scope, + output = full, + ) + assertTrue(Files.isRegularFile(fullIr.resolve("Parent.ts.json"))) + + assertFailsWith { base.copy(requiredIrSources = setOf("Unfrozen.ts")) } + } +}