Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect more cases when Scala target is already configured in parameters #24059

Merged
merged 1 commit into from Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -36,13 +36,20 @@
public class ScalaCompileOptionsConfigurer {

private static final int FALLBACK_JVM_TARGET = 8;

/**
* Support for these flags in different minor releases of Scala varies,
* but we need to detect as many variants as possible to avoid overriding the target or release.
*/
private static final List<String> TARGET_DEFINING_PARAMETERS = Arrays.asList(
// Scala 2
"-target:", "--target:",
"-target", "--target",
// Scala 2 and 3
"-release:", "--release:",
"-release", "--release",
// Scala 3
"-Xtarget:", "-java-output-version:", "-Xunchecked-java-output-version:"
"-java-output-version", "--java-output-version",
"-Xunchecked-java-output-version", "--Xunchecked-java-output-version",
"-Xtarget", "--Xtarget"
);

private static final VersionNumber PLAIN_TARGET_FORMAT_SINCE_VERSION = VersionNumber.parse("2.13.1");
Expand Down Expand Up @@ -76,7 +83,8 @@ public static void configure(ScalaCompileOptions scalaCompileOptions, JavaInstal
}

private static boolean hasTargetDefiningParameter(List<String> additionalParameters) {
return additionalParameters.stream().anyMatch(s -> TARGET_DEFINING_PARAMETERS.stream().anyMatch(s::startsWith));
return additionalParameters.stream()
.anyMatch(s -> TARGET_DEFINING_PARAMETERS.stream().anyMatch(param -> param.equals(s) || s.startsWith(param + ":")));
}

/**
Expand Down
Expand Up @@ -120,21 +120,38 @@ class ScalaCompileOptionsConfigurerTest extends Specification {
]
}

def 'does not configure target jvm if scala compiler already has a target via #targetFlag flag'() {
def 'does not configure target jvm if scala compiler already has a configured target via #targetFlagName flag'() {
given:
ScalaCompileOptions scalaCompileOptions = TestUtil.newInstance(ScalaCompileOptions)
scalaCompileOptions.additionalParameters = [targetFlag]
scalaCompileOptions.additionalParameters = targetFlagParts.toList()
Set<File> classpath = [new File("scala-library-2.13.1.jar")]

when:
ScalaCompileOptionsConfigurer.configure(scalaCompileOptions, createToolchain(17, false), classpath)

then:
scalaCompileOptions.additionalParameters.find { it == targetFlag }
scalaCompileOptions.additionalParameters.find { it == targetFlagParts[0] }
scalaCompileOptions.additionalParameters.find { it.contains("17") } == null

where:
targetFlag << ['-target:8', '--target:8', '-release:8', '--release:8', '-Xtarget:8', '-java-output-version:8', '-Xunchecked-java-output-version:8']
targetFlagParts | _
['-target:8'] | _
['--target:8'] | _
['-target', '8'] | _
['-release:8'] | _
['--release:8'] | _
['-release', '8'] | _
['--release', '8'] | _
['-java-output-version:8'] | _
['-java-output-version', '8'] | _
['-Xtarget:8'] | _
['-Xtarget', '8'] | _
['--Xtarget:8'] | _
['--Xtarget', '8'] | _
['-Xunchecked-java-output-version:8'] | _
['-Xunchecked-java-output-version', '8'] | _

targetFlagName = targetFlagParts[0]
}

private JavaToolchain createToolchain(int javaVersion, boolean isFallback) {
Expand Down