Set up Kotlin linting step in ci with ktlint (#143478)

Adds a kotlin linting step to the linux analyze check, using a version of [ktlint](https://github.com/pinterest/ktlint) hosted on [CIPD](https://chrome-infra-packages.appspot.com/p/flutter/ktlint/linux-amd64/+/version_1_1_1).

Configured to disallow trailing commas, because of (and now acting as a test for) https://github.com/flutter/flutter/pull/145718. Because of this configuration the PR also removes some trailing commas in some kotlin files.

Also checks in a baseline file.
This commit is contained in:
Gray Mackall 2024-04-03 14:21:00 -07:00 committed by GitHub
parent 64af93b707
commit 29ed214e7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 69 additions and 9 deletions

View file

@ -298,6 +298,10 @@ targets:
timeout: 60 timeout: 60
properties: properties:
shard: analyze shard: analyze
dependencies: >-
[
{"dependency": "ktlint", "version": "version_1_1_1"}
]
tags: > tags: >
["framework","hostonly","shard","linux"] ["framework","hostonly","shard","linux"]

View file

@ -23,7 +23,7 @@ class MainActivity : FragmentActivity() {
root.layoutParams = root.layoutParams =
LinearLayout.LayoutParams( LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT
) )
root.orientation = LinearLayout.VERTICAL root.orientation = LinearLayout.VERTICAL
root.weightSum = numberOfFlutters.toFloat() root.weightSum = numberOfFlutters.toFloat()
@ -36,7 +36,7 @@ class MainActivity : FragmentActivity() {
val dartEntrypoint = val dartEntrypoint =
DartExecutor.DartEntrypoint( DartExecutor.DartEntrypoint(
FlutterInjector.instance().flutterLoader().findAppBundlePath(), FlutterInjector.instance().flutterLoader().findAppBundlePath(),
"main", "main"
) )
val engines = val engines =
generateSequence(0) { it + 1 } generateSequence(0) { it + 1 }
@ -51,7 +51,7 @@ class MainActivity : FragmentActivity() {
LinearLayout.LayoutParams( LinearLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT,
1.0f, 1.0f
) )
val engine = engines[i] val engine = engines[i]
FlutterEngineCache.getInstance().put(i.toString(), engine) FlutterEngineCache.getInstance().put(i.toString(), engine)
@ -61,7 +61,7 @@ class MainActivity : FragmentActivity() {
.beginTransaction() .beginTransaction()
.add( .add(
12345 + i, 12345 + i,
flutterFragment, flutterFragment
) )
.commit() .commit()
} }

View file

@ -43,7 +43,7 @@ class MainActivity : FlutterActivity() {
flutterEngine.dartExecutor, flutterEngine.dartExecutor,
"dev.flutter.echo.background.standard", "dev.flutter.echo.background.standard",
StandardMessageCodec.INSTANCE, StandardMessageCodec.INSTANCE,
taskQueue, taskQueue
) )
backgroundStandard.setMessageHandler { message, reply -> reply.reply(message) } backgroundStandard.setMessageHandler { message, reply -> reply.reply(message) }
super.configureFlutterEngine(flutterEngine) super.configureFlutterEngine(flutterEngine)

View file

@ -153,6 +153,9 @@ Future<void> run(List<String> arguments) async {
printProgress('Taboo words...'); printProgress('Taboo words...');
await verifyTabooDocumentation(flutterRoot); await verifyTabooDocumentation(flutterRoot);
printProgress('Lint Kotlin files...');
await lintKotlinFiles(flutterRoot);
// Ensure that all package dependencies are in sync. // Ensure that all package dependencies are in sync.
printProgress('Package dependencies...'); printProgress('Package dependencies...');
await runCommand(flutter, <String>['update-packages', '--verify-only'], await runCommand(flutter, <String>['update-packages', '--verify-only'],
@ -1973,6 +1976,22 @@ Future<void> verifyTabooDocumentation(String workingDirectory, { int minimumMatc
} }
} }
Future<void> lintKotlinFiles(String workingDirectory) async {
const String baselineRelativePath = 'dev/bots/test/analyze-test-input/ktlint-baseline.xml';
const String editorConfigRelativePath = 'dev/bots/test/analyze-test-input/.editorconfig';
final EvalResult lintResult = await _evalCommand('ktlint',
<String>['--baseline=$flutterRoot/$baselineRelativePath', '--editorconfig=$flutterRoot/$editorConfigRelativePath'],
workingDirectory: workingDirectory);
if (lintResult.exitCode != 0) {
final String errorMessage = 'Found lint violations in Kotlin files:\n ${lintResult.stdout}\n\n'
'To reproduce this lint locally:\n'
'1. Identify the CIPD version tag used to resolve this particular version of ktlint (check the dependencies section of this shard in the ci.yaml). \n'
'2. Download that version from https://chrome-infra-packages.appspot.com/p/flutter/ktlint/linux-amd64/+/<version_tag>\n'
'3. From the repository root, run `<path_to_ktlint>/ktlint --editorconfig=$editorConfigRelativePath --baseline=$baselineRelativePath`';
foundError(<String>[errorMessage]);
}
}
const List<String> _kIgnoreList = <String>[ const List<String> _kIgnoreList = <String>[
'Runner.rc.tmpl', 'Runner.rc.tmpl',
'flutter_window.cpp', 'flutter_window.cpp',

View file

@ -0,0 +1,4 @@
[*.{kt,kts}]
# Disable trailing commas to allow compatibility with Kotlin versions less than 1.4.
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="dev/a11y_assessments/android/app/src/main/kotlin/com/example/a11y_assessments/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/benchmarks/platform_channels_benchmarks/android/app/src/main/kotlin/com/example/platform_channels_benchmarks/MainActivity.kt">
<error line="5" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/abstract_method_smoke_test/android/app/src/main/kotlin/com/example/abstract_method_smoke_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/android_embedding_v2_smoke_test/android/app/src/main/kotlin/com/example/android_embedding_v2_smoke_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/deferred_components_test/android/app/src/main/kotlin/io/flutter/integration/deferred_components_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/non_nullable/android/app/src/main/kotlin/com/example/non_nullable/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/integration_tests/spell_check/android/app/src/main/kotlin/com/example/sc_int_test/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/manual_tests/android/app/src/main/kotlin/dev/flutter/manual_tests/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="dev/tracing_tests/android/app/src/main/kotlin/com/example/tracing_tests/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
<file name="examples/api/android/app/src/main/kotlin/dev/flutter/flutter_api_samples/MainActivity.kt">
<error line="1" column="9" source="standard:package-name" />
</file>
</baseline>

View file

@ -30,7 +30,7 @@ class MainActivity : FlutterActivity() {
} }
override fun configureFlutterEngine( override fun configureFlutterEngine(
@NonNull flutterEngine: FlutterEngine, @NonNull flutterEngine: FlutterEngine
) { ) {
GeneratedPluginRegistrant.registerWith(flutterEngine) GeneratedPluginRegistrant.registerWith(flutterEngine)
@ -43,11 +43,11 @@ class MainActivity : FlutterActivity() {
override fun create( override fun create(
context: Context?, context: Context?,
viewId: Int, viewId: Int,
args: Any?, args: Any?
): PlatformView { ): PlatformView {
return SimplePlatformView(this@MainActivity) return SimplePlatformView(this@MainActivity)
} }
}, }
) )
// Triggers the Android keyboard, which causes the resize of the Flutter view. // Triggers the Android keyboard, which causes the resize of the Flutter view.

View file

@ -7,7 +7,7 @@ import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity : FlutterActivity() { class MainActivity : FlutterActivity() {
override fun configureFlutterEngine( override fun configureFlutterEngine(
@NonNull flutterEngine: FlutterEngine, @NonNull flutterEngine: FlutterEngine
) { ) {
GeneratedPluginRegistrant.registerWith(flutterEngine) GeneratedPluginRegistrant.registerWith(flutterEngine)
} }