diff --git a/packages/opencode/src/altimate/tools/dbt-defer-state.ts b/packages/opencode/src/altimate/tools/dbt-defer-state.ts index 9df344ec1b..c1ca71787b 100644 --- a/packages/opencode/src/altimate/tools/dbt-defer-state.ts +++ b/packages/opencode/src/altimate/tools/dbt-defer-state.ts @@ -1,16 +1,306 @@ import z from "zod" +import fs from "fs" +import os from "os" +import path from "path" +import { createScanner, parse as parseJsonc, stripComments, type ParseError } from "jsonc-parser" import { Tool } from "../../tool/tool" +/** Resolve Jinja `{{ env_var('NAME') }}` and `{{ env_var('NAME', 'default') }}`, in single or double quotes. */ +export function resolveEnvVars(value: unknown): unknown { + if (typeof value !== "string") return value + return value.replace( + /\{\{\s*env_var\s*\(\s*(['"])((?:(?!\1)[\s\S])+)\1\s*(?:,\s*(['"])((?:(?!\3)[\s\S])*)\3\s*)?\)\s*\}\}/g, + (_match, _q1: string, envName: string, _q2?: string, defaultValue?: string) => process.env[envName] ?? defaultValue ?? "", + ) +} + +/** dbt's profiles.yml lookup: DBT_PROFILES_DIR, then the project folder, then ~/.dbt. */ +function resolveProfilesPath(projectDir?: string): string { + const envDir = process.env.DBT_PROFILES_DIR + if (envDir) { + const envPath = path.join(envDir, "profiles.yml") + if (fs.existsSync(envPath)) return envPath + } + if (projectDir) { + const projectPath = path.join(projectDir, "profiles.yml") + if (fs.existsSync(projectPath)) return projectPath + } + return path.join(os.homedir(), ".dbt", "profiles.yml") +} + +// The Power User setting this tool reads: `dbt.deferConfigPerProject`, keyed by the dbt project's path +// relative to its workspace folder. +const SETTING = "dbt.deferConfigPerProject" + +export interface DeferSetting { + deferToProduction?: boolean + favorState?: boolean + manifestPathForDeferral?: string + manifestPathType?: string + dbtCoreIntegrationId?: string | number +} + +export interface DeferStateResult { + enabled: boolean + favorState: boolean + /** "local" | "remote" | "none" (no entry for this project, or a manifest type this tool does not know) */ + mode: "local" | "remote" | "none" + /** Directory holding the manifest to defer to; set whenever the configured local one can be used, also while defer is off. */ + stateDir?: string + /** Why the configured defer cannot be used: no usable state directory, remote mode, an unknown manifest type, a settings file that cannot be read. */ + reason?: string + /** The settings key that matched, relative to the workspace folder. */ + settingsKey?: string + settingsFile?: string + profile?: string + defaultTarget?: string + targets: string[] +} + +/** Expand `${env:NAME}` and `${workspaceFolder}`; an unset variable is left as written. */ +function expandVariables(value: string, workspaceFolder: string): string { + return value + .replace(/\$\{env:(.*?)\}/g, (match, name: string) => process.env[name] ?? match) + .replace(/\$\{workspaceFolder\}/g, () => workspaceFolder) +} + +/** A `/* ...` block comment that never closes: stripComments drops it silently, but VS Code cannot read the file. */ +function hasUnterminatedComment(text: string): boolean { + const scanner = createScanner(text, false) + while (scanner.scan() !== 17 /* SyntaxKind.EOF */) { + if (scanner.getTokenError() === 1 /* ScanError.UnexpectedEndOfComment */) return true + } + return false +} + +const UNREADABLE = Symbol("unreadable settings") + +/** The parsed settings; undefined when there is no file, UNREADABLE when there is one that cannot be trusted. */ +function readSettings(file: string): Record | typeof UNREADABLE | undefined { + let text: string + try { + text = fs.readFileSync(file, "utf-8") + } catch (err) { + return (err as NodeJS.ErrnoException).code === "ENOENT" || (err as NodeJS.ErrnoException).code === "ENOTDIR" ? undefined : UNREADABLE + } + // VS Code reads a file with a leading byte order mark as it reads one without. + text = text.replace(/^\uFEFF/, "") + // An empty or whitespace-only file is a settings file with nothing in it. + if (text.trim() === "") return {} + const errors: ParseError[] = [] + // .vscode/settings.json allows comments and trailing commas. + const parsed = parseJsonc(text, errors, { allowTrailingComma: true }) + // A file that does not parse cannot be trusted to say defer is on. + // A file holding only comments has no value to parse, which VS Code reads as empty settings; a stray token is not that. + if (parsed === undefined && stripComments(text).trim() === "" && !hasUnterminatedComment(text)) return {} + if (errors.length > 0) return UNREADABLE + if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return UNREADABLE + return parsed as Record +} + +/** + * Find the entry for the project. The workspace folder is the nearest ancestor of the project (or the + * project itself) whose `.vscode/settings.json` has an entry under the project's relative path. A settings + * file that cannot be read or parsed ends the search. + */ +function lookupDeferSetting(projectDir: string, workspaceDir?: string) { + const real = (p: string) => { + try { + return fs.realpathSync(path.resolve(p)) + } catch { + return path.resolve(p) + } + } + const search = (start: string, workspace?: string) => { + const candidates: string[] = [] + if (workspace) candidates.push(workspace) + else { + for (let dir = start; ; dir = path.dirname(dir)) { + candidates.push(dir) + if (path.dirname(dir) === dir) break + } + } + for (const folder of candidates) { + const file = path.join(folder, ".vscode", "settings.json") + const settings = readSettings(file) + // The nearest settings file that cannot be read ends the search: an outer folder's entry must not stand in for it. + if (settings === UNREADABLE) return { unreadable: file } + const perProject = settings?.[SETTING] + if (!perProject || typeof perProject !== "object") continue + const key = path.relative(folder, start) + const entry = (perProject as Record)[key] + if (entry && typeof entry === "object") { + return { entry: entry as DeferSetting, key, file, folder } + } + // A key naming the project through a symlink: compare the real paths. + const target = real(start) + for (const [k, v] of Object.entries(perProject as Record)) { + if (!v || typeof v !== "object") continue + if (real(path.resolve(folder, k)) === target) return { entry: v as DeferSetting, key: k, file, folder } + } + } + return undefined + } + // The paths as given first, as VS Code builds the key; then with symlinks followed, for a workspace reached through a link. + const lexical = [path.resolve(projectDir), workspaceDir ? path.resolve(workspaceDir) : undefined] as const + const resolved = [real(projectDir), workspaceDir ? real(workspaceDir) : undefined] as const + const passes = lexical[0] === resolved[0] && lexical[1] === resolved[1] ? [lexical] : [lexical, resolved] + let unreadable: { unreadable: string } | undefined + for (const [start, workspace] of passes) { + const found = search(start, workspace) + // An unreadable file ends the search of its own pass; the pass over the real paths may still reach a readable one. + if (found && typeof found.unreadable === "string") { + unreadable ??= { unreadable: found.unreadable } + continue + } + if (found) return found + } + return unreadable +} + +function profilesFile(projectDir: string): string | undefined { + const file = resolveProfilesPath(projectDir) + return fs.existsSync(file) ? file : undefined +} + +async function parseYaml(text: string): Promise { + const mod = await import("yaml") + // dbt reads YAML with PyYAML, which merges `<<` keys. + return (mod.default ?? mod).parse(text, { merge: true }) +} + +/** The project's profile name, that profile's default `target` and its output names. */ +export async function readProfileTargets(projectDir: string) { + const out: { profile?: string; defaultTarget?: string; targets: string[] } = { targets: [] } + try { + const project = await parseYaml(fs.readFileSync(path.join(projectDir, "dbt_project.yml"), "utf-8")) + const profile = resolveEnvVars(project?.profile) + // A profile dbt would render from an expression this does not evaluate is unknown, not that text. + if (typeof profile === "string" && profile !== "" && !/\{[{%#]/.test(profile)) out.profile = profile + } catch { + return out + } + if (!out.profile) return out + const file = profilesFile(projectDir) + if (!file) return out + try { + const profiles = await parseYaml(fs.readFileSync(file, "utf-8")) + const profile = profiles?.[out.profile] + if (profile && typeof profile === "object") { + const target = resolveEnvVars(profile.target) + if (profile.outputs && typeof profile.outputs === "object") out.targets = Object.keys(profile.outputs) + // A target dbt would render from an expression this does not evaluate is unknown, not that text. + if (typeof target === "string" && target !== "" && !/\{[{%#]/.test(target)) out.defaultTarget = target + // With no `target`, dbt runs the output named "default". + else if (profile.target === undefined && out.targets.includes("default")) out.defaultTarget = "default" + } + } catch { + // An unreadable profiles.yml leaves the target unknown. + } + return out +} + +function isFile(file: string): boolean { + try { + return fs.statSync(file).isFile() + } catch { + return false + } +} + +function isDirectory(dir: string): boolean { + try { + return fs.statSync(dir).isDirectory() + } catch { + return false + } +} + +/** The entry for the project; undefined when there is none or the settings file cannot be trusted. */ +export function findDeferSetting(projectDir: string, workspaceDir?: string) { + const found = lookupDeferSetting(projectDir, workspaceDir) + return found && "unreadable" in found ? undefined : found +} + +export async function resolveDeferState(projectDir: string, workspaceDir?: string): Promise { + const root = path.resolve(projectDir) + const targets = await readProfileTargets(root) + const base: DeferStateResult = { enabled: false, favorState: false, mode: "none", ...targets } + const found = lookupDeferSetting(root, workspaceDir) + if (!found) return base + // Said outright: the same silence as "no setting" would send the caller on without --defer and no one the wiser. + if ("unreadable" in found) return { ...base, settingsFile: found.unreadable, reason: `The settings file could not be read or parsed, so defer is left off: ${found.unreadable}` } + + const { entry } = found + const result: DeferStateResult = { + ...base, + settingsKey: found.key, + settingsFile: found.file, + favorState: entry.favorState === true, + } + const wanted = entry.deferToProduction === true + + // Power User writes "local" or "remote"; an entry without a type is local. Anything else is not understood. + const type = entry.manifestPathType + if (type !== undefined && type !== "" && type !== "local" && type !== "remote") { + return { ...result, reason: `Unknown manifestPathType "${String(type)}": expected "local" or "remote".` } + } + if (type === "remote") { + // The request that downloads the manifest is not implemented, so remote mode cannot supply a state dir. + return { ...result, mode: "remote", reason: "Remote defer mode is not supported yet: download the manifest and use a local path." } + } + result.mode = "local" + + // The directory is resolved whether or not defer is on, so a session can still switch it on. + const raw = entry.manifestPathForDeferral + if (typeof raw !== "string" || raw.trim() === "") { + return { ...result, reason: "manifestPathForDeferral is not set." } + } + // Power User expands ${workspaceFolder} to the dbt project's own folder. + const expanded = expandVariables(raw, root) + const stateDir = path.isAbsolute(expanded) ? expanded : path.resolve(root, expanded) + if (!isDirectory(stateDir)) { + return { ...result, reason: `manifestPathForDeferral is not a directory: ${raw}` } + } + if (!isFile(path.join(stateDir, "manifest.json"))) { + return { ...result, reason: `No manifest.json in manifestPathForDeferral: ${raw}` } + } + return { ...result, enabled: wanted, stateDir } +} + export const DbtDeferStateTool = Tool.define("dbt_defer_state", { - description: "dbt Power User's defer-to-production setting for a dbt project, and the profile's default target.", + description: + "Read the dbt Power User defer setting for a dbt project (`dbt.deferConfigPerProject` in .vscode/settings.json) and the project's profile target. Returns whether defer is on, whether to favor state, the state directory to pass as `dbt --defer --state ` (use it only when `enabled` is true), and the profile's default target and target names. Read-only.", parameters: z.object({ - project_dir: z.string().describe("dbt project folder"), + project_dir: z.string().describe("The dbt project directory (the folder holding dbt_project.yml)"), + workspace_dir: z + .string() + .optional() + .describe("The workspace folder that holds .vscode/settings.json (default: the nearest ancestor of project_dir whose settings have an entry for the project)"), }), - async execute() { - return { - title: "dbt_defer_state: not implemented", - metadata: { success: false, error: "not implemented" }, - output: "dbt_defer_state is not implemented in this build of altimate-code yet.", + async execute(args) { + try { + const result = await resolveDeferState(args.project_dir, args.workspace_dir) + const lines = [ + `defer: ${result.enabled ? "on" : "off"}${result.favorState ? " (favor state)" : ""}`, + `mode: ${result.mode}`, + ] + if (result.stateDir) lines.push(`state dir${result.enabled ? "" : " (not used: defer is off)"}: ${result.stateDir}`) + if (result.reason) lines.push(`note: ${result.reason}`) + if (result.defaultTarget) lines.push(`default target: ${result.defaultTarget}`) + if (result.targets.length) lines.push(`targets: ${result.targets.join(", ")}`) + return { + title: `dbt Defer: ${result.enabled ? "on" : "off"}`, + metadata: { success: true, error: undefined as string | undefined, ...result }, + output: lines.join("\n"), + } + } catch (e) { + const msg = e instanceof Error ? e.message : String(e) + return { + title: "dbt Defer: ERROR", + metadata: { success: false, error: msg as string | undefined, enabled: false }, + output: `Failed: ${msg}`, + } } }, }) diff --git a/packages/opencode/test/altimate/dbt-defer-state.test.ts b/packages/opencode/test/altimate/dbt-defer-state.test.ts new file mode 100644 index 0000000000..4fa0fb9500 --- /dev/null +++ b/packages/opencode/test/altimate/dbt-defer-state.test.ts @@ -0,0 +1,400 @@ +import { describe, test, expect, beforeEach, afterEach } from "bun:test" +import fs from "fs" +import os from "os" +import path from "path" +import { Effect } from "effect" +import type { Tool } from "../../src/tool/tool" +import { SessionID, MessageID } from "../../src/session/schema" +import { resolveEnvVars } from "../../src/altimate/tools/dbt-defer-state" +import { DbtDeferStateTool, resolveDeferState, findDeferSetting } from "../../src/altimate/tools/dbt-defer-state" +import { initTool } from "./tool-fixture" + +describe("dbt_defer_state", () => { + let root: string + let savedProfilesDir: string | undefined + + const write = (rel: string, content: string) => { + const file = path.join(root, rel) + fs.mkdirSync(path.dirname(file), { recursive: true }) + fs.writeFileSync(file, content) + } + const settings = (perProject: Record) => + write(".vscode/settings.json", `// comment\n{\n "dbt.deferConfigPerProject": ${JSON.stringify(perProject)},\n}\n`) + + beforeEach(() => { + root = fs.mkdtempSync(path.join(os.tmpdir(), "defer-state-")) + savedProfilesDir = process.env.DBT_PROFILES_DIR + process.env.DBT_PROFILES_DIR = path.join(root, "profiles") + write("profiles/profiles.yml", "shop:\n target: dev\n outputs:\n dev:\n type: duckdb\n prod:\n type: duckdb\n") + write("analytics/dbt_project.yml", "name: shop\nprofile: shop\n") + write("analytics/prod-state/manifest.json", "{}") + }) + + afterEach(() => { + if (savedProfilesDir === undefined) delete process.env.DBT_PROFILES_DIR + else process.env.DBT_PROFILES_DIR = savedProfilesDir + fs.rmSync(root, { recursive: true, force: true }) + }) + + test("local mode returns the state dir, favor state and the profile's targets", async () => { + settings({ analytics: { deferToProduction: true, favorState: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ + enabled: true, + favorState: true, + mode: "local", + stateDir: path.join(root, "analytics", "prod-state"), + settingsKey: "analytics", + defaultTarget: "dev", + targets: ["dev", "prod"], + profile: "shop", + }) + }) + + test("a settings file with a byte order mark is read like one without", async () => { + write(".vscode/settings.json", "\uFEFF" + JSON.stringify({ "dbt.deferConfigPerProject": { analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } } })) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: true, mode: "local" }) + }) + + test("a profile that inherits its target and outputs through a YAML merge key keeps them", async () => { + write("profiles/profiles.yml", "base: &base\n target: dev\n outputs:\n dev:\n type: duckdb\nshop:\n <<: *base\n") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.defaultTarget).toBe("dev") + expect(result.targets).toEqual(["dev"]) + }) + + test("a target written as a Jinja expression this does not evaluate is unknown, not that text", async () => { + write("profiles/profiles.yml", "shop:\n target: '{{ env_var(\"DEFER_TEST_UNSET_ENV\", \"DEV\") | lower }}'\n outputs:\n dev:\n type: duckdb\n") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.defaultTarget).toBeUndefined() + expect(result.targets).toEqual(["dev"]) + }) + + test("a profile with an output named default and no target runs that output, as dbt does", async () => { + write("profiles/profiles.yml", "shop:\n outputs:\n default:\n type: duckdb\n prod:\n type: duckdb\n") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.defaultTarget).toBe("default") + }) + + test("defer off still returns the configured state dir, so a session can switch it on", async () => { + settings({ analytics: { deferToProduction: false, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.mode).toBe("local") + expect(result.stateDir).toBe(path.join(root, "analytics", "prod-state")) + expect(result.defaultTarget).toBe("dev") + }) + + test("a project without an entry reports mode none", async () => { + settings({ other: { deferToProduction: true, manifestPathForDeferral: "x" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + }) + + test("a project at the workspace root uses the empty key", async () => { + write("dbt_project.yml", "name: shop\nprofile: shop\n") + write("prod-state/manifest.json", "{}") + settings({ "": { deferToProduction: true, manifestPathForDeferral: "prod-state" } }) + const result = await resolveDeferState(root) + expect(result.enabled).toBe(true) + expect(result.stateDir).toBe(path.join(root, "prod-state")) + }) + + test("a state path that is not a directory is reported, not used", async () => { + write("analytics/prod-state-file", "x") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state-file" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("not a directory") + }) + + test("a state dir without manifest.json is reported, not used", async () => { + fs.mkdirSync(path.join(root, "analytics", "empty-state")) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/empty-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("manifest.json") + }) + + test("a manifest.json that is a directory is not a usable state", async () => { + fs.mkdirSync(path.join(root, "analytics", "dir-state", "manifest.json"), { recursive: true }) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/dir-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.enabled).toBe(false) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("manifest.json") + }) + + test("${workspaceFolder} is the dbt project's own folder, as in Power User", async () => { + fs.mkdirSync(path.join(root, "prod-state"), { recursive: true }) + fs.writeFileSync(path.join(root, "prod-state", "manifest.json"), "{}") + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.stateDir).toBe(path.join(root, "analytics", "prod-state")) + }) + + test("a settings file that does not parse ends the search; an outer folder's entry does not stand in", async () => { + write("analytics/.vscode/settings.json", '{ "dbt.deferConfigPerProject": oops') + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + expect(result.reason).toContain(path.join(root, "analytics", ".vscode", "settings.json")) + }) + + test("an error about the state path names the setting as written, not an expanded environment variable", async () => { + const saved = process.env.DEFER_TEST_SECRET + process.env.DEFER_TEST_SECRET = "s3cr3t-value" + try { + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${env:DEFER_TEST_SECRET}" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.reason).toContain("${env:DEFER_TEST_SECRET}") + expect(result.reason).not.toContain("s3cr3t-value") + } finally { + if (saved === undefined) delete process.env.DEFER_TEST_SECRET + else process.env.DEFER_TEST_SECRET = saved + } + }) + + test("an unknown manifest type does not enable defer", async () => { + settings({ analytics: { deferToProduction: true, manifestPathType: "typo", manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("typo") + }) + + test("a settings file that does not parse is not trusted", async () => { + write(".vscode/settings.json", '{ "dbt.deferConfigPerProject": { "analytics": { "deferToProduction": true, "manifestPathForDeferral": "${workspaceFolder}/prod-state" } } } oops') + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none" }) + expect(result.reason).toContain("could not be read or parsed") + }) + + test("${env:NAME} in the state path is expanded, and an unset one is left as written", async () => { + const saved = { set: process.env.DEFER_TEST_STATE, unset: process.env.DEFER_TEST_UNSET } + process.env.DEFER_TEST_STATE = path.join(root, "analytics", "prod-state") + delete process.env.DEFER_TEST_UNSET + try { + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${env:DEFER_TEST_STATE}" } }) + expect((await resolveDeferState(path.join(root, "analytics"))).stateDir).toBe(path.join(root, "analytics", "prod-state")) + settings({ analytics: { deferToProduction: true, manifestPathForDeferral: "${env:DEFER_TEST_UNSET}" } }) + expect((await resolveDeferState(path.join(root, "analytics"))).reason).toContain("${env:DEFER_TEST_UNSET}") + } finally { + if (saved.set === undefined) delete process.env.DEFER_TEST_STATE + else process.env.DEFER_TEST_STATE = saved.set + if (saved.unset === undefined) delete process.env.DEFER_TEST_UNSET + else process.env.DEFER_TEST_UNSET = saved.unset + } + }) + + test("an explicit workspace folder and an absolute state path are used", async () => { + write("elsewhere/.vscode/settings.json", JSON.stringify({ "dbt.deferConfigPerProject": { "../analytics": { deferToProduction: true, manifestPathForDeferral: path.join(root, "analytics", "prod-state") } } })) + const result = await resolveDeferState(path.join(root, "analytics"), path.join(root, "elsewhere")) + expect(result).toMatchObject({ enabled: true, stateDir: path.join(root, "analytics", "prod-state"), settingsKey: "../analytics" }) + }) + + test("a project or target written as env_var() is resolved, with its default", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: \"{{ env_var('DEFER_TEST_PROFILE', 'shop') }}\"\n") + write("profiles/profiles.yml", "shop:\n target: \"{{ env_var('DEFER_TEST_TARGET', 'dev') }}\"\n outputs:\n dev:\n type: duckdb\n qa:\n type: duckdb\n") + const saved = { profile: process.env.DEFER_TEST_PROFILE, target: process.env.DEFER_TEST_TARGET } + delete process.env.DEFER_TEST_PROFILE + process.env.DEFER_TEST_TARGET = "qa" + try { + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ profile: "shop", defaultTarget: "qa", targets: ["dev", "qa"] }) + delete process.env.DEFER_TEST_TARGET + expect((await resolveDeferState(path.join(root, "analytics"))).defaultTarget).toBe("dev") + } finally { + if (saved.profile === undefined) delete process.env.DEFER_TEST_PROFILE + else process.env.DEFER_TEST_PROFILE = saved.profile + if (saved.target === undefined) delete process.env.DEFER_TEST_TARGET + else process.env.DEFER_TEST_TARGET = saved.target + } + }) + + test("a profile written as an expression this does not evaluate is unknown, not that text", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: \"{{ var('p') }}\"\n") + write("profiles/profiles.yml", "shop:\n target: dev\n outputs:\n dev:\n type: duckdb\n") + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result.profile).toBeUndefined() + expect(result.targets).toEqual([]) + }) + + test("double-quoted env_var() names and defaults are resolved too", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: '{{ env_var(\"DEFER_TEST_PROFILE\", \"shop\") }}'\n") + write("profiles/profiles.yml", "shop:\n target: '{{ env_var(\"DEFER_TEST_TARGET2\", \"dev\") }}'\n outputs:\n dev:\n type: duckdb\n") + const saved = { profile: process.env.DEFER_TEST_PROFILE, target: process.env.DEFER_TEST_TARGET2 } + try { + delete process.env.DEFER_TEST_PROFILE + delete process.env.DEFER_TEST_TARGET2 + expect(await resolveDeferState(path.join(root, "analytics"))).toMatchObject({ profile: "shop", defaultTarget: "dev" }) + } finally { + if (saved.profile === undefined) delete process.env.DEFER_TEST_PROFILE + else process.env.DEFER_TEST_PROFILE = saved.profile + if (saved.target === undefined) delete process.env.DEFER_TEST_TARGET2 + else process.env.DEFER_TEST_TARGET2 = saved.target + } + }) + + test("a settings file holding only a comment does not hide the workspace entry", () => { + settings({ analytics: { deferToProduction: true } }) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "// nothing yet\n") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("a settings file holding a stray token is still unreadable", async () => { + write(".vscode/settings.json", "}") + expect(findDeferSetting(path.join(root, "analytics"))).toBeUndefined() + write(".vscode/settings.json", JSON.stringify({ "dbt.deferConfigPerProject": { analytics: { deferToProduction: true } } })) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "}") + expect(findDeferSetting(path.join(root, "analytics"))).toBeUndefined() + }) + + test("a settings file with a block comment that never closes is unreadable and ends the search", () => { + settings({ analytics: { deferToProduction: true } }) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "/* unfinished") + expect(findDeferSetting(path.join(root, "analytics"))).toBeUndefined() + }) + + test("an env_var() default may hold the other quote character", async () => { + write("analytics/dbt_project.yml", "name: shop\nprofile: shop\n") + write("profiles/profiles.yml", "shop:\n target: \"{{ env_var('DEFER_TEST_Q', 'say \\\"hi\\\"') }}\"\n outputs:\n dev:\n type: duckdb\n") + delete process.env.DEFER_TEST_Q + expect((await resolveDeferState(path.join(root, "analytics"))).defaultTarget).toBe('say "hi"') + }) + + test("an env_var() default may span lines", () => { + delete process.env.DEFER_TEST_KEY + expect(resolveEnvVars("{{ env_var('DEFER_TEST_KEY', 'line1\nline2') }}")).toBe("line1\nline2") + }) + + test("a settings key naming the project through a symlink matches the real project path", () => { + const outside = fs.mkdtempSync(path.join(path.dirname(root), "ext2-")) + fs.symlinkSync(outside, path.join(root, "linked")) + try { + settings({ linked: { deferToProduction: true } }) + expect(findDeferSetting(outside, root)?.key).toBe("linked") + } finally { + fs.rmSync(path.join(root, "linked")) + fs.rmSync(outside, { recursive: true, force: true }) + } + }) + + test("remote mode is reported as unsupported with no state dir", async () => { + settings({ analytics: { deferToProduction: true, manifestPathType: "remote", dbtCoreIntegrationId: 7 } }) + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "remote" }) + expect(result.stateDir).toBeUndefined() + expect(result.reason).toContain("Remote defer mode") + }) + + test("missing settings file and unreadable profiles give defaults", async () => { + fs.rmSync(path.join(root, ".vscode"), { recursive: true, force: true }) + // An invalid file in place (not a removed directory) keeps the lookup off the developer's ~/.dbt/profiles.yml. + write("profiles/profiles.yml", "shop: [unclosed\n") + const result = await resolveDeferState(path.join(root, "analytics")) + expect(result).toMatchObject({ enabled: false, mode: "none", targets: [] }) + expect(result.defaultTarget).toBeUndefined() + }) + + test("an empty settings file in the project does not hide the workspace entry", () => { + settings({ analytics: { deferToProduction: true } }) + fs.mkdirSync(path.join(root, "analytics", ".vscode"), { recursive: true }) + fs.writeFileSync(path.join(root, "analytics", ".vscode", "settings.json"), "") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("a symlinked workspace path still finds the project's entry", () => { + settings({ analytics: { deferToProduction: true } }) + const link = path.join(root, "..", path.basename(root) + "-link") + fs.symlinkSync(root, link) + try { + expect(findDeferSetting(path.join(root, "analytics"), link)?.key).toBe("analytics") + } finally { + fs.rmSync(link) + } + }) + + test("a symlinked project folder still finds the entry under its path in the workspace", () => { + settings({ analytics: { deferToProduction: true } }) + const outside = fs.mkdtempSync(path.join(path.dirname(root), "ext-")) + fs.rmSync(path.join(root, "analytics"), { recursive: true, force: true }) + fs.symlinkSync(outside, path.join(root, "analytics")) + try { + expect(findDeferSetting(path.join(root, "analytics"), root)?.key).toBe("analytics") + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + } finally { + fs.rmSync(path.join(root, "analytics")) + fs.rmSync(outside, { recursive: true, force: true }) + } + }) + + test("an unparseable settings file above a symlinked project does not hide the entry on the real path", () => { + const real = fs.mkdtempSync(path.join(path.dirname(root), "real-")) + fs.mkdirSync(path.join(real, "analytics")) + fs.mkdirSync(path.join(real, ".vscode")) + fs.writeFileSync(path.join(real, ".vscode", "settings.json"), JSON.stringify({ "dbt.deferConfigPerProject": { analytics: { deferToProduction: true } } })) + const holder = fs.mkdtempSync(path.join(path.dirname(root), "hold-")) + fs.mkdirSync(path.join(holder, ".vscode")) + fs.writeFileSync(path.join(holder, ".vscode", "settings.json"), "{ not json") + const link = path.join(holder, "analytics") + fs.symlinkSync(path.join(real, "analytics"), link) + try { + expect(findDeferSetting(link)?.entry.deferToProduction).toBe(true) + } finally { + fs.rmSync(real, { recursive: true, force: true }) + fs.rmSync(holder, { recursive: true, force: true }) + } + }) + + test("an unparseable settings file above a symlinked project is named when the real path has no entry", async () => { + const real = fs.mkdtempSync(path.join(path.dirname(root), "real-")) + fs.mkdirSync(path.join(real, "analytics")) + const holder = fs.mkdtempSync(path.join(path.dirname(root), "hold-")) + fs.mkdirSync(path.join(holder, ".vscode")) + fs.writeFileSync(path.join(holder, ".vscode", "settings.json"), "{ not json") + const link = path.join(holder, "analytics") + fs.symlinkSync(path.join(real, "analytics"), link) + try { + const result = await resolveDeferState(link) + expect(result.enabled).toBe(false) + expect(result.reason).toContain(path.join(holder, ".vscode", "settings.json")) + } finally { + fs.rmSync(real, { recursive: true, force: true }) + fs.rmSync(holder, { recursive: true, force: true }) + } + }) + + test("settings with comments and trailing commas parse", () => { + settings({ analytics: { deferToProduction: true } }) + expect(findDeferSetting(path.join(root, "analytics"))?.key).toBe("analytics") + }) + + test("the tool answers with the result as metadata and a readable report", async () => { + settings({ analytics: { deferToProduction: true, favorState: true, manifestPathForDeferral: "${workspaceFolder}/prod-state" } }) + const ctx: Tool.Context = { + sessionID: SessionID.make("ses_defer_state"), + messageID: MessageID.make("msg_defer_state"), + agent: "analyst", + abort: new AbortController().signal, + messages: [], + metadata: () => Effect.void, + ask: () => Effect.void, + } + const tool = await initTool(DbtDeferStateTool as any) + const ok = await tool.execute({ project_dir: path.join(root, "analytics") }, ctx) + expect(ok.title).toBe("dbt Defer: on") + expect(ok.metadata).toMatchObject({ success: true, enabled: true, favorState: true, stateDir: path.join(root, "analytics", "prod-state"), defaultTarget: "dev", targets: ["dev", "prod"] }) + expect(ok.output).toContain(`state dir: ${path.join(root, "analytics", "prod-state")}`) + expect(ok.output).toContain("defer: on (favor state)") + }) +})