import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import type { RunConfig } from "@/platform/ipc"; import { usePlatform } from "@/platform/platformInfo"; import { useProject } from "@/platform/project"; import { useFlutterDevices } from "@/workbench/flutterDevices"; import { isJinjaTemplate } from "../breakpoints/languages"; import { debugUnsupportedReason, launchConfigFor, withEnvFiles } from "../launch"; const ipc = vi.hoisted(() => { const ok = (data: T) => Promise.resolve({ status: "ok" as const, data }); return { ok, runEnvFiles: vi.fn((_files: string[]) => ok({} as Record)) }; }); vi.mock("@/platform/ipc", async (importOriginal) => { const real = await importOriginal(); const { ok: _ok, ...cmds } = ipc; return { ...real, commands: { ...real.commands, ...cmds } }; }); const rc = (p: Partial): RunConfig => ({ id: "x", name: "cfg", kind: "shell", command: "true", args: [], cwd: null, env: {}, detected: true, isTest: false, debugAdapter: null, ...p, }); const extra = (l: { extraJson: string | null } | null) => JSON.parse(l?.extraJson ?? "{}"); const originalPlatform = usePlatform.getState(); beforeEach(() => { usePlatform.setState({ os: "linux" }); vi.clearAllMocks(); }); afterEach(() => usePlatform.setState(originalPlatform)); describe("Flutter device", () => { const device = { platformType: null, category: "desktop", emulator: false, unavailable: null }; const flutter = (p: Partial = {}) => rc({ kind: "flutter", command: "flutter", args: ["run", "-t", "lib/main.dart"], debugAdapter: "dart", ...p }); beforeEach(() => useFlutterDevices.setState({ preferred: "macos", list: { status: { kind: "ready" }, devices: [ { ...device, id: "chrome", name: "Chrome" }, { ...device, id: "macos", name: "macOS" }, ], }, }), ); afterEach(() => useFlutterDevices.setState({ preferred: null, list: null })); it("debugs on the toolbar's device, through the backend's Flutter check", () => { const launch = launchConfigFor(flutter({ args: ["run", "-t", "lib/app.dart", "--flavor", "dev"] }), "/p"); expect(launch?.program).toBe("/p/lib/app.dart"); expect(extra(launch)).toEqual({ toolArgs: ["--flavor", "dev"], __flutterDevice: "macos" }); }); it("takes the entry point from the target option, not from any argument ending in .dart", () => { const debug = (...args: string[]) => launchConfigFor(flutter({ args: ["run", ...args] }), "/p"); expect(debug("--target=lib/app.dart")?.program).toBe("/p/lib/app.dart"); expect(debug("--target", "lib/app.dart")?.program).toBe("/p/lib/app.dart"); const define = debug("-t", "lib/main.dart", "--dart-define=CFG=dev.dart"); expect(define?.program).toBe("/p/lib/main.dart"); expect(extra(define).toolArgs).toEqual(["--dart-define=CFG=dev.dart"]); expect(debug("--dart-define=CFG=dev.dart")?.program).toBe("/p/lib/main.dart"); }); it("keeps a file-scoped Dart configuration's last .dart argument as its entry point", () => { const file = rc({ kind: "dart", command: "dart", args: ["run", "bin/tool.dart"], debugAdapter: "dart" }); expect(launchConfigFor(file, "/p")?.program).toBe("/p/bin/tool.dart"); }); it("reads the pinned device from every spelling of the option", () => { const pinned = flutter({ args: ["run", "--device-id=chrome"] }); expect(extra(launchConfigFor(pinned, "/p"))).toEqual({ __flutterDevice: "chrome" }); }); it("prefers the device the configuration pins", () => { const pinned = flutter({ args: ["run", "-t", "lib/main.dart", "-d", "chrome"] }); expect(extra(launchConfigFor(pinned, "/p"))).toEqual({ __flutterDevice: "chrome" }); }); it("falls back to a connected device when the chosen one is gone", () => { useFlutterDevices.setState({ preferred: "R5C" }); expect(extra(launchConfigFor(flutter(), "/p")).__flutterDevice).toBe("chrome"); }); it("attaches on the pinned device, else the toolbar's", () => { const attach = (params: Record) => extra(launchConfigFor(rc({ debugAdapter: "dart", params: { request: "attach", ...params } }), "/p")); expect(attach({}).toolArgs).toEqual(["-d", "macos"]); expect(attach({ device: "chrome" }).toolArgs).toEqual(["-d", "chrome"]); }); it("debugs the build mode and flavor of the command line", () => { const launch = launchConfigFor( flutter({ args: ["run", "-t", "lib/main.dart", "--profile", "--flavor=dev"] }), "/p", ); expect(extra(launch).toolArgs).toEqual(["--profile", "--flavor=dev"]); }); it("attaches a Flutter configuration with its entry point and attach args", () => { const config = flutter({ args: ["run", "-t", "lib/dev.dart", "--flavor=dev"], params: { entry: "lib/dev.dart", flavor: "dev", attachArgs: "--debug-port 9000", request: "attach" }, }); const launch = launchConfigFor(config, "/p"); expect(launch).toMatchObject({ request: "attach", program: "/p/lib/dev.dart" }); expect(extra(launch)).toEqual({ __flutter: true, toolArgs: ["-d", "macos", "--debug-port", "9000"] }); }); it("leaves the choice to flutter without a device list", () => { useFlutterDevices.setState({ list: null }); expect(launchConfigFor(flutter(), "/p")?.extraJson).toBeNull(); }); }); describe("run config → launch config", () => { it("returns null without an adapter", () => { expect(launchConfigFor(rc({}), "/p")).toBeNull(); }); it("php-debug: the script runs on the PHP from Settings with Xdebug's debug mode, on a free port", () => { const xdebug = ["-dxdebug.mode=debug", "-dxdebug.start_with_request=yes", "-dxdebug.client_port=${port}"]; // `artisan serve` with PHP at Default: the script is a file of the project. const serve = launchConfigFor( rc({ kind: "php", command: "php", args: ["artisan", "serve"], debugAdapter: "php-debug" }), "/app", "/opt/php@8.3/bin/php", )!; expect(serve).toMatchObject({ adapter: "php-debug", program: "/app/artisan", args: ["serve"], cwd: "/app" }); expect(extra(serve)).toEqual({ port: 0, runtimeArgs: xdebug, runtimeExecutable: "/opt/php@8.3/bin/php" }); // The PHP the command names wins, and its options stay with it. const file = launchConfigFor( rc({ kind: "php", command: "/usr/bin/php8.4", args: ["-dmemory_limit=1G", "src/a.php", "x"], debugAdapter: "php-debug", }), "/app", "/opt/php@8.3/bin/php", )!; expect(file).toMatchObject({ program: "/app/src/a.php", args: ["x"] }); expect(extra(file)).toEqual({ port: 0, runtimeArgs: [...xdebug, "-dmemory_limit=1G"], runtimeExecutable: "/usr/bin/php8.4", }); // PHPUnit runs its script directly; without a PHP in Settings, php-debug uses the `php` on PATH. const unit = launchConfigFor( rc({ kind: "phpunit", command: "vendor/bin/phpunit", args: ["--filter", "testA"], debugAdapter: "php-debug" }), "/app", )!; expect(unit).toMatchObject({ program: "/app/vendor/bin/phpunit", args: ["--filter", "testA"] }); expect(extra(unit)).toEqual({ port: 0, runtimeArgs: xdebug }); }); it("cargo run builds the binary through codelldb's cargo integration", () => { const l = launchConfigFor( rc({ kind: "cargo", command: "cargo", args: ["run", "-p", "app", "--bin", "app", "--", "--port", "1"], debugAdapter: "codelldb", }), "/p", )!; expect(l.adapter).toBe("codelldb"); expect(l.program).toBeNull(); expect(l.args).toEqual(["--port", "1"]); expect(l.cwd).toBe("/p"); expect(extra(l)).toEqual({ cargo: { args: ["build", "-p", "app", "--bin", "app"], filter: { name: "app", kind: "bin" } }, sourceLanguages: ["rust"], }); }); it("cargo run under lldb-dap builds the same way and passes the environment as strings", () => { const l = launchConfigFor( rc({ kind: "cargo", command: "cargo", args: ["run"], env: { RUST_LOG: "debug" }, debugAdapter: "lldb-dap" }), "/p", )!; expect(l.adapter).toBe("lldb-dap"); expect(extra(l)).toEqual({ cargo: { args: ["build"], filter: { kind: "bin" } }, env: ["RUST_LOG=debug"] }); }); it("cargo test builds test binaries without running", () => { const l = launchConfigFor( rc({ kind: "cargo", command: "cargo", args: ["test", "--workspace"], debugAdapter: "codelldb" }), "/p", )!; expect(extra(l).cargo).toEqual({ args: ["test", "--no-run", "--workspace"] }); }); it("debugpy: script, module, console script, venv interpreter", () => { const script = launchConfigFor( rc({ kind: "python", command: ".venv/bin/python", args: ["manage.py", "runserver"], debugAdapter: "debugpy" }), "/p", )!; expect(script.program).toBe("/p/manage.py"); expect(script.args).toEqual(["runserver"]); expect(extra(script)).toMatchObject({ python: "/p/.venv/bin/python", django: true, console: "integratedTerminal" }); const mod = launchConfigFor( rc({ kind: "python", command: "python3", args: ["-m", "app", "-v"], debugAdapter: "debugpy" }), "/p", )!; expect(mod.program).toBeNull(); expect(mod.args).toEqual(["-v"]); expect(extra(mod)).toMatchObject({ module: "app", python: "python3" }); const pytest = launchConfigFor( rc({ kind: "pytest", command: ".venv/bin/pytest", args: ["-x"], debugAdapter: "debugpy" }), "/p", )!; expect(extra(pytest)).toMatchObject({ module: "pytest", python: "/p/.venv/bin/python" }); expect(pytest.args).toEqual(["-x"]); const globalPytest = launchConfigFor(rc({ kind: "pytest", command: "pytest", debugAdapter: "debugpy" }), "/p")!; expect(extra(globalPytest).python).toBe("python3"); }); it("debugpy: Jinja2 templates for Flask, or when a template has a breakpoint (#6371)", () => { const debug = (command: string, args: string[], templates = { django: false, jinja: false }) => extra(launchConfigFor(rc({ kind: "python", command, args, debugAdapter: "debugpy" }), "/p", null, templates)); expect(debug("uv", ["run", "flask", "--app", "app", "run"])).toMatchObject({ module: "flask", jinja: true }); expect(debug("python3", ["-m", "flask", "run"]).jinja).toBe(true); expect(debug("python3", ["app.py"]).jinja).toBeUndefined(); const jinja = { django: false, jinja: true }; expect(debug("python3", ["app.py"], jinja).jinja).toBe(true); // Django renders its own templates: `jinja` would make debugpy read them as Jinja2. expect(debug("python3", ["manage.py", "runserver"], jinja)).toMatchObject({ django: true }); expect(debug("python3", ["manage.py", "runserver"], jinja).jinja).toBeUndefined(); expect(debug("uv", ["run", "django-admin", "runserver"], jinja).jinja).toBeUndefined(); // A Django Template's breakpoint: `django` however the server starts. const asgi = debug("uv", ["run", "uvicorn", "mysite.asgi:application"], { django: true, jinja: false }); expect(asgi).toMatchObject({ module: "uvicorn", django: true }); expect(asgi.jinja).toBeUndefined(); }); it("debugpy: Flask without the in-browser debugger, and no reloader unless subprocesses are attached", () => { const debug = (kind: string, args: string[]) => launchConfigFor(rc({ kind, command: "python3", args, debugAdapter: "debugpy" }), "/p")!.args; expect(debug("flask", ["-m", "flask", "run", "--port", "5001"])).toEqual([ "run", "--no-debugger", "--port", "5001", ]); expect(debug("python", ["-m", "flask", "--app", "app", "run", "--no-debugger"])).toEqual([ "--app", "app", "run", "--no-debugger", ]); expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app", "--reload"]); const settings = useProject.getState().settings; try { useProject.setState({ settings: { ...settings, debugger: { python: { attachSubprocess: false } } } as never }); expect(debug("flask", ["-m", "flask", "run"])).toEqual(["run", "--no-debugger", "--no-reload"]); expect(debug("fastapi", ["-m", "uvicorn", "main:app", "--reload"])).toEqual(["main:app"]); } finally { useProject.setState({ settings }); } }); it("Jinja2 templates: their extensions anywhere, markup or text in a templates folder", () => { for (const p of [ "/p/mail/welcome.j2", "/p/x/page.JINJA", "/p/a.jinja2", "/p/templates/base.html", "/p/app/templates/x/y.txt", ]) expect(isJinjaTemplate(p), p).toBe(true); for (const p of ["/p/static/index.html", "/p/templates.py", "/p/templates/style.css", "/p/app.py"]) expect(isJinjaTemplate(p), p).toBe(false); // A file whose Override File Type is Jinja2 Template. expect(isJinjaTemplate("/p/static/index.html", "jinja")).toBe(true); expect(isJinjaTemplate("/p/static/index.html", "jinja-html")).toBe(true); }); it("debugpy: uv / poetry run the debuggee in the project's environment", () => { const debug = (command: string, args: string[]) => launchConfigFor(rc({ kind: "python", command, args, debugAdapter: "debugpy" }), "/p")!; const script = debug("uv", ["run", "python", "main.py", "-v"]); expect(script.program).toBe("/p/main.py"); expect(script.args).toEqual(["-v"]); expect(extra(script)).toMatchObject({ python: ["uv", "run", "python"], debugLauncherPython: "python3" }); expect(extra(script).module).toBeUndefined(); const mod = debug("poetry", ["run", "python3", "-m", "app"]); expect(extra(mod)).toMatchObject({ module: "app", python: ["poetry", "run", "python3"] }); const tool = debug("uv", ["run", "pytest", "-x"]); expect(extra(tool)).toMatchObject({ module: "pytest", python: ["uv", "run", "python"] }); expect(tool.args).toEqual(["-x"]); const django = debug("pdm", ["run", "python", "manage.py", "runserver"]); expect(extra(django)).toMatchObject({ django: true, python: ["pdm", "run", "python"] }); // Django templates' breakpoints need `django: true` however Django is started (#6370). expect(extra(debug("uv", ["run", "python", "-m", "django", "runserver"]))).toMatchObject({ module: "django", django: true, }); const admin = debug("uv", ["run", "django-admin", "runserver"]); expect(extra(admin)).toMatchObject({ module: "django", django: true }); expect(admin.args).toEqual(["runserver"]); expect(extra(debug("python3", ["-m", "app"])).django).toBeUndefined(); // The manager's own options stay with it; `uv run app.py` / `-m app` start Python directly (#5874). const withOptions = debug("uv", ["run", "--with", "rich", "--no-sync", "main.py", "-v"]); expect(withOptions.program).toBe("/p/main.py"); expect(withOptions.args).toEqual(["-v"]); expect(extra(withOptions)).toMatchObject({ python: ["uv", "run", "--with", "rich", "--no-sync", "python"] }); expect(extra(withOptions).module).toBeUndefined(); const member = debug("uv", ["run", "--project", "member", "--module", "app", "x"]); expect(extra(member)).toMatchObject({ module: "app", python: ["uv", "run", "--project", "member", "python"] }); expect(member.args).toEqual(["x"]); const toolWithOptions = debug("uv", ["run", "--frozen", "pytest", "-x"]); expect(extra(toolWithOptions)).toMatchObject({ module: "pytest", python: ["uv", "run", "--frozen", "python"] }); expect(toolWithOptions.args).toEqual(["-x"]); }); it("debugpy: Windows falls back to python, not the python3 Store alias", () => { usePlatform.setState({ os: "windows" }); const globalPytest = launchConfigFor(rc({ kind: "pytest", command: "pytest", debugAdapter: "debugpy" }), "/p")!; expect(extra(globalPytest).python).toBe("python"); const uv = launchConfigFor( rc({ kind: "python", command: "uv", args: ["run", "python", "main.py"], debugAdapter: "debugpy" }), "/p", )!; expect(extra(uv)).toMatchObject({ python: ["uv", "run", "python"], debugLauncherPython: "python" }); }); it("debugpy: a Windows venv interpreter or tool path debugs with that interpreter", () => { usePlatform.setState({ os: "windows" }); const venv = "C:\\work\\app\\.venv\\Scripts\\"; const script = launchConfigFor( rc({ kind: "python", command: `${venv}python.exe`, args: ["main.py"], debugAdapter: "debugpy" }), "C:/work/app", )!; expect(extra(script).python).toBe(`${venv}python.exe`); expect(extra(script).module).toBeUndefined(); expect(script.program).toBe("C:/work/app/main.py"); const pytest = launchConfigFor( rc({ kind: "pytest", command: `${venv}pytest.exe`, args: ["-x"], debugAdapter: "debugpy" }), "C:/work/app", )!; expect(extra(pytest)).toMatchObject({ module: "pytest", python: `${venv}python.exe` }); }); it("js-debug: npm scripts run through the package manager, node scripts directly", () => { const npm = launchConfigFor( rc({ kind: "npm", command: "pnpm", args: ["run", "dev"], cwd: "web", debugAdapter: "js-debug" }), "/p", )!; expect(npm.cwd).toBe("/p/web"); expect(npm.program).toBeNull(); expect(extra(npm)).toMatchObject({ type: "pwa-node", runtimeExecutable: "pnpm", runtimeArgs: ["run", "dev"] }); const node = launchConfigFor(rc({ command: "node", args: ["server.js", "--x"], debugAdapter: "js-debug" }), "/p")!; expect(node.program).toBe("/p/server.js"); expect(node.args).toEqual(["--x"]); expect(extra(node).runtimeExecutable).toBeUndefined(); // A located Node.js (e.g. 22.6+ for TypeScript) is used instead of PATH's. const ts = launchConfigFor( rc({ command: "/opt/node22/bin/node", args: ["a.ts"], debugAdapter: "js-debug" }), "/p", )!; expect(ts.program).toBe("/p/a.ts"); expect(extra(ts).runtimeExecutable).toBe("/opt/node22/bin/node"); }); it("js-debug: TypeScript through a runner or type-stripping flag runs as the runtime", () => { const flag = launchConfigFor( rc({ kind: "node", command: "/bin/node", args: ["--experimental-strip-types", "a.ts"], debugAdapter: "js-debug", }), "/p", )!; expect(flag.program).toBeNull(); expect(extra(flag)).toMatchObject({ runtimeExecutable: "/bin/node", runtimeArgs: ["--experimental-strip-types", "a.ts"], }); const tsx = launchConfigFor( rc({ kind: "node", command: "npx", args: ["tsx", "a.ts"], debugAdapter: "js-debug" }), "/p", )!; expect(extra(tsx)).toMatchObject({ runtimeExecutable: "npx", runtimeArgs: ["tsx", "a.ts"] }); }); it("deno: waits on its inspector at a free port that js-debug attaches to", () => { const l = launchConfigFor( rc({ kind: "deno", command: "/bin/deno", args: ["run", "-A", "--inspect", "main.ts", "x"], debugAdapter: "js-debug", }), "/p", )!; expect(l.adapter).toBe("js-debug"); expect(l.program).toBeNull(); expect(extra(l)).toMatchObject({ type: "pwa-node", runtimeExecutable: "/bin/deno", runtimeArgs: ["run", "--inspect-wait=127.0.0.1:${freePort}", "-A", "main.ts", "x"], attachSimplePort: "${freePort}", }); const test = launchConfigFor( rc({ kind: "deno", command: "deno", args: ["test"], debugAdapter: "js-debug" }), "/p", )!; expect(extra(test).runtimeArgs).toEqual(["test", "--inspect-wait=127.0.0.1:${freePort}"]); }); it("deno task and Bun can't be debugged, with a reason", () => { const task = rc({ kind: "deno", command: "deno", args: ["task", "dev"], debugAdapter: "js-debug" }); expect(launchConfigFor(task, "/p")).toBeNull(); expect(debugUnsupportedReason(task)).toContain("deno run"); const bun = rc({ kind: "bun", command: "/bin/bun", args: ["run", "a.ts"], debugAdapter: "js-debug" }); expect(launchConfigFor(bun, "/p")).toBeNull(); expect(debugUnsupportedReason(bun)).toContain("WebKit"); // `bun run