Skip to content

Commit 412c871

Browse files
committed
Add ktfmt to devshell and flake checks
1 parent 7d50844 commit 412c871

33 files changed

Lines changed: 1039 additions & 741 deletions

flake.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(gradle.override ({ java = jdk; }))
3333
jdk
3434
jq
35+
ktfmt
3536
(maven.override ({ jdk_headless = jdk; }))
3637
nixfmt
3738
nodejs
@@ -48,6 +49,31 @@
4849
${pkgs.actionlint}/bin/actionlint ${./.github/workflows}/*.yml
4950
touch $out
5051
'';
52+
ktfmt =
53+
pkgs.runCommand "check-ktfmt"
54+
{
55+
nativeBuildInputs = [
56+
pkgs.findutils
57+
pkgs.git
58+
pkgs.gnugrep
59+
];
60+
}
61+
''
62+
cp -r ${./.}/. .
63+
chmod -R u+w .
64+
git init -q
65+
git add -A
66+
# Exclude minimized Kotlin snapshots: the input fixtures are
67+
# coupled to generated SCIP goldens with exact line/column
68+
# annotations, so their layout must not be reformatted here.
69+
git ls-files -z -- '*.kt' '*.kts' \
70+
| grep -zv '^scip-kotlinc/minimized/' \
71+
| xargs -0 -r ${pkgs.ktfmt}/bin/ktfmt \
72+
--kotlinlang-style \
73+
--dry-run \
74+
--set-exit-if-changed
75+
touch $out
76+
'';
5177
nixfmt = pkgs.runCommand "check-nixfmt" { } ''
5278
${pkgs.nixfmt}/bin/nixfmt --check ${./flake.nix}
5379
touch $out

scip-java/src/main/kotlin/com/sourcegraph/scip_java/BuildInfo.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@ import java.util.Properties
55
/**
66
* Build metadata for the scip-java CLI.
77
*
8-
* Replaces the previously sbt-generated `BuildInfo.java`. The static
9-
* [javacModuleOptions] live here directly; the build-time [version] is injected
10-
* by sbt into the `scip-java.properties` classpath resource (see the
11-
* `resourceGenerators` for the `cli` project in build.sbt).
8+
* Replaces the previously sbt-generated `BuildInfo.java`. The static [javacModuleOptions] live here
9+
* directly; the build-time [version] is injected by sbt into the `scip-java.properties` classpath
10+
* resource (see the `resourceGenerators` for the `cli` project in build.sbt).
1211
*/
1312
object BuildInfo {
1413

1514
/**
16-
* `--add-exports` flags required to access internal javac APIs from the
17-
* SCIP compiler plugin. Java 11+ is the supported baseline.
15+
* `--add-exports` flags required to access internal javac APIs from the SCIP compiler plugin.
16+
* Java 11+ is the supported baseline.
1817
*
19-
* Kept in sync with `javacModuleOptions` in build.sbt, which applies the
20-
* same flags when compiling the plugin and the test fixtures.
18+
* Kept in sync with `javacModuleOptions` in build.sbt, which applies the same flags when
19+
* compiling the plugin and the test fixtures.
2120
*/
2221
val javacModuleOptions: List<String> =
2322
listOf(
@@ -29,16 +28,13 @@ object BuildInfo {
2928
)
3029

3130
/**
32-
* The scip-java release version. Read from the `scip-java.properties`
33-
* resource that sbt generates into the jar. Falls back to a placeholder
34-
* when running without the generated resource on the classpath.
31+
* The scip-java release version. Read from the `scip-java.properties` resource that sbt
32+
* generates into the jar. Falls back to a placeholder when running without the generated
33+
* resource on the classpath.
3534
*/
3635
val version: String by lazy {
3736
val props = Properties()
38-
BuildInfo::class
39-
.java
40-
.getResourceAsStream("/scip-java.properties")
41-
?.use { props.load(it) }
37+
BuildInfo::class.java.getResourceAsStream("/scip-java.properties")?.use { props.load(it) }
4238
props.getProperty("version") ?: "0.0.0-SNAPSHOT"
4339
}
4440
}

scip-java/src/main/kotlin/com/sourcegraph/scip_java/CliEnvironment.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import java.nio.file.Paths
77
/**
88
* Captures the per-invocation environment of a scip-java CLI run.
99
*
10-
* Tests inject a custom environment to redirect stdout/stderr into a
11-
* byte buffer, point the working directory at a temporary fixture
12-
* directory, and so on.
10+
* Tests inject a custom environment to redirect stdout/stderr into a byte buffer, point the working
11+
* directory at a temporary fixture directory, and so on.
1312
*/
1413
data class CliEnvironment(
1514
val workingDirectory: Path = Paths.get("").toAbsolutePath(),

scip-java/src/main/kotlin/com/sourcegraph/scip_java/CliReporter.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import java.nio.file.NoSuchFileException
55
import java.util.concurrent.atomic.AtomicInteger
66

77
/**
8-
* Console reporter: `info` goes to stdout, `warning`/`error` to stderr.
9-
* Doubles as the [ScipAggregatorReporter] consumed by the aggregator.
8+
* Console reporter: `info` goes to stdout, `warning`/`error` to stderr. Doubles as the
9+
* [ScipAggregatorReporter] consumed by the aggregator.
1010
*/
1111
class CliReporter(private val env: CliEnvironment) : ScipAggregatorReporter() {
1212
private val errorCount = AtomicInteger()
@@ -29,8 +29,7 @@ class CliReporter(private val env: CliEnvironment) : ScipAggregatorReporter() {
2929
}
3030

3131
/** Dropped to avoid leaking noise into snapshot tests. */
32-
@Suppress("UNUSED_PARAMETER")
33-
fun debug(message: String) {}
32+
@Suppress("UNUSED_PARAMETER") fun debug(message: String) {}
3433

3534
override fun error(e: Throwable) {
3635
if (e is NoSuchFileException) {

scip-java/src/main/kotlin/com/sourcegraph/scip_java/Embedded.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,26 @@ object Embedded {
2727
Files.createDirectories(bin)
2828
Files.write(
2929
java,
30-
("#!/usr/bin/env bash\n" +
31-
"java \"\$@\"\n").toByteArray(StandardCharsets.UTF_8),
30+
("#!/usr/bin/env bash\n" + "java \"\$@\"\n").toByteArray(StandardCharsets.UTF_8),
3231
)
3332
val newJavacopts = tmp.resolve("javac_newarguments")
3433
// --add-exports flags required to access internal javac APIs from our
3534
// SCIP plugin. Always set; Java 11+ is the supported baseline.
3635
val javacModuleOptions = BuildInfo.javacModuleOptions.joinToString(" ")
3736
val injectScipArguments =
3837
listOf(
39-
"java",
40-
"-Dscip.errorpath=$errorpath",
41-
"-Dscip.pluginpath=$pluginpath",
42-
"-Dscip.sourceroot=$sourceroot",
43-
"-Dscip.targetroot=$targetroot",
44-
"-Dscip.output=\$NEW_JAVAC_OPTS",
45-
"-Dscip.old-output=$javacopts",
46-
"-classpath $pluginpath",
47-
"com.sourcegraph.scip_javac.InjectScipOptions",
48-
"\"\$@\"",
49-
).joinToString(" ")
38+
"java",
39+
"-Dscip.errorpath=$errorpath",
40+
"-Dscip.pluginpath=$pluginpath",
41+
"-Dscip.sourceroot=$sourceroot",
42+
"-Dscip.targetroot=$targetroot",
43+
"-Dscip.output=\$NEW_JAVAC_OPTS",
44+
"-Dscip.old-output=$javacopts",
45+
"-classpath $pluginpath",
46+
"com.sourcegraph.scip_javac.InjectScipOptions",
47+
"\"\$@\"",
48+
)
49+
.joinToString(" ")
5050
val script = buildString {
5151
append("#!/usr/bin/env bash\n")
5252
append("set -eu\n")
@@ -71,11 +71,10 @@ object Embedded {
7171
}
7272

7373
/**
74-
* The custom javac wrapper reports errors to a specific file if unexpected
75-
* errors happen. The javac wrapper gets invoked by builds tools like
76-
* Gradle/Maven, which hide the actual errors from the script because they
77-
* assume the standard output is from javac. This file is used a side-channel
78-
* to avoid relying on the error reporting from Gradle/Maven.
74+
* The custom javac wrapper reports errors to a specific file if unexpected errors happen. The
75+
* javac wrapper gets invoked by builds tools like Gradle/Maven, which hide the actual errors
76+
* from the script because they assume the standard output is from javac. This file is used a
77+
* side-channel to avoid relying on the error reporting from Gradle/Maven.
7978
*/
8079
fun reportUnexpectedJavacErrors(reporter: CliReporter, tmp: Path): ProcessResult? {
8180
val errorpath = javacErrorpath(tmp)

scip-java/src/main/kotlin/com/sourcegraph/scip_java/ScipJava.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package com.sourcegraph.scip_java
33
import java.io.PrintStream
44

55
/**
6-
* Public entry point for the scip-java CLI. The single [app] instance is
7-
* shared across test suites and reset between invocations.
6+
* Public entry point for the scip-java CLI. The single [app] instance is shared across test suites
7+
* and reset between invocations.
88
*/
99
object ScipJava {
1010

11-
@JvmField
12-
val app: ScipJavaApp = ScipJavaApp()
11+
@JvmField val app: ScipJavaApp = ScipJavaApp()
1312

1413
@JvmStatic
1514
fun main(args: Array<String>) {
@@ -19,9 +18,8 @@ object ScipJava {
1918
fun printHelp(out: PrintStream) {
2019
out.println("```text")
2120
out.println("$ scip-java index --help")
22-
val replacement = ScipJavaApp().apply {
23-
env = env.withStandardOutput(out).withStandardError(out)
24-
}
21+
val replacement =
22+
ScipJavaApp().apply { env = env.withStandardOutput(out).withStandardError(out) }
2523
replacement.run(listOf("index", "--help"))
2624
out.println("```")
2725
}

scip-java/src/main/kotlin/com/sourcegraph/scip_java/ScipJavaApp.kt

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,17 @@ import com.github.ajalt.clikt.core.subcommands
1313
import com.github.ajalt.clikt.parameters.options.versionOption
1414
import com.sourcegraph.scip_java.buildtools.ProcessResult
1515
import com.sourcegraph.scip_java.buildtools.ProcessRunner
16-
import com.sourcegraph.scip_java.commands.IndexCommand
1716
import com.sourcegraph.scip_java.commands.AggregateCommand
17+
import com.sourcegraph.scip_java.commands.IndexCommand
1818
import com.sourcegraph.scip_java.commands.SnapshotCommand
1919
import java.nio.file.Paths
2020

2121
/**
22-
* Stateful, mutable container for the scip-java CLI runtime. Tests inject
23-
* a fresh environment (with redirected stdout/stderr, a temp working
24-
* directory, etc.) before invoking [run].
22+
* Stateful, mutable container for the scip-java CLI runtime. Tests inject a fresh environment (with
23+
* redirected stdout/stderr, a temp working directory, etc.) before invoking [run].
2524
*
26-
* Each invocation of [run] builds a fresh root clikt command tree so option
27-
* state from a previous run never leaks into the next one.
25+
* Each invocation of [run] builds a fresh root clikt command tree so option state from a previous
26+
* run never leaks into the next one.
2827
*/
2928
class ScipJavaApp {
3029

@@ -47,13 +46,10 @@ class ScipJavaApp {
4746
fun error(message: String) = reporter.error(message)
4847

4948
/**
50-
* Spawn an external process using the current working directory.
51-
* Stdout and stderr are streamed to the env's PrintStreams line-by-line.
49+
* Spawn an external process using the current working directory. Stdout and stderr are streamed
50+
* to the env's PrintStreams line-by-line.
5251
*/
53-
fun runProcess(
54-
command: List<String>,
55-
env: Map<String, String> = emptyMap(),
56-
): ProcessResult {
52+
fun runProcess(command: List<String>, env: Map<String, String> = emptyMap()): ProcessResult {
5753
val syntax = command.joinToString(" ") { if (' ' in it) "'$it'" else it }
5854
this.env.standardOutput.println("$ $syntax")
5955
return ProcessRunner.run(
@@ -99,10 +95,10 @@ class ScipJavaApp {
9995
}
10096

10197
/**
102-
* The Bazel aspect passes nested options as `--aggregate.<flag>` on
103-
* `scip-java index` (e.g. `--aggregate.allow-empty-index`). clikt forbids
104-
* `.` in option names, so we rewrite the dotted prefix to `-` to match the
105-
* options declared on [IndexCommand]. Tokens after `--` are left untouched.
98+
* The Bazel aspect passes nested options as `--aggregate.<flag>` on `scip-java index` (e.g.
99+
* `--aggregate.allow-empty-index`). clikt forbids `.` in option names, so we rewrite the dotted
100+
* prefix to `-` to match the options declared on [IndexCommand]. Tokens after `--` are left
101+
* untouched.
106102
*/
107103
private fun rewriteNestedOptions(args: List<String>): List<String> {
108104
var sawDoubleDash = false
@@ -113,19 +109,17 @@ class ScipJavaApp {
113109
sawDoubleDash = true
114110
arg
115111
}
116-
arg.startsWith("--aggregate.") ->
117-
"--aggregate-" + arg.removePrefix("--aggregate.")
112+
arg.startsWith("--aggregate.") -> "--aggregate-" + arg.removePrefix("--aggregate.")
118113
else -> arg
119114
}
120115
}
121116
}
122117

123118
/**
124-
* `--cwd` may appear in any position (including after the subcommand name),
125-
* unlike a regular clikt parent option, so we extract it here before handing
126-
* the remaining arguments to clikt and apply it to the working directory.
127-
* Tokens after `--` pass through untouched so a trailing build command can
128-
* legitimately contain `--cwd`.
119+
* `--cwd` may appear in any position (including after the subcommand name), unlike a regular
120+
* clikt parent option, so we extract it here before handing the remaining arguments to clikt
121+
* and apply it to the working directory. Tokens after `--` pass through untouched so a trailing
122+
* build command can legitimately contain `--cwd`.
129123
*/
130124
private fun applyGlobalCwd(args: List<String>): List<String> {
131125
val result = ArrayList<String>(args.size)
@@ -163,14 +157,12 @@ class ScipJavaApp {
163157
}
164158

165159
/**
166-
* Root clikt command that plumbs the parent [ScipJavaApp] into the
167-
* clikt context so subcommands can pick it up via
168-
* `currentContext.findObject`.
160+
* Root clikt command that plumbs the parent [ScipJavaApp] into the clikt context so subcommands
161+
* can pick it up via `currentContext.findObject`.
169162
*/
170163
private class RootCommand(val app: ScipJavaApp) : CliktCommand(name = "scip-java") {
171164

172-
override fun help(context: Context) =
173-
"scip-java: index Java/Kotlin codebases into SCIP."
165+
override fun help(context: Context) = "scip-java: index Java/Kotlin codebases into SCIP."
174166

175167
private val sharedApp by findOrSetObject { app }
176168

0 commit comments

Comments
 (0)