Skip to content

Enable strict null-safety for Kotlin projects #591

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

Closed
mkheck opened this issue Feb 5, 2018 · 7 comments
Closed

Enable strict null-safety for Kotlin projects #591

mkheck opened this issue Feb 5, 2018 · 7 comments
Assignees
Milestone

Comments

@mkheck
Copy link

mkheck commented Feb 5, 2018

In order to leverage our prior & continuing efforts to incorporate jsr305-style annotations throughout Spring Framework 5 & various Spring projects (e.g. Spring Boot 2) to indicate the nullability/non-nullability of APIs for full Kotlin interoperability/support, we should provide freeCompilerArgs directives, as indicated below, in build files created by the Initializr:

sourceCompatibility = 1.8
compileKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = ["-Xjsr305=strict"]
    }
}
compileTestKotlin {
    kotlinOptions {
        jvmTarget = "1.8"
        freeCompilerArgs = ["-Xjsr305=strict"]
    }
}

Currently, the freeCompilerArgs directives aren't being produced/placed in build.gradle files produced for Spring Boot 2.0 Kotlin applications, which by default results in no nullability information for Spring libraries being available to the Kotlin compiler.

If you need any other information, happy to provide it. Thanks very much!

@mkheck
Copy link
Author

mkheck commented Feb 5, 2018

We should also include the corresponding directives in the generated pom.xml file for Maven users, of course.

Per the following referenced discussions, here are the appropriate Maven pom additions:

<args>
    <arg>-Xjsr305=strict</arg>
</args>

References:
https://youtrack.jetbrains.com/issue/KT-21592
https://kotlinlang.org/docs/reference/using-maven.html

I'll submit a PR with the noted additions for review.

@mkheck mkheck changed the title Add freeCompilerArgs = ["-Xjsr305=strict"] to Kotlin compiler options in generated build.gradle file Add jsr305-style support to Kotlin compiler options in generated build files Feb 5, 2018
@snicoll
Copy link
Contributor

snicoll commented Feb 7, 2018

I have no opinion on the feature as I am not using it myself (but I've pinged @sdeleuze for feedback).

On a more pragmatic front, the release notes announcing this feature states:

Note that the strict mode JSR-305 support in the compiler is an experimental feature.

I am not keen to enable an experimental feature by default. We've had that discussion before and I think that's for the best. Let's wait to hear from @sdeleuze

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Feb 7, 2018
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2018

While it is indeed defined as experimental, null-safety has been refined and documented in 1.1.60, has seen no change/regression in Kotlin 1.2, but more importantly all the feedbacks I got during conferences shows that this is considered as the number 1 feature of Spring Kotlin support. Notice that Spring Framework leverages JSR 305 annotations, but also Spring Data and Reactor. Without this option enabled, you can have NPE when using Spring Data, defeating Kotlin null-safety key feature.

Given my experience on various Kotlin projects, I would vote for enabling this feature most likely wanted by almost all Spring + Kotlin developers and leveraging this Spring Kotlin support key feature.

@wilkinsona
Copy link
Contributor

FWIW, I'm in favour of enabling this. If that turns out to be a mistake we can always revert.

@mkheck
Copy link
Author

mkheck commented Feb 7, 2018 via email

@snicoll snicoll self-assigned this Feb 7, 2018
@snicoll snicoll added type: enhancement and removed status: waiting-for-feedback We need additional information before we can continue labels Feb 7, 2018
@snicoll snicoll added this to the 0.5.0 milestone Feb 7, 2018
@snicoll snicoll closed this as completed in e58b7bf Feb 7, 2018
@snicoll snicoll changed the title Add jsr305-style support to Kotlin compiler options in generated build files Enable strict null-safety for Kotlin projects Feb 7, 2018
@mkheck
Copy link
Author

mkheck commented Feb 7, 2018

OK, so I guess you just did it yourself. Thanks.

@snicoll
Copy link
Contributor

snicoll commented Feb 7, 2018

I had started to investigate if we shouldn't externalize the generation of such options rather than hard-coding them and realized half-way that it's fine as is right now. The job was done at that point so I wasn't going to revert it.

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

No branches or pull requests

4 participants