Skip to content

armgit / Ix

Public Imported from GitHub
0

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

fix(diff): emit the unresolved_target slug, not the sentence (#566)

KageBinarya2cab21ea3bf
diff --git a/ix-cli/src/cli/__tests__/diff-unresolved-target.test.ts b/ix-cli/src/cli/__tests__/diff-unresolved-target.test.ts
new file mode 100644
index 00000000..950ae62f
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/diff-unresolved-target.test.ts
@@ -0,0 +1,90 @@
+/**
+ * diff-unresolved-target.test.ts — Ix #566
+ *
+ * `ix diff` was the one command that resolves a target and did not emit the
+ * shared `unresolved_target` record. Its `--format json` branch put the human
+ * sentence in the `error` field, where every other command puts the machine
+ * slug, and `docs/llm-format.md` says a target that does not exist is always
+ * `unresolved_target`.
+ */
+import { Command } from "commander";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("../../client/api.js", () => ({
+ IxClient: class {
+ async workspaceSystem() { return { systemId: null }; }
+ async search() { return []; }
+ },
+}));
+
+async function run(args: string[]): Promise<{ stdout: string; stderr: string }> {
+ const { registerDiffCommand } = await import("../commands/diff.js");
+ const program = new Command();
+ program.name("ix").exitOverride();
+ registerDiffCommand(program);
+ const stdout: string[] = [];
+ const stderr: string[] = [];
+ const log = vi.spyOn(console, "log").mockImplementation((...parts) => stdout.push(parts.join(" ")));
+ const error = vi.spyOn(console, "error").mockImplementation((...parts) => stderr.push(parts.join(" ")));
+ try {
+ await program.parseAsync(args, { from: "user" });
+ } finally {
+ log.mockRestore();
+ error.mockRestore();
+ }
+ return { stdout: stdout.join("\n"), stderr: stderr.join("\n") };
+}
+
+describe("ix diff — unresolved target", () => {
+ let savedExitCode: number | string | undefined;
+
+ beforeEach(() => {
+ savedExitCode = process.exitCode;
+ process.exitCode = undefined;
+ });
+
+ afterEach(() => {
+ process.exitCode = savedExitCode;
+ });
+
+ // The symbol path: resolveEntityFull finds nothing.
+ it("emits the shared slug and message as json for a symbol target", async () => {
+ const result = await run(["diff", "3", "5", "DefinitelyMissing", "--format", "json"]);
+
+ expect(JSON.parse(result.stdout)).toEqual({
+ error: "unresolved_target",
+ message: 'No entity found matching "DefinitelyMissing".',
+ });
+ });
+
+ // The file-like path: a target with an extension skips resolveEntityFull and
+ // goes through resolveFileOrEntity, which is a separate branch with its own
+ // copy of the record. Both were wrong; both must be right.
+ it("emits the shared slug and message as json for a file-like target", async () => {
+ const result = await run(["diff", "3", "5", "DefinitelyMissing.ts", "--format", "json"]);
+
+ expect(JSON.parse(result.stdout)).toEqual({
+ error: "unresolved_target",
+ message: 'No entity found matching "DefinitelyMissing.ts".',
+ });
+ });
+
+ it("emits an llm error record with the same wording as every other command", async () => {
+ const result = await run(["diff", "3", "5", "DefinitelyMissing", "--format", "llm"]);
+
+ expect(result.stdout).toBe(
+ 'error code=unresolved_target message="No entity found matching \\"DefinitelyMissing\\"."',
+ );
+ });
+
+ // Deliberate scope boundary, not an oversight. Making `ix diff` exit non-zero
+ // is a breaking change for the plugins that call it (CONTRIBUTING -> CLI
+ // Standards -> Exit codes); ix-cursor-plugin's ix_diff tool routes on the
+ // exit status. That half is queued behind Ix #547. This test fails the moment
+ // someone adds the exit code without doing that work.
+ it("does not yet exit non-zero — that half is queued behind the plugin fixes", async () => {
+ await run(["diff", "3", "5", "DefinitelyMissing", "--format", "json"]);
+
+ expect(process.exitCode).toBeUndefined();
+ });
+});
diff --git a/ix-cli/src/cli/commands/diff.ts b/ix-cli/src/cli/commands/diff.ts
index 0611f512..073ad1c9 100644
--- a/ix-cli/src/cli/commands/diff.ts
+++ b/ix-cli/src/cli/commands/diff.ts
@@ -11,7 +11,7 @@ import { getEndpoint } from "../config.js";
import { resolveFileOrEntity, resolveEntityFull, printResolved, looksFileLike, type ResolvedEntity } from "../resolve.js";
import { formatDiff, relativePath } from "../format.js";
import { llmLine, llmError } from "../llm.js";
-import { reportFailure } from "../ui.js";
+import { reportFailure, unresolvedTargetMessage, unresolvedTargetRecord } from "../ui.js";
import { parsePickOption } from "../options.js";
/**
@@ -511,20 +511,20 @@ export function registerDiffCommand(program: Command): void {
console.log(JSON.stringify(fullResult.result, null, 2));
return;
} else {
- console.log(JSON.stringify({ error: `No entity found matching "${target}".` }, null, 2));
+ console.log(JSON.stringify(unresolvedTargetRecord(target), null, 2));
return;
}
} else {
resolved = await resolveFileOrEntity(client, target, resolveOpts);
if (!resolved) {
- console.log(JSON.stringify({ error: `No entity found matching "${target}".` }, null, 2));
+ console.log(JSON.stringify(unresolvedTargetRecord(target), null, 2));
return;
}
}
} else {
resolved = await resolveFileOrEntity(client, target, resolveOpts);
if (!resolved) {
- if (opts.format === "llm") console.log(llmError("unresolved_target", `No entity resolved for "${target}".`));
+ if (opts.format === "llm") console.log(llmError("unresolved_target", unresolvedTargetMessage(target)));
return;
}
if (opts.format === "text") printResolved(resolved);
diff --git a/ix-cli/src/cli/ui.ts b/ix-cli/src/cli/ui.ts
index 6cc33bdd..6f6d35a5 100644
--- a/ix-cli/src/cli/ui.ts
+++ b/ix-cli/src/cli/ui.ts
@@ -133,10 +133,27 @@ export function reportFailure(code: string, message: string, format?: string): v
}
/** Report a resolver miss without corrupting machine-readable stdout. */
+/**
+ * The one message for a target that does not exist.
+ *
+ * Split out from `reportUnresolvedTarget` so a command can emit the shared
+ * record *without* the exit code. `ix diff` needs exactly that: its payload is
+ * wrong today and can be fixed now, while the exit-code half is queued behind
+ * the plugin work (CONTRIBUTING -> CLI Standards -> Exit codes).
+ */
+export function unresolvedTargetMessage(target: string): string {
+ return `No entity found matching "${target}".`;
+}
+
+/** The `--format json` record for a target that does not exist. */
+export function unresolvedTargetRecord(target: string): { error: string; message: string } {
+ return { error: "unresolved_target", message: unresolvedTargetMessage(target) };
+}
+
export function reportUnresolvedTarget(target: string, format?: string): void {
- const message = `No entity found matching "${target}".`;
+ const message = unresolvedTargetMessage(target);
if (format === "json") {
- console.log(JSON.stringify({ error: "unresolved_target", message }, null, 2));
+ console.log(JSON.stringify(unresolvedTargetRecord(target), null, 2));
} else if (format === "llm") {
console.log(llmError("unresolved_target", message));
}