Skip to content

Commit b9a310d

Browse files
committed
fix(jvm): read the sources from disk on every Gradle build
On macOS, Gradle's file-system watching can deliver the events for files cstudy writes just before a build late. Gradle then treats its cached view as current and reports the compile task up to date over the changed sources, so a verify run fails with ClassNotFoundException and a test run can build stale code. Every Gradle command cstudy runs, the doctor warm-up included, now passes --no-watch-fs.
1 parent a071fc0 commit b9a310d

7 files changed

Lines changed: 52 additions & 12 deletions

File tree

‎cli/src/coding_study/adapters/languages/jvm/runner.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@
6464
"""JDWP prints this to standard output once it listens for the IDE."""
6565
JDWP_REFUSED_MARKER = "transport error 202: connect failed"
6666
"""JDWP prints this to standard error, then the JVM exits, when no IDE listens to connect to."""
67-
GRADLE_FLAGS = ("-q", "--console=plain")
67+
GRADLE_FLAGS = ("-q", "--console=plain", "--no-watch-fs")
68+
"""Without file-system watching, every build reads the sources from disk: on macOS the watch
69+
events for files written just before a build can arrive late, and Gradle then reports the compile
70+
task up to date over the changed sources."""
6871
CLASSPATH_TASK = "csClasspath"
6972
ONLY_PROPERTY = "cs.only"
7073
RELEASE_PROPERTY = "cs.release"

‎cli/src/coding_study/application/doctor.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def __str__(self) -> str:
261261
"jvm",
262262
(
263263
"./gradlew",
264+
"--no-watch-fs",
264265
":java:csClasspath",
265266
":kotlin:csClasspath",
266267
f"-Pcs.only={HANDSHAKE_META.snake_slug}",
@@ -287,6 +288,7 @@ def __str__(self) -> str:
287288
f"{PROJECT_WARMUPS}/{language}",
288289
"-q",
289290
"--console=plain",
291+
"--no-watch-fs",
290292
"csClasspath",
291293
),
292294
runs_on_jdk=True,

‎cli/tests/adapters/languages/jvm/test_jvm_projects.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from tests.adapters.languages.jvm.fake_gradle import LIBRARY, ProjectGradle, jvm_context
3636
from tests.helpers import REPOSITORY_ROOT
3737

