@intentius/chant 0.124.97 → 0.124.98

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 (223) hide show
  1. package/dist/audit/core.d.ts +29 -0
  2. package/dist/audit/core.d.ts.map +1 -1
  3. package/dist/audit/core.js +48 -11
  4. package/dist/audit/identity.d.ts +27 -0
  5. package/dist/audit/identity.d.ts.map +1 -0
  6. package/dist/audit/identity.js +36 -0
  7. package/dist/audit/report-html.d.ts +3 -1
  8. package/dist/audit/report-html.d.ts.map +1 -1
  9. package/dist/audit/report-html.js +1 -1
  10. package/dist/audit/report-model.d.ts +16 -0
  11. package/dist/audit/report-model.d.ts.map +1 -1
  12. package/dist/audit/report-model.js +5 -1
  13. package/dist/ci-agent.d.ts +17 -0
  14. package/dist/ci-agent.d.ts.map +1 -1
  15. package/dist/ci-agent.js +32 -0
  16. package/dist/ci-comment-allow.d.ts +88 -0
  17. package/dist/ci-comment-allow.d.ts.map +1 -0
  18. package/dist/ci-comment-allow.js +178 -0
  19. package/dist/ci-comment-commands.d.ts +12 -0
  20. package/dist/ci-comment-commands.d.ts.map +1 -1
  21. package/dist/ci-comment-commands.js +22 -0
  22. package/dist/ci-control.d.ts +6 -1
  23. package/dist/ci-control.d.ts.map +1 -1
  24. package/dist/ci-control.js +6 -1
  25. package/dist/ci-product.d.ts +3 -0
  26. package/dist/ci-product.d.ts.map +1 -1
  27. package/dist/ci-product.js +3 -0
  28. package/dist/cli/commands/audit.d.ts +3 -1
  29. package/dist/cli/commands/audit.d.ts.map +1 -1
  30. package/dist/cli/commands/audit.js +23 -6
  31. package/dist/cli/commands/import.d.ts +8 -1
  32. package/dist/cli/commands/import.d.ts.map +1 -1
  33. package/dist/cli/commands/import.js +31 -6
  34. package/dist/cli/handlers/agent.d.ts.map +1 -1
  35. package/dist/cli/handlers/agent.js +12 -2
  36. package/dist/cli/handlers/pr-comment.d.ts +6 -0
  37. package/dist/cli/handlers/pr-comment.d.ts.map +1 -1
  38. package/dist/cli/handlers/pr-comment.js +12 -0
  39. package/dist/cli/handlers/promote.d.ts.map +1 -1
  40. package/dist/cli/handlers/promote.js +53 -6
  41. package/dist/cli/main.d.ts.map +1 -1
  42. package/dist/cli/main.js +29 -6
  43. package/dist/cli/parse-args.d.ts.map +1 -1
  44. package/dist/cli/parse-args.js +23 -0
  45. package/dist/cli/registry.d.ts +8 -0
  46. package/dist/cli/registry.d.ts.map +1 -1
  47. package/dist/cloud-lister/mask.d.ts +30 -1
  48. package/dist/cloud-lister/mask.d.ts.map +1 -1
  49. package/dist/cloud-lister/mask.js +66 -3
  50. package/dist/components/driver.d.ts +8 -1
  51. package/dist/components/driver.d.ts.map +1 -1
  52. package/dist/components/driver.js +1 -0
  53. package/dist/components/product-pipeline.d.ts +23 -1
  54. package/dist/components/product-pipeline.d.ts.map +1 -1
  55. package/dist/components/product-pipeline.js +40 -7
  56. package/dist/components/product-plan.d.ts +20 -0
  57. package/dist/components/product-plan.d.ts.map +1 -1
  58. package/dist/components/product-plan.js +11 -1
  59. package/dist/control/applied.d.ts +8 -1
  60. package/dist/control/applied.d.ts.map +1 -1
  61. package/dist/control/applied.js +18 -2
  62. package/dist/control/dispatch.d.ts +52 -0
  63. package/dist/control/dispatch.d.ts.map +1 -1
  64. package/dist/control/dispatch.js +140 -4
  65. package/dist/control/train.d.ts +198 -0
  66. package/dist/control/train.d.ts.map +1 -0
  67. package/dist/control/train.js +294 -0
  68. package/dist/deep-observation.js +2 -2
  69. package/dist/floor-table.data.d.ts.map +1 -1
  70. package/dist/floor-table.data.js +11 -0
  71. package/dist/graph-detail.d.ts +9 -0
  72. package/dist/graph-detail.d.ts.map +1 -1
  73. package/dist/graph-detail.js +38 -13
  74. package/dist/graph-refs.d.ts +233 -3
  75. package/dist/graph-refs.d.ts.map +1 -1
  76. package/dist/graph-refs.js +106 -15
  77. package/dist/lint/post-synth.d.ts +22 -0
  78. package/dist/lint/post-synth.d.ts.map +1 -1
  79. package/dist/lint/rules/declarable-naming-convention.d.ts +12 -0
  80. package/dist/lint/rules/declarable-naming-convention.d.ts.map +1 -1
  81. package/dist/lint/rules/declarable-naming-convention.js +38 -8
  82. package/dist/lint/rules/imported-source.d.ts +20 -0
  83. package/dist/lint/rules/imported-source.d.ts.map +1 -0
  84. package/dist/lint/rules/imported-source.js +27 -0
  85. package/dist/lint/rules/no-unused-declarable.d.ts.map +1 -1
  86. package/dist/lint/rules/no-unused-declarable.js +3 -0
  87. package/dist/op/gate.d.ts +2 -0
  88. package/dist/op/gate.d.ts.map +1 -1
  89. package/dist/op/gate.js +8 -1
  90. package/dist/pr-forge-reads.d.ts +17 -0
  91. package/dist/pr-forge-reads.d.ts.map +1 -1
  92. package/dist/pr-forge-reads.js +55 -0
  93. package/dist/release-floors.d.ts +9 -1
  94. package/dist/release-floors.d.ts.map +1 -1
  95. package/dist/release-floors.js +9 -1
  96. package/dist/repo-path.d.ts +29 -0
  97. package/dist/repo-path.d.ts.map +1 -0
  98. package/dist/repo-path.js +81 -0
  99. package/dist/sensitive-key.d.ts +23 -0
  100. package/dist/sensitive-key.d.ts.map +1 -1
  101. package/dist/sensitive-key.js +37 -0
  102. package/dist/terraform/hcl2json.d.ts +7 -0
  103. package/dist/terraform/hcl2json.d.ts.map +1 -1
  104. package/dist/terraform/hcl2json.js +7 -0
  105. package/dist/wave-runtime.mjs +352 -345
  106. package/dist/workspace/catalog-edges.d.ts +147 -0
  107. package/dist/workspace/catalog-edges.d.ts.map +1 -0
  108. package/dist/workspace/catalog-edges.js +461 -0
  109. package/dist/workspace/compose-graph.d.ts +73 -3
  110. package/dist/workspace/compose-graph.d.ts.map +1 -1
  111. package/dist/workspace/declaration.d.ts +2 -0
  112. package/dist/workspace/declaration.d.ts.map +1 -1
  113. package/dist/workspace/declaration.js +1 -0
  114. package/dist/workspace/declaration.schema.json +6 -0
  115. package/dist/workspace/discover.d.ts +134 -0
  116. package/dist/workspace/discover.d.ts.map +1 -0
  117. package/dist/workspace/discover.js +372 -0
  118. package/dist/workspace/graph-cli.d.ts +20 -0
  119. package/dist/workspace/graph-cli.d.ts.map +1 -1
  120. package/dist/workspace/graph-cli.js +178 -20
  121. package/dist/workspace/graph.schema.json +195 -2
  122. package/dist/workspace/release-train.d.ts +214 -3
  123. package/dist/workspace/release-train.d.ts.map +1 -1
  124. package/dist/workspace/release-train.js +552 -16
  125. package/dist/workspace/repo-ref.d.ts +14 -0
  126. package/dist/workspace/repo-ref.d.ts.map +1 -1
  127. package/dist/workspace/repo-ref.js +26 -1
  128. package/dist/workspace/workspace-gates.d.ts +24 -0
  129. package/dist/workspace/workspace-gates.d.ts.map +1 -1
  130. package/dist/workspace/workspace-gates.js +29 -5
  131. package/dist/workspace/workspace-ledger.d.ts +31 -5
  132. package/dist/workspace/workspace-ledger.d.ts.map +1 -1
  133. package/dist/workspace/workspace-ledger.js +2 -1
  134. package/dist/workspace/workspace-run.d.ts +14 -0
  135. package/dist/workspace/workspace-run.d.ts.map +1 -1
  136. package/dist/workspace/workspace-run.js +2 -2
  137. package/dist/yaml.d.ts.map +1 -1
  138. package/dist/yaml.js +33 -8
  139. package/package.json +1 -1
  140. package/src/audit/core.test.ts +64 -1
  141. package/src/audit/core.ts +75 -10
  142. package/src/audit/identity.test.ts +31 -0
  143. package/src/audit/identity.ts +43 -0
  144. package/src/audit/report-html.ts +4 -2
  145. package/src/audit/report-model.ts +21 -1
  146. package/src/ci-agent.test.ts +32 -0
  147. package/src/ci-agent.ts +42 -0
  148. package/src/ci-comment-allow.test.ts +380 -0
  149. package/src/ci-comment-allow.ts +184 -0
  150. package/src/ci-comment-commands.ts +25 -0
  151. package/src/ci-control.ts +42 -0
  152. package/src/ci-product.ts +3 -0
  153. package/src/cli/commands/audit-parser-notice.test.ts +74 -0
  154. package/src/cli/commands/audit.test.ts +12 -0
  155. package/src/cli/commands/audit.ts +25 -7
  156. package/src/cli/commands/import-layout.test.ts +2 -1
  157. package/src/cli/commands/import.ts +29 -6
  158. package/src/cli/handlers/agent.ts +13 -2
  159. package/src/cli/handlers/pr-comment.ts +12 -0
  160. package/src/cli/handlers/promote.test.ts +68 -0
  161. package/src/cli/handlers/promote.ts +52 -6
  162. package/src/cli/main.test.ts +5 -0
  163. package/src/cli/main.ts +29 -6
  164. package/src/cli/parse-args.ts +18 -0
  165. package/src/cli/registry.ts +8 -0
  166. package/src/cloud-lister/kubernetes.k3d.e2e.test.ts +104 -0
  167. package/src/cloud-lister/mask-references.test.ts +132 -0
  168. package/src/cloud-lister/mask.ts +68 -3
  169. package/src/components/driver.ts +9 -1
  170. package/src/components/product-pipeline.ts +57 -7
  171. package/src/components/product-plan.ts +27 -2
  172. package/src/control/applied.ts +33 -3
  173. package/src/control/dispatch.ts +156 -5
  174. package/src/control/train.test.ts +215 -0
  175. package/src/control/train.ts +413 -0
  176. package/src/deep-observation.ts +2 -2
  177. package/src/floor-table.data.ts +6 -0
  178. package/src/graph-detail.ts +37 -12
  179. package/src/graph-refs.test.ts +45 -1
  180. package/src/graph-refs.ts +321 -14
  181. package/src/lint/post-synth.ts +20 -0
  182. package/src/lint/rules/declarable-naming-convention.test.ts +35 -1
  183. package/src/lint/rules/declarable-naming-convention.ts +35 -8
  184. package/src/lint/rules/imported-source.test.ts +22 -0
  185. package/src/lint/rules/imported-source.ts +31 -0
  186. package/src/lint/rules/no-unused-declarable.test.ts +16 -0
  187. package/src/lint/rules/no-unused-declarable.ts +7 -0
  188. package/src/op/gate.ts +10 -2
  189. package/src/pr-forge-reads.ts +62 -0
  190. package/src/release-floors.ts +13 -1
  191. package/src/repo-path.test.ts +72 -0
  192. package/src/repo-path.ts +103 -0
  193. package/src/sensitive-key.ts +38 -0
  194. package/src/terraform/hcl2json.ts +8 -0
  195. package/src/workspace/application-set.e2e.test.ts +405 -0
  196. package/src/workspace/catalog-edges.ts +507 -0
  197. package/src/workspace/ci-green-workflow.test.ts +2 -2
  198. package/src/workspace/cluster-join.e2e.test.ts +150 -0
  199. package/src/workspace/compose-graph.ts +74 -5
  200. package/src/workspace/declaration.schema.json +6 -0
  201. package/src/workspace/declaration.ts +3 -0
  202. package/src/workspace/discover.e2e.test.ts +122 -0
  203. package/src/workspace/discover.test.ts +225 -0
  204. package/src/workspace/discover.ts +406 -0
  205. package/src/workspace/external-secret-catalog.e2e.test.ts +98 -0
  206. package/src/workspace/external-secret-catalog.test.ts +142 -0
  207. package/src/workspace/graph-cli.ts +207 -21
  208. package/src/workspace/graph-contract.test.ts +92 -0
  209. package/src/workspace/graph.schema.json +195 -2
  210. package/src/workspace/helm-references.e2e.test.ts +135 -0
  211. package/src/workspace/identity-annotation-catalog.e2e.test.ts +91 -0
  212. package/src/workspace/identity-annotation-catalog.test.ts +139 -0
  213. package/src/workspace/release-train-repos.test.ts +304 -0
  214. package/src/workspace/release-train.test.ts +296 -0
  215. package/src/workspace/release-train.ts +631 -16
  216. package/src/workspace/repo-ref.ts +26 -1
  217. package/src/workspace/runs-on.test.ts +45 -0
  218. package/src/workspace/workspace-gates.ts +48 -5
  219. package/src/workspace/workspace-ledger.ts +28 -5
  220. package/src/workspace/workspace-run.test.ts +1 -1
  221. package/src/workspace/workspace-run.ts +2 -2
  222. package/src/yaml.test.ts +96 -8
  223. package/src/yaml.ts +36 -8
@@ -189,10 +189,74 @@ export interface MemberCollector {
189
189
  export const COLLECTOR_META_KEY = "collector";
190
190
 
191
191
  /** `nested` names the nested workspace's member a node was read through (#2551); absent for the outer workspace's own nodes. */
192
- export type ComposedNode = IRNode & { member: string; nested?: string };
192
+ export type ComposedNode = IRNode & { member: string; nested?: string; generatedBy?: GeneratedBy; expansion?: NodeExpansion };
193
+
194
+ /**
195
+ * On a node the catalog pass generated from another (#5102), an Application
196
+ * an ApplicationSet's generator yields: the node that generated it, the rule,
197
+ * the generator, the parameters it was made with and, for a generator that
198
+ * reads cluster registrations, the registration it was made for.
199
+ */
200
+ export interface GeneratedBy {
201
+ node: string;
202
+ rule: string;
203
+ generator: string;
204
+ params: Record<string, string>;
205
+ cluster?: string;
206
+ }
207
+
208
+ /** On a node whose rule generates nodes (#5102): how many it generated, and what it did not expand. */
209
+ export interface NodeExpansion {
210
+ rule: string;
211
+ generated: number;
212
+ notExpanded: string[];
213
+ }
193
214
  export type ComposedEdge = IREdge & { member: string };
194
- /** A member's dangling reference (#3910, #3922): `from` is prefixed like a node id. `nested` as on {@link ComposedNode}. */
195
- export type ComposedDangling = IRDanglingRef & { member: string; nested?: string };
215
+ /**
216
+ * A member's dangling reference (#3910, #3922): `from` is prefixed like a node id. `nested` as on {@link ComposedNode}.
217
+ * One the reference catalog recorded (#5097) also names its `rule`, and the `targetKind` that rule looks for.
218
+ * A path a generated node names (#5102) has the generating node as `through`, and the cluster registration it was made for as `cluster`.
219
+ */
220
+ export type ComposedDangling = IRDanglingRef & { member: string; nested?: string; rule?: string; targetKind?: string; through?: string; cluster?: string };
221
+
222
+ /**
223
+ * A reference the catalog pass found more than one node for (#5097): no edge
224
+ * is drawn, and every candidate is listed. `from` and the candidates are
225
+ * node ids of the document; `member` is the holder's member.
226
+ */
227
+ export interface ComposedAmbiguous {
228
+ from: string;
229
+ member: string;
230
+ rule: string;
231
+ path: string;
232
+ value: string;
233
+ /** Every node the value matched, sorted. */
234
+ candidates: string[];
235
+ }
236
+
237
+ /** One catalog rule as the document names it (#5097): what {@link IREdge.rule} and a dangling or ambiguous entry's `rule` refer to. */
238
+ export interface CatalogRuleEntry {
239
+ lexicon: string;
240
+ /** The holder kind the rule reads. */
241
+ from: string;
242
+ path: string;
243
+ relation: "reference" | "containment";
244
+ /** The kinds the rule's target can be: one rule id can stand for several rules, one per kind. Empty when a rule takes any kind. */
245
+ targetKinds: string[];
246
+ label?: string;
247
+ }
248
+
249
+ /** What the catalog pass over the declared graph read (#5097, `./catalog-edges.ts`). */
250
+ export interface CatalogSummary {
251
+ /** The lexicons whose reference catalog ran, sorted. */
252
+ lexicons: string[];
253
+ /** The members whose nodes the pass read, sorted. */
254
+ members: string[];
255
+ /** Every rule an edge, a dangling entry or an ambiguous entry names, by rule id. */
256
+ rules: Record<string, CatalogRuleEntry>;
257
+ /** By node id: arguments the pass did not read because their value is not literal. Absent when there are none. */
258
+ nonLiteral?: Record<string, number>;
259
+ }
196
260
 
197
261
  export interface WorkspaceGraph {
198
262
  version: number;
@@ -207,10 +271,15 @@ export interface WorkspaceGraph {
207
271
  derivedAttrs?: Record<string, string[]>;
208
272
  /**
209
273
  * The members' dangling CI references (#3910, #3922): each `ir.dangling`
210
- * entry with `from` prefixed by its member and a `member` field, sorted by
211
- * `from`, `path` and `value`. Absent when no member has one.
274
+ * entry with `from` prefixed by its member and a `member` field, then the
275
+ * references the catalog pass found no node for, with their `rule`
276
+ * (#5097). Sorted by `from`, `path` and `value`. Absent when there is none.
212
277
  */
213
278
  dangling?: ComposedDangling[];
279
+ /** References the catalog pass matched to more than one node (#5097), sorted by `from`, `rule` and `value`. Absent when there are none. */
280
+ ambiguous?: ComposedAmbiguous[];
281
+ /** What the catalog pass read (#5097). Absent when it did not run: a live read, or a caller that did not ask for it. */
282
+ catalog?: CatalogSummary;
214
283
  /**
215
284
  * Member links (#2524 D6, #2539), declared and inferred, then the links of
216
285
  * the records read with `--kind` (#2549). Empty when no declaration is given.
@@ -714,6 +714,12 @@
714
714
  "fields": {
715
715
  "$ref": "#/$defs/memberFields"
716
716
  },
717
+ "runsOn": {
718
+ "type": "string",
719
+ "minLength": 1,
720
+ "pattern": "^[^/\\s]+/\\S+$",
721
+ "description": "The cluster resource this member runs on, as `<member>/<address>`: the member that holds it and its address (`infra/google_container_cluster.prod`), or its node id in the graph. chant workspace graph draws a declared edge from each Argo CD cluster Secret this member holds to that node, for a repo where the Secret's name is not the cluster's (#5101). A node it names that the graph does not hold, or more than one, is a warning and draws no edge. Added in schema 1 by chant 0.124.98, so a declaration that uses it sets minReader to 0.124.98 or newer."
722
+ },
717
723
  "links": {
718
724
  "type": "array",
719
725
  "description": "Member links (#2524 D6, #2539): the outputs of other members this member reads. A link is stated once, here on the consumer, and matched exactly against the producer's outputs. chant workspace check resolves it in source.",
@@ -795,6 +795,8 @@ export interface Member {
795
795
  outputs: string[] | null;
796
796
  /** The values the entry sets for its kind's fields (#3151), as written, or null when it sets none. Checked against the kind by `resolveMemberFields`. */
797
797
  fields: Record<string, unknown> | null;
798
+ /** The cluster resource the member runs on, `<member>/<address>` (#5101), or null when the entry names none. */
799
+ runsOn: string | null;
798
800
  /** The links this member states as a consumer, in file order (#2539). */
799
801
  links: LinkDeclaration[];
800
802
  /** The record kinds this member declares, in file order (#2680). */
@@ -1132,6 +1134,7 @@ export function parseDeclaration(text: string, file: string, reader: string = re
1132
1134
  generated,
1133
1135
  outputs: e.outputs === undefined ? null : [...(e.outputs as string[])],
1134
1136
  fields: e.fields === undefined ? null : structuredClone(e.fields as Record<string, unknown>),
1137
+ runsOn: (e.runsOn as string | undefined) ?? null,
1135
1138
  links,
1136
1139
  records,
1137
1140
  diagrams,
@@ -0,0 +1,122 @@
1
+ /**
2
+ * `chant workspace graph <dir> --discover` end to end (#5095): a directory
3
+ * with no declaration, read through the terraform and k8s lexicons installed
4
+ * in this checkout, and nothing written inside it or in this repository's
5
+ * git directory.
6
+ *
7
+ * The fixture is a plain directory, not a git repository, as a folder handed
8
+ * to a viewer is. Its `infra/net` root calls `../modules/vpc`, which is a
9
+ * module and no member, `argo` holds an Argo CD Application, and `db` is
10
+ * claimed by two sql kinds at one precedence.
11
+ */
12
+
13
+ import { execFileSync } from "node:child_process";
14
+ import { createHash } from "node:crypto";
15
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
16
+ import { isAbsolute, join } from "node:path";
17
+ import { afterAll, beforeAll, describe, expect, test } from "vitest";
18
+ import { cleanScratch, contract, REPO, scratchDir, writeFiles } from "./__fixtures__/contract-repo";
19
+ import { CACHE_DIR_ENV } from "./graph-cache";
20
+ import { workspaceGraph, type GraphDocument } from "./graph-cli";
21
+ import schema from "./graph.schema.json";
22
+
23
+ const TIMEOUT = 240_000;
24
+ const { expectValid } = contract(schema);
25
+
26
+ let cacheBefore: string | undefined;
27
+ beforeAll(() => {
28
+ cacheBefore = process.env[CACHE_DIR_ENV];
29
+ process.env[CACHE_DIR_ENV] = scratchDir("chant-discover-cache-");
30
+ });
31
+ afterAll(() => {
32
+ if (cacheBefore === undefined) delete process.env[CACHE_DIR_ENV];
33
+ else process.env[CACHE_DIR_ENV] = cacheBefore;
34
+ cleanScratch();
35
+ });
36
+
37
+ const FX = {
38
+ "infra/net/main.tf":
39
+ 'provider "aws" {\n region = "us-east-1"\n}\n\nresource "aws_s3_bucket" "logs" {\n bucket = "logs"\n}\n\nmodule "vpc" {\n source = "../modules/vpc"\n}\n',
40
+ "infra/modules/vpc/main.tf": 'resource "aws_vpc" "main" {\n cidr_block = "10.0.0.0/16"\n}\n',
41
+ "argo/app.yaml":
42
+ "apiVersion: argoproj.io/v1alpha1\nkind: Application\nmetadata:\n name: web\n namespace: argocd\nspec:\n project: default\n source:\n repoURL: https://example.com/repo.git\n path: apps/web\n destination:\n server: https://kubernetes.default.svc\n namespace: web\n",
43
+ "db/V1__init.sql": "create table orders (id int);\n",
44
+ "db/1_init.up.sql": "create table orders (id int);\n",
45
+ };
46
+
47
+ /** Every path under `root` with its kind, mode, size, mtime and content hash. */
48
+ function snapshot(root: string): string[] {
49
+ const out: string[] = [];
50
+ const walk = (dir: string, rel: string) => {
51
+ for (const name of readdirSync(dir).sort()) {
52
+ const path = join(dir, name);
53
+ const at = rel ? `${rel}/${name}` : name;
54
+ const st = statSync(path);
55
+ if (st.isDirectory()) {
56
+ out.push(`d ${at} ${st.mode} ${st.mtimeMs}`);
57
+ walk(path, at);
58
+ } else {
59
+ out.push(`f ${at} ${st.mode} ${st.size} ${st.mtimeMs} ${createHash("sha256").update(readFileSync(path)).digest("hex")}`);
60
+ }
61
+ }
62
+ };
63
+ walk(root, "");
64
+ return out;
65
+ }
66
+
67
+ /** What chant keeps in this repository's git directory: its refs and `.git/chant`. A worktree shares the main checkout's. */
68
+ function chantGitState(): string[] {
69
+ const common = execFileSync("git", ["rev-parse", "--git-common-dir"], { cwd: REPO, encoding: "utf-8" }).trim();
70
+ const dir = isAbsolute(common) ? common : join(REPO, common);
71
+ const refs = execFileSync("git", ["for-each-ref", "--format=%(refname) %(objectname)", "refs/heads/chant", "refs/tags/ci", "refs/chant"], { cwd: REPO, encoding: "utf-8" })
72
+ .split("\n")
73
+ .filter(Boolean);
74
+ const own = join(dir, "chant");
75
+ return [...refs, ...(existsSync(own) ? snapshot(own) : [])];
76
+ }
77
+
78
+ function result(doc: GraphDocument): Extract<GraphDocument, { nodes: unknown }> {
79
+ if ("error" in doc) throw new Error(`${doc.error.code}: ${doc.error.message}`);
80
+ return doc;
81
+ }
82
+
83
+ describe("workspace graph --discover (#5095)", () => {
84
+ test(
85
+ "reads infra/net and argo as members, leaves the module and the tie out, and writes nothing",
86
+ async () => {
87
+ const root = scratchDir("chant-discover-fx-");
88
+ writeFiles(root, FX);
89
+ const before = snapshot(root);
90
+ const gitBefore = chantGitState();
91
+
92
+ const { doc, failed } = await workspaceGraph({ cwd: root, discover: true, noCache: false });
93
+ const g = result(doc);
94
+ expectValid(g);
95
+ expect(g.contract).toBe(1);
96
+ expect(failed).toBe(false);
97
+
98
+ expect(g.members.map((m) => [m.name, m.dir, m.kind, m.status])).toEqual([
99
+ ["argo", "argo", "argo-app", "composed"],
100
+ ["infra-net", "infra/net", "terraform", "composed"],
101
+ ]);
102
+ // The module is read with the root that calls it, and is no member of its own.
103
+ const ids = g.nodes.map((n) => n.id);
104
+ expect(ids).toContain("infra-net/infra-net/aws_s3_bucket.logs");
105
+ expect(ids).toContain("infra-net/infra-net/module.vpc/aws_vpc.main");
106
+ expect(ids.some((id) => id.startsWith("argo/"))).toBe(true);
107
+
108
+ expect(g.discovered!.map((e) => [e.dir, e.outcome, e.member, e.kind])).toEqual([
109
+ ["argo", "member", "argo", "argo-app"],
110
+ ["db", "tie", null, null],
111
+ ["infra/modules/vpc", "module", null, "terraform"],
112
+ ["infra/net", "member", "infra-net", "terraform"],
113
+ ]);
114
+ const tie = g.discovered!.find((e) => e.dir === "db")!;
115
+ expect(tie.claims.filter((c) => c.precedence === tie.claims[0].precedence).map((c) => c.kind)).toEqual(["flyway", "golang-migrate"]);
116
+
117
+ expect(snapshot(root)).toEqual(before);
118
+ expect(chantGitState()).toEqual(gitBefore);
119
+ },
120
+ TIMEOUT,
121
+ );
122
+ });
@@ -0,0 +1,225 @@
1
+ /**
2
+ * `chant workspace graph <dir> --discover` (#5095): the walk, the module rule,
3
+ * ties, member names and the in-memory declaration. These read files only;
4
+ * `discover.e2e.test.ts` reads a directory through the lexicons.
5
+ */
6
+
7
+ import { createHash } from "node:crypto";
8
+ import { readdirSync, readFileSync, rmSync, statSync, symlinkSync } from "node:fs";
9
+ import { join } from "node:path";
10
+ import { afterAll, describe, expect, test } from "vitest";
11
+ import { cleanScratch, repo, scratchDir, writeFiles } from "./__fixtures__/contract-repo";
12
+ import { parseDeclaration } from "./declaration";
13
+ import { declares, discover, discoveredMemberNames, discoveredTree, discoveryKinds, kindPackagesBeside, localModuleSources, locateDiscovered, type DiscoveryKinds } from "./discover";
14
+ import { planGraph, workspaceGraph } from "./graph-cli";
15
+ import { createKindRegistry, type MemberKind } from "./kinds";
16
+ import { workingTree } from "./tree";
17
+
18
+ afterAll(cleanScratch);
19
+
20
+ const kind = (name: string, precedence: number, anyFile: string[]): MemberKind => ({
21
+ name,
22
+ description: name,
23
+ probe: { anyFile },
24
+ precedence,
25
+ shape: "member",
26
+ outputs: { from: "declared", names: [] },
27
+ source: `@test/${name}`,
28
+ });
29
+
30
+ /** Kinds that need no package: terraform by `*.tf`, and two that tie on `*.sql`. */
31
+ const KINDS: DiscoveryKinds = {
32
+ registry: createKindRegistry([kind("terraform", 400, ["*.tf"]), kind("migrate-a", 300, ["*.sql"]), kind("migrate-b", 300, ["*.sql"]), kind("charts", 330, ["Chart.yaml"])]),
33
+ packages: [
34
+ { package: "@test/charts", version: "2.0.0", dir: "/nowhere" },
35
+ { package: "@test/terraform", version: "1.0.0", dir: "/nowhere" },
36
+ ],
37
+ problems: [],
38
+ };
39
+
40
+ /** Every path under `root` with its kind, mode, size and content hash. */
41
+ function snapshot(root: string): string[] {
42
+ const out: string[] = [];
43
+ const walk = (dir: string, rel: string) => {
44
+ for (const name of readdirSync(dir).sort()) {
45
+ const path = join(dir, name);
46
+ const at = rel ? `${rel}/${name}` : name;
47
+ const st = statSync(path);
48
+ if (st.isDirectory()) {
49
+ out.push(`d ${at} ${st.mode}`);
50
+ walk(path, at);
51
+ } else {
52
+ out.push(`f ${at} ${st.mode} ${st.size} ${createHash("sha256").update(readFileSync(path)).digest("hex")}`);
53
+ }
54
+ }
55
+ };
56
+ walk(root, "");
57
+ return out;
58
+ }
59
+
60
+ describe("localModuleSources", () => {
61
+ test("reads local sources in module blocks only, past comments and nested blocks", () => {
62
+ const text = [
63
+ 'terraform {\n required_providers {\n aws = { source = "hashicorp/aws" }\n }\n}',
64
+ 'module "vpc" {\n # source = "./not-this"\n source = "../modules/vpc"\n tags = { a = "}" }\n}',
65
+ 'module "reg" {\n source = "terraform-aws-modules/vpc/aws"\n version = "5.0.0"\n}',
66
+ 'module "git" {\n source = "git::https://example.com/m.git//sub"\n}',
67
+ 'module "here" {\n dynamic "x" {\n content {\n source = "./inner"\n }\n }\n source = "./here"\n}',
68
+ '/* module "c" {\n source = "./commented"\n} */',
69
+ 'resource "null_resource" "r" {\n provisioner "file" {\n source = "./files"\n }\n}',
70
+ ].join("\n\n");
71
+ expect(localModuleSources(text)).toEqual(["../modules/vpc", "./here"]);
72
+ });
73
+ });
74
+
75
+ describe("discoveredMemberNames", () => {
76
+ test("names come from the path alone: / and other runs become -, . is root", () => {
77
+ const names = discoveredMemberNames([".", "infra/net", "argo", "infra/gcp/terraform/k8s-infra-prow-build-trusted", "A/b", "a-b", "x.k8s.io"]);
78
+ expect(names.get(".")).toBe("root");
79
+ expect(names.get("infra/net")).toBe("infra-net");
80
+ expect(names.get("argo")).toBe("argo");
81
+ expect(names.get("x.k8s.io")).toBe("x-k8s-io");
82
+ // Too long: the head of the name and a digest of the path.
83
+ const long = names.get("infra/gcp/terraform/k8s-infra-prow-build-trusted")!;
84
+ expect(long).toMatch(/^infra-gcp-terraform-k8s-infra-pro-[0-9a-f]{6}$/);
85
+ expect(long.length).toBeLessThanOrEqual(40);
86
+ // Shared after the rewrite: both carry a digest, so neither depends on the other being listed first.
87
+ expect(names.get("A/b")).toMatch(/^a-b-[0-9a-f]{6}$/);
88
+ expect(names.get("a-b")).toMatch(/^a-b-[0-9a-f]{6}$/);
89
+ expect(names.get("A/b")).not.toBe(names.get("a-b"));
90
+ for (const n of names.values()) expect(n).toMatch(/^[a-z0-9][a-z0-9-]{0,39}$/);
91
+ // The same path gets the same name on another walk.
92
+ expect(discoveredMemberNames(["infra/gcp/terraform/k8s-infra-prow-build-trusted"]).get("infra/gcp/terraform/k8s-infra-prow-build-trusted")).toBe(long);
93
+ });
94
+ });
95
+
96
+ describe("discover", () => {
97
+ const FX = {
98
+ "infra/net/main.tf": 'provider "aws" {}\n\nresource "aws_vpc" "main" {}\n\nmodule "vpc" {\n source = "../modules/vpc"\n}\n',
99
+ "infra/modules/vpc/main.tf": 'resource "aws_subnet" "a" {}\n',
100
+ "infra/net/extra/main.tf": 'resource "aws_eip" "x" {}\n',
101
+ "db/V1__init.sql": "create table t ();\n",
102
+ "charts/web/Chart.yaml": "name: web\n",
103
+ "node_modules/pkg/main.tf": 'resource "x" "y" {}\n',
104
+ ".git-like/main.tf": 'resource "x" "y" {}\n',
105
+ };
106
+
107
+ test("members, modules, ties and nested roots, in the discovered list and the declaration", () => {
108
+ const root = scratchDir("chant-discover-");
109
+ writeFiles(root, FX);
110
+ const found = discover(workingTree(root), KINDS, "fx");
111
+ expect(found.entries.map((e) => [e.dir, e.outcome, e.member, e.kind, e.claims.map((c) => c.kind)])).toEqual([
112
+ ["charts/web", "member", "charts-web", "charts", ["charts"]],
113
+ ["db", "tie", null, null, ["migrate-a", "migrate-b"]],
114
+ ["infra/modules/vpc", "module", null, "terraform", ["terraform"]],
115
+ ["infra/net", "member", "infra-net", "terraform", ["terraform"]],
116
+ ["infra/net/extra", "inside-member", null, "terraform", ["terraform"]],
117
+ ]);
118
+ const tie = found.entries.find((e) => e.dir === "db")!;
119
+ expect(tie).toMatchObject({ source: null, probe: null });
120
+ expect(tie.note).toContain("migrate-a and migrate-b");
121
+ expect(found.entries.find((e) => e.dir === "infra/modules/vpc")!.note).toContain("infra/net");
122
+ expect(found.entries.find((e) => e.dir === "infra/net")!.probe).toEqual({ anyFile: ["*.tf"] });
123
+
124
+ // The declaration is a real one, and pins only the packages whose kinds it uses.
125
+ const decl = parseDeclaration(found.declaration, "chant.workspace.json");
126
+ expect(decl.name).toBe("fx");
127
+ expect(decl.members.map((m) => [m.name, m.dir, m.kind])).toEqual([
128
+ ["charts-web", "charts/web", "charts"],
129
+ ["infra-net", "infra/net", "terraform"],
130
+ ]);
131
+ expect(decl.pins.map((p) => [p.package, p.version])).toEqual([
132
+ ["@test/charts", "2.0.0"],
133
+ ["@test/terraform", "1.0.0"],
134
+ ]);
135
+ });
136
+
137
+ test("a root at <dir> itself is member . and may hold the others", () => {
138
+ const root = scratchDir("chant-discover-");
139
+ writeFiles(root, { "main.tf": 'resource "a" "b" {}\n', "sub/main.tf": 'resource "c" "d" {}\n' });
140
+ const found = discover(workingTree(root), KINDS, "fx");
141
+ expect(found.entries.map((e) => [e.dir, e.outcome, e.member])).toEqual([
142
+ [".", "member", "root"],
143
+ ["sub", "member", "sub"],
144
+ ]);
145
+ });
146
+
147
+ test("a module source that leaves <dir> is not followed, and a symlinked directory is not walked", () => {
148
+ const root = scratchDir("chant-discover-");
149
+ const outside = scratchDir("chant-discover-outside-");
150
+ writeFiles(outside, { "main.tf": 'resource "x" "y" {}\n' });
151
+ writeFiles(root, { "net/main.tf": 'module "m" {\n source = "../../elsewhere"\n}\n' });
152
+ symlinkSync(outside, join(root, "linked"), "dir");
153
+ const where = locateDiscovered(root);
154
+ expect(discover(where.base, KINDS, "fx").entries.map((e) => [e.dir, e.outcome])).toEqual([["net", "member"]]);
155
+ });
156
+
157
+ test("the walk writes nothing", () => {
158
+ const root = scratchDir("chant-discover-");
159
+ writeFiles(root, FX);
160
+ const before = snapshot(root);
161
+ discover(locateDiscovered(root).base, discoveryKinds(), "fx");
162
+ expect(snapshot(root)).toEqual(before);
163
+ });
164
+ });
165
+
166
+ describe("the declaration laid over the tree", () => {
167
+ test("shows chant.workspace.json at the root and reads everything else through", () => {
168
+ const root = scratchDir("chant-discover-");
169
+ writeFiles(root, { "a/main.tf": "x\n" });
170
+ const tree = discoveredTree(workingTree(root), '{"name":"fx","schema":1,"members":[]}\n');
171
+ expect(declares(workingTree(root))).toBe(false);
172
+ expect(declares(tree)).toBe(true);
173
+ expect(tree.list("")!.map((e) => e.name).sort()).toEqual(["a", "chant.workspace.json"]);
174
+ expect(tree.read("a/main.tf")).toBe("x\n");
175
+ expect(tree.list("a")).toEqual([{ name: "main.tf", type: "file" }]);
176
+ });
177
+ });
178
+
179
+ describe("kinds beside the running chant", () => {
180
+ test("come from the installed @intentius packages that publish workspace kinds", () => {
181
+ const names = kindPackagesBeside().map((p) => p.package);
182
+ expect(names).toContain("@intentius/chant-lexicon-terraform");
183
+ expect(names).toContain("@intentius/chant-lexicon-k8s");
184
+ const kinds = discoveryKinds();
185
+ expect(kinds.registry.get("terraform")?.source).toBe("@intentius/chant-lexicon-terraform");
186
+ expect(kinds.registry.get("argo-app")?.source).toBe("@intentius/chant-lexicon-k8s");
187
+ });
188
+ });
189
+
190
+ describe("workspace graph without a declaration", () => {
191
+ test("fails with declaration-missing unless --discover is given", async () => {
192
+ const root = scratchDir("chant-discover-");
193
+ writeFiles(root, { "infra/net/main.tf": 'resource "aws_vpc" "main" {}\n' });
194
+ const { doc, failed } = await workspaceGraph({ cwd: root });
195
+ expect(failed).toBe(true);
196
+ expect("error" in doc && doc.error.code).toBe("declaration-missing");
197
+ });
198
+
199
+ test("--discover --dry-run plans the discovered members", () => {
200
+ const root = scratchDir("chant-discover-");
201
+ writeFiles(root, { "infra/net/main.tf": 'resource "aws_vpc" "main" {}\n', "argo/app.yaml": "apiVersion: argoproj.io/v1alpha1\nkind: Application\nmetadata:\n name: web\n" });
202
+ const plan = planGraph({ cwd: root, discover: true });
203
+ expect(plan.groups.flatMap((g) => g.units.map((u) => [u.member, u.kind, u.dir]))).toEqual([
204
+ ["argo", "argo-app", "argo"],
205
+ ["infra-net", "terraform", "infra/net"],
206
+ ]);
207
+ });
208
+
209
+ test("--discover --at discovers in the commit's tree under <dir>", () => {
210
+ const top = repo({ "estate/infra/net/main.tf": 'resource "aws_vpc" "main" {}\n', "estate/argo/app.yaml": "apiVersion: argoproj.io/v1alpha1\nkind: Application\nmetadata:\n name: web\n" }, true);
211
+ rmSync(join(top, "estate", "argo"), { recursive: true });
212
+ const root = join(top, "estate");
213
+ expect(planGraph({ cwd: root, discover: true }).groups.flatMap((g) => g.units.map((u) => u.member))).toEqual(["infra-net"]);
214
+ expect(planGraph({ cwd: root, discover: true, at: "HEAD" }).groups.flatMap((g) => g.units.map((u) => u.member))).toEqual(["argo", "infra-net"]);
215
+ expect(locateDiscovered(root, "HEAD").root).toBe("estate");
216
+ });
217
+
218
+ test("with a declaration in <dir>, --discover reads it as declared", () => {
219
+ const root = scratchDir("chant-discover-");
220
+ writeFiles(root, { "chant.workspace.json": '{"name":"declared","schema":1,"members":[{"name":"docs","dir":"docs","kind":"other","because":"prose"}]}\n', "docs/main.tf": "x\n" });
221
+ const plan = planGraph({ cwd: root, discover: true });
222
+ expect(plan.workspace.name).toBe("declared");
223
+ expect(plan.skipped.map((s) => s.name)).toEqual(["docs"]);
224
+ });
225
+ });