repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

Replaced UV version subprocess with synchronous executable access checks over PATH; reload tax fell 86% Win32, seven controlled-PATH tests passed

91ad67e76aadb438888bfc8835588ad685d4b522

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Result: {"status":"keep","uv_reload_tax_ms":11.263,"elapsed_s":341.8,"win32_start_reload_ms":8422.463,"win32_extension_tax_ms":2916.592,"win32_startup_ms":6947.187,"win32_reload_ms":1475.276,"wsl_start_reload_ms":2206.537,"wsl_extension_tax_ms":979.081,"wsl_uv_reload_tax_ms":5.159}

Changed files (2)

extensions/uv/__tests__/harness.test.ts

Modified
1 +import {
2 + chmodSync,
3 + copyFileSync,
4 + mkdtempSync,
5 + rmSync,
6 + writeFileSync,
7 +} from "node:fs";
8 +import { tmpdir } from "node:os";
9 +import { join } from "node:path";
1 10 import { afterEach, describe, expect, it } from "vitest";
2 11 import { createTestSession, type TestSession } from "../../../test/harness";
3 12 import uvExtension, { __test } from "../index";
5 14 let t: TestSession | undefined;
15 +let fakeBin: string | undefined;
7 17 afterEach(() => {
8 18 t?.dispose();
19 + if (fakeBin) rmSync(fakeBin, { recursive: true, force: true });
9 20 t = undefined;
21 + fakeBin = undefined;
12 24 describe("uv extension", () => {
16 28 expect(t.session.extensionRunner.extensions).toHaveLength(1);
31 + it("detects a controlled executable on PATH through the extension", async () => {
32 + fakeBin = mkdtempSync(join(tmpdir(), "pi-uv-bin-"));
33 + if (process.platform === "win32") {
34 + copyFileSync(process.execPath, join(fakeBin, "uv.exe"));
35 + } else {
36 + const uv = join(fakeBin, "uv");
37 + writeFileSync(uv, "#!/bin/sh\nexit 0\n", "utf8");
38 + chmodSync(uv, 0o755);
39 + }
41 + t = await createTestSession({
42 + env: { PATH: fakeBin },
43 + extensionFactories: [uvExtension],
44 + });
46 + expect(t.events.uiCallsFor("notify")).toEqual([]);
47 + });
49 + it("warns through the extension when PATH has no uv executable", async () => {
50 + fakeBin = mkdtempSync(join(tmpdir(), "pi-uv-bin-"));
51 + t = await createTestSession({
52 + env: { PATH: fakeBin },
53 + extensionFactories: [uvExtension],
54 + });
56 + expect(t.events.uiCallsFor("notify").at(-1)?.args).toEqual([
57 + "uv extension: uv not found on PATH",
58 + "warning",
59 + ]);
60 + });
19 62 it("blocks pip-family commands with uv guidance", () => {
20 63 expect(__test.getBlockedCommandMessage("pip install rich")).toContain(
21 64 "uv add PACKAGE",

extensions/uv/index.ts

Modified
1 -import { spawnSync } from "node:child_process";
1 +import { accessSync, constants } from "node:fs";
2 2 import { delimiter, join } from "node:path";
3 3 import {
4 4 type BashSpawnContext,
11 11 "intercepted-commands",
14 -function uvInstalled(): boolean {
15 - const result = spawnSync("uv", ["--version"], { stdio: "ignore" });
16 - return !result.error && result.status === 0;
14 +function uvInstalled(env = process.env, platform = process.platform): boolean {
15 + const names = platform === "win32" ? ["uv.exe", "uv.cmd", "uv.bat"] : ["uv"];
16 + const mode = platform === "win32" ? constants.F_OK : constants.X_OK;
17 + for (const directory of (env[pathEnvKey(env)] ?? "").split(delimiter)) {
18 + for (const name of names) {
19 + try {
20 + accessSync(join(directory.replace(/^"|"$/gu, ""), name), mode);
21 + return true;
22 + } catch {}
23 + }
24 + }
25 + return false;
19 28 function pathEnvKey(env: NodeJS.ProcessEnv): string {
104 113 return null;
107 -export const __test = { getBlockedCommandMessage, prependInterceptedCommands };
116 +export const __test = {
117 + getBlockedCommandMessage,
118 + prependInterceptedCommands,
119 + uvInstalled,
109 122 export default function uvExtension(pi: ExtensionAPI) {
110 123 pi.on("session_start", (_event, ctx) => {