Skip to content

Kotlin: Set up ktfmt in OSS #52064

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mateoguzmana
Copy link
Collaborator

Summary:

This PR adds the basic ktfmt setup in OSS to lint Kotlin files before they're imported into the Meta codebase, making collaboration with external contributors smoother for Android related PRs.

I tried to put together certain rules that mimic the current code style and it seems to work well as I get no errors for properly formatted files but this still might need some input to have the correct configuration.

Added two scripts to the main package.json:

  • To check the files format you can run: yarn lint-kotlin-check
  • To apply formatting fixes, run: yarn lint-kotlin

Changelog:

[INTERNAL] - Kotlin: Set up ktfmt in OSS

Test Plan:

Unformat any random Kotlin file inside ReactAndroid and then run:

yarn lint-kotlin-check
yarn lint-kotlin

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 16, 2025
@@ -18,6 +18,8 @@
"featureflags": "yarn --cwd packages/react-native featureflags",
"lint-ci": "./.github/workflow-scripts/analyze_code.sh && yarn shellcheck",
"lint-java": "node ./scripts/lint-java.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW We can nuke this and also remove the script

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tackling this separately in #52092

package.json Outdated
Comment on lines 21 to 22
"lint-kotlin-check": "./gradlew :packages:react-native:ReactAndroid:ktfmtCheck",
"lint-kotlin": "./gradlew :packages:react-native:ReactAndroid:ktfmtFormat",
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably want a top level task inside the build.gradle.kts file that invokes several other tasks.

So you will invoke ./gradlew ktfmtCheck and it will format both ReactAndroid and the Gradle plugin, etc.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved it to the top level build.gradle.kts and configured it to also check other projects other than ReactAndroid. However, I'm not sure whether my new approach is the correct one and whether there is something else I should include.

Tested mostly with the packages react-native/ReactAndroid and gradle-plugin

}
}

// Disable the problematic ktfmt script tasks due to symbolic link issues
Copy link
Contributor

Choose a reason for hiding this comment

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

why this?

Copy link
Collaborator Author

@mateoguzmana mateoguzmana Jun 17, 2025

Choose a reason for hiding this comment

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

If I don't disable those, I start getting this when running the check/format tasks:

* What went wrong:
Execution failed for task ':packages:react-native:ReactAndroid:ktfmtCheckScripts'.
> Couldn't follow symbolic link '/Users/mateoguzman/Documents/react-native/packages/react-native/ReactAndroid/hermes-engine/build/hermes/API/hermes/hermes.framework/hermes'.

@mateoguzmana mateoguzmana mentioned this pull request Jun 17, 2025
@mateoguzmana mateoguzmana requested a review from cortinico June 17, 2025 21:16
@mateoguzmana mateoguzmana marked this pull request as ready for review June 17, 2025 21:16
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jun 17, 2025
facebook-github-bot pushed a commit that referenced this pull request Jun 18, 2025
Summary:
Following up from #52064 (comment), this PR removes lint-java and its related files.

The codebase is moving entirely to Kotlin and a Kotlin linter is being setup as well, the usage of the Java linter will become unnecessary.

## Changelog:

[INTERNAL] - Remove lint-java

Pull Request resolved: #52092

Test Plan: Relying on CI here to be green.

Reviewed By: cortinico

Differential Revision: D76880712

Pulled By: sbuggay

fbshipit-source-id: 2736772e7347f435b17d007e0322e1afc2fb2d7b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants