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

Adds KotlinPoet integration to Room Processing #137

Closed

Conversation

gfreivasc
Copy link
Contributor

@gfreivasc gfreivasc commented Mar 9, 2021

Proposed Changes

Adds write() method that accepts KotlinPoet's an instance of
FileSpec. Also adds KotlinPoet as an API dependency and an extension
function similar to that of JavaPoet.

Also verifies if processing backend is set for generating kotlin code
and fails for KotlinPoet if it isn't.

Testing

Test: ./gradlew test connectedCheck without benchmarks

Fixes

Fixes: b/182195680

@googlebot
Copy link
Collaborator

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

1 similar comment
@google-cla
Copy link

google-cla bot commented Mar 9, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@gfreivasc
Copy link
Contributor Author

@googlebot I signed it!

@googlebot
Copy link
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

1 similar comment
@google-cla
Copy link

google-cla bot commented Mar 9, 2021

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Collaborator

@yigit yigit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gfreivasc
Copy link
Contributor Author

Sure, working on it

@gfreivasc
Copy link
Contributor Author

@yigit Is there anyway I can get the current generation path? I've stumbled upon two situations:

  • Java environment will not generate the Kotlin file, I could either ignore that scenario since it targets Java codebases (?).
  • Sources.kotlin() API isn't as simple as it does not take the qualified name, and internally the matcher expects the full path of the generated code instead of just the relative path, as seen on Source.kt#L132.

I am investigating what can be done about the latter but not sure how to handle the java scenario yet.

@yigit
Copy link
Collaborator

yigit commented Mar 10, 2021

@yigit Is there anyway I can get the current generation path? I've stumbled upon two situations:

  • Java environment will not generate the Kotlin file, I could either ignore that scenario since it targets Java codebases (?).
    We would need to solve it eventually but does not have to be in this CL
  • Sources.kotlin() API isn't as simple as it does not take the qualified name, and internally the matcher expects the full path of the generated code instead of just the relative path, as seen on Source.kt#L132.

I am investigating what can be done about the latter but not sure how to handle the java scenario yet.

We can maybe ignore kotlin code verification for now and instead throw an exception that it is not supported if an assertion receives a kotlin file w/ the java processor (to avoid false positives).
XTestInvocation has an isKsp property that can be used in the test side to avoid calling that method if desired.

I'm also not against changing Source.kotlin to require a qName but that would be inconsistent with how kotlin works so I'd rather don't do that for now. We can resolve it later.

@gfreivasc
Copy link
Contributor Author

gfreivasc commented Mar 10, 2021

We can maybe ignore kotlin code verification for now and instead throw an exception that it is not supported if an assertion receives a kotlin file w/ the java processor (to avoid false positives).
XTestInvocation has an isKsp property that can be used in the test side to avoid calling that method if desired.

I've managed to run the tests by verifying if TestRunner's toString() is "java" and skipping the test if positive. This allows to validate for kapt which is expected to work.

I'm also not against changing Source.kotlin to require a qName but that would be inconsistent with how kotlin works so I'd rather don't do that for now. We can resolve it later.

I agree, and modified it to take the relative path instead of the absolute. All tests are passing (the CI looks to have broken but they're passing locally).

Copy link
Collaborator

@yigit yigit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, just small nits in the test.

you can ignore presubmit, i think we broke playground again, it will work fine in AOSP if it is passing locally (just don't rebase until we fix playground:) )

@Test
fun successfulGeneratedKotlinCodeMatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (runTest.toString() == "java") return
if (runTest.toString() == "java") throw AssumptionViolatedException()

so that test is reported as skippped instead of succeeded.

@Test
fun missingGeneratedKotlinCode_mismatch() {
// java environment will not generate kotlin files
if (runTest.toString() == "java") return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here. eventually we should probaby change TestRunner instead of equals check but not important for this PR.
it also probably makes sense to move this to a helper function assumeCanGenerateKotlinCode that throws assumption violation.

@yigit
Copy link
Collaborator

yigit commented Mar 10, 2021

lgtm, just small nits in the test.

you can ignore presubmit, i think we broke playground again, it will work fine in AOSP if it is passing locally (just don't rebase until we fix playground:) )

it should be fixed now: https://android-review.googlesource.com/c/platform/frameworks/support/+/1624289

@gfreivasc
Copy link
Contributor Author

gfreivasc commented Mar 10, 2021

lgtm, just small nits in the test.
you can ignore presubmit, i think we broke playground again, it will work fine in AOSP if it is passing locally (just don't rebase until we fix playground:) )

it should be fixed now: https://android-review.googlesource.com/c/platform/frameworks/support/+/1624289

Ok, I will try rebasing now. I also applied precondition checks for running KotlinPoet on javaAP without kapt. I can remove those if you don't think that's applicable. Will push the changes once tests pass.

Edit: playground still broken to me after rebase

Copy link
Collaborator

@yigit yigit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks for adding the kapt parameter check as well.

if CL is fine, it should pass in AOSP which is what matters anyways. github might block it but if it is the only one, we'll skip.

Approving so that it gets mirrored into AOSP

@yigit
Copy link
Collaborator

yigit commented Mar 11, 2021

the aosp build failed due to some unused imports.

Detected these failing tasks: [':room:room-compiler-processing:ktlint']
> Task :room:room-compiler-processing:ktlint
$SUPPORT/room/compiler-processing/src/main/java/androidx/room/compiler/processing/javac/JavacFiler.kt:22:1: Unused import

> Task :room:room-compiler-processing:ktlint
"checkstyle" report written to $OUT_DIR/androidx/room/room-compiler-processing/build/reports/ktlint/ktlint-checkstyle-report.xml

FAILURE: Build failed with an exception.

You can validate these locally as described here:
https://github.com/androidx/androidx/blob/androidx-main/CONTRIBUTING.md#validating-changes-locally

Looks like the lint changes are still breaking playground. Locally, you can workaround it by commenting the source sets in ../lint-checks/build.gradle.

Adds `write()` method that accepts KotlinPoet's an instance of
`FileSpec`. Also adds KotlinPoet as an API dependency and an extension
function similar to that of JavaPoet.

Test: ./gradlew test connectedCheck without benchmarks
Fixes: b/182195680
Modifies `KotlinSource` to take relative path instead of absolute path
for testing, making it somewhat similar to how `JavaSource` takes
qualified name.
Also adds tests for the changes and marks tests which skip backends it
does not target with `AssumptionViolatedException`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants