refactor(cli): drop runtime.ts step 3 — cache fallback was unreachable

resolveRuntime's runtimeVersion parameter and cache-fallback step were
dead code from production's perspective. The --runtime-version path is
handled entirely in run.ts (findCachedRelease + downloadRuntimeVersion +
findBundleRoot + validateBundle), and run.ts only ever called
resolveRuntime() with no argument. The parameter and step 3 only existed
because tests exercised them.

Two parallel "compute cache dir" implementations (runtime.ts:65 and
run.ts:157) were a drift hazard if the cache layout ever changes.

Changes:
- Drop the runtimeVersion parameter from resolveRuntime.
- Drop the cache step from runtime.ts entirely; remove the unused
  CACHE_DIR + findBundleRoot imports.
- Narrow RuntimeSource from "env" | "npm" | "cache" to "env" | "npm",
  and drop the now-unused tag field from ResolvedRuntime.
- Remove the three "cache fallback" tests from runtime.test.ts. The
  --runtime-version cache lookup is still covered by run.test.ts via
  findCachedRelease tests.

Tests: 134 → 131 (three deleted tests are obsolete, the production
behavior they were verifying is now covered by run.test.ts).
This commit is contained in:
José Almeida
2026-05-05 00:27:51 +01:00
parent a25e61ce57
commit feb7c5a9bd
2 changed files with 14 additions and 51 deletions
-28
View File
@@ -154,34 +154,6 @@ describe("resolveRuntime", () => {
});
});
describe("cache fallback (step 3 — runtimeVersion only)", () => {
it("resolves from cache when runtimeVersion is given and cache exists", () => {
const tag = "v0.16.0";
const cacheDir = path.join(tmpDir, tag, PLATFORM, "kandev");
createFakeBundle(cacheDir);
const result = resolveRuntime(tag);
expect(result.source).toBe("cache");
expect(result.tag).toBe(tag);
expect(result.bundleDir).toContain(tag);
});
it("throws when runtimeVersion is given but cache dir is missing", () => {
expect(() => resolveRuntime("v9.9.9")).toThrow(/Backend binary not found/);
});
it("does not check cache when runtimeVersion is not given", () => {
// Create a cache entry — should not be picked up without runtimeVersion
const tag = "v0.16.0";
const cacheDir = path.join(tmpDir, tag, PLATFORM, "kandev");
createFakeBundle(cacheDir);
// No KANDEV_BUNDLE_DIR, no npm package (require.resolve will fail in test)
// Should throw the "no runtime found" error, not resolve from cache
expect(() => resolveRuntime()).toThrow(/No Kandev runtime found/);
});
});
describe("no runtime found", () => {
it("throws an actionable error message mentioning install paths", () => {
let error: Error | null = null;
+14 -23
View File
@@ -1,12 +1,9 @@
import path from "node:path";
import fs from "node:fs";
import { findBundleRoot, resolveWebServerPath } from "./bundle";
import { CACHE_DIR } from "./constants";
import { resolveWebServerPath } from "./bundle";
import { getBinaryName, getPlatformDir, type PlatformDir } from "./platform";
// Maps platform.ts dir strings to npm @kdlbs/runtime-* package names.
// platform.ts uses internal names (macos, windows); npm uses os identifiers (darwin, win32).
const PLATFORM_TO_NPM_PACKAGE: Record<PlatformDir, string> = {
"linux-x64": "@kdlbs/runtime-linux-x64",
"linux-arm64": "@kdlbs/runtime-linux-arm64",
@@ -15,28 +12,29 @@ const PLATFORM_TO_NPM_PACKAGE: Record<PlatformDir, string> = {
"windows-x64": "@kdlbs/runtime-win32-x64",
};
export type RuntimeSource = "env" | "npm" | "cache";
export type RuntimeSource = "env" | "npm";
export type ResolvedRuntime = {
bundleDir: string;
source: RuntimeSource;
tag?: string;
};
/**
* Resolve the runtime bundle directory using a three-step priority chain:
* Resolve the runtime bundle directory using a two-step priority chain:
*
* 1. KANDEV_BUNDLE_DIR env var — set by the Homebrew formula wrapper and useful
* for local testing. Skips all other resolution.
* 2. Installed npm runtime package — looks for @kdlbs/runtime-{platform} in
* node_modules via Node module resolution. Only works after `npx kandev@latest`
* or `npm install -g kandev` (npm 7+).
* 3. Cached download — only checked when runtimeVersion is explicitly specified
* via --runtime-version. Used by the explicit version download path in run.ts.
* 1. KANDEV_BUNDLE_DIR env var — set by the Homebrew formula wrapper and
* useful for local testing. Skips all other resolution.
* 2. Installed npm runtime package — looks for @kdlbs/runtime-{platform}
* in node_modules via Node module resolution. Works after
* `npx kandev@latest` or `npm install -g kandev` (requires npm 7+).
*
* The explicit `--runtime-version <tag>` download path is handled directly
* in run.ts (which manages the GitHub download + cache itself); it does
* not flow through this function.
*
* Throws with an actionable error message if no runtime is found.
*/
export function resolveRuntime(runtimeVersion?: string): ResolvedRuntime {
export function resolveRuntime(): ResolvedRuntime {
const envBundleDir = process.env.KANDEV_BUNDLE_DIR;
if (envBundleDir) {
validateBundle(envBundleDir);
@@ -50,7 +48,7 @@ export function resolveRuntime(runtimeVersion?: string): ResolvedRuntime {
pkgJsonPath = require.resolve(`${packageName}/package.json`);
} catch {
// MODULE_NOT_FOUND — npm runtime package is not installed. Fall through
// to step 3 (cache fallback only if --runtime-version is set).
// to the actionable error below.
}
if (pkgJsonPath) {
// The package IS installed. If validateBundle throws here, the bundle is
@@ -61,13 +59,6 @@ export function resolveRuntime(runtimeVersion?: string): ResolvedRuntime {
return { bundleDir: packageRoot, source: "npm" };
}
if (runtimeVersion) {
const cacheDir = path.join(CACHE_DIR, runtimeVersion, platformDir);
const bundleDir = findBundleRoot(cacheDir);
validateBundle(bundleDir);
return { bundleDir, source: "cache", tag: runtimeVersion };
}
throw new Error(
`No Kandev runtime found for ${platformDir}.\n` +
` Install via npm (requires npm 7+): npx kandev@latest\n` +