@intentius/chant 0.124.21 → 0.124.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. package/dist/audit/rules-doc.js +1 -1
  2. package/dist/cli/handlers/graph.js +24 -1
  3. package/dist/codegen/docs-pages.d.ts +1 -0
  4. package/dist/codegen/docs-pages.d.ts.map +1 -1
  5. package/dist/codegen/docs-pages.js +1 -0
  6. package/dist/codegen/docs-sections.d.ts +2 -0
  7. package/dist/codegen/docs-sections.d.ts.map +1 -1
  8. package/dist/codegen/docs-sections.js +8 -0
  9. package/dist/codegen/docs-types.d.ts +7 -0
  10. package/dist/codegen/docs-types.d.ts.map +1 -1
  11. package/dist/codegen/docs.d.ts.map +1 -1
  12. package/dist/codegen/docs.js +20 -0
  13. package/dist/fold/subset.d.ts +3 -1
  14. package/dist/fold/subset.d.ts.map +1 -1
  15. package/dist/fold/subset.js +3 -1
  16. package/dist/graph-ir.d.ts +43 -0
  17. package/dist/graph-ir.d.ts.map +1 -1
  18. package/dist/graph-ir.js +27 -0
  19. package/dist/lexicon.d.ts +25 -1
  20. package/dist/lexicon.d.ts.map +1 -1
  21. package/dist/lint/rules/evl004-spread-non-const.d.ts.map +1 -1
  22. package/dist/lint/rules/evl004-spread-non-const.js +19 -1
  23. package/dist/wave-runtime.mjs +222 -206
  24. package/dist/workspace/checks/links.d.ts +5 -0
  25. package/dist/workspace/checks/links.d.ts.map +1 -1
  26. package/dist/workspace/checks/links.js +54 -8
  27. package/dist/workspace/checks.d.ts +4 -1
  28. package/dist/workspace/checks.d.ts.map +1 -1
  29. package/dist/workspace/checks.js +6 -2
  30. package/dist/workspace/compose-graph.d.ts +4 -1
  31. package/dist/workspace/compose-graph.d.ts.map +1 -1
  32. package/dist/workspace/compose-graph.js +3 -2
  33. package/dist/workspace/dbt-project-reader.d.ts +36 -0
  34. package/dist/workspace/dbt-project-reader.d.ts.map +1 -0
  35. package/dist/workspace/dbt-project-reader.js +290 -0
  36. package/dist/workspace/graph-cli.d.ts.map +1 -1
  37. package/dist/workspace/graph-cli.js +3 -1
  38. package/dist/workspace/graph.schema.json +41 -3
  39. package/dist/workspace/intent.js +7 -2
  40. package/dist/workspace/kind-link-readers.d.ts +84 -0
  41. package/dist/workspace/kind-link-readers.d.ts.map +1 -0
  42. package/dist/workspace/kind-link-readers.js +53 -0
  43. package/dist/workspace/kinds.d.ts +15 -0
  44. package/dist/workspace/kinds.d.ts.map +1 -1
  45. package/dist/workspace/kinds.js +11 -0
  46. package/dist/workspace/lexicon-outputs.d.ts +63 -0
  47. package/dist/workspace/lexicon-outputs.d.ts.map +1 -0
  48. package/dist/workspace/lexicon-outputs.js +241 -0
  49. package/dist/workspace/links.d.ts +29 -7
  50. package/dist/workspace/links.d.ts.map +1 -1
  51. package/dist/workspace/links.js +109 -34
  52. package/dist/workspace/member-handles.d.ts +18 -1
  53. package/dist/workspace/member-handles.d.ts.map +1 -1
  54. package/dist/workspace/member-handles.js +31 -14
  55. package/dist/workspace/records-checkout.d.ts +11 -1
  56. package/dist/workspace/records-checkout.d.ts.map +1 -1
  57. package/dist/workspace/records-checkout.js +38 -2
  58. package/dist/workspace/status.d.ts.map +1 -1
  59. package/dist/workspace/status.js +3 -2
  60. package/dist/workspace/workspace-kinds.schema.json +5 -0
  61. package/package.json +1 -1
  62. package/src/audit/rules-doc.ts +1 -1
  63. package/src/cli/handlers/graph.ts +22 -1
  64. package/src/codegen/docs-pages.ts +1 -0
  65. package/src/codegen/docs-sections.ts +8 -0
  66. package/src/codegen/docs-site/AgentPrompt.astro +153 -0
  67. package/src/codegen/docs-types.ts +7 -0
  68. package/src/codegen/docs.ts +27 -0
  69. package/src/fold/subset.ts +3 -1
  70. package/src/graph-ir.test.ts +23 -1
  71. package/src/graph-ir.ts +65 -0
  72. package/src/lexicon.ts +26 -1
  73. package/src/lint/rules/evl004-spread-non-const.test.ts +18 -0
  74. package/src/lint/rules/evl004-spread-non-const.ts +16 -1
  75. package/src/wave-loop-golden.test.ts +3 -0
  76. package/src/workspace/checks/links.ts +57 -8
  77. package/src/workspace/checks.e2e.test.ts +1 -1
  78. package/src/workspace/checks.ts +9 -2
  79. package/src/workspace/compose-graph.ts +5 -3
  80. package/src/workspace/dbt-kind.e2e.test.ts +95 -0
  81. package/src/workspace/dbt-kind.test.ts +283 -0
  82. package/src/workspace/dbt-project-reader.ts +291 -0
  83. package/src/workspace/graph-cli.ts +3 -1
  84. package/src/workspace/graph.schema.json +41 -3
  85. package/src/workspace/intent.ts +7 -2
  86. package/src/workspace/kind-link-readers.ts +109 -0
  87. package/src/workspace/kinds.ts +21 -1
  88. package/src/workspace/lexicon-outputs.ts +272 -0
  89. package/src/workspace/links.test.ts +22 -0
  90. package/src/workspace/links.ts +129 -34
  91. package/src/workspace/member-handles.ts +28 -8
  92. package/src/workspace/records-checkout.ts +36 -2
  93. package/src/workspace/records-uncommitted.test.ts +5 -1
  94. package/src/workspace/status.ts +3 -2
  95. package/src/workspace/workspace-kinds.schema.json +5 -0
