Skip to content

Commit a8629b3

Browse files
committed
Do not fail on unknown -X flags
For better compatibility if we add experimental stuff in 1.0.X compilers
1 parent 37d612d commit a8629b3

File tree

8 files changed

+61
-16
lines changed

8 files changed

+61
-16
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public abstract class CommonCompilerArguments {
5858

5959
public List<String> freeArgs = new SmartList<String>();
6060

61+
public List<String> unknownExtraFlags = new SmartList<String>();
62+
6163
@NotNull
6264
public String executableScriptFileName() {
6365
return "kotlinc";

compiler/cli/src/org/jetbrains/kotlin/cli/common/CLICompiler.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import com.intellij.openapi.util.Disposer;
2323
import com.intellij.openapi.util.SystemInfo;
2424
import com.sampullara.cli.Args;
25+
import kotlin.Pair;
26+
import kotlin.collections.CollectionsKt;
27+
import kotlin.jvm.functions.Function1;
2528
import org.fusesource.jansi.AnsiConsole;
2629
import org.jetbrains.annotations.NotNull;
2730
import org.jetbrains.annotations.Nullable;
@@ -87,7 +90,7 @@ public ExitCode execFullPathsInMessages(@NotNull PrintStream errStream, @NotNull
8790
private A parseArguments(@NotNull PrintStream errStream, @NotNull MessageRenderer messageRenderer, @NotNull String[] args) {
8891
try {
8992
A arguments = createArguments();
90-
arguments.freeArgs = Args.parse(arguments, args);
93+
parseArguments(args, arguments);
9194
return arguments;
9295
}
9396
catch (IllegalArgumentException e) {
@@ -104,6 +107,26 @@ private A parseArguments(@NotNull PrintStream errStream, @NotNull MessageRendere
104107
return null;
105108
}
106109

110+
@SuppressWarnings("WeakerAccess") // Used in maven (see KotlinCompileMojoBase.java)
111+
public void parseArguments(@NotNull String[] args, @NotNull A arguments) {
112+
Pair<List<String>, List<String>> unparsedArgs =
113+
CollectionsKt.partition(Args.parse(arguments, args, false), new Function1<String, Boolean>() {
114+
@Override
115+
public Boolean invoke(String s) {
116+
return s.startsWith("-X");
117+
}
118+
});
119+
120+
arguments.unknownExtraFlags = unparsedArgs.getFirst();
121+
arguments.freeArgs = unparsedArgs.getSecond();
122+
123+
for (String argument : arguments.freeArgs) {
124+
if (argument.startsWith("-")) {
125+
throw new IllegalArgumentException("Invalid argument: " + argument);
126+
}
127+
}
128+
}
129+
107130
/**
108131
* Allow derived classes to add additional command line arguments
109132
*/
@@ -169,6 +192,8 @@ public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull Servic
169192
messageCollector = new FilteringMessageCollector(messageCollector, Predicates.equalTo(CompilerMessageSeverity.WARNING));
170193
}
171194

195+
reportUnknownExtraFlags(messageCollector, arguments);
196+
172197
GroupingMessageCollector groupingCollector = new GroupingMessageCollector(messageCollector);
173198
try {
174199
ExitCode exitCode = OK;
@@ -226,6 +251,16 @@ public ExitCode exec(@NotNull MessageCollector messageCollector, @NotNull Servic
226251
}
227252
}
228253

254+
private void reportUnknownExtraFlags(@NotNull MessageCollector collector, @NotNull A arguments) {
255+
for (String flag : arguments.unknownExtraFlags) {
256+
collector.report(
257+
CompilerMessageSeverity.WARNING,
258+
"Flag is not supported by this version of the compiler: " + flag,
259+
CompilerMessageLocation.NO_LOCATION
260+
);
261+
}
262+
}
263+
229264
@NotNull
230265
protected abstract ExitCode doExecute(
231266
@NotNull A arguments,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-Xabcdefghijklm
2+
$TESTDATA_DIR$/simple.kt
3+
-Xnopqrstuvwxyz
4+
-d
5+
$TEMP_DIR$
6+
-XXxxxxxxxxxxxx
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
warning: flag is not supported by this version of the compiler: -Xabcdefghijklm
2+
warning: flag is not supported by this version of the compiler: -Xnopqrstuvwxyz
3+
warning: flag is not supported by this version of the compiler: -XXxxxxxxxxxxxx
4+
OK

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ public void testSyntheticAccessorSignatureClash() throws Exception {
241241
doJvmTest(fileName);
242242
}
243243

244+
@TestMetadata("unknownExtraFlags.args")
245+
public void testUnknownExtraFlags() throws Exception {
246+
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/unknownExtraFlags.args");
247+
doJvmTest(fileName);
248+
}
249+
244250
@TestMetadata("version.args")
245251
public void testVersion() throws Exception {
246252
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/cli/jvm/version.args");

libraries/tools/kotlin-maven-plugin-test/src/it/test-extraArguments/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<arg>-Xno-optimize</arg>
7575
<arg>-Xno-call-assertions</arg>
7676
<arg>-Xno-param-assertions</arg>
77+
<arg>-Xunknown-extra-argument-abcdefghijklmnopqrstuvwxyz</arg>
7778
</args>
7879
</configuration>
7980
</plugin>

libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/K2JVMCompileMojo.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.jetbrains.kotlin.maven;
1818

19-
import com.intellij.util.ArrayUtil;
20-
import com.sampullara.cli.Args;
2119
import kotlin.collections.CollectionsKt;
2220
import kotlin.jvm.functions.Function1;
2321
import org.apache.maven.artifact.Artifact;
@@ -100,13 +98,6 @@ protected void configureSpecificCompilerArguments(@NotNull K2JVMCompilerArgument
10098
arguments.moduleName = moduleName;
10199
getLog().info("Module name is " + moduleName);
102100

103-
try {
104-
Args.parse(arguments, ArrayUtil.toStringArray(args));
105-
}
106-
catch (IllegalArgumentException e) {
107-
throw new MojoExecutionException(e.getMessage());
108-
}
109-
110101
if (arguments.noOptimize) {
111102
getLog().info("Optimization is turned off");
112103
}

libraries/tools/kotlin-maven-plugin/src/main/java/org/jetbrains/kotlin/maven/KotlinCompileMojoBase.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.intellij.openapi.util.io.FileUtil;
2020
import com.intellij.util.ArrayUtil;
2121
import com.intellij.util.Processor;
22-
import com.sampullara.cli.Args;
2322
import org.apache.maven.plugin.AbstractMojo;
2423
import org.apache.maven.plugin.MojoExecutionException;
2524
import org.apache.maven.plugin.MojoFailureException;
@@ -115,9 +114,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
115114
}
116115

117116
A arguments = createCompilerArguments();
118-
configureCompilerArguments(arguments);
119-
120117
CLICompiler<A> compiler = createCompiler();
118+
119+
configureCompilerArguments(arguments, compiler);
121120
printCompilerArgumentsIfDebugEnabled(arguments, compiler);
122121

123122
MessageCollector messageCollector = new MessageCollector() {
@@ -224,7 +223,7 @@ protected ExitCode executeCompiler(
224223
*/
225224
protected abstract void configureSpecificCompilerArguments(@NotNull A arguments) throws MojoExecutionException;
226225

227-
private void configureCompilerArguments(@NotNull A arguments) throws MojoExecutionException {
226+
private void configureCompilerArguments(@NotNull A arguments, @NotNull CLICompiler<A> compiler) throws MojoExecutionException {
228227
if (getLog().isDebugEnabled()) {
229228
arguments.verbose = true;
230229
}
@@ -245,18 +244,19 @@ private void configureCompilerArguments(@NotNull A arguments) throws MojoExecuti
245244

246245
arguments.suppressWarnings = nowarn;
247246

248-
arguments.freeArgs.addAll(sources);
249247
getLog().info("Compiling Kotlin sources from " + sources);
250248

251249
configureSpecificCompilerArguments(arguments);
252250

253251
try {
254-
Args.parse(arguments, ArrayUtil.toStringArray(args));
252+
compiler.parseArguments(ArrayUtil.toStringArray(args), arguments);
255253
}
256254
catch (IllegalArgumentException e) {
257255
throw new MojoExecutionException(e.getMessage());
258256
}
259257

258+
arguments.freeArgs.addAll(sources);
259+
260260
if (arguments.noInline) {
261261
getLog().info("Method inlining is turned off");
262262
}

0 commit comments

Comments
 (0)