Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(ingest): open config files non-blocking, and don't half-load a dropped extends

KageBinary08cd7779d74c
diff --git a/ix-cli/knip.json b/ix-cli/knip.json
index 501bc7ab..e3616b42 100644
--- a/ix-cli/knip.json
+++ b/ix-cli/knip.json
@@ -3,5 +3,5 @@
"entry": ["src/cli/main.ts", "src/index.ts", "test/parser.smoke.mjs"],
"project": ["src/**/*.ts"],
"ignore": ["test/fixtures/**", "test/parser.stress.mjs"],
- "ignoreBinaries": ["ix", "cygpath", "tar", "start", "xdg-open"]
+ "ignoreBinaries": ["ix", "cygpath", "tar", "start", "xdg-open", "mkfifo"]
}
diff --git a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
index 9865e4c9..a36373ef 100644
--- a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
+++ b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
@@ -1,3 +1,4 @@
+import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, relative, sep } from "node:path";
@@ -225,6 +226,72 @@ describe("createTypeScriptModuleResolver", () => {
},
);
+ it.skipIf(process.platform === "win32")(
+ "refuses an extends that reaches a FIFO without blocking on the open",
+ () => {
+ // The guard that rejects non-regular files runs *after* the open, and
+ // opening a FIFO for reading blocks until a writer appears — so without
+ // O_NONBLOCK the open never returns and no later check can save it. The
+ // /dev/zero case does not cover this: a character device opens fine.
+ const root = workspace();
+ const fifo = join(root, "piped.json");
+ try {
+ execFileSync("mkfifo", [fifo]);
+ } catch {
+ return; // no mkfifo available — nothing to assert
+ }
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: "./piped.json" }));
+ const consumer = write(root, "src/consumer.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, fifo]);
+
+ expect(resolve("src/consumer.ts", "@core")).toBeUndefined();
+ },
+ );
+
+ it("accepts a sibling directory whose name begins with dots", () => {
+ // Containment must compare path segments: `..shared` is inside the
+ // workspace, but a bare startsWith("..") reads it as an escape.
+ const root = workspace();
+ write(
+ root,
+ "..shared/tsconfig.json",
+ JSON.stringify({ compilerOptions: { paths: { "@core": ["src/worker"] } } }),
+ );
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: "./..shared" }));
+ const consumer = write(root, "src/consumer.ts");
+ const worker = write(root, "..shared/src/worker.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, worker]);
+
+ expect(resolve("src/consumer.ts", "@core")).toEqual(["..shared/src/worker.ts"]);
+ });
+
+ it("does not claim authority when an out-of-scope extends was dropped", () => {
+ // A scoped ingest roots containment at the ingest path, so a shared base
+ // config above it is dropped. Half-loading the remainder is worse than main:
+ // the local `paths` rule matches, fails against the missing baseUrl, and
+ // returns an authoritative [] that deletes an edge main resolves.
+ const outer = workspace();
+ writeFileSync(
+ join(outer, "tsconfig.base.json"),
+ JSON.stringify({ compilerOptions: { baseUrl: "." } }),
+ );
+ const scoped = join(outer, "packages", "web");
+ mkdirSync(scoped, { recursive: true });
+ const config = write(
+ scoped,
+ "tsconfig.json",
+ JSON.stringify({
+ extends: "../../tsconfig.base.json",
+ compilerOptions: { paths: { "@app/*": ["src/*"] } },
+ }),
+ );
+ const consumer = write(scoped, "src/consumer.ts");
+ const resolve = createTypeScriptModuleResolver(scoped, [config, consumer]);
+
+ // undefined ("no opinion") lets the stem fallback run; [] would delete the edge.
+ expect(resolve("src/consumer.ts", "@app/missing")).toBeUndefined();
+ });
+
it("follows a relative extends that names a directory", () => {
// Previously an existsSync probe stopped at the directory itself and never
// tried <dir>/tsconfig.json, so the parent's paths were silently dropped.
diff --git a/ix-cli/src/cli/ts-module-resolution.ts b/ix-cli/src/cli/ts-module-resolution.ts
index 87192b8e..e4c64c23 100644
--- a/ix-cli/src/cli/ts-module-resolution.ts
+++ b/ix-cli/src/cli/ts-module-resolution.ts
@@ -145,14 +145,21 @@ function readObject(filePath: string): Record<string, unknown> | undefined {
// Open once and fstat the same handle so the checks and the read observe the
// same inode — no stat-then-read window (CodeQL js/file-system-race). This is
// the pattern ingest.ts already uses.
- const handle = fs.openSync(filePath, "r");
+ //
+ // O_NONBLOCK is load-bearing, not tidiness: opening a FIFO for reading
+ // *blocks until a writer appears*, so without it the open never returns and
+ // no later check can help. It is the one case a guard placed after the open
+ // cannot cover. Undefined on Windows, where `|` with undefined degrades to
+ // a plain read — and Windows has no FIFO to open this way.
+ const handle = fs.openSync(filePath, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
let text: string;
try {
const stats = fs.fstatSync(handle);
// `openSync` follows symlinks, so fstat describes the *target*. Refusing
// anything that is not a regular file is what stops a link to /dev/zero
- // (unbounded read) or a FIFO (blocks the event loop forever) — neither of
- // which a size check catches, since both report size 0.
+ // (an unbounded read) and a FIFO (which would otherwise deliver no data
+ // and no EOF) — neither of which a size check catches, since both
+ // report size 0.
if (!stats.isFile() || stats.size > MAX_CONFIG_BYTES) return undefined;
text = fs.readFileSync(handle, "utf8");
} finally {
@@ -187,7 +194,14 @@ function resolveExtends(workspaceRoot: string, configPath: string, value: unknow
function isWithinWorkspace(workspaceRoot: string, candidate: string): boolean {
const relative = nodePath.relative(nodePath.resolve(workspaceRoot), candidate);
- return relative !== "" && !relative.startsWith("..") && !nodePath.isAbsolute(relative);
+ // Compare path *segments*: a bare `..startsWith` also rejects a legitimate
+ // sibling directory whose name merely begins with dots, e.g. `..shared/`.
+ return (
+ relative !== "" &&
+ relative !== ".." &&
+ !relative.startsWith(`..${nodePath.sep}`) &&
+ !nodePath.isAbsolute(relative)
+ );
}
function loadConfig(
@@ -208,11 +222,25 @@ function loadConfig(
return undefined;
}
+ const extendsCandidates = resolveExtends(workspaceRoot, absoluteConfig, raw.extends);
let parent: LoadedConfig | undefined;
- for (const candidate of resolveExtends(workspaceRoot, absoluteConfig, raw.extends)) {
+ for (const candidate of extendsCandidates) {
parent = loadConfig(workspaceRoot, candidate, cache, loading);
if (parent) break;
}
+
+ // A scoped run (`ix map ./packages/web`) roots containment at the ingest path,
+ // so a monorepo's shared `tsconfig.base.json` one level up is dropped. Half-
+ // loading what remains is worse than not loading it: this config's own `paths`
+ // would still match, fail to resolve against the missing `baseUrl`, and return
+ // an *authoritative* empty — which suppresses the stem fallback and deletes an
+ // edge main resolves fine. Treat the config as unusable instead, which is
+ // exactly main's behaviour.
+ if (typeof raw.extends === "string" && raw.extends.startsWith(".") && !parent) {
+ cache.set(absoluteConfig, undefined);
+ loading.delete(absoluteConfig);
+ return undefined;
+ }
const configDir = nodePath.dirname(absoluteConfig);
const compilerOptions =
raw.compilerOptions && typeof raw.compilerOptions === "object"