@@ -0,0 +1,291 @@
1
+ /**
2
+ * The `dbt-project` link reader (#3932): a dbt project's sources, as links to
3
+ * the tables and columns a sql member exposes (#3931).
4
+ *
5
+ * The reader parses YAML and nothing else. It never runs dbt, never renders
6
+ * Jinja and never reads `profiles.yml` or a target, so the links it derives
7
+ * are the same in every environment.
8
+ *
9
+ * - The project is `dbt_project.yml`. Sources are read from every `.yml` and
10
+ * `.yaml` file under its `model-paths` (`source-paths` in older projects;
11
+ * `models` when it names neither), as dbt reads them.
12
+ * - Each enabled source table links to `<schema>.<table>`, and each of its
13
+ * `columns` to `<schema>.<table>.<column>`. The schema is the source's
14
+ * `schema`, or the source's `name` when it sets none, which is dbt's own
15
+ * fallback. The table is its `identifier`, or its `name`. `database` is not
16
+ * part of the name: a Postgres output has none, and dbt-clickhouse reads a
17
+ * source's `schema` as the ClickHouse database.
18
+ * - Case follows the member's `dialect` field. For `postgres`, an unquoted
19
+ * name folds to lowercase and a quoted one keeps its case. dbt quotes a
20
+ * source's schema and identifier on Postgres unless `quoting:` (in
21
+ * `dbt_project.yml`, on the source, or on the table) turns it off, and a
22
+ * column is quoted only with `quote: true`. For `clickhouse`, names are
23
+ * kept as written.
24
+ * - A value that is a Jinja template (`{{ ... }}` or `{% ... %}`) is not
25
+ * resolved: the reader records a gap and leaves that source, table or
26
+ * column out, rather than guess what a target would render.
27
+ * - The producer is the member the `sqlMember` field names. Unset, each link
28
+ * leaves the producer to `resolveLinks`, which picks the one member that
29
+ * exposes the table.
30
+ */
31
+
32
+ import { posix } from "node:path";
33
+ import { parseYAMLDocument, YAMLParseError } from "../yaml";
34
+ import type { KindLinkRead, KindReadGap, KindReadLink, KindReaderInput } from "./kind-link-readers";
35
+ import { joinPath, skippedDir, type WorkspaceTree } from "./tree";
36
+
37
+ export const DBT_PROJECT_FILE = "dbt_project.yml";
38
+ const DEFAULT_MODEL_PATHS = ["models"];
39
+
40
+ /** The dialects whose naming the reader follows; the member's `dialect` field picks one. */
41
+ export const DBT_DIALECTS = ["postgres", "clickhouse"] as const;
42
+ type Dialect = (typeof DBT_DIALECTS)[number];
43
+
44
+ interface Quoting {
45
+ schema: boolean;
46
+ identifier: boolean;
47
+ }
48
+
49
+ const isTemplated = (v: string) => /\{\{|\{%/.test(v);
50
+ const isObject = (v: unknown): v is Record<string, unknown> => v !== null && typeof v === "object" && !Array.isArray(v);
51
+
52
+ export function readDbtProject(input: KindReaderInput): KindLinkRead {
53
+ const { dir, tree, fields } = input;
54
+ const links: KindReadLink[] = [];
55
+ const gaps: KindReadGap[] = [];
56
+ const result: KindLinkRead = { reader: "dbt-project", links, gaps, producerField: "sqlMember" };
57
+ const projectFile = joinPath(dir, DBT_PROJECT_FILE);
58
+
59
+ const producer = typeof fields?.sqlMember === "string" && fields.sqlMember !== "" ? fields.sqlMember : null;
60
+ const dialect: Dialect = fields?.dialect === "clickhouse" ? "clickhouse" : "postgres";
61
+
62
+ let project: unknown;
63
+ try {
64
+ project = parseYAMLDocument(tree.read(projectFile));
65
+ } catch (err) {
66
+ gaps.push({ file: projectFile, line: err instanceof YAMLParseError ? err.line : 1, message: `${DBT_PROJECT_FILE} could not be read (${(err as Error).message}), so no source was read` });
67
+ return result;
68
+ }
69
+ if (!isObject(project)) {
70
+ gaps.push({ file: projectFile, line: 1, message: `${DBT_PROJECT_FILE} is not a mapping, so no source was read` });
71
+ return result;
72
+ }
73
+ const projectLines = lines(tree.read(projectFile));
74
+
75
+ // Where sources live: model-paths, or the older source-paths.
76
+ const key = project["model-paths"] !== undefined ? "model-paths" : project["source-paths"] !== undefined ? "source-paths" : null;
77
+ const rawPaths = key ? project[key] : DEFAULT_MODEL_PATHS;
78
+ const paths: string[] = [];
79
+ if (!Array.isArray(rawPaths)) {
80
+ gaps.push({ file: projectFile, line: keyLine(projectLines, key ?? ""), message: `${key} is not a list, so no source was read` });
81
+ return result;
82
+ }
83
+ for (const p of rawPaths) {
84
+ if (typeof p !== "string" || isTemplated(p)) {
85
+ gaps.push({ file: projectFile, line: keyLine(projectLines, key ?? ""), message: `${key} entry ${JSON.stringify(p)} is not a plain path; its sources were not read` });
86
+ continue;
87
+ }
88
+ const at = posix.normalize(joinPath(dir, p));
89
+ if (at.startsWith("../") || at === ".." || posix.isAbsolute(p)) {
90
+ gaps.push({ file: projectFile, line: keyLine(projectLines, key ?? ""), message: `${key} entry ${p} lies outside the workspace; its sources were not read` });
91
+ continue;
92
+ }
93
+ paths.push(at === "." ? "" : at);
94
+ }
95
+
96
+ const quoting = readQuoting({ schema: true, identifier: true }, project.quoting, projectFile, keyLine(projectLines, "quoting"), "dbt_project.yml quoting", gaps);
97
+
98
+ const files = new Set<string>();
99
+ for (const p of paths) for (const f of yamlFiles(tree, p)) files.add(f);
100
+ const name = (v: string, quoted: boolean) => (dialect === "postgres" && !quoted ? v.toLowerCase() : v);
101
+
102
+ for (const file of [...files].sort()) {
103
+ let text: string;
104
+ let doc: unknown;
105
+ try {
106
+ text = tree.read(file);
107
+ doc = parseYAMLDocument(text);
108
+ } catch (err) {
109
+ gaps.push({ file, line: err instanceof YAMLParseError ? err.line : 1, message: `the file could not be read as YAML (${(err as Error).message}); any sources in it were not read` });
110
+ continue;
111
+ }
112
+ if (!isObject(doc) || doc.sources === undefined || doc.sources === null) continue;
113
+ const fileLines = lines(text);
114
+ const sourcesLine = keyLine(fileLines, "sources");
115
+ if (!Array.isArray(doc.sources)) {
116
+ gaps.push({ file, line: sourcesLine, message: "sources is not a list; its sources were not read" });
117
+ continue;
118
+ }
119
+ let cursor = sourcesLine;
120
+ for (const source of doc.sources) {
121
+ if (!isObject(source)) continue;
122
+ const sourceName = scalar(source.name);
123
+ const sourceLine = nameLine(fileLines, sourceName, cursor);
124
+ cursor = sourceLine;
125
+ if (sourceName === undefined) {
126
+ gaps.push({ file, line: sourceLine, message: "a source has no name; its tables were not read" });
127
+ continue;
128
+ }
129
+ const where = `source ${sourceName}`;
130
+ if (isTemplated(sourceName)) {
131
+ gaps.push({ file, line: sourceLine, message: `${where}: its name is a Jinja template, which chant does not render; its tables were not read` });
132
+ continue;
133
+ }
134
+ const enabled = readEnabled(source, file, sourceLine, where, gaps);
135
+ if (enabled !== true) continue;
136
+ const schema = source.schema === undefined || source.schema === null ? sourceName : scalar(source.schema);
137
+ if (schema === undefined || isTemplated(schema)) {
138
+ gaps.push({
139
+ file,
140
+ line: valueLine(fileLines, "schema", sourceLine),
141
+ message: `${where}: schema ${JSON.stringify(source.schema)} is a Jinja template or not a name; chant reads names as declared and never renders them for a target, so its tables were not read`,
142
+ });
143
+ continue;
144
+ }
145
+ const sourceQuoting = readQuoting(quoting, source.quoting, file, sourceLine, `${where} quoting`, gaps);
146
+ if (source.tables === undefined || source.tables === null) continue;
147
+ if (!Array.isArray(source.tables)) {
148
+ gaps.push({ file, line: sourceLine, message: `${where}: tables is not a list; its tables were not read` });
149
+ continue;
150
+ }
151
+ let tableCursor = sourceLine;
152
+ for (const table of source.tables) {
153
+ if (!isObject(table)) continue;
154
+ const tableName = scalar(table.name);
155
+ const tableLine = nameLine(fileLines, tableName, tableCursor);
156
+ tableCursor = tableLine;
157
+ if (tableName === undefined) {
158
+ gaps.push({ file, line: tableLine, message: `${where}: a table has no name; it was not read` });
159
+ continue;
160
+ }
161
+ const tableWhere = `${where} table ${tableName}`;
162
+ if (isTemplated(tableName)) {
163
+ gaps.push({ file, line: tableLine, message: `${tableWhere}: its name is a Jinja template, which chant does not render; it was not read` });
164
+ continue;
165
+ }
166
+ if (readEnabled(table, file, tableLine, tableWhere, gaps) !== true) continue;
167
+ const identifier = table.identifier === undefined || table.identifier === null ? tableName : scalar(table.identifier);
168
+ if (identifier === undefined || isTemplated(identifier)) {
169
+ gaps.push({
170
+ file,
171
+ line: valueLine(fileLines, "identifier", tableLine),
172
+ message: `${tableWhere}: identifier ${JSON.stringify(table.identifier)} is a Jinja template or not a name, which chant does not render; it was not read`,
173
+ });
174
+ continue;
175
+ }
176
+ const q = readQuoting(sourceQuoting, table.quoting, file, tableLine, `${tableWhere} quoting`, gaps);
177
+ const tableOutput = `${name(schema, q.schema)}.${name(identifier, q.identifier)}`;
178
+ links.push({ member: producer, output: tableOutput, file, line: tableLine });
179
+ if (table.columns === undefined || table.columns === null) continue;
180
+ if (!Array.isArray(table.columns)) {
181
+ gaps.push({ file, line: tableLine, message: `${tableWhere}: columns is not a list; its columns were not read` });
182
+ continue;
183
+ }
184
+ let columnCursor = tableLine;
185
+ for (const column of table.columns) {
186
+ if (!isObject(column)) continue;
187
+ const columnName = scalar(column.name);
188
+ const columnLine = nameLine(fileLines, columnName, columnCursor);
189
+ columnCursor = columnLine;
190
+ if (columnName === undefined || isTemplated(columnName)) {
191
+ gaps.push({ file, line: columnLine, message: `${tableWhere}: column ${JSON.stringify(column.name)} is a Jinja template or not a name, which chant does not render; it was not read` });
192
+ continue;
193
+ }
194
+ const quoted = column.quote === true;
195
+ links.push({ member: producer, output: `${tableOutput}.${name(columnName, quoted)}`, anchor: tableOutput, file, line: columnLine });
196
+ }
197
+ }
198
+ }
199
+ }
200
+ return result;
201
+ }
202
+
203
+ /** A name as text: a string, or a number or boolean YAML read as one. */
204
+ function scalar(v: unknown): string | undefined {
205
+ if (typeof v === "string") return v;
206
+ if (typeof v === "number" || typeof v === "boolean") return String(v);
207
+ return undefined;
208
+ }
209
+
210
+ /** Whether an entry is enabled: `enabled`, or `config.enabled`. A templated value is a gap, and the entry is left out. */
211
+ function readEnabled(entry: Record<string, unknown>, file: string, line: number, where: string, gaps: KindReadGap[]): boolean {
212
+ const config = isObject(entry.config) ? entry.config : {};
213
+ const raw = entry.enabled ?? config.enabled;
214
+ if (raw === undefined || raw === null || raw === true) return true;
215
+ if (raw === false) return false;
216
+ gaps.push({ file, line, message: `${where}: enabled is ${JSON.stringify(raw)}, which chant does not render; it was not read` });
217
+ return false;
218
+ }
219
+
220
+ /** `base` with a `quoting:` block's schema and identifier applied. A value that isn't a boolean is a gap and keeps the base. */
221
+ function readQuoting(base: Quoting, raw: unknown, file: string, line: number, where: string, gaps: KindReadGap[]): Quoting {
222
+ if (raw === undefined || raw === null) return base;
223
+ if (!isObject(raw)) {
224
+ gaps.push({ file, line, message: `${where} is not a mapping; the quoting above it applies` });
225
+ return base;
226
+ }
227
+ const out = { ...base };
228
+ for (const key of ["schema", "identifier"] as const) {
229
+ const v = raw[key];
230
+ if (v === undefined) continue;
231
+ if (typeof v === "boolean") out[key] = v;
232
+ else gaps.push({ file, line, message: `${where}.${key} is ${JSON.stringify(v)}, not true or false; the quoting above it applies` });
233
+ }
234
+ return out;
235
+ }
236
+
237
+ /** Every `.yml` and `.yaml` file under tree-relative directory `dir`, skipping dot directories and node_modules. */
238
+ function yamlFiles(tree: WorkspaceTree, dir: string): string[] {
239
+ if (dir !== "" && tree.stat(dir) !== "dir") return [];
240
+ const out: string[] = [];
241
+ const walk = (at: string) => {
242
+ for (const e of tree.list(at) ?? []) {
243
+ const path = joinPath(at, e.name);
244
+ if (e.type === "dir") {
245
+ if (!skippedDir(e.name)) walk(path);
246
+ } else if (/\.ya?ml$/.test(e.name)) {
247
+ out.push(path);
248
+ }
249
+ }
250
+ };
251
+ walk(dir);
252
+ return out;
253
+ }
254
+
255
+ function lines(text: string): string[] {
256
+ return text.replace(/\r\n/g, "\n").split("\n");
257
+ }
258
+
259
+ function escapeRegExp(text: string): string {
260
+ return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
261
+ }
262
+
263
+ /** The line (from 1) of top-level key `key`, or 1. */
264
+ function keyLine(fileLines: string[], key: string): number {
265
+ const re = new RegExp(`^${escapeRegExp(key)}\\s*:`);
266
+ const i = fileLines.findIndex((l) => re.test(l));
267
+ return i < 0 ? 1 : i + 1;
268
+ }
269
+
270
+ /**
271
+ * The line of the first `name: <value>` entry after line `from`, or `from`
272
+ * itself. Sources, tables and columns are listed in file order, so
273
+ * searching on from the parent's line finds each one's own line. A file in
274
+ * flow style has no such lines, and its entries are reported at the parent.
275
+ */
276
+ function nameLine(fileLines: string[], value: string | undefined, from: number): number {
277
+ return value === undefined ? from : valueLineOf(fileLines, "name", value, from);
278
+ }
279
+
280
+ /** The line of the first `<key>:` after line `from`, or `from`. */
281
+ function valueLine(fileLines: string[], key: string, from: number): number {
282
+ const re = new RegExp(`^\\s*(?:-\\s+)?${escapeRegExp(key)}\\s*:`);
283
+ for (let i = Math.max(from, 0); i < fileLines.length; i++) if (re.test(fileLines[i])) return i + 1;
284
+ return from;
285
+ }
286
+
287
+ function valueLineOf(fileLines: string[], key: string, value: string, from: number): number {
288
+ const re = new RegExp(`^\\s*(?:-\\s+)?${escapeRegExp(key)}\\s*:\\s*(["']?)${escapeRegExp(value)}\\1\\s*(?:#.*)?$`);
289
+ for (let i = Math.max(from, 0); i < fileLines.length; i++) if (re.test(fileLines[i])) return i + 1;
290
+ return from;
291
+ }
@@ -36,6 +36,7 @@ import { openGraphCache, splitCached, storeReads, withoutUnits } from "./graph-c
36
36
  import { readDeclaration, readerVersion, WORKSPACE_ERROR_CODES, WorkspaceReadError, type ErrorLocation } from "./declaration";
37
37
  import { describePlan, emitDocument, executePlan, memberStatus, planJson, planMembers, type MemberPlan, type Toolchain, type UnitResult } from "./member-commands";
38
38
  import { loadKindRegistry } from "./kinds";
39
+ import { readKindLinks } from "./kind-link-readers";
39
40
  import { prepareKindReaders, type PreparedReaders } from "./kind-readers";
40
41
  import { recordLinkRows } from "./record-assets";
41
42
  import { RecordReadError } from "./records";
@@ -258,7 +259,8 @@ export async function workspaceGraph(query: GraphQuery): Promise<GraphResult> {
258
259
  member.stamp = split?.stamps.get(member.name) ?? null;
259
260
  }
260
261
  // Links (#2539) resolve against the declaration that was read, the revision's for --at, and the kinds installed now.
261
- const graph = composeWorkspaceGraph({ name: declaration.name, root: located.root }, inputs, { declaration, kinds });
262
+ // A kind's link reader (#3932) reads the member's files in the tree read, as check does.
263
+ const graph = composeWorkspaceGraph({ name: declaration.name, root: located.root }, inputs, { declaration, kinds, read: readKindLinks(declaration, located.tree, kinds) });
262
264
  // The box's factory and listing (#3146), from the declaration read, so --at gives them at that revision.
263
265
  for (const member of graph.members) member.box = graphBox(declaration.members.find((m) => m.name === member.name)?.box ?? null, located.tree, declaration.members);
264
266
  // Nested workspaces (#2551, ws-071): read-only, through their own chant workspace graph.
@@ -181,6 +181,13 @@
181
181
  },
182
182
  "member": {
183
183
  "type": "string"
184
+ },
185
+ "previously": {
186
+ "type": "array",
187
+ "items": {
188
+ "type": "string"
189
+ },
190
+ "description": "Names this output had before, from a rename hint a lexicon read in the member's source (a sql `-- previously:` comment). A link to one of them is told the new name."
184
191
  }
185
192
  }
186
193
  }
