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

FormatKotlin task doesn't seem to format code in /kotlin sourceset. #115

Closed
paulwang007 opened this issue Aug 12, 2019 · 8 comments · Fixed by #124
Closed

FormatKotlin task doesn't seem to format code in /kotlin sourceset. #115

paulwang007 opened this issue Aug 12, 2019 · 8 comments · Fixed by #124

Comments

@paulwang007
Copy link

FormatKotlin task doesn't seem to work for code inside /src/main/kotlin/* sourcesets. But it works just fine for /src/main/java/*.

@jeremymailen
Copy link
Owner

Thanks for the report. Which kotlin plugin are you using -- jvm or android? Any customizations in the gradle build DSL I should note to reproduce it?

@paulwang007
Copy link
Author

Plugins:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 21
targetSdkVersion 29
versionCode versionCode
versionName version

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
useProguard false
}
debug {
useProguard false
}
}

compileOptions {
// Source code java level(Source code of this project).
sourceCompatibility '1.8'
// Target JVM level(JVM on Android devices).
targetCompatibility '1.8'
}

kotlinOptions {
jvmTarget = '1.8'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}

testOptions {
unitTests.returnDefaultValues = true
unitTests.includeAndroidResources = true
}

buildToolsVersion = '29.0.2'
}

@jeremymailen
Copy link
Owner

jeremymailen commented Aug 19, 2019

I think I see what's happening. kotlinter-gradle is resolving the srcDirs and assigning them to each lint and format task before the customizations are evaluated.

I think it's fixable if we use a gradle Provider or Closure to provide the final srcDirs as use time instead of configuration time. I'll try to get something out this week.

@jonasbark
Copy link

The tasks don't even appear for Kotlin Multiplatform projects - is it possible that that's the same issue?

@jeremymailen
Copy link
Owner

I don't believe so @jonasbark, kotlinter doesn't yet support automatically creating tasks for the multiplatform plugin, although you could create them manually.

@mateuszkwiecinski
Copy link
Contributor

mateuszkwiecinski commented Jan 13, 2020

I know the issue is quite outdated, but I faced exactly the same issue as the original one.

From my understanding calling:

sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
    test.java.srcDirs += 'src/test/kotlin'
}

could be easily replaced with:

android {
    sourceSets.whenObjectAdded { set ->
        def withKotlin = set.java.srcDirs.collect { it.path.replace("java", "kotlin") }
        set.java.setSrcDirs(set.java.srcDirs + withKotlin)
    }
}

(it also works for build variant specific sources like i.e. src/debug/kotlin or src/release/kotlin)
which happens early enough that kotlinter sets proper sources for lint and format tasks.

@jeremymailen
Copy link
Owner

Thanks for the tip @mateuszkwiecinski!

This issue is still very much alive and on my mind. I just need to find some time to refactor so that sources are lazily evaluated. PRs are also very welcome.

jeremymailen pushed a commit that referenced this issue Jan 24, 2020
* Unify project setup

* Add tests covering modified sourcesets

* Lazily evaluate taks sources, use lazy task configuration api

* Add few checks for extension evaluation

* Change function name to match existing naming convention

* Remove unused dependency. Fix test assertion.

* Restore kotlin plugin version. Update comments

* Fix invalid AGP extension usages

* Add test covering new sourceset
Rename variable
@jeremymailen
Copy link
Owner

Fixed in 2.3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants