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.

feat(infra): a container's environment reaches its own program

Martin Vogel631673353264
diff --git a/Makefile.cbm b/Makefile.cbm
index f1402881b..307c39574 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -541,6 +541,7 @@ PIPELINE_SRCS = \
src/pipeline/pass_shell.c \
src/pipeline/pass_terraform.c \
src/pipeline/pass_docker.c \
+ src/pipeline/pass_envjoin.c \
src/pipeline/pass_ci.c \
src/pipeline/pass_similarity.c \
src/pipeline/pass_semantic_edges.c \
diff --git a/internal/cbm/cbm.h b/internal/cbm/cbm.h
index 9fac8cf55..a855290e1 100644
--- a/internal/cbm/cbm.h
+++ b/internal/cbm/cbm.h
@@ -713,8 +713,8 @@ typedef struct {
* CBM_K8S_FACT_SOURCE: object def_index (a Flux OCIRepository) fetches
* from the URL `name`; a HelmRelease whose chartRef names it deploys the
* chart there.
- * CBM_K8S_FACT_IMAGE: object def_index runs a container of image `name`
- * (a literal reference); via "container".
+ * CBM_K8S_FACT_IMAGE: object def_index runs its container `container`
+ * (NULL: unnamed) from image `name` (a literal reference); via "container".
* CBM_K8S_FACT_ENV: object def_index sets environment variable `name` in
* its container `container` (NULL: unnamed) at `line`; via = where the
* value comes from ("value", or the valueFrom kind) -- never the value. */
diff --git a/internal/cbm/extract_k8s.c b/internal/cbm/extract_k8s.c
index e6602c3f2..6f051572d 100644
--- a/internal/cbm/extract_k8s.c
+++ b/internal/cbm/extract_k8s.c
@@ -773,35 +773,13 @@ static void k8s_emit_ref(k8s_walk_t *w, const char *kind, const char *ns, const
cbm_k8sfact_push(&w->ctx->result->k8s_facts, a, f);
}
-// A container's literal image: `path` ends in containers[].image,
-// initContainers[].image or ephemeralContainers[].image at any depth (pod
-// templates, CronJobs, the pod templates of custom resources).
-static bool k8s_image_leaf(const char *path, const char *value) {
- static const char *const LEAVES[] = {"containers[].image", "initContainers[].image",
- "ephemeralContainers[].image"};
- if (!value[0] || strchr(value, '$') || strchr(value, ' ') || strstr(value, "{{")) {
- return false;
- }
- size_t pl = strlen(path);
- for (size_t i = 0; i < sizeof(LEAVES) / sizeof(LEAVES[0]); i++) {
- size_t ll = strlen(LEAVES[i]);
- if (pl >= ll && strcmp(path + pl - ll, LEAVES[i]) == 0 &&
- (pl == ll || path[pl - ll - SKIP_ONE] == '.')) {
- return true;
- }
- }
- return false;
+// A literal image reference: no variable, no template, no blank.
+static bool k8s_image_literal(const char *value) {
+ return value && value[0] && !strchr(value, '$') && !strchr(value, ' ') && !strstr(value, "{{");
}
// A scalar at `path`: a leaf reference, or a CRD's group / kind.
static void k8s_leaf(k8s_walk_t *w, const char *path, const char *value) {
- if (k8s_image_leaf(path, value)) {
- CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE,
- .def_index = w->def_index,
- .name = cbm_arena_strdup(w->ctx->arena, value),
- .via = "container"};
- cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
- }
const char *pod = k8s_pod_prefix(w->kind);
size_t pl = strlen(pod);
if (pl && strncmp(path, pod, pl) == 0 && path[pl] == '.') {
@@ -1086,10 +1064,13 @@ static const char *k8s_env_source(k8s_walk_t *w, TSNode item) {
return "value";
}
-// The environment variables a container (`path` ends in containers[],
-// initContainers[] or ephemeralContainers[], any depth) sets: each env[]
-// name, with where its value comes from -- never the value.
-static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container) {
+// A container (`path` ends in containers[], initContainers[] or
+// ephemeralContainers[], any depth: pod templates, CronJobs, the pod
+// templates of custom resources): its literal image, and the environment
+// variables it sets -- each env[] name, with where its value comes from,
+// never the value. Both carry the container's name, so a variable joins the
+// program of its own container's image.
+static void k8s_container(k8s_walk_t *w, const char *path, TSNode container) {
static const char *const ITEMS[] = {"containers[]", "initContainers[]",
"ephemeralContainers[]"};
size_t pl = strlen(path);
@@ -1104,6 +1085,15 @@ static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container)
}
const char *cname = k8s_text(w, container, "name");
cname = cname && cname[0] && !strstr(cname, "{{") ? cname : NULL;
+ const char *image = k8s_text(w, container, "image");
+ if (k8s_image_literal(image)) {
+ CBMK8sFact f = {.fact = CBM_K8S_FACT_IMAGE,
+ .def_index = w->def_index,
+ .name = image,
+ .container = cname,
+ .via = "container"};
+ cbm_k8sfact_push(&w->ctx->result->k8s_facts, w->ctx->arena, f);
+ }
k8s_iter_t env = k8s_iter(k8s_child(w, container, "env"));
TSNode item;
while (k8s_next(&env, &item)) {
@@ -1123,7 +1113,7 @@ static void k8s_container_env(k8s_walk_t *w, const char *path, TSNode container)
// A mapping at `path` that holds a reference in its kind/name/namespace.
static void k8s_mapping(k8s_walk_t *w, const char *path, TSNode mapping) {
- k8s_container_env(w, path, mapping);
+ k8s_container(w, path, mapping);
for (size_t i = 0; i < sizeof(k8s_mapping_refs) / sizeof(k8s_mapping_refs[0]); i++) {
const k8s_mapping_ref_t *r = &k8s_mapping_refs[i];
if (k8s_kind_in(r->kinds, w->kind) && strcmp(path, r->path) == 0) {
diff --git a/scripts/check-lsp-originality.sh b/scripts/check-lsp-originality.sh
index 56c0fdcde..1d522437c 100755
--- a/scripts/check-lsp-originality.sh
+++ b/scripts/check-lsp-originality.sh
@@ -83,7 +83,7 @@ REFS=(
# Infrastructure hybrid LSP sources outside internal/cbm/lsp/ (scanned too).
INFRA_SRC=(internal/cbm/extract_k8s.c src/pipeline/pass_k8s.c src/pipeline/pass_operator.c
src/pipeline/pass_shell.c src/pipeline/pass_terraform.c
- src/pipeline/pass_ci.c src/pipeline/pass_docker.c)
+ src/pipeline/pass_ci.c src/pipeline/pass_docker.c src/pipeline/pass_envjoin.c)
ONLY_LANG=""; REFRESH=0; LIST_ONLY=0
while [ $# -gt 0 ]; do
diff --git a/src/pipeline/pass_docker.c b/src/pipeline/pass_docker.c
index 4a2d83c2a..b9cb65eba 100644
--- a/src/pipeline/pass_docker.c
+++ b/src/pipeline/pass_docker.c
@@ -52,6 +52,7 @@
#include "cbm.h"
#include "helpers.h"
#include "lang_specs.h"
+#include "yyjson/yyjson.h"
#include <ctype.h>
#include <stdatomic.h>
@@ -103,6 +104,7 @@ typedef struct {
typedef struct {
CBMHashTable *files; /* repo path -> File node */
CBMHashTable *gomain; /* repo dir -> the `main` Function of its package */
+ CBMHashTable *dirs; /* every directory holding a file (not the root) */
} dk_index_t;
typedef struct dk_var {
@@ -549,46 +551,100 @@ static int dk_json_array(dk_t *d, TSNode arr, const char **out, int max) {
return n;
}
+/* The logical line at byte `at` of the source, as Docker joins it: a line
+ * ending in `\` (blanks after it aside) goes on in the next one;
+ * dk_uncomment already made comment lines inside a continuation bare
+ * continuations. */
+static char *dk_logical_line(dk_t *d, size_t at) {
+ const char *s = d->src + at;
+ char *out = cbm_arena_alloc(d->a, strlen(s) + SKIP_ONE);
+ size_t n = 0;
+ while (out && *s) {
+ const char *e = strchr(s, '\n');
+ size_t l = e ? (size_t)(e - s) : strlen(s);
+ size_t t = l;
+ while (t > 0 &&
+ (s[t - SKIP_ONE] == ' ' || s[t - SKIP_ONE] == '\t' || s[t - SKIP_ONE] == '\r')) {
+ t--;
+ }
+ bool more = e && t > 0 && s[t - SKIP_ONE] == '\\';
+ size_t keep = more ? t - SKIP_ONE : t;
+ memcpy(out + n, s, keep);
+ n += keep;
+ if (!more) {
+ break;
+ }
+ out[n++] = ' ';
+ s = e + SKIP_ONE;
+ }
+ if (out) {
+ out[n] = '\0';
+ }
+ return out;
+}
+
+/* A COPY / ADD in JSON form: its strings, expanded. */
+static int dk_copy_json(dk_t *d, const char *p, const char **out, int max) {
+ yyjson_doc *doc = yyjson_read(p, strlen(p), YYJSON_READ_STOP_WHEN_DONE);
+ yyjson_val *arr = doc ? yyjson_doc_get_root(doc) : NULL;
+ int n = 0;
+ yyjson_arr_iter it;
+ yyjson_val *v;
+ if (yyjson_is_arr(arr)) {
+ for (yyjson_arr_iter_init(arr, &it); n < max && (v = yyjson_arr_iter_next(&it));) {
+ const char *s = yyjson_get_str(v);
+ out[n++] = s ? dk_expand(d, d->local, cbm_arena_strdup(d->a, s)) : NULL;
+ }
+ }
+ if (doc) {
+ yyjson_doc_free(doc);
+ }
+ return n;
+}
+
+/* COPY / ADD as Docker reads it, from the instruction's own text: flags
+ * first, then the JSON form or whitespace-separated words (a heredoc
+ * source is unknown). The grammar loses a JSON form opening with `/`
+ * (otel accounting) and, after a word with trailing blanks, takes the
+ * instructions that follow for more words (awesome-compose wasmedge). */
static void dk_copy(dk_t *d, TSNode ins) {
static const char FROM[] = "--from=";
const char *words[DK_MAX_WORDS];
int nw = 0;
dk_copy_t *c = cbm_arena_alloc(d->a, sizeof(*c));
- if (c) {
- *c = (dk_copy_t){.workdir = d->workdir};
+ char *p = dk_logical_line(d, ts_node_start_byte(ins));
+ if (!c || !p) {
+ return;
}
- for (uint32_t i = 0; i < ts_node_named_child_count(ins); i++) {
- TSNode p = ts_node_named_child(ins, i);
- if (dk_type(p, "path") && nw < DK_MAX_WORDS) {
- const char *t = dk_text(d, p);
- words[nw++] = t ? dk_expand(d, d->local, t) : NULL;
- continue;
- }
- if (dk_type(p, "json_string_array")) {
- const char *raw[DK_MAX_WORDS];
- int n = dk_json_array(d, p, raw, DK_MAX_WORDS - nw);
- for (int k = 0; k < n; k++) {
- words[nw++] = raw[k] ? dk_expand(d, d->local, raw[k]) : NULL;
- }
- continue;
- }
- if (!dk_type(p, "param")) {
- continue;
- }
- const char *t = dk_text(d, p);
- if (!t || strncmp(t, FROM, sizeof(FROM) - SKIP_ONE) != 0) {
- continue;
+ *c = (dk_copy_t){.workdir = d->workdir};
+ p += strcspn(p, " \t"); /* the keyword */
+ for (;;) {
+ p += strspn(p, " \t");
+ if (strncmp(p, "--", PAIR_LEN) != 0) {
+ break;
}
- const char *ref = dk_expand(d, d->local, t + sizeof(FROM) - SKIP_ONE);
- if (c) {
+ size_t l = strcspn(p, " \t");
+ if (l > sizeof(FROM) - SKIP_ONE && strncmp(p, FROM, sizeof(FROM) - SKIP_ONE) == 0) {
+ const char *v =
+ cbm_arena_strndup(d->a, p + sizeof(FROM) - SKIP_ONE, l - (sizeof(FROM) - SKIP_ONE));
+ const char *ref = v ? dk_expand(d, d->local, v) : NULL;
c->has_from = true;
c->from = ref;
+ if (ref && ref[0] && !dk_is_stage(d, ref)) {
+ dk_link(d, ref, "copy-from", dk_line(ins));
+ }
}
- if (ref && ref[0] && !dk_is_stage(d, ref)) {
- dk_link(d, ref, "copy-from", dk_line(ins));
- }
+ p += l;
}
- if (!c || nw < PAIR_LEN || d->stage >= DK_MAX_STAGES) {
+ bool json = *p == '[' && (nw = dk_copy_json(d, p, words, DK_MAX_WORDS)) > 0;
+ while (!json && *p && nw < DK_MAX_WORDS) {
+ size_t l = strcspn(p, " \t");
+ const char *w = cbm_arena_strndup(d->a, p, l);
+ words[nw++] = w && strncmp(w, "<<", PAIR_LEN) != 0 ? dk_expand(d, d->local, w) : NULL;
+ p += l;
+ p += strspn(p, " \t");
+ }
+ if (nw < PAIR_LEN || d->stage >= DK_MAX_STAGES) {
return;
}
c->srcs = cbm_arena_alloc(d->a, (size_t)(nw - SKIP_ONE) * sizeof(*c->srcs));
@@ -1148,6 +1204,199 @@ static void dk_envs(dk_t *d) {
cbm_ht_free(seen);
}
+/* ── The image's files (F1: the env join) ───────────────────────────── */
+
+static char *dk_read(const char *path, size_t *len);
+
+enum { DK_SET_CANDS = 2 };
+
+static bool dk_glob(const char *s) {
+ return strpbrk(s, "*?[") != NULL;
+}
+
+/* Context source `src` as a repository path under `ctx`; NULL when it climbs
+ * out of the repository. */
+static const char *dk_in_ctx(dk_t *d, const char *ctx, const char *src) {
+ while (*src == '/') {
+ src++;
+ }
+ const char *p = dk_norm(d, dk_join(d, ctx, src));
+ return p && strncmp(p, "..", PAIR_LEN) != 0 ? p : NULL;
+}
+
+static bool dk_exists(const dk_t *d, const char *p) {
+ return !p[0] || cbm_ht_get(d->ix->files, p) || (d->ix->dirs && cbm_ht_get(d->ix->dirs, p));
+}
+
+/* The stages the image is made of: the last, the stages each is built FROM
+ * and the stages each COPYs --from, transitively. */
+static void dk_image_stages(const dk_t *d, bool *in) {
+ in[d->stage] = true;
+ for (bool grew = true; grew;) {
+ grew = false;
+ for (int s = 0; s <= d->stage; s++) {
+ if (!in[s]) {
+ continue;
+ }
+ int p = d->st[s].parent;
+ if (p >= 0 && p < DK_MAX_STAGES && !in[p]) {
+ in[p] = grew = true;
+ }
+ for (const dk_copy_t *c = d->st[s].copies; c; c = c->next) {
+ int f = c->has_from ? dk_stage_of(d, c->from) : -1;
+ if (f >= 0 && f < DK_MAX_STAGES && !in[f]) {
+ in[f] = grew = true;
+ }
+ }
+ }
+ }
+}
+
+/* Calls fn(d, src, ud) for each context source (no --from, no URL) the
+ * image's stages COPY / ADD. */
+static void dk_image_sources(dk_t *d, const bool *in, void (*fn)(dk_t *, const char *, void *),
+ void *ud) {
+ for (int s = 0; s <= d->stage; s++) {
+ for (const dk_copy_t *c = in[s] ? d->st[s].copies : NULL; c; c = c->next) {
+ for (int k = 0; !c->has_from && k < c->nsrc; k++) {
+ if (c->srcs[k] && !strstr(c->srcs[k], "://")) {
+ fn(d, c->srcs[k], ud);
+ }
+ }
+ }
+ }
+}
+
+typedef struct {
+ const char *cands[DK_SET_CANDS];
+ int ncand;
+ int hits[DK_SET_CANDS];
+} dk_ctx_vote_t;
+
+/* A literal source that names something below the context votes for each
+ * candidate context it exists under. */
+static void dk_ctx_vote(dk_t *d, const char *src, void *ud) {
+ dk_ctx_vote_t *v = ud;
+ const char *n = dk_glob(src) ? NULL : dk_in_ctx(d, "", src);
+ for (int i = 0; n && n[0] && i < v->ncand; i++) {
+ const char *p = dk_in_ctx(d, v->cands[i], src);
+ v->hits[i] += p && dk_exists(d, p);
+ }
+}
+
+typedef struct {
+ const char *ctx;
+ const char *json; /* the array so far, without its closing ']' */
+ CBMHashTable *seen;
+} dk_json_list_t;
+
+static void dk_json_add(dk_t *d, dk_json_list_t *l, const char *item) {
+ if (!item || !l->json || cbm_ht_get(l->seen, item)) {
+ return;
+ }
+ cbm_ht_set(l->seen, item, (void *)item);
+ size_t cap = strlen(item) * PAIR_LEN + PAIR_LEN;
+ char *esc = cbm_arena_alloc(d->a, cap);
+ if (!esc) {
+ l->json = NULL;
+ return;
+ }
+ cbm_json_escape(esc, (int)cap, item);
+ l->json = cbm_arena_sprintf(d->a, "%s%s\"%s\"", l->json, l->json[SKIP_ONE] ? "," : "", esc);
+}
+
+static void dk_file_add(dk_t *d, const char *src, void *ud) {
+ dk_json_list_t *l = ud;
+ dk_json_add(d, l, dk_in_ctx(d, l->ctx, src));
+}
+
+/* The context's ignore file, as repository-path patterns ("!" kept):
+ * <Dockerfile>.dockerignore beside the Dockerfile first, else
+ * <context>/.dockerignore. */
+static void dk_ignore_add(dk_t *d, dk_json_list_t *l) {
+ const char *root = d->ctx->repo_path;
+ if (!root) {
+ return;
+ }
+ char path[CBM_SZ_4K];
+ size_t len = 0;
+ snprintf(path, sizeof(path), "%s/%s.dockerignore", root, d->file);
+ char *src = dk_read(path, &len);
+ if (!src) {
+ snprintf(path, sizeof(path), "%s/%s%s.dockerignore", root, l->ctx, l->ctx[0] ? "/" : "");
+ src = dk_read(path, &len);
+ }
+ for (char *line = src; line && *line;) {
+ char *end = strchr(line, '\n');
+ char *next = end ? end + SKIP_ONE : line + strlen(line);
+ if (end) {
+ *end = '\0';
+ }
+ char *s = line;
+ while (*s == ' ' || *s == '\t') {
+ s++;
+ }
+ size_t sl = strlen(s);
+ while (sl > 0 &&
+ (s[sl - SKIP_ONE] == ' ' || s[sl - SKIP_ONE] == '\t' || s[sl - SKIP_ONE] == '\r')) {
+ s[--sl] = '\0';
+ }
+ bool neg = s[0] == '!';
+ const char *pat = neg ? s + SKIP_ONE : s;
+ const char *p = s[0] && s[0] != '#' ? dk_in_ctx(d, l->ctx, pat) : NULL;
+ if (p && strcmp(p, l->ctx) != 0) {
+ dk_json_add(d, l, neg ? cbm_arena_sprintf(d->a, "!%s", p) : p);
+ }
+ line = next;
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
+}
+
+/* F1: the files the image is made of -- the context sources its stages COPY
+ * / ADD, resolved in the build context: the Dockerfile's directory or the
+ * repository root, the one more of the literal sources exist under (a tie:
+ * the directory) -- and the context's ignore patterns, kept on the
+ * Dockerfile's Module (image_context, image_files, image_ignore) for the env
+ * join, which reads them back in an incremental run. */
+static void dk_image_set(dk_t *d) {
+ cbm_gbuf_node_t *mod = (cbm_gbuf_node_t *)cbm_gbuf_find_by_id(d->ctx->gbuf, d->module);
+ if (!mod || d->stage < 0 || d->stage >= DK_MAX_STAGES) {
+ return;
+ }
+ bool in[DK_MAX_STAGES] = {false};
+ dk_image_stages(d, in);
+ dk_ctx_vote_t v = {.cands = {d->dir, ""}, .ncand = d->dir[0] ? DK_SET_CANDS : SKIP_ONE};
+ dk_image_sources(d, in, dk_ctx_vote, &v);
+ const char *ctx = v.ncand > SKIP_ONE && v.hits[SKIP_ONE] > v.hits[0] ? "" : d->dir;
+ dk_json_list_t files = {.ctx = ctx, .json = "[", .seen = cbm_ht_create(0)};
+ dk_json_list_t ignore = {.ctx = ctx, .json = "[", .seen = cbm_ht_create(0)};
+ if (files.seen && ignore.seen) {
+ dk_image_sources(d, in, dk_file_add, &files);
+ dk_ignore_add(d, &ignore);
+ }
+ const char *props =
+ mod->properties_json && mod->properties_json[0] ? mod->properties_json : "{}";
+ size_t pl = strlen(props);
+ if (files.seen && ignore.seen && files.json && files.json[SKIP_ONE] && ignore.json &&
+ pl >= PAIR_LEN && props[pl - SKIP_ONE] == '}') {
+ char cesc[CBM_SZ_4K];
+ cbm_json_escape(cesc, (int)sizeof(cesc), ctx);
+ bool empty = strspn(props + SKIP_ONE, " \t\r\n") == pl - PAIR_LEN;
+ const char *out = cbm_arena_sprintf(
+ d->a, "%.*s%s\"image_context\":\"%s\",\"image_files\":%s],\"image_ignore\":%s]}",
+ (int)(pl - SKIP_ONE), props, empty ? "" : ",", cesc, files.json, ignore.json);
+ if (out) {
+ (void)cbm_gbuf_node_set_properties_json(mod, out);
+ }
+ }
+ if (files.seen) {
+ cbm_ht_free(files.seen);
+ }
+ if (ignore.seen) {
+ cbm_ht_free(ignore.seen);
+ }
+}
+
static void dk_file(dk_t *d, TSNode root) {
for (uint32_t i = 0; i < ts_node_named_child_count(root); i++) {
TSNode ins = ts_node_named_child(root, i);
@@ -1172,6 +1421,7 @@ static void dk_file(dk_t *d, TSNode root) {
}
dk_spawn(d);
dk_envs(d);
+ dk_image_set(d);
}
static char *dk_read(const char *path, size_t *len) {
@@ -1427,17 +1677,32 @@ static bool dk_node_before(const cbm_gbuf_node_t *a, const cbm_gbuf_node_t *b) {
return c < 0 || (c == 0 && a->start_line < b->start_line);
}
-/* The repository's File nodes by path, and the `main` of each Go main
- * package by directory (the first by file and line). */
+/* Every directory above repository path `p` into `dirs`. */
+static void dk_index_dirs(CBMArena *a, CBMHashTable *dirs, const char *p) {
+ for (const char *s = strchr(p, '/'); s; s = strchr(s + SKIP_ONE, '/')) {
+ char *dir = cbm_arena_strndup(a, p, (size_t)(s - p));
+ if (!dir || cbm_ht_get(dirs, dir)) {
+ continue;
+ }
+ cbm_ht_set(dirs, dir, dir);
+ }
+}
+
+/* The repository's File nodes by path, its directories, and the `main` of
+ * each Go main package by directory (the first by file and line). */
static void dk_index_build(cbm_pipeline_ctx_t *ctx, CBMArena *a, dk_index_t *ix) {
ix->files = cbm_ht_create(0);
ix->gomain = cbm_ht_create(0);
+ ix->dirs = cbm_ht_create(0);
const cbm_gbuf_node_t **nodes = NULL;
int n = 0;
if (ix->files && cbm_gbuf_find_by_label(ctx->gbuf, "File", &nodes, &n) == 0) {
for (int i = 0; i < n; i++) {
if (nodes[i]->file_path) {
cbm_ht_set(ix->files, nodes[i]->file_path, (void *)nodes[i]);
+ if (ix->dirs) {
+ dk_index_dirs(a, ix->dirs, nodes[i]->file_path);
+ }
}
}
}
@@ -1963,6 +2228,9 @@ void cbm_pipeline_pass_docker(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *fi
if (ix.gomain) {
cbm_ht_free(ix.gomain);
}
+ if (ix.dirs) {
+ cbm_ht_free(ix.dirs);
+ }
edges += dk_link_builds(ctx, &arena);
edges += dk_image_hubs(ctx, &arena);
cbm_arena_destroy(&arena);
diff --git a/src/pipeline/pass_envjoin.c b/src/pipeline/pass_envjoin.c
new file mode 100644
index 000000000..203aa88a8
--- /dev/null
+++ b/src/pipeline/pass_envjoin.c
@@ -0,0 +1,483 @@
+/*
+ * pass_envjoin.c — F1: which program reads the variables a container sets.
+ *
+ * P4.5 gives each variable a container sets an EnvVar of its own
+ * (`<owner QN>#env/<container | ->/<KEY>`); a program's readers point at the
+ * global EnvVar of the name (CONFIGURES {strategy:env_access}). The same name
+ * is read by many programs, so the join connects a setter only to the
+ * readers inside its container's own program -- the files its image is built
+ * from:
+ * EnvVar -CONFIGURES {strategy:env_read, dockerfile}-> reader
+ * The image: a K8s container's literal image (USAGE {ref:image, container})
+ * built from exactly one Dockerfile (Image -DEPENDS_ON {via:build}->); a
+ * compose service's own build edge, else its image's; a Dockerfile's own ENV.
+ * Its files: the Dockerfile Module's image_files (pass_docker.c) less the
+ * image_ignore patterns, never a test file. Measured on 17 corpora: 80
+ * pairs, 80 correct; a join through the global name was in the program for
+ * 23 % of the pairs.
+ *
+ * Every run recomputes every such edge. An incremental repair holds only the
+ * changed files' nodes and edges: the unchanged files' inputs come from the
+ * store (cbm_envjoin_base_t, read before the K8s pass), and the patch replaces
+ * the stored edges wholesale.
+ */
+#include "foundation/constants.h"
+#include "foundation/hash_table.h"
+#include "foundation/log.h"
+#include "foundation/mem_core.h"
+#include "foundation/str_util.h"
+#include "graph_buffer/graph_buffer.h"
+#include "pipeline/pipeline.h"
+#include "pipeline/pipeline_internal.h"
+#include "yyjson/yyjson.h"
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+/* ── Docker's path patterns ─────────────────────────────────────────── */
+
+/* `[...]` at p against c; *end past the class. A malformed class matches
+ * nothing. */
+static bool ej_class(const char *p, char c, const char **end) {
+ const char *q = p + SKIP_ONE;
+ bool neg = *q == '^' || *q == '!';
+ q += neg;
+ bool hit = false;
+ for (bool first = true; *q && (first || *q != ']'); first = false) {
+ char lo = *q == '\\' && q[SKIP_ONE] ? *++q : *q;
+ q++;
+ char hi = lo;
+ if (q[0] == '-' && q[SKIP_ONE] && q[SKIP_ONE] != ']') {
+ q++;
+ hi = *q == '\\' && q[SKIP_ONE] ? *++q : *q;
+ q++;
+ }
+ hit = hit || (c >= lo && c <= hi);
+ }
+ if (*q != ']') {
+ *end = NULL;
+ return false;
+ }
+ *end = q + SKIP_ONE;
+ return hit != neg;
+}
+
+/* Docker's pattern syntax over a whole repository path: `**` any run (`**`
+ * followed by `/`: whole segments only), `*` a run inside one segment, `?`
+ * one character of a segment, `[...]` a class, `\` an escape. */
+static bool ej_match(const char *p, const char *s) {
+ while (*p) {
+ if (p[0] == '*' && p[SKIP_ONE] == '*') {
+ p += PAIR_LEN;
+ bool segs = *p == '/';
+ p += segs;
+ for (const char *t = s;; t++) {
+ if ((!segs || t == s || t[-SKIP_ONE] == '/') && ej_match(p, t)) {
+ return true;
+ }
+ if (!*t) {
+ return false;
+ }
+ }
+ }
+ if (*p == '*') {
+ p++;
+ for (const char *t = s;; t++) {
+ if (ej_match(p, t)) {
+ return true;
+ }
+ if (!*t || *t == '/') {
+ return false;
+ }
+ }
+ }
+ if (!*s || (*s == '/' && (*p == '?' || *p == '['))) {
+ return false;
+ }
+ if (*p == '[') {
+ const char *end = NULL;
+ if (!ej_class(p, *s, &end) || !end) {
+ return false;
+ }
+ p = end;
+ s++;
+ continue;
+ }
+ if (*p == '\\' && p[SKIP_ONE]) {
+ p++;
+ }
+ if (*p != '?' && *p != *s) {
+ return false;
+ }
+ p++;
+ s++;
+ }
+ return *s == '\0';
+}
+
+/* Pattern `p` matches path `f` or a directory above it (a matched directory
+ * holds all of its files). */
+static bool ej_match_up(const char *p, const char *f) {
+ if (ej_match(p, f)) {
+ return true;
+ }
+ char buf[CBM_SZ_4K];
+ for (const char *s = strchr(f, '/'); s; s = strchr(s + SKIP_ONE, '/')) {
+ size_t n = (size_t)(s - f);
+ if (n >= sizeof(buf)) {
+ return false;
+ }
+ memcpy(buf, f, n);
+ buf[n] = '\0';
+ if (ej_match(p, buf)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+/* ── The image's files ──────────────────────────────────────────────── */
+
+typedef struct {
+ const char *dockerfile; /* the Dockerfile's path */
+ const char **files; /* repository paths / patterns the image copies */
+ int nfiles;
+ const char **ignore; /* ignore patterns, "!" = an exception */
+ int nignore;
+} ej_set_t;
+
+static const char **ej_strings(CBMArena *a, yyjson_val *arr, int *n) {
+ *n = 0;
+ size_t cnt = yyjson_is_arr(arr) ? yyjson_arr_size(arr) : 0;
+ const char **out = cnt ? cbm_arena_alloc(a, cnt * sizeof(*out)) : NULL;
+ yyjson_arr_iter it;
+ yyjson_val *v;
+ for (yyjson_arr_iter_init(arr, &it); out && (v = yyjson_arr_iter_next(&it));) {
+ const char *s = yyjson_get_str(v);
+ if (s) {
+ out[(*n)++] = cbm_arena_strdup(a, s);
+ }
+ }
+ return out;
+}
+
+/* A Dockerfile Module's image set, parsed from its properties; NULL: none. */
+static ej_set_t *ej_set_parse(CBMArena *a, const char *path, const char *props) {
+ if (!props || !strstr(props, "\"image_files\"")) {
+ return NULL;
+ }
+ yyjson_doc *doc = yyjson_read(props, strlen(props), 0);
+ yyjson_val *root = doc ? yyjson_doc_get_root(doc) : NULL;
+ ej_set_t *s = root ? cbm_arena_alloc(a, sizeof(*s)) : NULL;
+ if (s) {
+ *s = (ej_set_t){.dockerfile = cbm_arena_strdup(a, path ? path : "")};
+ s->files = ej_strings(a, yyjson_obj_get(root, "image_files"), &s->nfiles);
+ s->ignore = ej_strings(a, yyjson_obj_get(root, "image_ignore"), &s->nignore);
+ }
+ if (doc) {
+ yyjson_doc_free(doc);
+ }
+ return s && s->nfiles > 0 ? s : NULL;
+}
+
+static bool ej_in_image(const ej_set_t *s, const char *f) {
+ bool in = false;
+ for (int i = 0; i < s->nfiles && !in; i++) {
+ const char *e = s->files[i];
+ if (strpbrk(e, "*?[")) {
+ in = ej_match_up(e, f);
+ } else {
+ size_t el = strlen(e);
+ in = el == 0 || strcmp(f, e) == 0 || (strncmp(f, e, el) == 0 && f[el] == '/');
+ }
+ }
+ bool ignored = false;
+ for (int i = 0; in && i < s->nignore; i++) {
+ bool neg = s->ignore[i][0] == '!';
+ if (ej_match_up(s->ignore[i] + neg, f)) {
+ ignored = !neg;
+ }
+ }
+ return in && !ignored;
+}
+
+bool cbm_envjoin_props_hold(const char *props, const char *path) {
+ CBMArena a;
+ cbm_arena_init_lazy(&a, CBM_SZ_1K);
+ ej_set_t *s = ej_set_parse(&a, "", props);
+ bool hit = false;
+ if (s) {
+ ej_set_t files = *s;
+ files.nignore = 0;
+ hit = ej_in_image(&files, path);
+ }
+ cbm_arena_destroy(&a);
+ return hit;
+}
+
+/* ── The join ───────────────────────────────────────────────────────── */
+
+typedef struct {
+ int64_t node;
+ const char *file;
+} ej_reader_t;
+
+typedef struct {
+ int64_t image;
+ const char *container; /* NULL: any (compose) */
+} ej_img_t;
+
+typedef struct {
+ cbm_pipeline_ctx_t *ctx;
+ CBMArena a;
+ CBMHashTable *sets; /* Dockerfile Module id -> ej_set_t */
+ CBMHashTable *image_df; /* Image id -> Dockerfile Module id, "" when not one */
+ CBMHashTable *owner_df; /* compose service id -> its own build's Dockerfile id */
+ CBMHashTable *readers; /* key -> ej_reader_t[] (count in nreaders) */
+ CBMHashTable *nreaders; /* key -> (intptr_t) count */
+ CBMHashTable *imgs; /* owner id -> ej_img_t[] (count in nimgs) */
+ CBMHashTable *nimgs; /* owner id -> (intptr_t) count */
+ CBMHashTable *done; /* "setter:reader" */
+ int edges;
+} ej_t;
+
+/* Appends `item` to the array table[key] (its count in counts[key]). */
+static void ej_push(CBMArena *a, CBMHashTable *table, CBMHashTable *counts, const char *key,
+ const void *item, size_t size) {
+ intptr_t n = (intptr_t)cbm_ht_get(counts, key);
+ const char *old = cbm_ht_get(table, key);
+ char *v = cbm_arena_alloc(a, (size_t)(n + SKIP_ONE) * size);
+ const char *k = cbm_arena_strdup(a, key);
+ if (!v || !k) {
+ return;
+ }
+ if (n) {
+ memcpy(v, old, (size_t)n * size);
+ }
+ memcpy(v + (size_t)n * size, item, size);
+ cbm_ht_set(table, k, v);
+ cbm_ht_set(counts, k, (void *)(n + SKIP_ONE));
+}
+
+static const char *ej_id(ej_t *j, int64_t id) {
+ return cbm_arena_sprintf(&j->a, "%lld", (long long)id);
+}
+
+static void ej_add_set(ej_t *j, int64_t node, const char *path, const char *props) {
+ const char *k = ej_id(j, node);
+ if (!k || cbm_ht_get(j->sets, k)) {
+ return;
+ }
+ ej_set_t *s = ej_set_parse(&j->a, path, props);
+ if (s) {
+ cbm_ht_set(j->sets, k, s);
+ }
+}
+
+static void ej_add_img(ej_t *j, int64_t owner, int64_t image, const char *props) {
+ const char *c = props ? strstr(props, "\"container\":\"") : NULL;
+ ej_img_t im = {image, NULL};
+ if (c) {
+ c += strlen("\"container\":\"");
+ const char *e = strchr(c, '"');
+ im.container = e ? cbm_arena_strndup(&j->a, c, (size_t)(e - c)) : NULL;
+ }
+ const char *k = ej_id(j, owner);
+ if (k) {
+ ej_push(&j->a, j->imgs, j->nimgs, k, &im, sizeof(im));
+ }
+}
+
+static void ej_add_reader(ej_t *j, const char *key, int64_t node, const char *file) {
+ if (!key || !file || !file[0] || cbm_is_test_path(file)) {
+ return;
+ }
+ ej_reader_t r = {node, cbm_arena_strdup(&j->a, file)};
+ if (r.file) {
+ ej_push(&j->a, j->readers, j->nreaders, key, &r, sizeof(r));
+ }
+}
+
+/* Build edges: an Image's Dockerfile (when exactly one), a compose service's
+ * own build. */
+static void ej_index_builds(ej_t *j) {
+ const cbm_gbuf_edge_t **es = NULL;
+ int n = 0;
+ if (cbm_gbuf_find_edges_by_type(j->ctx->gbuf, "DEPENDS_ON", &es, &n) != 0) {
+ return;
+ }
+ for (int i = 0; i < n; i++) {
+ const char *p = es[i]->properties_json;
+ if (!p || !strstr(p, "\"via\":\"build\"")) {
+ continue;
+ }
+ const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(j->ctx->gbuf, es[i]->source_id);
+ if (!src || !src->label) {
+ continue;
+ }
+ CBMHashTable *t = strcmp(src->label, "Image") == 0 ? j->image_df
+ : strcmp(src->label, "Resource") == 0 ? j->owner_df
+ : NULL;
+ const char *k = t ? ej_id(j, src->id) : NULL;
+ const char *df = k ? ej_id(j, es[i]->target_id) : NULL;
+ if (!df) {
+ continue;
+ }
+ const char *have = cbm_ht_get(t, k);
+ cbm_ht_set(t, k, (void *)(have && strcmp(have, df) != 0 ? "" : df));
+ }
+}
+
+static void ej_index_gbuf(ej_t *j) {
+ const cbm_gbuf_node_t **mods = NULL;
+ int nm = 0;
+ if (cbm_gbuf_find_by_label(j->ctx->gbuf, "Module", &mods, &nm) == 0) {
+ for (int i = 0; i < nm; i++) {
+ ej_add_set(j, mods[i]->id, mods[i]->file_path, mods[i]->properties_json);
+ }
+ }
+ const cbm_gbuf_edge_t **es = NULL;
+ int n = 0;
+ if (cbm_gbuf_find_edges_by_type(j->ctx->gbuf, "USAGE", &es, &n) == 0) {
+ for (int i = 0; i < n; i++) {
+ const char *p = es[i]->properties_json;
+ if (p && strstr(p, "\"ref\":\"image\"")) {
+ ej_add_img(j, es[i]->source_id, es[i]->target_id, p);
+ }
+ }
+ }
+ if (cbm_gbuf_find_edges_by_type(j->ctx->gbuf, "CONFIGURES", &es, &n) == 0) {
+ for (int i = 0; i < n; i++) {
+ const char *p = es[i]->properties_json;
+ if (!p || !strstr(p, "\"strategy\":\"env_access\"")) {
+ continue;
+ }
+ const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(j->ctx->gbuf, es[i]->source_id);
+ const cbm_gbuf_node_t *env = cbm_gbuf_find_by_id(j->ctx->gbuf, es[i]->target_id);
+ if (src && env) {
+ ej_add_reader(j, env->name, src->id, src->file_path);
+ }
+ }
+ }
+ ej_index_builds(j);
+}
+
+static void ej_index_base(ej_t *j, const cbm_envjoin_base_t *b) {
+ for (int i = 0; b && i < b->set_count; i++) {
+ ej_add_set(j, b->sets[i].node, b->sets[i].path, b->sets[i].props);
+ }
+ for (int i = 0; b && i < b->img_count; i++) {
+ ej_add_img(j, b->imgs[i].owner, b->imgs[i].image, b->imgs[i].props);
+ }
+ for (int i = 0; b && i < b->reader_count; i++) {
+ ej_add_reader(j, b->readers[i].key, b->readers[i].reader, b->readers[i].file);
+ }
+}
+
+static void ej_edge(ej_t *j, int64_t setter, const ej_reader_t *r, const ej_set_t *s) {
+ const char *k = cbm_arena_sprintf(&j->a, "%lld:%lld", (long long)setter, (long long)r->node);
+ if (!k || cbm_ht_get(j->done, k) || setter == r->node) {
+ return;
+ }
+ cbm_ht_set(j->done, k, (void *)k);
+ char esc[CBM_SZ_4K];
+ cbm_json_escape(esc, (int)sizeof(esc), s->dockerfile);
+ char props[CBM_SZ_4K + CBM_SZ_64];
+ snprintf(props, sizeof(props), "{\"strategy\":\"env_read\",\"dockerfile\":\"%s\"}", esc);
+ if (cbm_gbuf_insert_edge(j->ctx->gbuf, setter, r->node, "CONFIGURES", props) > 0) {
+ j->edges++;
+ }
+}
+
+/* Join one setter with the readers of its key in Dockerfile `df`'s image. */
+static void ej_join_df(ej_t *j, int64_t setter, const char *key, const char *df) {
+ const ej_set_t *s = df && df[0] ? cbm_ht_get(j->sets, df) : NULL;
+ const ej_reader_t *rs = s ? cbm_ht_get(j->readers, key) : NULL;
+ intptr_t n = rs ? (intptr_t)cbm_ht_get(j->nreaders, key) : 0;
+ for (intptr_t i = 0; i < n; i++) {
+ if (ej_in_image(s, rs[i].file)) {
+ ej_edge(j, setter, &rs[i], s);
+ }
+ }
+}
+
+/* A scoped EnvVar: its owner, container and key from the QN. */
+static void ej_join_setter(ej_t *j, const cbm_gbuf_node_t *ev) {
+ const char *mark = strstr(ev->qualified_name, "#env/");
+ const char *slash = mark ? strrchr(mark, '/') : NULL;
+ if (!mark || !slash || slash < mark + strlen("#env/")) {
+ return;
+ }
+ const char *owner_qn =
+ cbm_arena_strndup(&j->a, ev->qualified_name, (size_t)(mark - ev->qualified_name));
+ const char *cstart = mark + strlen("#env/");
+ const char *container = cbm_arena_strndup(&j->a, cstart, (size_t)(slash - cstart));
+ const char *key = slash + SKIP_ONE;
+ const cbm_gbuf_node_t *owner = owner_qn ? cbm_gbuf_find_by_qn(j->ctx->gbuf, owner_qn) : NULL;
+ if (!owner || !container || !key[0]) {
+ return;
+ }
+ const char *oid = ej_id(j, owner->id);
+ if (oid && cbm_ht_get(j->sets, oid)) { /* a Dockerfile's own ENV */
+ ej_join_df(j, ev->id, key, oid);
+ return;
+ }
+ const char *own = oid ? cbm_ht_get(j->owner_df, oid) : NULL;
+ if (own) { /* a compose service's own build */
+ ej_join_df(j, ev->id, key, own);
+ return;
+ }
+ const ej_img_t *ims = oid ? cbm_ht_get(j->imgs, oid) : NULL;
+ intptr_t ni = ims ? (intptr_t)cbm_ht_get(j->nimgs, oid) : 0;
+ for (intptr_t i = 0; i < ni; i++) {
+ if (ims[i].container && strcmp(ims[i].container, container) != 0) {
+ continue;
+ }
+ const char *iid = ej_id(j, ims[i].image);
+ ej_join_df(j, ev->id, key, iid ? cbm_ht_get(j->image_df, iid) : NULL);
+ }
+}
+
+int cbm_pipeline_env_join(cbm_pipeline_ctx_t *ctx) {
+ ej_t j = {.ctx = ctx};
+ cbm_arena_init_lazy(&j.a, CBM_SZ_4K);
+ j.sets = cbm_ht_create(0);
+ j.image_df = cbm_ht_create(0);
+ j.owner_df = cbm_ht_create(0);
+ j.readers = cbm_ht_create(0);
+ j.nreaders = cbm_ht_create(0);
+ j.imgs = cbm_ht_create(0);
+ j.nimgs = cbm_ht_create(0);
+ j.done = cbm_ht_create(0);
+ const cbm_gbuf_node_t **evs = NULL;
+ int n = 0;
+ if (j.sets && j.image_df && j.owner_df && j.readers && j.nreaders && j.imgs && j.nimgs &&
+ j.done) {
+ ej_index_gbuf(&j);
+ ej_index_base(&j, ctx->envjoin_base);
+ if (cbm_ht_count(j.sets) > 0 &&
+ cbm_gbuf_find_by_label(ctx->gbuf, "EnvVar", &evs, &n) == 0) {
+ for (int i = 0; i < n; i++) {
+ if (evs[i]->qualified_name && strstr(evs[i]->qualified_name, "#env/")) {
+ ej_join_setter(&j, evs[i]);
+ }
+ }
+ }
+ }
+ CBMHashTable *tables[] = {j.sets, j.image_df, j.owner_df, j.readers,
+ j.nreaders, j.imgs, j.nimgs, j.done};
+ for (size_t i = 0; i < sizeof(tables) / sizeof(tables[0]); i++) {
+ if (tables[i]) {
+ cbm_ht_free(tables[i]);
+ }
+ }
+ cbm_arena_destroy(&j.a);
+ if (j.edges > 0) {
+ char buf[CBM_SZ_32];
+ snprintf(buf, sizeof(buf), "%d", j.edges);
+ cbm_log_info("pass.envjoin.edges", "edges", buf);
+ }
+ return j.edges;
+}
diff --git a/src/pipeline/pass_k8s.c b/src/pipeline/pass_k8s.c
index 3dfa35770..a3113a37b 100644
--- a/src/pipeline/pass_k8s.c
+++ b/src/pipeline/pass_k8s.c
@@ -1161,6 +1161,13 @@ typedef struct {
int line;
} k8s_env_t;
+/* A container image an object runs, and the container running it. */
+typedef struct {
+ int64_t node;
+ char *image;
+ char *container; /* NULL: unnamed */
+} k8s_img_t;
+
typedef struct {
k8s_ref_t *refs;
int ref_n;
@@ -1177,7 +1184,7 @@ typedef struct {
k8s_hr_t *srcs; /* an OCIRepository (node) and the URL it fetches (chart) */
int src_n;
int src_cap;
- k8s_hr_t *imgs; /* an object (node) and a container image it runs (chart) */
+ k8s_img_t *imgs;
int img_n;
int img_cap;
k8s_env_t *envs;
@@ -1208,7 +1215,8 @@ static void k8s_facts_free(k8s_facts_t *f) {
cbm_free(CBM_MEM_CLASS_OTHER, f->srcs[i].chart);
}
for (int i = 0; i < f->img_n; i++) {
- cbm_free(CBM_MEM_CLASS_OTHER, f->imgs[i].chart);
+ cbm_free(CBM_MEM_CLASS_OTHER, f->imgs[i].image);
+ cbm_free(CBM_MEM_CLASS_OTHER, f->imgs[i].container);
}
for (int i = 0; i < f->env_n; i++) {
cbm_free(CBM_MEM_CLASS_OTHER, f->envs[i].key);
@@ -1297,8 +1305,9 @@ static void k8s_facts_keep(k8s_facts_t *f, const CBMFileResult *res, const int64
}
} else if (x->fact == CBM_K8S_FACT_IMAGE) {
if (x->name && k8s_grow((void **)&f->imgs, &f->img_cap, f->img_n, sizeof(*f->imgs))) {
- f->imgs[f->img_n++] =
- (k8s_hr_t){node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name), NULL};
+ f->imgs[f->img_n++] = (k8s_img_t){
+ node, cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->name),
+ x->container ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, x->container) : NULL};
}
} else if (x->fact == CBM_K8S_FACT_ENV) {
if (x->name && k8s_grow((void **)&f->envs, &f->env_cap, f->env_n, sizeof(*f->envs))) {
@@ -3317,19 +3326,24 @@ static void k8s_link_chart_refs(k8s_dep_link_t *l, const k8s_hr_t *oci) {
}
}
-/* A container image an object runs: USAGE {ref:image} to its Image node --
- * the node a Dockerfile building or building on that image joins. */
+/* A container image an object runs: USAGE {ref:image, container} to its
+ * Image node -- the node a Dockerfile building or building on that image
+ * joins; the container names whose environment runs it. */
static void k8s_link_images(cbm_pipeline_ctx_t *ctx, const k8s_facts_t *f) {
for (int i = 0; i < f->img_n; i++) {
- int64_t img = cbm_pipeline_image_node(ctx, f->imgs[i].chart);
+ const k8s_img_t *im = &f->imgs[i];
+ int64_t img = cbm_pipeline_image_node(ctx, im->image);
if (img <= 0) {
continue;
}
char esc[CBM_SZ_1K];
- cbm_json_escape(esc, (int)sizeof(esc), f->imgs[i].chart);
+ cbm_json_escape(esc, (int)sizeof(esc), im->image);
+ char cesc[CBM_SZ_512];
+ cbm_json_escape(cesc, (int)sizeof(cesc), im->container ? im->container : "-");
char props[CBM_SZ_2K];
- snprintf(props, sizeof(props), "{\"ref\":\"image\",\"image\":\"%s\"}", esc);
- cbm_gbuf_insert_edge(ctx->gbuf, f->imgs[i].node, img, "USAGE", props);
+ snprintf(props, sizeof(props), "{\"ref\":\"image\",\"image\":\"%s\",\"container\":\"%s\"}",
+ esc, cesc);
+ cbm_gbuf_insert_edge(ctx->gbuf, im->node, img, "USAGE", props);
}
}
@@ -3548,6 +3562,8 @@ int cbm_pipeline_pass_k8s(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
cbm_pipeline_pass_shell(ctx, files, file_count);
cbm_pipeline_pass_terraform(ctx, files, file_count);
cbm_pipeline_pass_docker(ctx, files, file_count);
+ /* The variables a container sets, to the readers in its own image. */
+ (void)cbm_pipeline_env_join(ctx);
cbm_log_info("pass.done", "pass", "k8s", "manifests", itoa_k8s(manifest_count));
(void)helm_count;
diff --git a/src/pipeline/pipeline_delta.c b/src/pipeline/pipeline_delta.c
index 0ccbbce48..365ae9430 100644
--- a/src/pipeline/pipeline_delta.c
+++ b/src/pipeline/pipeline_delta.c
@@ -151,7 +151,12 @@ int cbm_delta_snapshot_inbound(cbm_store_t *store, const char *project, const ch
int step_rc;
while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) {
const char *type = (const char *)sqlite3_column_text(stmt, 2);
- if (delta_edge_type_is_recomputed(type)) {
+ const char *eprops = (const char *)sqlite3_column_text(stmt, 3);
+ /* the env join's edges come from file-owned EnvVars, but every
+ * run recomputes them whole (pass_envjoin.c) */
+ bool joined = type && strcmp(type, CBM_ENVJOIN_EDGE_TYPE) == 0 && eprops &&
+ strstr(eprops, "\"strategy\":\"env_read\"");
+ if (delta_edge_type_is_recomputed(type) || joined) {
continue;
}
if (count >= cap) {
@@ -340,6 +345,157 @@ int cbm_delta_image_components(cbm_store_t *store, const char *project, CBMArena
a, out);
}
+/* Calls fn for each row of `sql` (?1 = project); the row count, or -1. */
+static int delta_each_row(cbm_store_t *store, const char *project, const char *sql,
+ void (*fn)(sqlite3_stmt *, void *), void *ud) {
+ sqlite3 *db = cbm_store_get_db(store);
+ sqlite3_stmt *stmt = NULL;
+ if (!db || sqlite3_prepare_v2(db, sql, CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT);
+ int n = 0;
+ int rc;
+ while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
+ fn(stmt, ud);
+ n++;
+ }
+ sqlite3_finalize(stmt);
+ return rc == SQLITE_DONE ? n : CBM_NOT_FOUND;
+}
+
+/* Room for one more of `size` bytes in arena array *items (count n). */
+static bool delta_room(CBMArena *a, void **items, int n, int *cap, size_t size) {
+ if (n < *cap) {
+ return true;
+ }
+ int ncap = *cap ? *cap * PAIR_LEN : CBM_SZ_64;
+ void *grown = cbm_arena_alloc(a, (size_t)ncap * size);
+ if (!grown) {
+ return false;
+ }
+ if (n) {
+ memcpy(grown, *items, (size_t)n * size);
+ }
+ *items = grown;
+ *cap = ncap;
+ return true;
+}
+
+typedef struct {
+ cbm_envjoin_base_t *b;
+ int cap;
+ bool failed;
+} delta_ej_t;
+
+static const char *delta_col(CBMArena *a, sqlite3_stmt *st, int i) {
+ const char *s = (const char *)sqlite3_column_text(st, i);
+ return cbm_arena_strdup(a, s ? s : "");
+}
+
+static void delta_ej_set(sqlite3_stmt *st, void *ud) {
+ delta_ej_t *e = ud;
+ cbm_envjoin_base_t *b = e->b;
+ if (!delta_room(&b->arena, (void **)&b->sets, b->set_count, &e->cap, sizeof(*b->sets))) {
+ e->failed = true;
+ return;
+ }
+ b->sets[b->set_count++] =
+ (cbm_envjoin_set_row_t){sqlite3_column_int64(st, 0), delta_col(&b->arena, st, SKIP_ONE),
+ delta_col(&b->arena, st, PAIR_LEN)};
+}
+
+static void delta_ej_img(sqlite3_stmt *st, void *ud) {
+ delta_ej_t *e = ud;
+ cbm_envjoin_base_t *b = e->b;
+ if (!delta_room(&b->arena, (void **)&b->imgs, b->img_count, &e->cap, sizeof(*b->imgs))) {
+ e->failed = true;
+ return;
+ }
+ b->imgs[b->img_count++] =
+ (cbm_envjoin_img_row_t){sqlite3_column_int64(st, 0), sqlite3_column_int64(st, SKIP_ONE),
+ delta_col(&b->arena, st, PAIR_LEN)};
+}
+
+static void delta_ej_reader(sqlite3_stmt *st, void *ud) {
+ delta_ej_t *e = ud;
+ cbm_envjoin_base_t *b = e->b;
+ if (!delta_room(&b->arena, (void **)&b->readers, b->reader_count, &e->cap,
+ sizeof(*b->readers))) {
+ e->failed = true;
+ return;
+ }
+ b->readers[b->reader_count++] =
+ (cbm_envjoin_reader_row_t){sqlite3_column_int64(st, 0), delta_col(&b->arena, st, SKIP_ONE),
+ delta_col(&b->arena, st, PAIR_LEN)};
+}
+
+int cbm_delta_envjoin_base(cbm_store_t *store, const char *project, cbm_envjoin_base_t *out) {
+ *out = (cbm_envjoin_base_t){0};
+ cbm_arena_init_lazy(&out->arena, CBM_SZ_4K);
+ delta_ej_t sets = {.b = out};
+ delta_ej_t imgs = {.b = out};
+ delta_ej_t readers = {.b = out};
+ if (delta_each_row(store, project,
+ "SELECT id, file_path, properties FROM nodes WHERE project = ?1"
+ " AND label = 'Module' AND instr(properties, '\"image_files\"') > 0"
+ " ORDER BY id",
+ delta_ej_set, &sets) < 0 ||
+ delta_each_row(store, project,
+ "SELECT source_id, target_id, properties FROM edges WHERE project = ?1"
+ " AND type = 'USAGE' AND instr(properties, '\"ref\":\"image\"') > 0"
+ " ORDER BY id",
+ delta_ej_img, &imgs) < 0 ||
+ delta_each_row(store, project,
+ "SELECT e.source_id, s.file_path, t.name FROM edges e"
+ " JOIN nodes s ON s.id = e.source_id JOIN nodes t ON t.id = e.target_id"
+ " WHERE e.project = ?1 AND e.type = 'CONFIGURES'"
+ " AND instr(e.properties, '\"strategy\":\"env_access\"') > 0"
+ " ORDER BY e.id",
+ delta_ej_reader, &readers) < 0 ||
+ sets.failed || imgs.failed || readers.failed) {
+ cbm_envjoin_base_free(out);
+ return CBM_NOT_FOUND;
+ }
+ return 0;
+}
+
+typedef struct {
+ const char *const *paths;
+ int path_count;
+ bool hit;
+} delta_holds_t;
+
+static void delta_holds_row(sqlite3_stmt *st, void *ud) {
+ delta_holds_t *h = ud;
+ const char *props = (const char *)sqlite3_column_text(st, 0);
+ for (int i = 0; props && !h->hit && i < h->path_count; i++) {
+ h->hit = cbm_envjoin_props_hold(props, h->paths[i]);
+ }
+}
+
+int cbm_delta_image_holds_any(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count) {
+ delta_holds_t h = {paths, path_count, false};
+ if (path_count <= 0) {
+ return 0;
+ }
+ if (delta_each_row(store, project,
+ "SELECT properties FROM nodes WHERE project = ?1 AND label = 'Module'"
+ " AND instr(properties, '\"image_files\"') > 0",
+ delta_holds_row, &h) < 0) {
+ return CBM_NOT_FOUND;
+ }
+ return h.hit ? SKIP_ONE : 0;
+}
+
+void cbm_envjoin_base_free(cbm_envjoin_base_t *b) {
+ if (b) {
+ cbm_arena_destroy(&b->arena);
+ *b = (cbm_envjoin_base_t){0};
+ }
+}
+
int cbm_delta_drop_edges_from_label(cbm_store_t *store, const char *project, const char *type,
const char *label) {
sqlite3 *db = cbm_store_get_db(store);
@@ -640,15 +796,18 @@ static void delta_patch_node(const cbm_gbuf_node_t *node, void *userdata) {
ctx->nodes++;
}
-/* An edge of the class recomputed wholesale on every run (an Image's build
- * edges): the staging graph dropped its old set, so it is written even
- * between pre-existing nodes. */
+/* An edge of a class recomputed wholesale on every run (an Image's build
+ * edges, the env join's edges): the staging graph dropped its old set, so it
+ * is written even between pre-existing nodes. */
static bool delta_edge_recomputed(const delta_patch_ctx_t *ctx, const cbm_gbuf_edge_t *edge) {
- if (!ctx->gbuf || strcmp(edge->type, CBM_BUILD_EDGE_TYPE) != 0) {
+ const char *want = strcmp(edge->type, CBM_BUILD_EDGE_TYPE) == 0 ? CBM_BUILD_EDGE_SOURCE
+ : strcmp(edge->type, CBM_ENVJOIN_EDGE_TYPE) == 0 ? CBM_ENVJOIN_EDGE_SOURCE
+ : NULL;
+ if (!ctx->gbuf || !want) {
return false;
}
const cbm_gbuf_node_t *src = cbm_gbuf_find_by_id(ctx->gbuf, edge->source_id);
- return src && src->label && strcmp(src->label, CBM_BUILD_EDGE_SOURCE) == 0;
+ return src && src->label && strcmp(src->label, want) == 0;
}
static void delta_patch_edge(const cbm_gbuf_edge_t *edge, void *userdata) {
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index c5f4ffafb..33e2ce6f7 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -404,8 +404,15 @@ static int semantic_manifest_control_entry(semantic_manifest_builder_t *builder,
}
bool root_control = (!rel_dir || !rel_dir[0]) && (strcmp(name, ".cbmignore") == 0 ||
strcmp(name, ".codebase-memory.json") == 0);
- bool control =
- strcmp(name, ".gitignore") == 0 || root_control || semantic_manifest_package_control(name);
+ /* A build context's ignore file (or BuildKit's <Dockerfile>.dockerignore)
+ * shapes which files an image holds (pass_docker.c image_ignore), and
+ * discovery indexes neither. */
+ static const char DOCKERIGNORE[] = ".dockerignore";
+ size_t nl = strlen(name);
+ bool dockerignore = nl >= sizeof(DOCKERIGNORE) - SKIP_ONE &&
+ strcmp(name + nl - (sizeof(DOCKERIGNORE) - SKIP_ONE), DOCKERIGNORE) == 0;
+ bool control = strcmp(name, ".gitignore") == 0 || root_control ||
+ semantic_manifest_package_control(name) || dockerignore;
return info.is_regular && control ? semantic_manifest_add(builder, project, rel_path, abs_path)
: 0;
}
@@ -2509,6 +2516,27 @@ static const char *closure_k8s_decline(cbm_store_t *store, const char *project,
return NULL;
}
+/* The env join (pass_envjoin.c) reads each image's files from its
+ * Dockerfile's Module, stored with the ignore patterns of its context: an
+ * ignore file changing, or a deletion of a file an image copies (which
+ * context a Dockerfile's sources resolve in can follow it), declines to the
+ * full rebuild. Adding a file declines already. Returns the reason, or NULL. */
+static const char *closure_envjoin_decline(cbm_store_t *store, const char *project, char **paths,
+ int n_changed, int n_deleted) {
+ for (int i = 0; i < n_changed + n_deleted; i++) {
+ const char *slash = strrchr(paths[i], '/');
+ const char *base = slash ? slash + SKIP_ONE : paths[i];
+ size_t bl = strlen(base);
+ size_t sl = strlen(".dockerignore");
+ if (bl >= sl && strcmp(base + bl - sl, ".dockerignore") == 0) {
+ return "dockerignore_changed";
+ }
+ }
+ int held = cbm_delta_image_holds_any(store, project, (const char *const *)paths + n_changed,
+ n_deleted);
+ return held > 0 ? "image_file_deleted" : held < 0 ? "image_lookup_failed" : NULL;
+}
+
/* rel's directory prefix of len bytes as the plain Folder QN, in the arena. */
static char *closure_prefix_folder_qn(CBMArena *a, const char *project, const char *rel,
size_t len) {
@@ -2714,6 +2742,10 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p
if (decline) {
goto done;
}
+ decline = closure_envjoin_decline(store, project, changed_paths, n_changed, n_deleted);
+ if (decline) {
+ goto done;
+ }
/* Load the previous generation's surfaces and probe the changed files'
* fresh ones. Missing rows fail closed. */
@@ -3014,6 +3046,9 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
* collisions of the whole discovered set, as in a full run. */
cbm_stem_marks_t stem_marks;
cbm_stem_marks_init(&stem_marks, project, files, file_count);
+ /* The env join's inputs in the files the repair leaves alone. */
+ cbm_envjoin_base_t envjoin_base = {0};
+ cbm_arena_init_lazy(&envjoin_base.arena, CBM_SZ_4K);
cbm_doclink_scope_t *doc_base = NULL;
int doc_base_count = 0;
cbm_doc_link_row_t *doc_fresh = NULL;
@@ -3076,7 +3111,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
goto out;
}
int64_t max_db_id = cbm_delta_preseed(staging, project, gbuf);
- if (max_db_id < 0) {
+ if (max_db_id < 0 || cbm_delta_envjoin_base(staging, project, &envjoin_base) != 0) {
goto out;
}
registry = cbm_registry_new();
@@ -3231,6 +3266,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
.build_decl_files = decl_files,
.build_decl_count = decl_count,
.stem_marks = &stem_marks,
+ .envjoin_base = &envjoin_base,
};
for (int i = 0; i < ci; i++) {
char *file_qn = cbm_pipeline_fqn_compute(project, changed_files[i].rel_path, "__file__");
@@ -3300,9 +3336,12 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
/* An Image's build edges (Image -> Dockerfile, pass_docker.c) belong to
* the skaffold / compose file declaring them, not to either end: the K8s
* pass just recomputed them from every build file, so they replace the
- * staging set wholesale. */
+ * staging set wholesale. So do the env join's edges (pass_envjoin.c),
+ * recomputed from this repair's files and the stored rest. */
if (cbm_delta_drop_edges_from_label(staging, project, CBM_BUILD_EDGE_TYPE,
- CBM_BUILD_EDGE_SOURCE) < 0) {
+ CBM_BUILD_EDGE_SOURCE) < 0 ||
+ cbm_delta_drop_edges_from_label(staging, project, CBM_ENVJOIN_EDGE_TYPE,
+ CBM_ENVJOIN_EDGE_SOURCE) < 0) {
goto out;
}
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
@@ -3497,6 +3536,7 @@ out:
cbm_delta_free_snapshot(snapshot, snapshot_count);
cbm_arena_destroy(&decl_arena);
cbm_stem_marks_free(&stem_marks);
+ cbm_envjoin_base_free(&envjoin_base);
surface_name_set_free(stale_surface_set);
cbm_doclinks_free_scopes(doc_base, doc_base_count);
cbm_doclinks_free_rows(doc_fresh, doc_fresh_count);
@@ -3831,12 +3871,15 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
/* An Image's build edges (Image -> Dockerfile, pass_docker.c) belong to
* the skaffold / compose file declaring them, not to either end: the K8s
* pass recomputes them all from every build file, so none is kept (nor
- * captured below). */
+ * captured below). Neither is an env join edge (pass_envjoin.c): the
+ * join recomputes them all over the whole loaded graph. */
CBMArena decl_arena; /* the files that declared image builds before */
cbm_arena_init_lazy(&decl_arena, CBM_SZ_4K);
const char **decl_files = NULL;
int decl_count = incr_build_decl_files(existing, &decl_arena, &decl_files);
(void)cbm_gbuf_delete_edges_from_label(existing, CBM_BUILD_EDGE_TYPE, CBM_BUILD_EDGE_SOURCE);
+ (void)cbm_gbuf_delete_edges_from_label(existing, CBM_ENVJOIN_EDGE_TYPE,
+ CBM_ENVJOIN_EDGE_SOURCE);
/* Snapshot inbound cross-file edges into changed files BEFORE purging, so
* the cascade delete doesn't permanently drop edges whose source lives in
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 5d8d9241c..8985e24e2 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -349,6 +349,37 @@ typedef struct {
void cbm_pkg_entries_init(cbm_pkg_entries_t *e);
void cbm_pkg_entries_free(cbm_pkg_entries_t *e);
+/* The env join's inputs held by the unchanged files of an incremental
+ * repair (pass_envjoin.c), read from the store before the K8s pass; ids are
+ * the stored rows, i.e. the repair buffer's proxies. */
+typedef struct {
+ int64_t node; /* a Dockerfile Module */
+ const char *path; /* its file */
+ const char *props; /* its properties (image_files, image_ignore) */
+} cbm_envjoin_set_row_t;
+
+typedef struct {
+ int64_t owner; /* USAGE {ref:image} source */
+ int64_t image; /* its Image */
+ const char *props;
+} cbm_envjoin_img_row_t;
+
+typedef struct {
+ int64_t reader; /* CONFIGURES {strategy:env_access} source */
+ const char *file;
+ const char *key; /* the global EnvVar's name */
+} cbm_envjoin_reader_row_t;
+
+typedef struct {
+ CBMArena arena; /* owns every string and array */
+ cbm_envjoin_set_row_t *sets;
+ int set_count;
+ cbm_envjoin_img_row_t *imgs;
+ int img_count;
+ cbm_envjoin_reader_row_t *readers;
+ int reader_count;
+} cbm_envjoin_base_t;
+
/* Shared context passed to each pass function.
* Derived from cbm_pipeline_t fields during run. */
typedef struct {
@@ -398,6 +429,10 @@ typedef struct {
* `qn#ext`. Borrowed for the extraction phase; NULL elsewhere. */
const cbm_stem_marks_t *stem_marks;
+ /* An incremental repair: the env join's inputs held by the unchanged
+ * files (pass_envjoin.c). Borrowed; NULL in a full run. */
+ const cbm_envjoin_base_t *envjoin_base;
+
/* Sequential cross-LSP registry arena. The lsp_cross pass builds its
* shared per-language registries here; resolved_calls entries may BORROW
* strings owned by these registries, and the later calls pass still
@@ -1289,6 +1324,16 @@ int64_t cbm_pipeline_env_setter(cbm_pipeline_ctx_t *ctx, const cbm_gbuf_node_t *
void cbm_pipeline_pass_docker(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count);
+/* The env join (pass_envjoin.c): a container's EnvVar CONFIGURES {strategy:
+ * env_read} each reader of its name inside the files the container's image
+ * is built from. Recomputes every such edge; after the Docker pass. Returns
+ * how many. */
+int cbm_pipeline_env_join(cbm_pipeline_ctx_t *ctx);
+
+/* Whether a Dockerfile Module's properties (image_files) copy `path` into
+ * the image, ignore patterns aside. */
+bool cbm_envjoin_props_hold(const char *props, const char *path);
+
/* Pre-dump pass: markdown → file REFERENCES_FILE linking. */
int cbm_pipeline_pass_doclinks(cbm_pipeline_ctx_t *ctx);
@@ -1439,8 +1484,15 @@ int cbm_pipeline_build_fresh_semantic_manifest(cbm_pipeline_t *p, const char *pr
* spelled `proj.pkg.foo/`, or, where the directory is a Java/Go package,
* the file's Module is `proj.pkg.foo#sh`. The Module the Folder used to
* swallow is a node again. An index built before holds neither, so it
+ * rebuilds once on upgrade.
+ * 9: a container's variable reaches its program: the EnvVar a container
+ * sets CONFIGURES {strategy:env_read} the readers of its name inside the
+ * files the container's image is built from (pass_envjoin.c); a K8s
+ * image reference says its container (USAGE {ref:image, container}) and
+ * a Dockerfile's Module its image's files (image_context, image_files,
+ * image_ignore). An index built before holds none of them, so it
* rebuilds once on upgrade. */
-enum { CBM_SEMANTIC_INDEX_VERSION = 8 };
+enum { CBM_SEMANTIC_INDEX_VERSION = 9 };
typedef struct {
cbm_gbuf_t *gbuf;
@@ -1543,6 +1595,22 @@ int cbm_delta_drop_unreferenced(cbm_store_t *store, const char *project, const c
* between pre-existing nodes. */
#define CBM_BUILD_EDGE_TYPE "DEPENDS_ON"
#define CBM_BUILD_EDGE_SOURCE "Image"
+/* The env join's edges (EnvVar -CONFIGURES {strategy:env_read}-> reader,
+ * pass_envjoin.c) likewise: no other producer draws CONFIGURES from an
+ * EnvVar, every run recomputes them whole. */
+#define CBM_ENVJOIN_EDGE_TYPE "CONFIGURES"
+#define CBM_ENVJOIN_EDGE_SOURCE "EnvVar"
+
+/* The env join's inputs the store holds (the unchanged files of a closure
+ * repair, read after the purge), into `out` (its arena owns them); 0, or -1
+ * on failure. cbm_envjoin_base_free releases it. */
+int cbm_delta_envjoin_base(cbm_store_t *store, const char *project, cbm_envjoin_base_t *out);
+void cbm_envjoin_base_free(cbm_envjoin_base_t *b);
+/* 1 when a stored image copies one of `paths` (a deletion then changes
+ * which context the Dockerfile's sources resolve in), 0 when none, -1 on
+ * failure. */
+int cbm_delta_image_holds_any(cbm_store_t *store, const char *project, const char *const *paths,
+ int path_count);
/* The files whose File node the shell pass marked as running a container
* build ({"declares_build":true}), into arena `a`; returns how many, or -1
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index f2a69bf7d..e9e92a702 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2461,6 +2461,95 @@ TEST(probe_container_env_sets_scoped_envvars) {
PASS();
}
+/* Every env join edge (CONFIGURES {strategy:env_read}) as
+ * "<setter file> <container>/<name> -> <reader file>", then the containers
+ * the image references name: "#image_containers=...". */
+static char *gpf_env_joins(const GpfFile *files, int nfiles) {
+ GpfProj lp;
+ cbm_store_t *store = gpf_index_files(&lp, files, nfiles);
+ char *out = NULL;
+ if (store) {
+ sqlite3_stmt *st = NULL;
+ const char *sql =
+ "SELECT coalesce((SELECT group_concat(x, char(10)) FROM (SELECT DISTINCT s.file_path "
+ "|| ' ' || substr(s.qualified_name, instr(s.qualified_name, '#env/') + 5) || ' -> ' "
+ "|| t.file_path AS x FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON "
+ "t.id=e.target_id WHERE e.project=?1 AND e.type='CONFIGURES' AND "
+ "json_extract(e.properties,'$.strategy')='env_read' ORDER BY x)), '') || char(10) || "
+ "'#image_containers=' || coalesce((SELECT group_concat(c, ',') FROM (SELECT "
+ "json_extract(e.properties,'$.container') AS c FROM edges e JOIN nodes s ON "
+ "s.id=e.source_id WHERE e.project=?1 AND e.type='USAGE' AND s.file_path='k8s/shop.yaml' "
+ "AND json_extract(e.properties,'$.ref')='image' ORDER BY c)), '')";
+ if (sqlite3_prepare_v2(cbm_store_get_db(store), sql, -1, &st, NULL) == SQLITE_OK) {
+ sqlite3_bind_text(st, 1, lp.project, -1, SQLITE_STATIC);
+ if (sqlite3_step(st) == SQLITE_ROW) {
+ out = strdup((const char *)sqlite3_column_text(st, 0));
+ }
+ }
+ sqlite3_finalize(st);
+ }
+ gpf_cleanup(&lp, store);
+ return out;
+}
+
+/* A variable a container sets joins the readers of its name inside the files
+ * its own image is built from: the K8s container's image (two containers,
+ * two images), built by compose from a Dockerfile; the stages the last one
+ * copies from; a JSON-form COPY opening with `/` and a COPY with a trailing
+ * blank before a comment (both lost by the grammar); a context the sources
+ * exist under (the repository root for side/, web/ for web/). Never: the same
+ * name in another container's image, a file the context's .dockerignore
+ * drops, a test file, or a reader in no image. */
+TEST(probe_container_env_joins_its_own_image_readers) {
+ static const GpfFile files[] = {
+ {"k8s/shop.yaml",
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: shop\nspec:\n template:\n"
+ " spec:\n containers:\n - name: web\n image: acme/web:1\n"
+ " env:\n - name: PORT\n value: \"8080\"\n"
+ " - name: MODE\n value: prod\n"
+ " - name: side\n image: acme/side:2\n env:\n"
+ " - name: PORT\n value: \"9090\"\n"},
+ {"compose.yaml", "services:\n web:\n image: acme/web:1\n build: ./web\n"
+ " environment:\n API_KEY: k\n"
+ " side:\n image: acme/side:2\n build:\n context: .\n"
+ " dockerfile: side/Dockerfile\n"},
+ {"web/Dockerfile", "FROM node:20 AS build\nCOPY src ./src \n# a comment after a blank\n"
+ "RUN npm ci\nFROM node:20-slim\nCOPY --from=build /src /app/src\n"
+ "COPY [\"/lib/\", \"/app/lib/\"]\nCOPY gen ./gen\n"
+ "CMD [\"node\", \"src/server.js\"]\n"},
+ {"web/.dockerignore", "gen/skip.js\n"},
+ {"web/src/server.js", "const port = process.env.PORT;\nconst mode = process.env.MODE;\n"
+ "const key = process.env.API_KEY;\n"},
+ {"web/src/server.test.js", "const p = process.env.PORT;\n"},
+ {"web/lib/conf.js", "module.exports = process.env.MODE;\n"},
+ {"web/gen/a.js", "exports.m = process.env.MODE;\n"},
+ {"web/gen/skip.js", "exports.m = process.env.MODE;\n"},
+ {"side/Dockerfile", "FROM golang:1.22 AS build\nCOPY side/main.go shared/util.go /src/\n"
+ "RUN go build -o /app /src\nFROM alpine:3.20\n"
+ "COPY --from=build /app /app\nENTRYPOINT [\"/app\"]\n"},
+ {"side/main.go", "package main\n\nimport \"os\"\n\nfunc main() { _ = os.Getenv(\"PORT\") }\n"},
+ {"shared/util.go", "package shared\n\nimport \"os\"\n\n"
+ "func Port() string { return os.Getenv(\"PORT\") }\n"},
+ {"other/main.go", "package main\n\nimport \"os\"\n\n"
+ "func main() { _ = os.Getenv(\"PORT\"); _ = os.Getenv(\"MODE\") }\n"}};
+ char *got = gpf_env_joins(files, (int)(sizeof(files) / sizeof(files[0])));
+ static const char want[] = "compose.yaml web/API_KEY -> web/src/server.js\n"
+ "k8s/shop.yaml side/PORT -> shared/util.go\n"
+ "k8s/shop.yaml side/PORT -> side/main.go\n"
+ "k8s/shop.yaml web/MODE -> web/gen/a.js\n"
+ "k8s/shop.yaml web/MODE -> web/lib/conf.js\n"
+ "k8s/shop.yaml web/MODE -> web/src/server.js\n"
+ "k8s/shop.yaml web/PORT -> web/src/server.js\n"
+ "#image_containers=side,web";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "env joins got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("env_read edge set");
+ }
+ free(got);
+ PASS();
+}
+
#define GPF_CM(name) "apiVersion: v1\nkind: ConfigMap\nmetadata:\n name: " name "\n"
#define GPF_KUST "apiVersion: kustomize.config.k8s.io/v1beta1\nkind: Kustomization\n"
@@ -4733,6 +4822,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_compose_services_are_resources);
RUN_TEST(probe_shell_docker_builds_build_images);
RUN_TEST(probe_container_env_sets_scoped_envvars);
+ RUN_TEST(probe_container_env_joins_its_own_image_readers);
RUN_TEST(probe_image_hub_splits_by_component);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 434ee1ba7..a99731d76 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -4779,6 +4779,84 @@ TEST(incremental_stem_mark_deletion_rebuilds) {
PASS();
}
+/* The env join on a closure repair: a reader file edited, the join edges of
+ * the files the repair leaves alone come back from the store (their setters,
+ * image references and readers are proxies there), the edited file's are
+ * recomputed -- the same set a full build gives. */
+TEST(incremental_env_join_repair_keeps_unchanged_pairs) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_envjoin_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ closure_probe_repo(tmp);
+ write_temp_file(tmp, "k8s/shop.yaml",
+ "apiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: shop\nspec:\n"
+ " template:\n spec:\n containers:\n - name: web\n"
+ " image: acme/web:1\n env:\n - name: PORT\n"
+ " value: \"8080\"\n - name: MODE\n"
+ " value: prod\n");
+ write_temp_file(tmp, "compose.yaml",
+ "services:\n web:\n image: acme/web:1\n build: ./web\n");
+ write_temp_file(tmp, "web/Dockerfile",
+ "FROM node:20\nCOPY src ./src\nCOPY lib ./lib\nCMD [\"node\", \"src/server.js\"]\n");
+ write_temp_file(tmp, "web/src/server.js",
+ "const port = process.env.PORT;\nconst mode = process.env.MODE;\n");
+ write_temp_file(tmp, "web/lib/conf.js", "module.exports = process.env.MODE;\n");
+ write_temp_file(tmp, "web/lib/more.js", "module.exports = process.env.PORT;\n");
+ write_temp_file(tmp, "misc/other.sh", "#!/bin/sh\nother() {\n echo other\n}\n");
+ char db[512];
+ snprintf(db, sizeof(db), "%s/envjoin.db", tmp);
+ static const char joins[] = "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id "
+ "WHERE e.type='CONFIGURES' AND s.label='EnvVar'";
+#define ENVJOIN_PAIR(key, file) \
+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id JOIN nodes t ON " \
+ "t.id=e.target_id WHERE e.type='CONFIGURES' AND s.label='EnvVar' AND s.qualified_name LIKE " \
+ "'%#env/web/" key "' AND t.file_path='" file "'"
+ static const char mode_server[] = ENVJOIN_PAIR("MODE", "web/src/server.js");
+ static const char mode_conf[] = ENVJOIN_PAIR("MODE", "web/lib/conf.js");
+ static const char port_server[] = ENVJOIN_PAIR("PORT", "web/src/server.js");
+ static const char port_more[] = ENVJOIN_PAIR("PORT", "web/lib/more.js");
+#undef ENVJOIN_PAIR
+
+ int full_rc = stem_index(tmp, db);
+ int full_joins = k8s_inc_count(db, joins);
+ int full_mode_server = k8s_inc_count(db, mode_server);
+
+ write_temp_file(tmp, "web/src/server.js", "const port = process.env.PORT;\n");
+ int inc_rc = stem_index(tmp, db);
+ cbm_incremental_route_t route = cbm_pipeline_incremental_test_last_route();
+ int inc_joins = k8s_inc_count(db, joins);
+ int inc_mode_server = k8s_inc_count(db, mode_server);
+ int inc_mode_conf = k8s_inc_count(db, mode_conf);
+ int inc_port_server = k8s_inc_count(db, port_server);
+ int inc_port_more = k8s_inc_count(db, port_more);
+
+ /* a repair far from every image: each join edge joins two files the
+ * repair leaves alone, and is written again although both ends are
+ * pre-existing rows */
+ write_temp_file(tmp, "misc/other.sh", "#!/bin/sh\nother() {\n echo changed\n}\n");
+ int far_rc = stem_index(tmp, db);
+ cbm_incremental_route_t far_route = cbm_pipeline_incremental_test_last_route();
+ int far_joins = k8s_inc_count(db, joins);
+ int far_mode_conf = k8s_inc_count(db, mode_conf);
+ th_rmtree(tmp);
+
+ ASSERT_EQ(full_rc, 0);
+ ASSERT_EQ(full_joins, 4);
+ ASSERT_EQ(full_mode_server, 1);
+ ASSERT_EQ(inc_rc, 0);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_EQ(inc_joins, 3);
+ ASSERT_EQ(inc_mode_server, 0);
+ ASSERT_EQ(inc_mode_conf, 1);
+ ASSERT_EQ(inc_port_server, 1);
+ ASSERT_EQ(inc_port_more, 1);
+ ASSERT_EQ(far_rc, 0);
+ ASSERT_EQ(far_route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_EQ(far_joins, 3);
+ ASSERT_EQ(far_mode_conf, 1);
+ PASS();
+}
+
TEST(pipeline_closure_repair_budget_declines_to_full) {
char tmp[256];
snprintf(tmp, sizeof(tmp), "/tmp/cbm_closure_budget_XXXXXX");
@@ -23339,6 +23417,7 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(pipeline_closure_repair_added_name_declines_to_full);
RUN_TEST(pipeline_closure_repair_new_file_declines_to_full);
RUN_TEST(incremental_stem_mark_deletion_rebuilds);
+ RUN_TEST(incremental_env_join_repair_keeps_unchanged_pairs);
RUN_TEST(pipeline_closure_repair_budget_declines_to_full);
RUN_TEST(pipeline_incremental_tsconfig_alias_change_matches_fresh_full);
#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API