38-
GRADLE_FLAGS = ["-q", "--console=plain", "--offline"]
38+
GRADLE_FLAGS = ["-q", "--console=plain", "--no-watch-fs", "--offline"]
3939
OFFLINE_FAILURE = """\
4040
* What went wrong:
4141
Could not resolve all dependencies for configuration ':testRuntimeClasspath'.
@@ -83,6 +83,12 @@ def test_a_suite_builds_the_folder_offline_with_its_test_classes(tmp_path, folde
8383
assert gradle.builds == [("-p", str(folder), *GRADLE_FLAGS, "csClasspath")]
8484

8585

86+
def test_a_project_build_rereads_the_sources_cstudy_just_wrote(tmp_path, folder):
87+
gradle = ProjectGradle()
88+
prepare(runner_on(tmp_path, gradle), ProjectSuite(folder))
89+
assert all("--no-watch-fs" in build for build in gradle.builds)
90+
91+
8692
def test_grading_builds_the_main_classes_only(tmp_path, folder):
8793
"""A test file that does not compile never blocks a submission."""
8894
gradle = ProjectGradle()

‎cli/tests/adapters/languages/jvm/test_jvm_runner.py‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,17 @@ def compile_failure(*lines: str) -> GradleOutcome:
6969
def test_user_code_builds_only_the_problem_package(root):
7070
gradle = FakeGradle(root)
7171
prepare(JvmRunner(jvm_context(root), Language.JAVA, gradle), UserCode(TWO_SUM))
72-
assert gradle.builds == [("-q", "--console=plain", ":java:csClasspath", "-Pcs.only=two_sum")]
72+
assert gradle.builds == [
73+
("-q", "--console=plain", "--no-watch-fs", ":java:csClasspath", "-Pcs.only=two_sum")
74+
]
75+
76+
77+
def test_every_build_rereads_the_sources_cstudy_just_wrote(root):
78+
"""Gradle's file-system watching can miss writes made a moment before a build on macOS, and
79+
then reports the compile task up to date over changed sources."""
80+
gradle = FakeGradle(root)
81+
prepare(JvmRunner(jvm_context(root), Language.JAVA, gradle), UserCode(TWO_SUM))
82+
assert all("--no-watch-fs" in build for build in gradle.builds)
7383

7484

7585
def test_kotlin_user_code_builds_the_kotlin_project(root):
@@ -78,7 +88,7 @@ def test_kotlin_user_code_builds_the_kotlin_project(root):
7888
JvmRunner(jvm_context(root), Language.KOTLIN, gradle),
7989
UserCode(TWO_SUM),
8090
)
81-
assert gradle.builds[0][2] == ":kotlin:csClasspath"
91+
assert ":kotlin:csClasspath" in gradle.builds[0]
8292

8393

8494
def test_launch_runs_main_on_the_built_classpath(root):
@@ -452,7 +462,9 @@ def test_a_normal_launch_has_no_agent(root):
452462

453463
def test_a_debug_run_is_built_like_any_other(root):
454464
_, gradle = debugging(root, Language.JAVA, DebugSession(Language.JAVA, 5005))
455-
assert gradle.builds == [("-q", "--console=plain", ":java:csClasspath", "-Pcs.only=two_sum")]
465+
assert gradle.builds == [
466+
("-q", "--console=plain", "--no-watch-fs", ":java:csClasspath", "-Pcs.only=two_sum")
467+
]
456468

457469

458470
def test_a_port_in_use_is_reported_before_building(root):

‎cli/tests/application/test_doctor.py‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ def test_fix_runs_the_setup_command_of_every_workspace_project():
11171117
(
11181118
"./gradlew",
11191119
f"-Dorg.gradle.java.home={JDK_HOME}",
1120+
"--no-watch-fs",
11201121
":java:csClasspath",
11211122
":kotlin:csClasspath",
11221123
"-Pcs.only=cs_doctor_handshake",
@@ -1133,7 +1134,15 @@ def test_the_jvm_warm_up_builds_a_project_folder_per_language(language):
11331134
needs, so a warm-up project of each language caches them."""
11341135
commands = {step.command for step in JVM_PROJECT_WARMUPS}
11351136
warmup = f"../.cs/runtime/runners/jvm/project-warmup/{language}"
1136-
assert ("./gradlew", "-p", warmup, "-q", "--console=plain", "csClasspath") in commands
1137+
assert (
1138+
"./gradlew",
1139+
"-p",
1140+
warmup,
1141+
"-q",
1142+
"--console=plain",
1143+
"--no-watch-fs",
1144+
"csClasspath",
1145+
) in commands
11371146

11381147

11391148
def test_the_jvm_warm_up_resolves_the_runtime_classpath():
@@ -1212,6 +1221,13 @@ def test_fix_warms_up_gradle_on_any_jdk_it_can_build_with(selected):
12121221

12131222

12141223
WARM_UP_STEPS = (JVM_FIX, *JVM_PROJECT_WARMUPS)
1224+
1225+
1226+
@pytest.mark.parametrize("step", WARM_UP_STEPS, ids=lambda step: step.command[-1])
1227+
def test_the_warm_up_rereads_the_runtime_it_just_refreshed(step):
1228+
assert "--no-watch-fs" in step.command
1229+
1230+
12151231
JDK_26 = SelectedJdk("26.0.2", 26, JdkSource.JAVA_HOME)
12161232
HOME_26 = Path("/opt/jdks/26")
12171233

@@ -1375,7 +1391,8 @@ def test_fix_announces_each_command_before_running_it():
13751391
lines: list[str] = []
13761392
fix(workspace_with(paths={"jvm"}), notify=lines.append)
13771393
assert lines == [
1378-
"running ./gradlew :java:csClasspath :kotlin:csClasspath -Pcs.only=cs_doctor_handshake "
1394+
"running ./gradlew --no-watch-fs :java:csClasspath :kotlin:csClasspath "
1395+
"-Pcs.only=cs_doctor_handshake "
13791396
"(in jvm)",
13801397
*(f"running {step}" for step in JVM_PROJECT_WARMUPS),
13811398
]