@@ -636,8 +643,23 @@
636
643
  "origin": {
637
644
  "enum": [
638
645
  "declared",
646
+ "kind-reader",
639
647
  "inferred:joinKey"
640
- ]
648
+ ],
649
+ "description": "How the row was found: declared in chant.workspace.json, read from the member's own files by its kind's link reader (#3932), or inferred by joinKey()."
650
+ },
651
+ "reader": {
652
+ "type": "string",
653
+ "description": "A kind-reader row (#3932): the link reader that found it, such as dbt-project."
654
+ },
655
+ "file": {
656
+ "type": "string",
657
+ "description": "A kind-reader row (#3932): the file that states the link, from the workspace root."
658
+ },
659
+ "line": {
660
+ "type": "integer",
661
+ "minimum": 1,
662
+ "description": "A kind-reader row (#3932): the line of file that states the link."
641
663
  },
642
664
  "resolves": {
643
665
  "const": "source"
@@ -728,8 +750,23 @@
728
750
  "origin": {
729
751
  "enum": [
730
752
  "declared",
753
+ "kind-reader",
731
754
  "inferred:joinKey"
732
- ]
755
+ ],
756
+ "description": "How the row was found: declared in chant.workspace.json, read from the member's own files by its kind's link reader (#3932), or inferred by joinKey()."
757
+ },
758
+ "reader": {
759
+ "type": "string",
760
+ "description": "A kind-reader row (#3932): the link reader that found it, such as dbt-project."
761
+ },
762
+ "file": {
763
+ "type": "string",
764
+ "description": "A kind-reader row (#3932): the file that states the link, from the workspace root."
765
+ },
766
+ "line": {
767
+ "type": "integer",
768
+ "minimum": 1,
769
+ "description": "A kind-reader row (#3932): the line of file that states the link."
733
770
  },
