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

Kotlin parser failures on project without Kotlin sources #658

Closed
Bananeweizen opened this issue Nov 16, 2023 · 2 comments · Fixed by #660
Closed

Kotlin parser failures on project without Kotlin sources #658

Bananeweizen opened this issue Nov 16, 2023 · 2 comments · Fixed by #660
Labels
bug Something isn't working

Comments

@Bananeweizen
Copy link
Contributor

The Maven plugin tries to run the Kotlin parser, and that fails. The project does not contain Kotlin sources, and there is no Kotlin tooling installed. I'm not sure if this is a Maven plugin issue or one for the project maintaining the Kotlin parser.

What version of OpenRewrite are you using?

I am using

  • Maven plugin 5.11.0

How are you running OpenRewrite?

I have quite some dependencies configured, because I'd like to use a lot of receipes. However, the issue occurs even with a single trivial Java recipe as the active recipe

				<plugin>
					<groupId>org.openrewrite.maven</groupId>
					<artifactId>rewrite-maven-plugin</artifactId>
					<version>${rewrite-maven-plugin-version}</version>
					<configuration>
						<activeRecipes>
							<recipe>org.openrewrite.java.RemoveObjectsIsNull</recipe>
						</activeRecipes>
					</configuration>
					<dependencies>
						<dependency>
							<groupId>org.openrewrite.recipe</groupId>
							<artifactId>rewrite-java-security</artifactId>
							<version>${rewrite-java-security-version}</version>
						</dependency>
						<dependency>
							<groupId>org.openrewrite.recipe</groupId>
							<artifactId>rewrite-java-security</artifactId>
							<version>${rewrite-java-security-version}</version>
						</dependency>
						<dependency>
							<groupId>org.openrewrite.recipe</groupId>
							<artifactId>rewrite-logging-frameworks</artifactId>
							<version>${rewrite-logging-frameworks-version}</version>
						</dependency>
						<dependency>
							<groupId>org.openrewrite.recipe</groupId>
							<artifactId>rewrite-static-analysis</artifactId>
							<version>${rewrite-static-analysis-version}</version>
						</dependency>
						<dependency>
							<groupId>org.openrewrite.recipe</groupId>
							<artifactId>rewrite-testing-frameworks</artifactId>
							<version>${rewrite-testing-frameworks-version}</version>
						</dependency>
					</dependencies>
				</plugin>

What is the full stack trace of any errors you encountered?

WARN: Error while reading zip file: C:\Users\myuserid\AppData\Local\Temp\file not yet available14437915253118885463.tmp: java.lang.IndexOutOfBoundsException
java.lang.IndexOutOfBoundsException
	at java.base/java.nio.Buffer.checkIndex(Buffer.java:749)
	at java.base/java.nio.DirectByteBuffer.getShort(DirectByteBuffer.java:599)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.ZipImplementationKt.getUnsignedShort(ZipImplementation.kt:165)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.ZipImplementationKt.parseCentralDirectoryRecordsNumberAndOffset(ZipImplementation.kt:134)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.ZipImplementationKt.parseCentralDirectory(ZipImplementation.kt:67)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.FastJarHandler.<init>(FastJarHandler.kt:28)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.FastJarFileSystem$myHandlers$1.fun(FastJarFileSystem.kt:24)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.FastJarFileSystem$myHandlers$1.fun(FastJarFileSystem.kt:22)
	at org.jetbrains.kotlin.com.intellij.util.containers.ConcurrentFactoryMap$2.create(ConcurrentFactoryMap.java:174)
	at org.jetbrains.kotlin.com.intellij.util.containers.ConcurrentFactoryMap.get(ConcurrentFactoryMap.java:40)
	at org.jetbrains.kotlin.cli.jvm.compiler.jarfs.FastJarFileSystem.findFileByPath(FastJarFileSystem.kt:51)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.findJarRoot(KotlinCoreEnvironment.kt:399)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.contentRootToVirtualFile(KotlinCoreEnvironment.kt:377)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.access$contentRootToVirtualFile(KotlinCoreEnvironment.kt:107)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$2.invoke(KotlinCoreEnvironment.kt:224)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$2.invoke(KotlinCoreEnvironment.kt:107)
	at org.jetbrains.kotlin.cli.jvm.compiler.ClasspathRootsResolver.convertClasspathRoots(ClasspathRootsResolver.kt:76)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:233)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.<init>(KotlinCoreEnvironment.kt:107)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(KotlinCoreEnvironment.kt:442)
	at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment.createForProduction(KotlinCoreEnvironment.kt)
	at org.openrewrite.kotlin.KotlinParser.parse(KotlinParser.java:362)
	at org.openrewrite.kotlin.KotlinParser.parseInputs(KotlinParser.java:162)
	at org.openrewrite.Parser.parse(Parser.java:59)
	at org.openrewrite.maven.MavenMojoProjectParser.lambda$processMainSources$5(MavenMojoProjectParser.java:332)

There are some things to note here:

  • There is no Kotlin code or anything Kotlin related in the sources being scanned.
  • There is no Kotlin tooling, compiler etc. on this machine.

I have no idea why OpenRewrite tries to use its Kotlin parser at all here.

Are you interested in contributing a fix to OpenRewrite?

Since I have no idea what's going on, I'm not sure that's possible.

@Bananeweizen Bananeweizen added the bug Something isn't working label Nov 16, 2023
@knutwannheden
Copy link
Contributor

@Bananeweizen Thanks for the report. This is really odd. The Kotlin parser should only be trying to parse Kotlin (.kt) sources. Could you maybe connect with the debugger to see what it is picking up here or can you share a reproducer?

@Bananeweizen
Copy link
Contributor Author

I haven't been able to debug, but after reading the code I would conclude that the maven plugin always invokes both Java and Kotlin parser, even if it has zero matching files of the respective language. There is no conditional code before or after

Stream<? extends SourceFile> kcus = Stream.of(kotlinParserBuilder)
.map(KotlinParser.Builder::build)
.flatMap(parser -> parser.parse(mainKotlinSources, baseDir, ctx));
IMO. I believe it should be possible to wrap both the Java and Kotlin parser calls with conditional code that avoids execution if the respective file list is empty.

Bananeweizen added a commit to Bananeweizen/rewrite-maven-plugin that referenced this issue Nov 16, 2023
With empty lists of main or test, Java or Kotlin files avoid invoking
the parsers completely. That's more performant and avoids any bad side
effects during initialization of the parsers.

Fixes openrewrite#658.
timtebeek pushed a commit that referenced this issue Nov 22, 2023
With empty lists of main or test, Java or Kotlin files avoid invoking
the parsers completely. That's more performant and avoids any bad side
effects during initialization of the parsers.

Fixes #658.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants