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): a comment ahead of a YAML value no longer hides it

Martin Vogelf08641ee5169
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index bad926e8e..88f8a46f8 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -134,6 +134,27 @@ static TSNode unwrap_block_mapping(TSNode doc_child) {
return mapping;
}
+// Unwrap a block_mapping_pair value through optional block_node. Comments are
+// skipped on both levels: one trailing the key line (`metadata: # note`) is a
+// named child of the pair ahead of its value, and a commented-out first entry
+// (`resources:` / ` # - a.yaml` / ` - b.yaml`) is the block_node's first
+// named child, ahead of the sequence.
+static TSNode unwrap_pair_value(TSNode pair) {
+ TSNode val_node = {0};
+ uint32_t n = ts_node_named_child_count(pair);
+ for (uint32_t i = SKIP_ONE; i < n; i++) {
+ TSNode child = ts_node_named_child(pair, i);
+ if (strcmp(ts_node_type(child), "comment") != 0) {
+ val_node = child;
+ break;
+ }
+ }
+ if (!ts_node_is_null(val_node) && strcmp(ts_node_type(val_node), "block_node") == 0) {
+ val_node = first_content_child(val_node);
+ }
+ return val_node;
+}
+
// Process a single block_mapping_pair for kustomize list keys.
static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {
if (strcmp(ts_node_type(pair), "block_mapping_pair") != 0) {
@@ -148,22 +169,13 @@ static void process_kustomize_pair(CBMExtractCtx *ctx, TSNode pair) {
return;
}
- TSNode val_node = ts_node_named_child(pair, SKIP_ONE);
- if (ts_node_is_null(val_node)) {
- return;
- }
- if (strcmp(ts_node_type(val_node), "block_node") == 0) {
- val_node = ts_node_named_child(val_node, 0);
- }
+ TSNode val_node = unwrap_pair_value(pair);
if (ts_node_is_null(val_node) || strcmp(ts_node_type(val_node), "block_sequence") != 0) {
return;
}
emit_kustomize_sequence(ctx, val_node, key_text);
}
-// Forward declaration: defined with the K8s-manifest helpers below.
-static TSNode unwrap_pair_value(TSNode pair);
-
// Emit a "Class" def named after the document's `kind` scalar. A kustomization
// file has no metadata.name, so the def name is the bare kind ("Kustomization").
// Mirrors the K8s manifest kind-def so Kustomize resources are also discoverable.
@@ -249,7 +261,7 @@ static void extract_metadata_name(CBMArena *a, TSNode meta_mapping, const char *
if (!mkey_text || strcmp(mkey_text, "name") != 0) {
continue;
}
- TSNode mval = ts_node_named_child(mpair, SKIP_ONE);
+ TSNode mval = unwrap_pair_value(mpair);
if (ts_node_is_null(mval)) {
continue;
}
@@ -260,18 +272,6 @@ static void extract_metadata_name(CBMArena *a, TSNode meta_mapping, const char *
}
}
-// Unwrap a block_mapping_pair value through optional block_node.
-static TSNode unwrap_pair_value(TSNode pair) {
- TSNode val_node = ts_node_named_child(pair, SKIP_ONE);
- if (ts_node_is_null(val_node)) {
- return val_node;
- }
- if (strcmp(ts_node_type(val_node), "block_node") == 0) {
- val_node = ts_node_named_child(val_node, 0);
- }
- return val_node;
-}
-
// Descend into the first block_mapping of a document and extract
// kind and metadata.name. Returns void; fills kind_buf and meta_name_buf.
static void extract_k8s_scalars(CBMExtractCtx *ctx, TSNode mapping, char *kind_buf, size_t kind_sz,
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index 5995b8f7e..37638e192 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -747,6 +747,21 @@ TEST(probe_k8s_manifest_after_leading_comment) {
PASS();
}
+/* A comment between a key and its block (`metadata: # owned by ...`) sits
+ * ahead of the value; the object still has its name. */
+TEST(probe_k8s_manifest_metadata_after_key_line_comment) {
+ GpfMetrics m = gpf_metrics("deploy.yaml",
+ "apiVersion: apps/v1\n"
+ "kind: Deployment\n"
+ "metadata: # owned by the platform team\n"
+ " name: web\n"
+ "spec:\n"
+ " replicas: 1\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) {
@@ -770,6 +785,55 @@ TEST(probe_kustomize_imports_after_leading_comment) {
PASS();
}
+/* A commented-out entry above the first item (kubernetes/k8s.io's
+ * kubernetes/apps: `# - argocd.yaml ...`) is a comment ahead of the list;
+ * the entries below it are still the list. */
+TEST(probe_kustomize_entries_after_commented_out_entry) {
+ static const GpfFile files[] = {
+ {"kustomization.yaml",
+ "apiVersion: kustomize.config.k8s.io/v1beta1\n"
+ "kind: Kustomization\n"
+ "resources:\n"
+ " # - argocd.yaml applied by hand\n"
+ " - atlantis.yaml\n"
+ " - datadog.yaml\n"},
+ {"atlantis.yaml",
+ "apiVersion: v1\n"
+ "kind: Namespace\n"
+ "metadata:\n"
+ " name: atlantis\n"},
+ {"datadog.yaml",
+ "apiVersion: v1\n"
+ "kind: Namespace\n"
+ "metadata:\n"
+ " name: datadog\n"}
+ };
+ GpfMetrics m = gpf_metrics_files(files, 3);
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.imports, 2);
+ PASS();
+}
+
+/* Same with the comment trailing the key line. */
+TEST(probe_kustomize_entries_after_key_line_comment) {
+ static const GpfFile files[] = {
+ {"kustomization.yaml",
+ "apiVersion: kustomize.config.k8s.io/v1beta1\n"
+ "kind: Kustomization\n"
+ "resources: # applied by Argo CD\n"
+ " - atlantis.yaml\n"},
+ {"atlantis.yaml",
+ "apiVersion: v1\n"
+ "kind: Namespace\n"
+ "metadata:\n"
+ " name: atlantis\n"}
+ };
+ GpfMetrics m = gpf_metrics_files(files, 2);
+ ASSERT_TRUE(m.ok);
+ ASSERT_EQ(m.imports, 1);
+ PASS();
+}
+
/* kustomize also reads an extensionless file named `Kustomization`
* (kubermatic's addons use it); it was never discovered as a kustomization. */
TEST(probe_kustomize_extensionless_file) {
@@ -2089,6 +2153,9 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_helm_unresolved_file_dependency_stays_external);
RUN_TEST(probe_kustomize_nested_sibling_entries);
RUN_TEST(probe_kustomize_single_module);
+ RUN_TEST(probe_kustomize_entries_after_commented_out_entry);
+ RUN_TEST(probe_kustomize_entries_after_key_line_comment);
+ RUN_TEST(probe_k8s_manifest_metadata_after_key_line_comment);
/* Nix */
RUN_TEST(probe_nix_attrset_node);