-
Notifications
You must be signed in to change notification settings - Fork 503
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
Disable extension point org.jetbrains.kotlin.com.intellij.treeCopyHandler #2044
Conversation
…n.com.intellij.treeCopyHandler` in the kotlin embeddable compiler to investigate the impact whenever this extension point will not be supported in Kotlin 1.9
…placeWithText(String)` Up until Kotlin 1.8 the KotlinPsiFactory registered the treeCopyHandler extension point which is needed in order to use the `LeafElement.replaceWithText(String)`. With the Kotlin 1.9 it is no longer possible to register this extension point which results in exception. The exception does not occur when using method `LeafElement.rawReplaceWithText(String)`.
…ddChild(AstNode, AstNode)` Up until Kotlin 1.8 the KotlinPsiFactory registered the treeCopyHandler extension point which is needed in order to use the `PsiElement.addAfter(PsiLeaf, PsiLeaf)`. With the Kotlin 1.9 it is no longer possible to register this extension point which results in exception. The exception does not occur when using method `AstNode.addChild(AstNode, AstNode)`. Note that this method inserts the new node (first) argument *before* the second argument node and as of that is not a simple replacement of the `PsiElement.addAfter(PsiElement, PsiElement)`.
…addChild(AstNode, AstNode)` and `AstNode.removeChild(AstNode)` Up until Kotlin 1.8 the KotlinPsiFactory registered the treeCopyHandler extension point which is needed in order to use the `PsiElement.replace(PsiElement)`. With the Kotlin 1.9 it is no longer possible to register this extension point which results in exception.
I'm wondering if it would be possible to log instead of require the user to opt in to use a flag. I'm thinking of Gradle warnings in the form of: `You are using treeCopyHandler. This will be unsupported in Kotlin 1.9.0 ....' |
It is not about opt-in by end users. If kotlin 1.9 does not support the extension point then the rule provider is responsible for making the rule compatible with 1.9 without falling back on this extension point. The idea of this change is that rule providers can check the vulnerability for failures in their rule whenever we upgrade to kotlin 1.9. For normal users of ktlint this functionality has to be invisible. That is why the Ktlint cli parameter is hidden from the help command. |
This was previously used by ktlint to support autocorrect for some rules, but is being phased out due to pinterest/ktlint#1981. detekt should do the same. This has no impact on any core rules, or the formatting module (since ktlint no longer depends on this extension as of ktlint v0.50.0). This will impact third party rule sets, only if: - they support autocorrect, and - autocorrect support relies on the treeCopyHandler extension ktlint rules were updated to remove the dependency on treeCopyHandler, so affected third party detekt rule sets can do the same. This is all being caused by upstream changes in Kotlin. There's some very useful analysis in pinterest/ktlint#2044 that can be reviewed by anyone affected downstream by this change.
* Stop applying treeCopyHandler extension in analysis environment This was previously used by ktlint to support autocorrect for some rules, but is being phased out due to pinterest/ktlint#1981. detekt should do the same. This has no impact on any core rules, or the formatting module (since ktlint no longer depends on this extension as of ktlint v0.50.0). This will impact third party rule sets, only if: - they support autocorrect, and - autocorrect support relies on the treeCopyHandler extension ktlint rules were updated to remove the dependency on treeCopyHandler, so affected third party detekt rule sets can do the same. This is all being caused by upstream changes in Kotlin. There's some very useful analysis in pinterest/ktlint#2044 that can be reviewed by anyone affected downstream by this change. * Remove unused test DetektPomModel is now an object so cannot be instantiated multiple times, avoiding concurrency issues. The problematic code that was tested by this test has also been removed. * Remove unused dependency contester is not used anymore in any test or production code.
Due to the reasons exposed in pinterest/ktlint#2044, this is not trivial - we need to rewrite the fixes manually or remove them altogether. Not being able to rely on the embedded kotlin compiler plugin for this is less than ideal, I wanted to stay away from ASTNode as much as possible :( TODO: - [x] Fix spotless version - [ ] Fix ViewModelInjection autofix - [ ] Fix PreviewPublic autofix
* Update ktlint to 1.0.0 Due to the reasons exposed in pinterest/ktlint#2044, this is not trivial - we need to rewrite the fixes manually or remove them altogether. Not being able to rely on the embedded kotlin compiler plugin for this is less than ideal, I wanted to stay away from ASTNode as much as possible :( TODO: - [x] Fix spotless version - [x] Fix ViewModelInjection autofix - [x] Fix PreviewPublic autofix * Fix ComposePreviewPublic autofix * Use ASTNode for ComposeViewModelInjection autofix
…kt#6255) * Stop applying treeCopyHandler extension in analysis environment This was previously used by ktlint to support autocorrect for some rules, but is being phased out due to pinterest/ktlint#1981. detekt should do the same. This has no impact on any core rules, or the formatting module (since ktlint no longer depends on this extension as of ktlint v0.50.0). This will impact third party rule sets, only if: - they support autocorrect, and - autocorrect support relies on the treeCopyHandler extension ktlint rules were updated to remove the dependency on treeCopyHandler, so affected third party detekt rule sets can do the same. This is all being caused by upstream changes in Kotlin. There's some very useful analysis in pinterest/ktlint#2044 that can be reviewed by anyone affected downstream by this change. * Remove unused test DetektPomModel is now an object so cannot be instantiated multiple times, avoiding concurrency issues. The problematic code that was tested by this test has also been removed. * Remove unused dependency contester is not used anymore in any test or production code.
Description
In Kotlin 1.9 the extension points of the embedded kotlin compiler will change. Ktlint only uses the
org.jetbrains.kotlin.com.intellij.treeCopyHandler
extension point. This extension is not yet supported in 1.9, neither is it documented (#KT-58704). Without this extension point it might happen that your custom rules will throw exceptions during runtime. See #1981.In Ktlint, 7 out of 77 rules needed small and sometimes bigger changes to become independent of the extension point
org.jetbrains.kotlin.com.intellij.treeCopyHandler
. The impact on your custom rules may vary dependent on the way the autocorrect has been implemented. When manipulatingASTNode
s there seems to be no impact. When, manipulatingPsiElement
s, some functions consistently result in a runtime exception.Based on the refactoring of the rules as provided by
ktlint-ruleset-standard
in Ktlint0.49.x
the suggested refactoring is as follows:LeafElement.replaceWithText(String)
withLeafElement.rawReplaceWithText(String)
.PsiElement.addAfter(PsiElement, PsiElement)
withAstNode.addChild(AstNode, AstNode)
. Note that this method inserts the new node (first) argument before the second argument node and as of that is not a simple replacement of thePsiElement.addAfter(PsiElement, PsiElement)
.PsiElement.replace(PsiElement)
with a sequence ofAstNode.addChild(AstNode, AstNode)
andAstNode.removeChild(AstNode)
.Be aware that your custom rules might use other functions which also throw exceptions when the extension point
org.jetbrains.kotlin.com.intellij.treeCopyHandler
is no longer supported.In order to help you to analyse and fix the problems with your custom rules, ktlint temporarily supports to disable the extension point
org.jetbrains.kotlin.com.intellij.treeCopyHandler
using a flag. This flag is available in the Ktlint CLI and in theKtlintRuleEngine
. By default, the extension point is enabled like it was in previous versions of ktlint.At least you should analyse the problems by running your test suits by running ktlint and disabling the extension point. Next you can start with fixing and releasing the updated rules. All rules in this version of ktlint have already been refactored and are not dependent on the extension point anymore. In Ktlint CLI the flag is to be activated with parameter
--disable-kotlin-extension-point
. API Consumers that use theKtlintRuleEngine
directly, have to set propertyenableKotlinCompilerExtensionPoint
tofalse
.At this point in time, it is not yet decided what the next steps will be. Ktlint might drop the support of the extension points entirely. Or, if the extension point
org.jetbrains.kotlin.com.intellij.treeCopyHandler
is fully supported at the time that ktlint will be based on kotlin 1.9 it might be kept. In either case, the flag will be dropped in a next version of ktlint.Checklist
CHANGELOG.md
is updatedIn case of adding a new rule: