Skip to content

Commit 29ac01f

Browse files
committed
KJS: allow to use packages with names starting with "kotlin" only if the -Xallow-kotlin-package command line option is specified
#KT-14668 Fixed
1 parent 7195e26 commit 29ac01f

File tree

16 files changed

+84
-30
lines changed

16 files changed

+84
-30
lines changed

build.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@
345345
<arg value="-kjsm"/>
346346
<arg line="-main noCall"/>
347347
<arg line="-module-kind commonjs"/>
348+
<arg value="-Xallow-kotlin-package"/>
348349
</java>
349350
</sequential>
350351
</macrodef>
@@ -1121,6 +1122,7 @@
11211122
<arg line="-main noCall"/>
11221123
<arg line="-module-kind umd"/>
11231124
<arg value="-Xmulti-platform"/>
1125+
<arg value="-Xallow-kotlin-package"/>
11241126
<arg value="libraries/kotlin.test/common/src/main/kotlin"/>
11251127
<arg value="libraries/kotlin.test/js/src/main/kotlin"/>
11261128
</java>

compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/CommonCompilerArguments.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public abstract class CommonCompilerArguments implements Serializable {
6363
@ValueDescription("<count>")
6464
public String repeat;
6565

66+
@Argument(value = "Xallow-kotlin-package", description = "Allow compiling code in package 'kotlin'")
67+
public boolean allowKotlinPackage;
68+
6669
@Argument(value = "Xplugin", description = "Load plugins from the given classpath")
6770
@ValueDescription("<path>")
6871
public String[] pluginClasspaths;

compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JVMCompilerArguments.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ public class K2JVMCompilerArguments extends CommonCompilerArguments {
9494
@Argument(value = "Xmultifile-parts-inherit", description = "Compile multifile classes as a hierarchy of parts and facade")
9595
public boolean inheritMultifileParts;
9696

97-
@Argument(value = "Xallow-kotlin-package", description = "Allow compiling code in package 'kotlin'")
98-
public boolean allowKotlinPackage;
99-
10097
@Argument(value = "Xskip-metadata-version-check", description = "Load classes with bad metadata version anyway (incl. pre-release classes)")
10198
public boolean skipMetadataVersionCheck;
10299

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2010-2017 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.jetbrains.kotlin.cli.common
18+
19+
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
20+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
21+
import org.jetbrains.kotlin.cli.common.messages.MessageUtil
22+
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
23+
import org.jetbrains.kotlin.name.FqName
24+
import org.jetbrains.kotlin.name.Name
25+
import org.jetbrains.kotlin.name.isSubpackageOf
26+
import org.jetbrains.kotlin.psi.KtFile
27+
28+
fun checkKotlinPackageUsage(environment: KotlinCoreEnvironment, files: Collection<KtFile>): Boolean {
29+
if (environment.configuration.getBoolean(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE)) {
30+
return true
31+
}
32+
val messageCollector = environment.configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
33+
val kotlinPackage = FqName.topLevel(Name.identifier("kotlin"))
34+
files.forEach {
35+
if (it.packageFqName.isSubpackageOf(kotlinPackage)) {
36+
messageCollector.report(CompilerMessageSeverity.ERROR,
37+
"Only the Kotlin standard library is allowed to use the 'kotlin' package",
38+
MessageUtil.psiElementToMessageLocation(it.packageDirective!!))
39+
return false
40+
}
41+
}
42+
return true
43+
}
44+

compiler/cli/src/org/jetbrains/kotlin/cli/js/K2JSCompiler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868

6969
import static org.jetbrains.kotlin.cli.common.ExitCode.COMPILATION_ERROR;
7070
import static org.jetbrains.kotlin.cli.common.ExitCode.OK;
71+
import static org.jetbrains.kotlin.cli.common.UtilsKt.checkKotlinPackageUsage;
7172
import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
7273

7374
public class K2JSCompiler extends CLICompiler<K2JSCompilerArguments> {
@@ -112,6 +113,10 @@ protected ExitCode doExecute(
112113
Project project = environmentForJS.getProject();
113114
List<KtFile> sourcesFiles = environmentForJS.getSourceFiles();
114115

116+
environmentForJS.getConfiguration().put(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE, arguments.allowKotlinPackage);
117+
118+
if (!checkKotlinPackageUsage(environmentForJS, sourcesFiles)) return ExitCode.COMPILATION_ERROR;
119+
115120
if (arguments.outputFile == null) {
116121
messageCollector.report(CompilerMessageSeverity.ERROR, "Specify output file via -output", CompilerMessageLocation.NO_LOCATION);
117122
return ExitCode.COMPILATION_ERROR;

compiler/cli/src/org/jetbrains/kotlin/cli/jvm/compiler/KotlinToJVMBytecodeCompiler.kt

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,24 @@
1616

1717
package org.jetbrains.kotlin.cli.jvm.compiler
1818

19-
import com.intellij.openapi.project.Project
2019
import com.intellij.openapi.application.ApplicationManager
20+
import com.intellij.openapi.project.Project
2121
import com.intellij.openapi.util.io.JarUtil
2222
import com.intellij.openapi.vfs.VfsUtilCore
2323
import com.intellij.openapi.vfs.VirtualFile
2424
import com.intellij.psi.PsiManager
2525
import com.intellij.psi.impl.PsiModificationTrackerImpl
2626
import com.intellij.psi.search.DelegatingGlobalSearchScope
2727
import com.intellij.psi.search.GlobalSearchScope
28-
import org.jetbrains.annotations.TestOnly
2928
import org.jetbrains.kotlin.analyzer.AnalysisResult
3029
import org.jetbrains.kotlin.asJava.FilteredJvmDiagnostics
3130
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection
3231
import org.jetbrains.kotlin.backend.common.output.SimpleOutputFileCollection
3332
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
3433
import org.jetbrains.kotlin.cli.common.ExitCode
34+
import org.jetbrains.kotlin.cli.common.checkKotlinPackageUsage
3535
import org.jetbrains.kotlin.cli.common.messages.*
3636
import org.jetbrains.kotlin.cli.common.output.outputUtils.writeAll
37-
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
3837
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
3938
import org.jetbrains.kotlin.cli.jvm.config.*
4039
import org.jetbrains.kotlin.codegen.ClassBuilderFactories
@@ -52,15 +51,14 @@ import org.jetbrains.kotlin.load.kotlin.ModuleVisibilityManager
5251
import org.jetbrains.kotlin.modules.Module
5352
import org.jetbrains.kotlin.modules.TargetId
5453
import org.jetbrains.kotlin.name.FqName
55-
import org.jetbrains.kotlin.name.Name
56-
import org.jetbrains.kotlin.name.isSubpackageOf
5754
import org.jetbrains.kotlin.progress.ProgressIndicatorAndCompilationCanceledStatus
5855
import org.jetbrains.kotlin.psi.KtFile
5956
import org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM
6057
import org.jetbrains.kotlin.util.PerformanceCounter
6158
import org.jetbrains.kotlin.utils.KotlinPaths
6259
import org.jetbrains.kotlin.utils.PathUtil
6360
import org.jetbrains.kotlin.utils.newLinkedHashMapWithExpectedSize
61+
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
6462
import java.io.File
6563
import java.io.IOException
6664
import java.lang.reflect.InvocationTargetException
@@ -154,7 +152,7 @@ object KotlinToJVMBytecodeCompiler {
154152
}
155153

156154
try {
157-
for ((module, state) in outputs) {
155+
for ((_, state) in outputs) {
158156
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled()
159157
writeOutput(state.configuration, state.factory, null)
160158
}
@@ -445,23 +443,6 @@ object KotlinToJVMBytecodeCompiler {
445443
return generationState
446444
}
447445

448-
private fun checkKotlinPackageUsage(environment: KotlinCoreEnvironment, files: Collection<KtFile>): Boolean {
449-
if (environment.configuration.getBoolean(CLIConfigurationKeys.ALLOW_KOTLIN_PACKAGE)) {
450-
return true
451-
}
452-
val messageCollector = environment.configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
453-
val kotlinPackage = FqName.topLevel(Name.identifier("kotlin"))
454-
files.forEach {
455-
if (it.packageFqName.isSubpackageOf(kotlinPackage)) {
456-
messageCollector.report(CompilerMessageSeverity.ERROR,
457-
"Only the Kotlin standard library is allowed to use the 'kotlin' package",
458-
MessageUtil.psiElementToMessageLocation(it.packageDirective!!))
459-
return false
460-
}
461-
}
462-
return true
463-
}
464-
465446
private val KotlinCoreEnvironment.messageCollector: MessageCollector
466447
get() = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
467448

compiler/testData/cli/js/jsExtraHelp.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Usage: kotlinc-js <options> <source files>
22
where advanced options include:
33
-Xno-inline Disable method inlining
44
-Xrepeat <count> Repeat compilation (for performance analysis)
5+
-Xallow-kotlin-package Allow compiling code in package 'kotlin'
56
-Xplugin <path> Load plugins from the given classpath
67
-Xmulti-platform Enable experimental language support for multi-platform projects
78
-Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
$TESTDATA_DIR$/../kotlinPackage.kt
2+
-no-stdlib
3+
-output
4+
$TEMP_DIR$/out.js
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
compiler/testData/cli/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package
2+
package kotlin.mylibrary
3+
^
4+
COMPILATION_ERROR

compiler/testData/cli/jvm/extraHelp.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ where advanced options include:
55
-Xno-optimize Disable optimizations
66
-Xreport-perf Report detailed performance statistics
77
-Xmultifile-parts-inherit Compile multifile classes as a hierarchy of parts and facade
8-
-Xallow-kotlin-package Allow compiling code in package 'kotlin'
98
-Xskip-metadata-version-check Load classes with bad metadata version anyway (incl. pre-release classes)
109
-Xskip-runtime-version-check Allow Kotlin runtime libraries of incompatible versions in the classpath
1110
-Xdump-declarations-to <path> Path to JSON file to dump Java to Kotlin declaration mappings
@@ -15,6 +14,7 @@ where advanced options include:
1514
Load definitions of built-in declarations from module dependencies, instead of from the compiler
1615
-Xno-inline Disable method inlining
1716
-Xrepeat <count> Repeat compilation (for performance analysis)
17+
-Xallow-kotlin-package Allow compiling code in package 'kotlin'
1818
-Xplugin <path> Load plugins from the given classpath
1919
-Xmulti-platform Enable experimental language support for multi-platform projects
2020
-Xno-check-impl Do not check presence of 'impl' modifier in multi-platform projects
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
$TESTDATA_DIR$/kotlinPackage.kt
1+
$TESTDATA_DIR$/../kotlinPackage.kt
22
-d
33
$TEMP_DIR$
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
compiler/testData/cli/jvm/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package
1+
compiler/testData/cli/kotlinPackage.kt:1:1: error: only the Kotlin standard library is allowed to use the 'kotlin' package
22
package kotlin.mylibrary
33
^
44
COMPILATION_ERROR

compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,12 @@ public void testJsHelp() throws Exception {
443443
doJsTest(fileName);
444444
}
445445

446+
@TestMetadata("kotlinPackage.args")
447+
public void testKotlinPackage() throws Exception {
448+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/kotlinPackage.args");
449+
doJsTest(fileName);
450+
}
451+
446452
@TestMetadata("languageVersion.args")
447453
public void testLanguageVersion() throws Exception {
448454
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/js/languageVersion.args");

libraries/kotlin.test/js/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
<configuration>
3636
<args>
37-
<!--<arg>-Xallow-kotlin-package</arg>-->
37+
<arg>-Xallow-kotlin-package</arg>
3838
<arg>-Xmulti-platform</arg>
3939
</args>
4040
<moduleKind>umd</moduleKind>

libraries/tools/kotlin-js-tests/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
<groupId>org.jetbrains.kotlin</groupId>
8080
<artifactId>kotlin-maven-plugin</artifactId>
8181
<version>${project.version}</version>
82+
83+
<configuration>
84+
<args>
85+
<arg>-Xallow-kotlin-package</arg>
86+
</args>
87+
</configuration>
88+
8289
<executions>
8390
<execution>
8491
<id>js</id>

0 commit comments

Comments
 (0)