‎docs/specs/cli.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ A JDK above the supported range is not supported yet. `cstudy` runs on it only t
150150
cstudy doctor [--fix | --json]
151151
```
152152

153-
Checks Python 3.13, uv, Node 24 or newer, pnpm, the [selected JDK](#the-selected-jdk) (21 to 26), Homebrew Gradle, Go 1.24 or newer, `rustc` 1.80 or newer and `cargo` (the Go and Rust toolchains are optional, so these three rows WARN, never FAIL, with a link to the [toolchains guide](../guides/toolchains.md)), Delve (`dlv`, found as [debug mode](runners.md#debug-mode) finds it; the row shows its version, and a release other than the tested 1.27.2 passes with `(cstudy is tested with 1.27.2)`) and Rust's debug server (`debugserver` on macOS, `lldb-server` on Linux; the row shows its path), which only `cstudy test --debug` needs, so they WARN too, with the install step and the guide (``needed for --debug with --lang go, run `go install github.com/go-delve/delve/cmd/dlv@v1.27.2`: <guide>#go``), the config file, the content root, the workspace (core template files FAIL when missing, the optional `go.work`, `go/` and `rust/` files WARN; [workspace checks](installation-and-workspaces.md#cstudy-doctor-in-a-workspace)), the progress database (its schema against the one this `cstudy` migrates to: WARN when older, FAIL when newer), the Gradle wrapper, the workspace venv, `debugpy` and `pytest` in it, the `.venv` links of the Python project folders, the `go.work`, `replace` lines and module paths of the Go project folders, the `.cargo/config.toml` and rust-analyzer link of the Rust ones, the libraries that Java and Kotlin project folders build with offline ([workspace checks](installation-and-workspaces.md#cstudy-doctor-in-a-workspace)), `node_modules`, and that the runner of each core language (`python`, `typescript`, `java`, `kotlin`; the Go and Rust runners are not handshaken, since that would compile them) answers `hello`: the runner is prepared for a problem with no files (`cs-doctor-handshake`, so the JVM build compiles no practice code) and started with no jobs. Outside a workspace only the toolchains, the config and the content are checked. `--fix` first writes every missing workspace template file (never overwriting one), refreshes the runner runtime and migrates an older `progress.db` to the latest schema (one line, `migrated progress.db from schema 4 to 5`), then runs, in each workspace project that exists, `uv sync` in `python` (followed, when the venv cannot import pytest, by `uv add --dev --offline "pytest>=9.1"`, retried without `--offline` when it fails, so a workspace created before the template listed pytest gets it; when both fail the line is `FAIL` with `fix: once online, run: uv add --dev "pytest>=9.1" --directory python`), `pnpm install` in `typescript` (and, when the content is a checkout, in its `runners/typescript` for the runner's own `tsc` and Node types), and in `jvm`, unless the selected JDK is older than 21 or missing (then one `FAIL` line, `skipped: java is not installed or does not run` or `skipped: JDK 17 is older than 21`, with the JDK fix, replaces the Gradle steps; a JDK above the range still warms up, so the problems that opt in to it build offline too), the Gradle warm-up: `./gradlew :java:csClasspath :kotlin:csClasspath -Pcs.only=cs_doctor_handshake` (builds the runner and support classes, resolves the runtime classpath so that the runner's own libraries are cached for offline builds, and warms the daemon without compiling practice files), then `./gradlew -p ../.cs/runtime/runners/jvm/project-warmup/java -q --console=plain csClasspath` and the same for `kotlin` (the warm-up projects, which cache what Java and Kotlin project folders build with: [JVM project folders](installation-and-workspaces.md#jvm-project-folders)), printing one line per command and the last 10 output lines of a failed one. Each Gradle command runs on the [selected JDK](#the-selected-jdk), like every other Gradle call of `cstudy`: it passes `-Dorg.gradle.java.home=<home>` and sets `JAVA_HOME`, so one command uses one JDK whatever the Gradle home configures; the lines above print the command without them. `--fix` outside a workspace exits 3.
153+
Checks Python 3.13, uv, Node 24 or newer, pnpm, the [selected JDK](#the-selected-jdk) (21 to 26), Homebrew Gradle, Go 1.24 or newer, `rustc` 1.80 or newer and `cargo` (the Go and Rust toolchains are optional, so these three rows WARN, never FAIL, with a link to the [toolchains guide](../guides/toolchains.md)), Delve (`dlv`, found as [debug mode](runners.md#debug-mode) finds it; the row shows its version, and a release other than the tested 1.27.2 passes with `(cstudy is tested with 1.27.2)`) and Rust's debug server (`debugserver` on macOS, `lldb-server` on Linux; the row shows its path), which only `cstudy test --debug` needs, so they WARN too, with the install step and the guide (``needed for --debug with --lang go, run `go install github.com/go-delve/delve/cmd/dlv@v1.27.2`: <guide>#go``), the config file, the content root, the workspace (core template files FAIL when missing, the optional `go.work`, `go/` and `rust/` files WARN; [workspace checks](installation-and-workspaces.md#cstudy-doctor-in-a-workspace)), the progress database (its schema against the one this `cstudy` migrates to: WARN when older, FAIL when newer), the Gradle wrapper, the workspace venv, `debugpy` and `pytest` in it, the `.venv` links of the Python project folders, the `go.work`, `replace` lines and module paths of the Go project folders, the `.cargo/config.toml` and rust-analyzer link of the Rust ones, the libraries that Java and Kotlin project folders build with offline ([workspace checks](installation-and-workspaces.md#cstudy-doctor-in-a-workspace)), `node_modules`, and that the runner of each core language (`python`, `typescript`, `java`, `kotlin`; the Go and Rust runners are not handshaken, since that would compile them) answers `hello`: the runner is prepared for a problem with no files (`cs-doctor-handshake`, so the JVM build compiles no practice code) and started with no jobs. Outside a workspace only the toolchains, the config and the content are checked. `--fix` first writes every missing workspace template file (never overwriting one), refreshes the runner runtime and migrates an older `progress.db` to the latest schema (one line, `migrated progress.db from schema 4 to 5`), then runs, in each workspace project that exists, `uv sync` in `python` (followed, when the venv cannot import pytest, by `uv add --dev --offline "pytest>=9.1"`, retried without `--offline` when it fails, so a workspace created before the template listed pytest gets it; when both fail the line is `FAIL` with `fix: once online, run: uv add --dev "pytest>=9.1" --directory python`), `pnpm install` in `typescript` (and, when the content is a checkout, in its `runners/typescript` for the runner's own `tsc` and Node types), and in `jvm`, unless the selected JDK is older than 21 or missing (then one `FAIL` line, `skipped: java is not installed or does not run` or `skipped: JDK 17 is older than 21`, with the JDK fix, replaces the Gradle steps; a JDK above the range still warms up, so the problems that opt in to it build offline too), the Gradle warm-up: `./gradlew --no-watch-fs :java:csClasspath :kotlin:csClasspath -Pcs.only=cs_doctor_handshake` (builds the runner and support classes, resolves the runtime classpath so that the runner's own libraries are cached for offline builds, and warms the daemon without compiling practice files), then `./gradlew -p ../.cs/runtime/runners/jvm/project-warmup/java -q --console=plain --no-watch-fs csClasspath` and the same for `kotlin` (the warm-up projects, which cache what Java and Kotlin project folders build with: [JVM project folders](installation-and-workspaces.md#jvm-project-folders)), printing one line per command and the last 10 output lines of a failed one. Each Gradle command runs on the [selected JDK](#the-selected-jdk), like every other Gradle call of `cstudy`: it passes `-Dorg.gradle.java.home=<home>` and sets `JAVA_HOME`, so one command uses one JDK whatever the Gradle home configures; the lines above print the command without them. `--fix` outside a workspace exits 3.
154154

155155
| Status | Meaning |
156156
|--------|---------|

‎docs/specs/runners.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ A runner is a small program per runtime that loads one target (the user's file,
2020
|--------|---------------------------|---------|-----|
2121
| Python | none (`cstudy doctor --fix` runs `uv sync` once) | `python/.venv/bin/python -m cs_runner --request R --out O` | `python` |
2222
| TypeScript | none | `node .cs/runtime/runners/typescript/runner.ts --request R --out O` | `typescript` |
23-
| Java | `./gradlew -q --console=plain :java:csClasspath -Pcs.only=<snake_slug>` | `java -cp <classpath> cs.runner.Main --request R --out O` | `jvm` |
24-
| Kotlin | `./gradlew -q --console=plain :kotlin:csClasspath -Pcs.only=<snake_slug>` | same as Java | `jvm` |
23+
| Java | `./gradlew -q --console=plain --no-watch-fs :java:csClasspath -Pcs.only=<snake_slug>` | `java -cp <classpath> cs.runner.Main --request R --out O` | `jvm` |
24+
| Kotlin | `./gradlew -q --console=plain --no-watch-fs :kotlin:csClasspath -Pcs.only=<snake_slug>` | same as Java | `jvm` |
2525
| Go | `go build -json [-overlay gen/<snake_slug>/overlay.json] -o bin/<scope>/<snake_slug> ./cmd/<scope>/<snake_slug>` in `.cs/build/go` ([Go build details](#go-build-details)) | `.cs/build/go/run/<pid>-<random>/<snake_slug> --request R --out O`, a private copy of the binary | `.cs/build/go` |
2626
| Rust | `cargo build --offline --release --message-format=json --target <host> -p <scope>_<snake_slug>` in `.cs/build/rust`, with one shared `CARGO_TARGET_DIR` ([Rust build details](#rust-build-details)) | `.cs/build/rust/run/<pid>-<random>/<scope>_<snake_slug> --request R --out O`, a private copy of the binary | `.cs/build/rust` |
2727

@@ -35,7 +35,7 @@ A [project job](#project-jobs) starts in the project root instead: the Python ru
3535
|------|------|
3636
| `-Pcs.only=<snake_slug>` | Restricts the main source set to `practice/<snake_slug>/**`, so a broken file for another problem never blocks this one. IntelliJ builds without the property and sees everything. |
3737
| `csClasspath` task | Depends on `classes`; writes the runtime classpath (classes dir plus runner and support jars) to `build/cs/runtime-classpath.txt` |
38-
| Gradle settings | Daemon on, configuration cache on, `org.gradle.console=plain` |
38+
| Gradle settings | Daemon on, configuration cache on, `org.gradle.console=plain`. Every build `cstudy` runs passes `--no-watch-fs`, so Gradle reads the sources from disk: on macOS, watch events for files written just before a build can arrive late, and Gradle then reports the compile task up to date over the changed sources |
3939
| JVM flags | `-XX:+UseSerialGC -Xshare:auto`; no tiering flags (they distort performance cases) |
4040
| Compile errors | Gradle exit code non-zero: the CLI prints compiler lines mentioning the user's file (max 40 lines) and exits 2. Gradle prints them twice, the second time indented under its failure report: the first copy is kept (the second only when it is the only one). Each error is a [diagnostic](cli.md#diagnostics) at the line and column the compiler names, from the absolute path or `file://` URL it prints |
4141
| Cold start | If no daemon is running, the CLI prints `warming Gradle daemon (first run)` before building |
@@ -597,7 +597,7 @@ A Java or Kotlin project folder is a Gradle build of its own ([starter](problem-
597597

598598
| Item | Rule |
599599
|------|------|
600-
| Command | `jvm/gradlew -p <root> -q --console=plain --offline <task>`, run in `jvm` like every JVM build, so it uses the workspace's Gradle and its daemon, on [the selected JDK](#the-jdk-and-the-release-level). `--offline`: a project build never reaches the network; `cstudy doctor --fix` caches what it needs |
600+
| Command | `jvm/gradlew -p <root> -q --console=plain --no-watch-fs --offline <task>`, run in `jvm` like every JVM build, so it uses the workspace's Gradle and its daemon, on [the selected JDK](#the-jdk-and-the-release-level). `--offline`: a project build never reaches the network; `cstudy doctor --fix` caches what it needs |
601601
| Tasks | A suite job builds `csClasspath`: the main and test classes, and both runtime classpaths, in `build/cs/main-classpath.txt` and `build/cs/test-classpath.txt`. A grading job builds `csMainClasspath`: the main classes and `build/cs/main-classpath.txt` only, so a test file that does not compile, such as a level's generated tests before its methods exist, never blocks a submission |
602602
| Locked levels | `-Pcs.exclude=<path>,...` names the generated test files of the levels above `cstudy test --level N`, relative to the root, and the plugin leaves them out of the test compile: they may call methods that do not exist yet. The runner reads no `exclude` |
603603
| Concurrent `cstudy` processes | One process at a time builds a folder and copies what it built: an exclusive `flock` on `.cs/build/jvm-locks/project-<folder name>-<digest of its absolute path>.lock`, held around the build, the read of the classpath file and the copy, with the runtime lock of [JVM build details](#jvm-build-details) inside it. Two folders, or a folder and a single-file problem, build side by side |

0 commit comments

Comments
 (0)