[Codenarc-user] Running rules which require compilerPhase > Phases.CONVERSION using Gradle's CodeNa
Brought to you by:
chrismair
From: Marcin E. <mar...@pr...> - 2017-03-03 14:13:10
|
Hi, I wrote a custom rule which requires SEMANTIC_ANALYSIS compiler phase and I'm trying to run it using Gradle. Unfortunately I'm getting compilation errors upon analysis telling me that compiler cannot load any of the classes imported by the analysed code, even though I checked and they are available in the codenarc configuration of my project which is added by Gradle's CodeNarc plugin and used to load CodeNarc classes, see: - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/CodeNarcPlugin.java#L72 - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/CodeNarcPlugin.java#L89 - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/CodeNarc.java#L104 - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/internal/CodeNarcInvoker.groovy#L45 I then dug around and realised that when CodeNarc compiles analysed code it uses the context class loader, see: - https://github.com/CodeNarc/CodeNarc/blob/v0.26.0/src/main/groovy/org/codenarc/source/AbstractSourceCode.groovy#L88 - https://github.com/groovy/groovy-core/blob/GROOVY_2_1_8/src/main/org/codehaus/groovy/control/CompilationUnit.java#L94 https://github.com/groovy/groovy-core/blob/GROOVY_2_1_8/src/main/org/codehaus/groovy/control/CompilationUnit.java#L136 - https://github.com/groovy/groovy-core/blob/GROOVY_2_1_8/src/main/org/codehaus/groovy/control/ProcessingUnit.java#L64 - https://github.com/groovy/groovy-core/blob/GROOVY_2_1_8/src/main/org/codehaus/groovy/control/ProcessingUnit.java#L103 And I finally realised that Gradle's BasicAntBuilder replaces the context class loader to only contain Ant classes which means that not even Groovy classes are available on the compilation classpath when CodeNarc compiles the analysed code: - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/code-quality/src/main/groovy/org/gradle/api/plugins/quality/internal/CodeNarcInvoker.groovy#L45 - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/core/src/main/java/org/gradle/api/internal/project/antbuilder/DefaultIsolatedAntBuilder.java#L151 - https://github.com/gradle/gradle/blob/v3.3.0/subprojects/core/src/main/java/org/gradle/api/internal/project/ant/BasicAntBuilder.java#L76 Given the above, here are my questions: 1. Has anybody ever managed to get their projects set up correctly and get violations for one of the built-in CodeNarc Enhanced Classpath Rules, e.g. CloneWithoutCloneable? If that's the case can they please explain how they achieved it? If one is to believe comments in Gradle's codebase (e.g. https://github.com/gradle/gradle/blob/v3.3.0/subprojects/core/src/main/java/org/gradle/api/internal/project/antbuilder/DefaultIsolatedAntBuilder.java#L140) then it looks like a similar problem to the one occurring when using Gradle would occur when using Ant. 2. Is there a reason for CodeNarc to use the context class loader instead of the class loader that loaded CodeNarc when compiling the analysed code? Given that it is a breaking change, would a PR changing that class loader to the one which loaded CodeNarc be a valid option? It is a breaking change but I don't see a reason why somebody would depend on current behaviour as it actually prevents usage of Enhanced Classpath Rules. Or should I look into changing Gradle's CodeNarc plugin to set the context class loader as per CodeNarc's expectations? Cheers, Marcin |