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): don't let baseUrl claim authority over every bare specifier

KageBinary795ec2ce8075
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 bd241983..8f1be5a8 100644
--- a/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
+++ b/ix-cli/src/cli/__tests__/ts-module-resolution.test.ts
@@ -96,7 +96,10 @@ describe("createTypeScriptModuleResolver", () => {
expect(resolve("src/consumer.ts", "services/worker")).toEqual(["src/services/worker.ts"]);
});
- it("treats an unresolved baseUrl module as authoritative", () => {
+ it("does not treat an unresolved baseUrl module as authoritative", () => {
+ // `baseUrl` matches every bare specifier, so its miss carries no information.
+ // Returning [] here tells the resolver "definitively not local", which drops
+ // the edge; `undefined` means "no opinion" and lets the stem fallback run.
const root = workspace();
const config = write(
root,
@@ -109,7 +112,77 @@ describe("createTypeScriptModuleResolver", () => {
const unrelated = write(root, "legacy/missing.ts");
const resolve = createTypeScriptModuleResolver(root, [config, consumer, unrelated]);
- expect(resolve("src/consumer.ts", "services/missing")).toEqual([]);
+ expect(resolve("src/consumer.ts", "services/missing")).toBeUndefined();
+ });
+
+ it("keeps third-party specifiers unresolved rather than authoritative under baseUrl", () => {
+ // The regression this guards: `baseUrl: "."` is the default in Next.js, CRA
+ // and most monorepo templates. Claiming authority over `react` there erased
+ // every third-party import edge in the graph.
+ const root = workspace();
+ const config = write(
+ root,
+ "tsconfig.json",
+ JSON.stringify({ compilerOptions: { baseUrl: "." } }),
+ );
+ const consumer = write(root, "packages/app/index.ts");
+ const sibling = write(root, "packages/lib/index.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, sibling]);
+
+ expect(resolve("packages/app/index.ts", "react")).toBeUndefined();
+ expect(resolve("packages/app/index.ts", "@org/lib")).toBeUndefined();
+ });
+
+ it("does not treat a catch-all paths rule as authoritative", () => {
+ // `"*": ["src/*"]` matches every specifier, so it is baseUrl by another name.
+ const root = workspace();
+ const config = write(
+ root,
+ "tsconfig.json",
+ JSON.stringify({
+ compilerOptions: { paths: { "*": ["src/*"] } },
+ }),
+ );
+ const consumer = write(root, "src/consumer.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer]);
+
+ expect(resolve("src/consumer.ts", "lodash")).toBeUndefined();
+ });
+
+ it("falls back to baseUrl when a matching paths rule resolves nothing", () => {
+ // TypeScript tries `paths` first, then `baseUrl`. A failed rule must not
+ // short-circuit the baseUrl lookup.
+ const root = workspace();
+ const config = write(
+ root,
+ "tsconfig.json",
+ JSON.stringify({
+ compilerOptions: { baseUrl: "src", paths: { "@core/*": ["missing/*"] } },
+ }),
+ );
+ const consumer = write(root, "src/consumer.ts");
+ const actual = write(root, "src/@core/thing.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, actual]);
+
+ expect(resolve("src/consumer.ts", "@core/thing")).toEqual(["src/@core/thing.ts"]);
+ });
+
+ 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.
+ const root = workspace();
+ write(
+ root,
+ "cfg/tsconfig.json",
+ JSON.stringify({ compilerOptions: { paths: { "@core": ["src/worker"] } } }),
+ );
+ const config = write(root, "tsconfig.json", JSON.stringify({ extends: "./cfg" }));
+ const consumer = write(root, "src/consumer.ts");
+ // `paths` in an extended config resolve against the config that declares them.
+ const worker = write(root, "cfg/src/worker.ts");
+ const resolve = createTypeScriptModuleResolver(root, [config, consumer, worker]);
+
+ expect(resolve("src/consumer.ts", "@core")).toEqual(["cfg/src/worker.ts"]);
});
it("prefers JavaScript for extensionless imports from JavaScript files", () => {
diff --git a/ix-cli/src/cli/ts-module-resolution.ts b/ix-cli/src/cli/ts-module-resolution.ts
index a742710b..f50e295d 100644
--- a/ix-cli/src/cli/ts-module-resolution.ts
+++ b/ix-cli/src/cli/ts-module-resolution.ts
@@ -143,11 +143,16 @@ function readObject(filePath: string): Record<string, unknown> | undefined {
}
}
-function resolveExtends(configPath: string, value: unknown): string | undefined {
- if (typeof value !== "string" || !value.startsWith(".")) return undefined;
+/**
+ * Candidate paths a relative `extends` may name, in TypeScript's own order.
+ * Returns every candidate rather than probing with `existsSync`: the caller
+ * reads each one and `readObject` already reports an unreadable file as
+ * `undefined`, so there is no check-then-use window (CodeQL js/file-system-race).
+ */
+function resolveExtends(configPath: string, value: unknown): string[] {
+ if (typeof value !== "string" || !value.startsWith(".")) return [];
const unresolved = nodePath.resolve(nodePath.dirname(configPath), value);
- const candidates = [unresolved, `${unresolved}.json`, nodePath.join(unresolved, "tsconfig.json")];
- return candidates.find((candidate) => fs.existsSync(candidate));
+ return [unresolved, `${unresolved}.json`, nodePath.join(unresolved, "tsconfig.json")];
}
function loadConfig(
@@ -168,8 +173,11 @@ function loadConfig(
return undefined;
}
- const parentPath = resolveExtends(absoluteConfig, raw.extends);
- const parent = parentPath ? loadConfig(workspaceRoot, parentPath, cache, loading) : undefined;
+ let parent: LoadedConfig | undefined;
+ for (const candidate of resolveExtends(absoluteConfig, raw.extends)) {
+ parent = loadConfig(workspaceRoot, candidate, cache, loading);
+ if (parent) break;
+ }
const configDir = nodePath.dirname(absoluteConfig);
const compilerOptions =
raw.compilerOptions && typeof raw.compilerOptions === "object"
@@ -287,6 +295,12 @@ export function createTypeScriptModuleResolver(
if (!specifier || specifier.startsWith(".") || specifier.startsWith("/")) return undefined;
const normalizedSource = normalizePath(sourcePath);
const config = configs.find((candidate) => isWithinDir(normalizedSource, candidate.dir));
+ // Only an *explicit* `paths` mapping licenses an authoritative "no match".
+ // A bare `baseUrl` — and a catch-all `"*"` rule, which is the same thing
+ // spelled differently — matches `react` exactly as readily as `@app/thing`,
+ // so treating its miss as authoritative would delete every third-party
+ // import edge in any repo that sets one.
+ let mappedExplicitly = false;
if (config) {
for (const rule of config.paths) {
const star = matchPathPattern(rule.pattern, specifier);
@@ -299,8 +313,11 @@ export function createTypeScriptModuleResolver(
);
if (matches.length > 0) return matches;
}
- return [];
+ mappedExplicitly = rule.pattern !== "*";
+ break;
}
+ // TypeScript tries `paths` first and then falls back to `baseUrl`, so a
+ // failed rule must not short-circuit the baseUrl lookup.
if (config.baseUrl) {
const matches = resolveCandidate(
nodePath.resolve(config.baseUrl, specifier),
@@ -310,6 +327,6 @@ export function createTypeScriptModuleResolver(
if (matches.length > 0) return matches;
}
}
- return config?.baseUrl ? [] : undefined;
+ return mappedExplicitly ? [] : undefined;
};
}