Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(k8s): read manifests and kustomizations behind a leading comment

Martin Vogel6bdb971c9072
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index 0396ee0e4..bad926e8e 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -102,15 +102,30 @@ static void emit_kustomize_sequence(CBMExtractCtx *ctx, TSNode seq_node, const c
}
}
+// First named child that is not a comment. A comment between `---` and the
+// mapping (the `# yaml-language-server: $schema=...` line editors add) is the
+// document's first named child; the content follows it.
+static TSNode first_content_child(TSNode node) {
+ uint32_t n = ts_node_named_child_count(node);
+ for (uint32_t i = 0; i < n; i++) {
+ TSNode child = ts_node_named_child(node, i);
+ if (strcmp(ts_node_type(child), "comment") != 0) {
+ return child;
+ }
+ }
+ TSNode null_node = {0};
+ return null_node;
+}
+
// Unwrap a YAML node through optional block_node wrapper to get block_mapping.
// Returns null node if not a block_mapping.
static TSNode unwrap_block_mapping(TSNode doc_child) {
- TSNode mapping = ts_node_named_child(doc_child, 0);
+ TSNode mapping = first_content_child(doc_child);
if (ts_node_is_null(mapping)) {
return mapping;
}
if (strcmp(ts_node_type(mapping), "block_node") == 0) {
- mapping = ts_node_named_child(mapping, 0);
+ mapping = first_content_child(mapping);
}
if (ts_node_is_null(mapping) || strcmp(ts_node_type(mapping), "block_mapping") != 0) {
TSNode null_node = {0};
@@ -307,16 +322,10 @@ static void extract_k8s_manifest(CBMExtractCtx *ctx) {
continue;
}
- TSNode mapping = ts_node_named_child(stream_child, 0);
+ TSNode mapping = unwrap_block_mapping(stream_child);
if (ts_node_is_null(mapping)) {
continue;
}
- if (strcmp(ts_node_type(mapping), "block_node") == 0) {
- mapping = ts_node_named_child(mapping, 0);
- }
- if (ts_node_is_null(mapping) || strcmp(ts_node_type(mapping), "block_mapping") != 0) {
- continue;
- }
char kind_buf[K8S_BUF_SIZE] = {0};
char meta_name_buf[K8S_BUF_SIZE] = {0};
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 3a6982ef6..4bc665309 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -239,6 +239,7 @@ static void k8s_scan_labels(const char *source, k8s_record_t *rec) {
} stack[K8S_PATH_DEPTH];
int depth = 0;
bool got_name = false;
+ bool seen_key = false;
const char *p = source;
while (p && *p) {
@@ -254,11 +255,20 @@ static void k8s_scan_labels(const char *source, k8s_record_t *rec) {
while (*trimmed == ' ') {
trimmed++;
}
+ /* A `---` before any key opens the first document; it does not end it. */
if (strncmp(trimmed, "---", 3) == 0 && line == trimmed) {
- break;
+ if (seen_key) {
+ break;
+ }
+ if (!eol) {
+ break;
+ }
+ p = eol + 1;
+ continue;
}
if (trimmed[0] && trimmed[0] != '#') {
+ seen_key = true;
int ind = k8s_indent(line);
while (depth > 0 && stack[depth - 1].indent >= ind) {
depth--;
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index ed734f052..a0673374f 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -470,6 +470,79 @@ TEST(probe_kustomize_with_patches) {
PASS();
}
+/* A comment between `---` and the mapping (the `# yaml-language-server:
+ * $schema=...` line editors add) is the document's first named child; the
+ * manifest behind it is still a manifest. onedr0p/home-ops opens 428 of its
+ * manifests this way and got a Resource for 26 of 390 objects. */
+TEST(probe_k8s_manifest_after_leading_comment) {
+ GpfMetrics m = gpf_metrics("helmrelease.yaml",
+ "---\n"
+ "# yaml-language-server: $schema=https://example.com/helmrelease.json\n"
+ "apiVersion: helm.toolkit.fluxcd.io/v2\n"
+ "kind: HelmRelease\n"
+ "metadata:\n"
+ " name: echo\n"
+ "spec:\n"
+ " interval: 1h\n");
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.resources, 1);
+ PASS();
+}
+
+/* The same comment in front of a kustomization hid its resource list:
+ * home-ops resolved 1 of 348 entries. */
+TEST(probe_kustomize_imports_after_leading_comment) {
+ static const GpfFile files[] = {
+ {"kustomization.yaml",
+ "---\n"
+ "# yaml-language-server: $schema=https://json.schemastore.org/kustomization\n"
+ "apiVersion: kustomize.config.k8s.io/v1beta1\n"
+ "kind: Kustomization\n"
+ "resources:\n"
+ " - namespace.yaml\n"},
+ {"namespace.yaml",
+ "apiVersion: v1\n"
+ "kind: Namespace\n"
+ "metadata:\n"
+ " name: network\n"}
+ };
+ GpfMetrics m = gpf_metrics_files(files, 2);
+ ASSERT_TRUE(m.ok);
+ ASSERT_TRUE(m.imports >= 1);
+ PASS();
+}
+
+/* The label scan stopped at a leading `---`, so a Service written that way
+ * had no selector and linked to nothing. The workload's name differs from the
+ * selector value, so only the pod-template label can match. */
+TEST(probe_k8s_selector_after_leading_separator) {
+ static const GpfFile files[] = {
+ {"service.yaml",
+ "---\n"
+ "apiVersion: v1\n"
+ "kind: Service\n"
+ "metadata:\n"
+ " name: web\n"
+ "spec:\n"
+ " selector:\n"
+ " app: web-pods\n"},
+ {"deploy.yaml",
+ "apiVersion: apps/v1\n"
+ "kind: Deployment\n"
+ "metadata:\n"
+ " name: web-deploy\n"
+ "spec:\n"
+ " template:\n"
+ " metadata:\n"
+ " labels:\n"
+ " app: web-pods\n"}
+ };
+ GpfMetrics m = gpf_metrics_files(files, 2);
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.infra_maps, 1);
+ PASS();
+}
+
/* ══════════════════════════════════════════════════════════════════
* GROUP 4 — Nix (.nix)
*
@@ -1567,6 +1640,9 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_kustomize_module_node);
RUN_TEST(probe_kustomize_imports_edges);
RUN_TEST(probe_kustomize_with_patches);
+ RUN_TEST(probe_k8s_manifest_after_leading_comment);
+ RUN_TEST(probe_kustomize_imports_after_leading_comment);
+ RUN_TEST(probe_k8s_selector_after_leading_separator);
/* Nix */
RUN_TEST(probe_nix_attrset_node);