feat(run): share Java preparation and add incremental build policies (#2156)

Run and Debug now share one Java preparation path: save documents, build with the configured policy, resolve the module classpath and JDK, and stop on a failed build. Incremental Make is the default; Rebuild, NoBuild and Build-no-error-check are persisted on the run configuration, and the form offers only what a build tool supports. Delegated Gradle goes through the same path, Debug reports save failures, Maven snapshots honour Rebuild, and scoped diagnostics are retained so incremental Maven cannot erase compilation errors.

Co-authored-by: misswell <28730815+misswell@users.noreply.github.com>
This commit is contained in:
misswell
2026-11-09 03:18:13 +08:00
committed by GitHub
co-authored by misswell
parent 43bcd66b2b
commit 9f9a2d8cf0
41 changed files with 868 additions and 136 deletions
+70 -6
View File
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { IpcError, type RunConfig } from "@/platform/ipc";
import { findJavaModule, prepareJavaLaunch } from "@/platform/javaLaunch";
import { useProject } from "@/platform/project";
import { findJavaModule, prepareJavaLaunch, prepareJavaModule } from "@/platform/javaLaunch";
import { _resetI18nForTests, setLocale } from "@/platform/i18n";
import { provideNotificationService } from "@/workbench/contributions";
import { applicationConfig } from "./application";
@@ -72,7 +73,7 @@ afterEach(() => {
it.each(["sibling", "/outside/modules"])("runs from %s using the stored module's classpath", async (cwd) => {
const session = await startRun({ ...config, module: "app", cwd });
expect(session?.status).toBe("running");
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, true, expect.any(Number));
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
expect(ipc.runStart).toHaveBeenCalledWith(
expect.objectContaining({ cwd, args: ["-cp", "/p/app/target/classes", "a.Main"] }),
expect.anything(),
@@ -80,10 +81,59 @@ it.each(["sibling", "/outside/modules"])("runs from %s using the stored module's
expect(ipc.findFiles).not.toHaveBeenCalled();
});
const launchInfo = (build: "succeeded" | "failed" | "withErrors") => ({
build,
projectRoot: "/p",
classPaths: ["/p/app/target/classes"],
modulePaths: [],
javaHome: null,
javaExec: "java",
shortenCommandLine: null,
errors: build === "succeeded" ? [] : [{ path: "/p/app/src/Main.java", line: 1, column: 1, message: "boom" }],
errorCount: build === "succeeded" ? 0 : 1,
hints: [],
});
it.each(["noBuild", "rebuild"] as const)("passes the %s build policy to the resolver", async (buildPolicy) => {
await startRun({ ...config, module: "app", buildPolicy });
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, buildPolicy, expect.any(Number));
});
it("always applies the launch policy after a Before launch Build Project", async () => {
useProject.setState({ project: { root: "/p" } as never });
await startRun({ ...config, module: "app" });
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
useProject.setState({ project: null });
});
it("does not start a configuration whose build failed", async () => {
ipc.javaResolveLaunch.mockResolvedValueOnce({ status: "ok", data: launchInfo("failed") });
const session = await startRun({ ...config, module: "app", buildPolicy: "make" });
expect(session?.status).toBe("failed");
expect(ipc.runStart).not.toHaveBeenCalled();
});
it("starts with the last compiled classes for makeAndContinueOnErrors", async () => {
ipc.javaResolveLaunch.mockResolvedValueOnce({ status: "ok", data: launchInfo("withErrors") });
const session = await startRun({ ...config, module: "app", buildPolicy: "makeAndContinueOnErrors" });
expect(session?.status).toBe("running");
expect(ipc.runStart).toHaveBeenCalled();
});
it.each([undefined, "automatic", "make", "rebuild"] as const)(
"does not launch scoped compilation-error bytecode under %s",
async (buildPolicy) => {
ipc.javaResolveLaunch.mockResolvedValueOnce({ status: "ok", data: launchInfo("withErrors") });
const session = await startRun({ ...config, module: "app", buildPolicy });
expect(session?.status).toBe("failed");
expect(ipc.runStart).not.toHaveBeenCalled();
},
);
it("finds the source module for an old saved configuration without a module", async () => {
await startRun({ ...config, cwd: "/outside/modules" });
expect(ipc.findFiles).toHaveBeenCalledWith("Main.java", 1000);
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, true, expect.any(Number));
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
expect(ipc.runStart).toHaveBeenCalledWith(expect.objectContaining({ cwd: "/outside/modules" }), expect.anything());
});
@@ -96,7 +146,7 @@ it.each([["-cp", "lib/*"], []])("colours Spring Boot runs without classpath reso
expect.objectContaining({ args: ["-Dspring.output.ansi.enabled=always", ...args] }),
expect.anything(),
);
expect(ipc.javaResolveLaunch).not.toHaveBeenCalled();
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
expect(boot.args).toBe(args);
expect(args).not.toContain("-Dspring.output.ansi.enabled=always");
});
@@ -105,14 +155,14 @@ it.each(["never", "detect", "always"])("preserves an explicit Spring Boot consol
const args = [`-Dspring.output.ansi.enabled=${value}`, "-cp", "lib/*", "a.Main"];
await startRun({ ...config, kind: "spring-boot", args });
expect(ipc.runStart).toHaveBeenCalledWith(expect.objectContaining({ args }), expect.anything());
expect(ipc.javaResolveLaunch).not.toHaveBeenCalled();
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
});
it("keeps plain Java runs with an explicit classpath free of Spring Boot options", async () => {
const args = ["-cp", "lib/*", "a.Main"];
await startRun({ ...config, args });
expect(ipc.runStart).toHaveBeenCalledWith(expect.objectContaining({ args }), expect.anything());
expect(ipc.javaResolveLaunch).not.toHaveBeenCalled();
expect(ipc.javaResolveLaunch).toHaveBeenCalledWith("app", false, "automatic", expect.any(Number));
});
it("adds the Spring Boot console option once when resolving the module classpath", async () => {
@@ -197,3 +247,17 @@ it("translates import recovery guidance for a root Spring Boot configuration", a
),
).rejects.toThrow("Java 模块 '.' 尚未导入");
});
it("does not resolve or launch when saving fails", async () => {
const saveAll = vi.fn().mockRejectedValue(new Error("save failed"));
await expect(
prepareJavaLaunch(config, {
request: 1,
signal: new AbortController().signal,
saveAll,
}),
).rejects.toThrow("save failed");
await expect(prepareJavaModule(config, saveAll)).rejects.toThrow("save failed");
expect(ipc.javaResolveLaunch).not.toHaveBeenCalled();
expect(ipc.runStart).not.toHaveBeenCalled();
});