Use K2Compiler via JvmFirPipeline to support Kotlin v2#6338
Use K2Compiler via JvmFirPipeline to support Kotlin v2#6338Crustack wants to merge 2 commits intoopenrewrite:mainfrom
Conversation
rewrite-kotlin/src/main/java/org/openrewrite/kotlin/KotlinParser.java
Outdated
Show resolved
Hide resolved
26310be to
7abd4c2
Compare
|
@timtebeek @shanman190 @jkschneider PR is now ready for proper review FYI: I also tested adding support for properly compiling |
timtebeek
left a comment
There was a problem hiding this comment.
Thanks for your continued efforts here @Crustack ! For the uninitiated (me) could you explain a bit about the what the FIR processing is / adds, and any trade offs as compared to what we had before, and impact on say Kotlin v1 support? That would mean we're better able to review this work, as it seems promising but it's a bit hard to say without more context.
| // TODO: Compiler Source File args need to be an actual file path therefore copying synthetic file to temp folder | ||
| public static String tempFile(ExecutionContext ctx, Input source, AtomicInteger idx) { | ||
| String fileName; | ||
| if ("openRewriteFile.kt".equals(source.getPath().toString())) { | ||
| fileName = "openRewriteFile" + idx.getAndIncrement() + ".kt"; | ||
| } else if ("openRewriteFile.kts".equals(source.getPath().toString())) { | ||
| fileName = "openRewriteFile" + idx.getAndIncrement() + ".kts"; | ||
| } else { | ||
| fileName = source.getPath().toString(); | ||
| } | ||
| if(TEMP_SOURCE_DIR == null) { | ||
| try { | ||
| TEMP_SOURCE_DIR = Files.createTempDirectory("rewrite"); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| addJvmClasspathRoot(compilerConfiguration, PathUtil.getResourcePathForClass(AnnotationTarget.class)); | ||
| Path tempFile = TEMP_SOURCE_DIR.resolve(fileName); | ||
| try { | ||
| Files.createDirectories(tempFile.getParent()); | ||
| Files.copy(source.getSource(ctx), tempFile, StandardCopyOption.REPLACE_EXISTING); | ||
| } catch (FileAlreadyExistsException ignored) { | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| return tempFile.toAbsolutePath().toString(); | ||
| } |
There was a problem hiding this comment.
Calling this out for my colleagues as creating files on disk might be a little different when it concerns the CLI / workers.
There was a problem hiding this comment.
I was trying to use the WorkingDirectoryExecutionContextView's working directory to create these temp files (which is only necessary for synthetic temp files in Unit Tests), but executing:
WorkingDirectoryExecutionContextView.view(ctx).getWorkingDirectory()In Unit Tests throws NullPointerException, because the context has no CURRENT_CYCLE` message. Not sure how to proceed on this.
| is IrSimpleType -> { | ||
| when (type.getPrimitiveType()) { | ||
| PrimitiveType.INT -> JavaType.Primitive.Int | ||
| PrimitiveType.BOOLEAN -> JavaType.Primitive.Boolean | ||
| PrimitiveType.BYTE -> JavaType.Primitive.Byte | ||
| PrimitiveType.CHAR -> JavaType.Primitive.Char | ||
| PrimitiveType.DOUBLE -> JavaType.Primitive.Double | ||
| PrimitiveType.FLOAT -> JavaType.Primitive.Float | ||
| PrimitiveType.LONG -> JavaType.Primitive.Long | ||
| PrimitiveType.SHORT -> JavaType.Primitive.Short | ||
| else -> when { | ||
| type.isStringClassType() -> JavaType.Primitive.String | ||
| type.isNothing() || type.isNullableNothing() -> JavaType.Primitive.None | ||
| else -> JavaType.Primitive.None |
There was a problem hiding this comment.
Love that you've thought to do this as well; it's been a bit of a sore point to me that some Spring migration method patterns failed to match because they were using Java primitives instead of Kotlin primitives.
Not sure if you'd seen my earlier comment @Crustack , but it would help us better judge the scope & impact of the work done here, and how it compares to our support for Kotlin v1. Is v1 still supported with your changes for instance? |
Hi @timtebeek , sorry for not responding earlier.
|
|
Thanks again for your help here @Crustack ! Youl'll find some of the same changes you've suggested merged in this PR Hope that has you going with 2.2 support for your use cases there! |
|
To be honest I'm a bit disappointed that over the course of 3 months this PR has not been actually reviewed and then to see an internal PR being created separately and merged immediately. Thanks to @timtebeek for the initial feedback, and I'm glad to see K2 support being added Have a nice week. |
|
Hi @Crustack ; understandable that you're disappointed to not see this explicitly evaluated in depth or merge;d know that it's never easy to decide which of competing PRs to continue from, as we had in this case. You might have noticed that in this case Marius' original work in #5575 was reflected in the commits. Know that your explorations did help validate the work done in the PR we merged, and was appreciated. With larger efforts like these it's often hard to get alignment among all involved, and different folks have different styles of working through changes. Pulling work from different branches can than complicate matters when we're looking to get this delivered, but know that it's not our standard way of working. Should you have any subsequent work you're considering do please reach out, via Slack or here, and I'll do my best to see things through where I can. In this case I myself wasn't confident enough to continue your work, as also evidenced by my earlier comments. But not that support for v2 is here, it should be easier to extend should you find room for improvement. 🙏🏻 |
|
@Crustack based on your hints above I figured explore Kotlin script templates for .kts; figured you'd be interested
Thanks again! |
What's changed?
2.2.21JvmFirPipelineto use current K2 phased compiler API (with disabled fail on compile errors) to generateFirFilesKotlinIrTypeMapping,IrTreeVisitor,KotlinTypeIrSignatureBuilderto be compilable, but they are not used atmKotlinTypeMapping,KotlinTypeSignatureBuilder,PsiElementAssociationsfor new compiler dependency versions to be compilablerewrite-kotlinandrewrite-gradletests still work and adjustKotlinTypeMapping,KotlinTypeSignatureBuilder,PsiElementAssociationsif necessaryWhat's your motivation?
Anyone you would like to review specifically?
@shanman190
@jkschneider
Checklist