K2 Scripting: report error on attempt to use scripts in common roots

#KT-65967 fixed
This commit is contained in:
Ilya Chernikov
2024-02-20 12:05:36 +01:00
committed by Space Team
parent cdf4b17052
commit 10dbe73828
17 changed files with 168 additions and 3 deletions
@@ -357,7 +357,7 @@ private inline fun <F> prepareSessions(
}
}
val nonScriptSessions =when {
val nonScriptSessions = when {
metadataCompilationMode || !isMppEnabled -> {
listOf(
createSingleSession(
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.pipeline.generateCodeFromIr
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.diagnostics.DiagnosticReporterFactory
@@ -56,6 +57,7 @@ import org.jetbrains.kotlin.resolve.multiplatform.hmppModuleName
import org.jetbrains.kotlin.resolve.multiplatform.isCommonSource
import org.jetbrains.kotlin.utils.addToStdlib.runIf
import org.jetbrains.kotlin.utils.addToStdlib.runUnless
import org.jetbrains.kotlin.utils.fileUtils.descendantRelativeTo
import java.io.File
object FirKotlinToJvmBytecodeCompiler {
@@ -194,6 +196,8 @@ object FirKotlinToJvmBytecodeCompiler {
AnalyzerWithCompilerReport.reportSyntaxErrors(ktFile, messageCollector).isHasErrors or errorsFound
}
val scriptsInCommonSourcesErrors = reportCommonScriptsError(ktFiles)
val sourceScope = projectEnvironment.getSearchScopeByPsiFiles(ktFiles) + projectEnvironment.getSearchScopeForProjectJavaSources()
var librariesScope = projectEnvironment.getSearchScopeForProjectLibraries()
@@ -224,7 +228,22 @@ object FirKotlinToJvmBytecodeCompiler {
}
outputs.runPlatformCheckers(diagnosticsReporter)
return runUnless(syntaxErrors || diagnosticsReporter.hasErrors) { FirResult(outputs) }
return runUnless(syntaxErrors || scriptsInCommonSourcesErrors || diagnosticsReporter.hasErrors) { FirResult(outputs) }
}
private fun FrontendContext.reportCommonScriptsError(ktFiles: List<KtFile>): Boolean {
val lastHmppModule = configuration.get(CommonConfigurationKeys.HMPP_MODULE_STRUCTURE)?.modules?.lastOrNull()
val commonScripts = ktFiles.filter { it.isScript() && (it.isCommonSource == true || it.hmppModuleName != lastHmppModule?.name) }
if (commonScripts.isNotEmpty()) {
val cwd = File(".").absoluteFile
fun renderFile(ktFile: KtFile) = File(ktFile.virtualFilePath).descendantRelativeTo(cwd).path
messageCollector.report(
CompilerMessageSeverity.ERROR,
"Script files in common source roots are not supported. Misplaced files:\n ${commonScripts.joinToString("\n ", transform = ::renderFile)}"
)
return true
}
return false
}
private fun CompilationContext.runBackend(
@@ -0,0 +1,21 @@
-Xuse-fir-lt=false
-Xallow-any-scripts-in-source-roots
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
$TESTDATA_DIR$/src/c.kt
$TESTDATA_DIR$/src/s.kts
$TESTDATA_DIR$/src/s1.kts
$TESTDATA_DIR$/src/s2.kts
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xfragments=a
-Xfragments=b
-Xfragments=c
-Xfragment-sources=a\:$TESTDATA_DIR$/src/a.kt,a\:$TESTDATA_DIR$/src/s1.kts
-Xfragment-sources=b\:$TESTDATA_DIR$/src/b.kt,b\:$TESTDATA_DIR$/src/s2.kts
-Xfragment-sources=c\:$TESTDATA_DIR$/src/c.kt,c\:$TESTDATA_DIR$/src/s.kts
-Xfragment-refines=b\:a
-Xfragment-refines=c\:b
@@ -0,0 +1,13 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
error: script files in common source roots are not supported. Misplaced files:
compiler/testData/cli/jvm/hmpp/src/s1.kts
compiler/testData/cli/jvm/hmpp/src/s2.kts
COMPILATION_ERROR
+2
View File
@@ -0,0 +1,2 @@
val x = 42
+2
View File
@@ -0,0 +1,2 @@
val x = 42
+2
View File
@@ -0,0 +1,2 @@
val x = 42
@@ -0,0 +1,20 @@
-Xuse-fir-lt=false
-Xallow-any-scripts-in-source-roots
$TESTDATA_DIR$/src/a.kt
$TESTDATA_DIR$/src/b.kt
$TESTDATA_DIR$/src/c.kt
$TESTDATA_DIR$/src/s.kts
-d
$TEMP_DIR$
-language-version
2.0
-XXLanguage\:+MultiPlatformProjects
-Xexpect-actual-classes
-Xfragments=a
-Xfragments=b
-Xfragments=c
-Xfragment-sources=a\:$TESTDATA_DIR$/src/a.kt
-Xfragment-sources=b\:$TESTDATA_DIR$/src/b.kt
-Xfragment-sources=c\:$TESTDATA_DIR$/src/c.kt,c\:$TESTDATA_DIR$/src/s.kts
-Xfragment-refines=b\:a
-Xfragment-refines=c\:b
@@ -0,0 +1,10 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
OK
@@ -0,0 +1,14 @@
-Xuse-fir-lt=false
-Xallow-any-scripts-in-source-roots
$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/common.kt
$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/jvm.kt
$TESTDATA_DIR$/scripts/s.kts
$TESTDATA_DIR$/scripts/s1.kts
-Xcommon-sources=$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/common.kt,$TESTDATA_DIR$/scripts/s1.kts
-language-version
2.0
-cp
.
-d
$TEMP_DIR$
-XXLanguage\:+MultiPlatformProjects
@@ -0,0 +1,12 @@
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
error: script files in common source roots are not supported. Misplaced files:
compiler/testData/cli/jvm/scripts/s1.kts
COMPILATION_ERROR
@@ -0,0 +1,15 @@
-Xuse-fir-lt=false
-Xallow-any-scripts-in-source-roots
$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/common.kt
$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/jvm.kt
$TESTDATA_DIR$/scripts/s.kts
-Xcommon-sources
$TESTDATA_DIR$/firMultiplatformCompilationWithoutErrors/common.kt
-language-version
2.0
-cp
.
-d
$TEMP_DIR$
-XXLanguage\:+MultiPlatformProjects
-Xexpect-actual-classes
@@ -0,0 +1,11 @@
warning: advanced option value is passed in an obsolete form. Please use the '=' character to specify the value: -Xcommon-sources=...
warning: ATTENTION!
This build uses unsafe internal compiler arguments:
-XXLanguage:+MultiPlatformProjects
This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!
OK
+2
View File
@@ -0,0 +1,2 @@
val x = 42
+2
View File
@@ -0,0 +1,2 @@
val x = 42
@@ -1,6 +1,6 @@
// TARGET_BACKEND: JVM_IR
// IGNORE_LIGHT_ANALYSIS
// JVM_ABI_K1_K2_DIFF:
// JVM_ABI_K1_K2_DIFF: KT-62465, KT-63960
// LANGUAGE: +ReferencesToSyntheticJavaProperties
// LANGUAGE: -SkipStandaloneScriptsInSourceRoots
// WITH_STDLIB
@@ -162,6 +162,11 @@ public class CliTestGenerated extends AbstractCliTest {
runTest("compiler/testData/cli/jvm/hmpp/sameSourceInDifferentFragments.args");
}
@TestMetadata("scriptInCommonFragment.args")
public void testScriptInCommonFragment() {
runTest("compiler/testData/cli/jvm/hmpp/scriptInCommonFragment.args");
}
@TestMetadata("sourceNotInAnyFragment.args")
public void testSourceNotInAnyFragment() {
runTest("compiler/testData/cli/jvm/hmpp/sourceNotInAnyFragment.args");
@@ -176,6 +181,11 @@ public class CliTestGenerated extends AbstractCliTest {
public void testSuccessfulCompilation2() {
runTest("compiler/testData/cli/jvm/hmpp/successfulCompilation2.args");
}
@TestMetadata("successfulCompilationWithScript.args")
public void testSuccessfulCompilationWithScript() {
runTest("compiler/testData/cli/jvm/hmpp/successfulCompilationWithScript.args");
}
}
@TestMetadata("compiler/testData/cli/jvm")
@@ -1000,6 +1010,16 @@ public class CliTestGenerated extends AbstractCliTest {
runTest("compiler/testData/cli/jvm/modulesWithDependencyCycle.args");
}
@TestMetadata("multiplatformCompilationWithCommonScript.args")
public void testMultiplatformCompilationWithCommonScript() {
runTest("compiler/testData/cli/jvm/multiplatformCompilationWithCommonScript.args");
}
@TestMetadata("multiplatformCompilationWithScript.args")
public void testMultiplatformCompilationWithScript() {
runTest("compiler/testData/cli/jvm/multiplatformCompilationWithScript.args");
}
@TestMetadata("multipleTextRangesInDiagnosticsOrder.args")
public void testMultipleTextRangesInDiagnosticsOrder() {
runTest("compiler/testData/cli/jvm/multipleTextRangesInDiagnosticsOrder.args");