734
771
  "resolves": {
735
772
  "const": "source"
@@ -746,7 +783,8 @@
746
783
  "description": "The consumer's parameter node, when known."
747
784
  },
748
785
  "input": {
749
- "type": "string"
786
+ "type": "string",
787
+ "description": "The consumer's parameter the join would feed; for a kind-reader row (#3932), the output its file reads, which no single member was found to produce."
750
788
  },
751
789
  "status": {
752
790
  "const": "ambiguous"
@@ -77,7 +77,7 @@ import { followSquashes, squashLineOrigins, type Forge, type SquashFollow } from
77
77
  import { memberDirOnDisk, memberSymbolResolvers, type SymbolPluginLoader } from "./lexicon-symbols";
78
78
  import { resolveSymbol, symbolResolverFor, type SymbolResolver } from "./symbols";
79
79
  import { readTrailerJoins, type CommitTrailerJoins } from "./trailer-joins";
80
- import { joinPath, skippedDir, type WorkspaceTree } from "./tree";
80
+ import { joinPath, skippedDir, workingTree, type WorkspaceTree } from "./tree";
81
81
  import { activeAttestors, type ProvenanceLevel } from "./trust/attestor";
82
82
  import { commitProvenance, policyAtBase, resolveBase } from "./trust/provenance";
83
83
  import { locateWorkspace, type LocatedWorkspace } from "./which-chant";
@@ -1722,7 +1722,12 @@ async function walk(query: IntentQuery, head: Head): Promise<IntentResult> {
1722
1722
  try {
1723
1723
  const groups = resolveGroups(declaration, located.tree);
1724
1724
  const kindsRegistry = loadKindRegistry(declaration.pins, located.rootOnDisk).registry;
1725
- rows = resolveLinks(declaration, sourceMemberHandles(declaration, located.tree, groups, kindsRegistry)).filter((r) => r.origin === "declared");
1725
+ const { gatherLexiconOutputs } = await import("./lexicon-outputs");
1726
+ const lexiconOutputs = await gatherLexiconOutputs(located.rootOnDisk, declaration, located.at ? workingTree(located.rootOnDisk) : located.tree, groups, kindsRegistry, {
1727
+ atRevision: located.at !== null,
1728
+ ...(query.loadLexicon ? { load: query.loadLexicon as unknown as import("./lexicon-outputs").OutputsPluginLoader } : {}),
1729
+ });
1730
+ rows = resolveLinks(declaration, sourceMemberHandles(declaration, located.tree, groups, kindsRegistry, lexiconOutputs)).filter((r) => r.origin === "declared");
1726
1731
  } catch (err) {
1727
1732
  if (!(err instanceof WorkspaceReadError)) throw err;
1728
1733
  }
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Links a kind reads from its member's own files (#3932).
3
+ *
4
+ * A member's links usually come from its entry in `chant.workspace.json`, or
5
+ * are inferred from a chant member's parameters by `joinKey()`. Some members
6
+ * already say what they read in files of their own: a dbt project names the
7
+ * tables it reads in its `sources:` YAML. A kind can name a link reader in its
8
+ * kinds file (`reader`), and chant runs that reader over the member's
9
+ * directory to derive the member's links.
10
+ *
11
+ * The readers are chant's code, listed in {@link KIND_LINK_READERS}: a kinds
12
+ * file stays data, and chant never imports the package that supplies it. A
13
+ * reader runs offline and parses files as data. It never invokes the tool
14
+ * whose files it reads, so `chant workspace check` keeps its promise of no
15
+ * network and no member code.
16
+ *
17
+ * A reader returns links with the file and line that state them, and gaps:
18
+ * the places it could not read without guessing, such as a templated value.
19
+ * `resolveLinks` (`./links.ts`) resolves the links beside the declared ones,
20
+ * with origin `kind-reader`; WSP093 and WSP094 name the reader's file and line,
21
+ * and WSP099 reports the gaps.
22
+ */
23
+
24
+ import type { Declaration, Member } from "./declaration";
25
+ import { resolveMemberFields, type FieldValue, type KindLinkReaderName, type KindRegistry } from "./kinds";
26
+ import type { WorkspaceTree } from "./tree";
27
+ import { readDbtProject } from "./dbt-project-reader";
28
+
29
+ /** One link a reader found in the member's files. */
30
+ export interface KindReadLink {
31
+ /**
32
+ * The producer the link names, or null when the member's files don't say
33
+ * which member produces it. A null producer is found among the members
34
+ * whose outputs include {@link anchor}; see `resolveLinks`.
35
+ */
36
+ member: string | null;
37
+ /** The producer's output the link reads. */
38
+ output: string;
39
+ /**
40
+ * The output that picks the producer when `member` is null; unset, the
41
+ * output itself. A dbt source column is anchored on its table, so a dropped
42
+ * column is missing from the member that still has the table.
43
+ */
44
+ anchor?: string;
45
+ /** The file that states the link, relative to the workspace root. */
46
+ file: string;
47
+ /** Its line, from 1. */
48
+ line: number;
49
+ }
50
+
51
+ /** A place the reader could not read without guessing. The links it would have stated are left out. */
52
+ export interface KindReadGap {
53
+ /** Relative to the workspace root. */
54
+ file: string;
55
+ line: number;
56
+ message: string;
57
+ }
58
+
59
+ export interface KindLinkRead {
60
+ /** The reader that ran. */
61
+ reader: KindLinkReaderName;
62
+ links: KindReadLink[];
63
+ gaps: KindReadGap[];
64
+ /**
65
+ * The member field that names the producer, for messages that ask the
66
+ * author to set it when the files don't say (`sqlMember` for dbt).
67
+ */
68
+ producerField?: string;
69
+ }
70
+
71
+ /** What a reader gets: the member, its directory and resolved fields, and the tree read. */
72
+ export interface KindReaderInput {
73
+ member: Member;
74
+ /** The member's directory, tree-relative; "" for the workspace root. */
75
+ dir: string;
76
+ /** The member's fields, defaults filled in (#3151); null when its kind declares none. */
77
+ fields: Record<string, FieldValue | null | Record<string, FieldValue | null>> | null;
78
+ tree: WorkspaceTree;
79
+ }
80
+
81
+ export type KindLinkReader = (input: KindReaderInput) => KindLinkRead;
82
+
83
+ /** Every reader chant ships, by the name a kinds file uses. */
84
+ export const LINK_READERS: Record<KindLinkReaderName, KindLinkReader> = {
85
+ "dbt-project": readDbtProject,
86
+ };
87
+
88
+ /**
89
+ * Run the link reader of every member whose kind has one. A member whose
90
+ * directory is missing is left out. A reader that throws is reported as a
91
+ * gap on the member's directory, so one unreadable project never stops
92
+ * the check.
93
+ */
94
+ export function readKindLinks(declaration: Declaration, tree: WorkspaceTree, kinds: KindRegistry): Map<string, KindLinkRead> {
95
+ const out = new Map<string, KindLinkRead>();
96
+ for (const m of declaration.members) {
97
+ const kind = kinds.get(m.kind);
98
+ if (!kind?.reader) continue;
99
+ const dir = m.dir === "." ? "" : m.dir;
100
+ if (tree.stat(dir) !== "dir") continue;
101
+ const { fields } = resolveMemberFields(kind, m.fields);
102
+ try {
103
+ out.set(m.name, LINK_READERS[kind.reader]({ member: m, dir, fields, tree }));
104
+ } catch (err) {
105
+ out.set(m.name, { reader: kind.reader, links: [], gaps: [{ file: dir || ".", line: 1, message: `the ${kind.reader} reader failed: ${(err as Error).message}` }] });
106
+ }
107
+ }
108
+ return out;
109
+ }
@@ -128,8 +128,24 @@ export interface MemberKind {
128
128
  graph?: KindGraph;
129
129
  /** The fields a member entry of this kind may set, with their defaults (#3151); unset, the kind declares none. */
130
130
  fields?: Record<string, KindField>;
131
+ /**
132
+ * The link reader chant runs over a member's own files (#3932), by name, from
133
+ * {@link KIND_LINK_READERS}. It derives the member's links offline, from
134
+ * files it parses as data; unset, the member's links are only those its
135
+ * entry states.
136
+ */
137
+ reader?: KindLinkReaderName;
131
138
  }
132
139
 
140
+ /**
141
+ * The link readers chant ships (#3932). A kind names one in its kinds file as
142
+ * `reader`. The readers are chant's own code, not the package's: a kinds file
143
+ * stays data, and chant never imports the package that supplies it. Closed:
144
+ * a kind naming another reader is kept without one, and the load reports it.
145
+ */
146
+ export const KIND_LINK_READERS = ["dbt-project"] as const;
147
+ export type KindLinkReaderName = (typeof KIND_LINK_READERS)[number];
148
+
133
149
  export interface KindRegistry {
134
150
  get(name: string): MemberKind | undefined;
135
151
  /** Every member kind's name, sorted, for "unknown kind" messages. */
@@ -384,7 +400,7 @@ export function parseKindData(text: string, source: string): KindData {
384
400
  const problems: string[] = [];
385
401
  const kinds: MemberKind[] = [];
386
402
  const seen = new Set<string>();
387
- for (const k of (raw as { kinds: { name: string; description: string; precedence: number; probe: FileProbe; outputs?: string[]; graph?: KindGraph; fields?: Record<string, KindField> }[] }).kinds) {
403
+ for (const k of (raw as { kinds: { name: string; description: string; precedence: number; probe: FileProbe; outputs?: string[]; graph?: KindGraph; fields?: Record<string, KindField>; reader?: string }[] }).kinds) {
388
404
  if (BUILTIN_KIND_NAMES.includes(k.name)) {
389
405
  problems.push(`${source}: kind ${k.name} is built in and can't be supplied by a package`);
390
406
  continue;
@@ -408,7 +424,11 @@ export function parseKindData(text: string, source: string): KindData {
408
424
  source,
409
425
  ...(k.graph ? { graph: { lexicon: k.graph.lexicon, config: structuredClone(k.graph.config) } } : {}),
410
426
  ...(k.fields ? { fields: structuredClone(k.fields) } : {}),
427
+ ...(k.reader && (KIND_LINK_READERS as readonly string[]).includes(k.reader) ? { reader: k.reader as KindLinkReaderName } : {}),
411
428
  });
429
+ if (k.reader && !(KIND_LINK_READERS as readonly string[]).includes(k.reader)) {
430
+ problems.push(`${source}: kind ${k.name} names link reader ${k.reader}, which this chant does not have (it has ${KIND_LINK_READERS.join(", ")}); the kind is kept, and its members' links are only those their entries state`);
431
+ }
412
432
  for (const problem of fieldDefaultProblems(k.fields ?? {})) problems.push(`${source}: kind ${k.name} ${problem}`);
413
433
  }
414
434
  return { kinds, problems };