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(shell): a container build command builds its images from the Dockerfile

Martin Vogel21332dd8f13e
diff --git a/src/pipeline/pass_shell.c b/src/pipeline/pass_shell.c
index 85adae26b..cce391672 100644
--- a/src/pipeline/pass_shell.c
+++ b/src/pipeline/pass_shell.c
@@ -377,6 +377,7 @@ typedef struct {
bool chart;
bool spawn; /* SPAWNS the script `tgt` / the Process `ext`, not DEPLOYS */
bool make; /* SPAWNS the rule of target `ext` that makefile `path` reads */
+ bool build; /* the image `ext` is built from the Dockerfile `path` */
} sh_hit_t;
typedef struct {
@@ -384,6 +385,7 @@ typedef struct {
int count;
int cap;
CBMArena arena;
+ CBMHashTable *declarers; /* files running a container build: path -> path */
} sh_hits_t;
typedef struct {
@@ -2158,6 +2160,181 @@ static bool sh_chdir(sh_t *s, const sh_argv_t *a) {
return true;
}
+/* A container build's flags that take a separate value. */
+static const char *const SH_BUILD_VALUED[] = {"--build-arg",
+ "--target",
+ "--platform",
+ "--label",
+ "--cache-from",
+ "--cache-to",
+ "--secret",
+ "--ssh",
+ "-o",
+ "--output",
+ "--network",
+ "--add-host",
+ "--iidfile",
+ "--progress",
+ "--builder",
+ "--build-context",
+ "--shm-size",
+ "--ulimit",
+ "-m",
+ "--memory",
+ "--memory-swap",
+ "--cpu-shares",
+ "-c",
+ "--cpuset-cpus",
+ "--cpuset-mems",
+ "--cgroup-parent",
+ "--isolation",
+ "--metadata-file",
+ "--annotation",
+ "--attest",
+ "--security-opt",
+ "--format",
+ "--jobs",
+ "--os",
+ "--arch",
+ "--variant",
+ "--manifest",
+ "--authfile",
+ "--cert-dir",
+ "--creds",
+ "--allow",
+ "--call",
+ NULL};
+
+enum { SH_BUILD_TAGS = 16, SH_IMAGE_NEST = 16 };
+
+/* The repository of image word `i` whose value is unknown: the word up to
+ * the ':' of its tag (or '@' of a digest) after its last '/', outside
+ * $( ) ${ } and backticks, evaluated alone -- the repository is the image's
+ * identity, a tag of unknown value keeps it. NULL: no tag to cut, or the
+ * repository is unknown too. */
+static const char *sh_image_repo(sh_t *s, TSNode cmd, const sh_argv_t *a, int i) {
+ uint32_t b = a->span[(size_t)i * PAIR_LEN];
+ uint32_t e = a->span[(size_t)i * PAIR_LEN + SKIP_ONE];
+ char closers[SH_IMAGE_NEST];
+ int depth = 0;
+ uint32_t cut = 0;
+ for (uint32_t k = b; k < e; k++) {
+ char c = s->src[k];
+ bool open2 = k + SKIP_ONE < e && c == '$' &&
+ (s->src[k + SKIP_ONE] == '(' || s->src[k + SKIP_ONE] == '{');
+ if (depth > 0 && c == closers[depth - SKIP_ONE]) {
+ depth--;
+ } else if ((open2 || c == '`') && depth < SH_IMAGE_NEST) {
+ closers[depth++] = c == '`' ? '`' : (s->src[k + SKIP_ONE] == '(' ? ')' : '}');
+ k += open2 ? SKIP_ONE : 0;
+ } else if (depth == 0 && c == '/') {
+ cut = 0;
+ } else if (depth == 0 && (c == ':' || c == '@') && cut == 0) {
+ cut = k;
+ }
+ }
+ if (cut == 0) {
+ return NULL;
+ }
+ TSNode w = ts_node_descendant_for_byte_range(cmd, b, e);
+ while (!ts_node_is_null(w) && (ts_node_start_byte(w) != b || ts_node_end_byte(w) != e)) {
+ w = ts_node_parent(w);
+ }
+ sh_str_t out = {0};
+ if (sh_type(w, "concatenation")) {
+ sh_concat(s, &out, w, b, cut, false);
+ } else if (sh_type(w, "string")) {
+ sh_concat(s, &out, w, b + SKIP_ONE, cut, true);
+ } else {
+ return NULL;
+ }
+ return sh_str_done(&out);
+}
+
+/* docker / podman / nerdctl [buildx | image | builder] build, buildah bud:
+ * each tagged image is built from the Dockerfile -f names (against the cwd),
+ * else <context>/Dockerfile -- one file of the repository. */
+static void sh_build(sh_t *s, TSNode cmd, const sh_argv_t *a) {
+ int p = sh_program(s, a);
+ const char *tool = p >= 0 && a->v[p] ? sh_basename(a->v[p]) : NULL;
+ int i = p + SKIP_ONE;
+ if (tool && strcmp(tool, "buildah") == 0) {
+ if (i >= a->n || !a->v[i] ||
+ !(sh_eq(a->v[i], "bud") || sh_eq(a->v[i], "build") ||
+ sh_eq(a->v[i], "build-using-dockerfile"))) {
+ return;
+ }
+ i++;
+ } else if (tool && (strcmp(tool, "docker") == 0 || strcmp(tool, "podman") == 0 ||
+ strcmp(tool, "nerdctl") == 0)) {
+ if (i < a->n && sh_eq(a->v[i], "build")) {
+ i++;
+ } else if (i + SKIP_ONE < a->n &&
+ (sh_eq(a->v[i], "buildx") || sh_eq(a->v[i], "image") ||
+ sh_eq(a->v[i], "builder")) &&
+ sh_eq(a->v[i + SKIP_ONE], "build")) {
+ i += PAIR_LEN;
+ } else {
+ return;
+ }
+ } else {
+ return;
+ }
+ if (s->hits->declarers) {
+ cbm_ht_set(s->hits->declarers, s->file, (void *)s->file);
+ }
+ int tags[SH_BUILD_TAGS];
+ const char *tag_vals[SH_BUILD_TAGS];
+ int nt = 0;
+ const char *dfile = NULL;
+ bool dfile_known = true;
+ const char *ctx = NULL;
+ bool ctx_seen = false;
+ for (; i < a->n; i++) {
+ const char *w = a->v[i];
+ if (w && (sh_eq(w, "-t") || sh_eq(w, "--tag")) && i + SKIP_ONE < a->n) {
+ if (nt < SH_BUILD_TAGS) {
+ tags[nt] = i + SKIP_ONE;
+ tag_vals[nt++] = a->v[i + SKIP_ONE];
+ }
+ i++;
+ } else if (w && (sh_eq(w, "-f") || sh_eq(w, "--file")) && i + SKIP_ONE < a->n) {
+ dfile = a->v[i + SKIP_ONE];
+ dfile_known = dfile != NULL;
+ i++;
+ } else if (w && strncmp(w, "--tag=", strlen("--tag=")) == 0) {
+ if (nt < SH_BUILD_TAGS && w[strlen("--tag=")]) {
+ tags[nt] = -SKIP_ONE;
+ tag_vals[nt++] = w + strlen("--tag=");
+ }
+ } else if (w && strncmp(w, "--file=", strlen("--file=")) == 0) {
+ dfile = w + strlen("--file=");
+ } else if (w && w[0] == '-') {
+ i += !strchr(w, '=') && sh_in(w, SH_BUILD_VALUED) ? SKIP_ONE : 0;
+ } else if (!ctx_seen) {
+ ctx_seen = true;
+ ctx = w;
+ }
+ }
+ if (nt == 0 || !ctx || !dfile_known || strcmp(ctx, "-") == 0 || strstr(ctx, "://") ||
+ strncmp(ctx, "git@", strlen("git@")) == 0) {
+ return;
+ }
+ const char *path = dfile ? dfile : sh_join(s->a, ctx, "Dockerfile");
+ int64_t id = 0;
+ const char *hit = path ? sh_unique_file(s, path, &id) : NULL;
+ for (int k = 0; hit && k < nt; k++) {
+ const char *img = tag_vals[k];
+ if (!img && tags[k] >= 0) {
+ img = sh_image_repo(s, cmd, a, tags[k]);
+ }
+ if (img && img[0]) {
+ sh_hit_push(s, cmd,
+ (sh_hit_t){.via = "docker-build", .ext = img, .path = hit, .build = true});
+ }
+ }
+}
+
static void sh_command(sh_t *s, TSNode cmd) {
sh_walk_substs(s, cmd);
sh_argv_t a = {0};
@@ -2166,6 +2343,7 @@ static void sh_command(sh_t *s, TSNode cmd) {
sh_src_t out;
sh_deploy_command(s, cmd, &a, NULL, &out);
sh_spawn(s, cmd, &a);
+ sh_build(s, cmd, &a);
}
sh_argv_free(&a);
}
@@ -2213,6 +2391,7 @@ static void sh_pipe_run(sh_t *s, TSNode n, sh_src_t *src) {
*src = out;
}
sh_spawn(s, cmd, &a);
+ sh_build(s, cmd, &a);
sh_argv_free(&a);
sh_redirects(s, el);
}
@@ -2418,10 +2597,11 @@ static void sh_walk(sh_t *s, TSNode n) {
/* ── Files ─────────────────────────────────────────────────────────── */
static bool sh_mentions_deploy_tool(const char *src, size_t n) {
- static const char *const tools[] = {"kubectl", "helm", "kustomize"};
+ static const char *const tools[] = {"kubectl", "helm", "kustomize", "docker",
+ "podman", "buildah", "nerdctl"};
for (size_t i = 0; i < n; i++) {
char c = src[i] | ' ';
- if (c != 'k' && c != 'h') {
+ if (!strchr("khdpbn", c)) {
continue;
}
for (size_t t = 0; t < sizeof(tools) / sizeof(tools[0]); t++) {
@@ -3275,6 +3455,30 @@ static int sh_link_spawn(cbm_pipeline_ctx_t *ctx, int64_t src, const sh_hit_t *x
}
/* The edge hit `x` draws from source `src`; 1 when drawn. */
+/* An image a build command makes DEPENDS_ON {via:build} the Dockerfile
+ * it is built from (edges recomputed whole every run, as the image belongs
+ * to no file). */
+static int sh_link_build(cbm_pipeline_ctx_t *ctx, const sh_hit_t *x, CBMHashTable *modules) {
+ int64_t mod = (int64_t)(intptr_t)cbm_ht_get(modules, x->path);
+ int64_t img = mod > 0 ? cbm_pipeline_image_node(ctx, x->ext) : 0;
+ if (img <= 0) {
+ return 0;
+ }
+ char esc_file[CBM_SZ_1K];
+ char esc_img[CBM_SZ_1K];
+ char ci[CBM_SZ_1K];
+ char props[CBM_SZ_4K];
+ cbm_json_escape(esc_file, (int)sizeof(esc_file), x->file);
+ cbm_json_escape(esc_img, (int)sizeof(esc_img), x->ext);
+ sh_ci_props(x, ci, sizeof(ci));
+ snprintf(props, sizeof(props),
+ "{\"via\":\"build\",\"tool\":\"docker-build\",\"file\":\"%s\",\"image\":\"%s\"%s,"
+ "\"line\":%d}",
+ esc_file, esc_img, ci, x->line);
+ cbm_gbuf_insert_edge(ctx->gbuf, img, mod, "DEPENDS_ON", props);
+ return SKIP_ONE;
+}
+
static int sh_link_hit(cbm_pipeline_ctx_t *ctx, const sh_hit_t *x, int64_t src,
CBMHashTable *modules) {
if (x->spawn) {
@@ -3310,8 +3514,9 @@ static int sh_link(cbm_pipeline_ctx_t *ctx, const sh_hits_t *h) {
for (int i = 0; i < h->count; i++) {
const sh_hit_t *x = &h->items[i];
cbm_ht_set(files, x->file, (void *)x->file);
- if (x->spawn && x->tgt > 0) {
- cbm_ht_set(files, x->path, (void *)x->path); /* a spawned script's Module */
+ if ((x->spawn && x->tgt > 0) || x->build) {
+ cbm_ht_set(files, x->path,
+ (void *)x->path); /* a spawned script's / Dockerfile's Module */
}
}
sh_span_t *spans = NULL;
@@ -3344,6 +3549,10 @@ static int sh_link(cbm_pipeline_ctx_t *ctx, const sh_hits_t *h) {
sh_srcs_t twins = {0};
for (int i = 0; i < h->count; i++) {
const sh_hit_t *x = &h->items[i];
+ if (x->build) {
+ edges += sh_link_build(ctx, x, modules);
+ continue;
+ }
twins.count = 0;
int64_t one = sh_source(ctx, spans, n, modules, x->file, x->line, &twins);
for (int k = 0; k < (twins.count ? twins.count : SKIP_ONE); k++) {
@@ -3433,39 +3642,97 @@ static CBMHashTable *sh_prow_files(cbm_pipeline_ctx_t *ctx) {
return out;
}
+/* One script, Makefile or CI configuration `rel` (on disk at `path`). */
+static void sh_pass_file(sh_pass_t *p, cbm_pipeline_ctx_t *ctx, CBMHashTable *prow, const char *rel,
+ const char *path, CBMLanguage lang) {
+ bool ci = cbm_ci_path(rel) || (prow && cbm_ht_get(prow, rel));
+ if (lang != CBM_LANG_BASH && lang != CBM_LANG_MAKEFILE && !ci) {
+ return;
+ }
+ size_t len = 0;
+ char *src = sh_read(path, &len);
+ if (!src) {
+ return;
+ }
+ if (!p->ix_ready) {
+ p->ix_ready = true;
+ p->ix_ok = sh_index_build(p->ix, ctx);
+ p->parser = ts_parser_new();
+ }
+ if (p->ix_ok && p->parser) {
+ if (ci) {
+ sh_ci_file(p, rel, src, len);
+ } else if (lang == CBM_LANG_BASH) {
+ sh_script(p, rel, src, len);
+ } else {
+ sh_makefile(p, rel, src, len);
+ }
+ }
+ cbm_free(CBM_MEM_CLASS_OTHER, src);
+}
+
+/* Mark the File node of each file running a container build
+ * {"declares_build":true} -- resolved or not: an incremental run reads these
+ * files again, as the image build edges are recomputed whole
+ * (pipeline_incremental.c). The mark lives and dies with its file's node. */
+static void sh_mark_declarers(cbm_pipeline_ctx_t *ctx, CBMHashTable *declarers) {
+ static const char MARK[] = "\"declares_build\":true";
+ const cbm_gbuf_node_t **nodes = NULL;
+ int n = 0;
+ if (cbm_ht_count(declarers) == 0 ||
+ cbm_gbuf_find_by_label(ctx->gbuf, "File", &nodes, &n) != 0) {
+ return;
+ }
+ for (int i = 0; i < n; i++) {
+ cbm_gbuf_node_t *f = (cbm_gbuf_node_t *)nodes[i];
+ const char *props = f->properties_json ? f->properties_json : "{}";
+ if (!f->file_path || !cbm_ht_get(declarers, f->file_path) || strstr(props, MARK)) {
+ continue;
+ }
+ size_t pl = strlen(props);
+ char buf[CBM_SZ_4K];
+ bool empty = pl < PAIR_LEN || strcmp(props, "{}") == 0;
+ if (props[pl - SKIP_ONE] != '}' || pl + sizeof(MARK) + PAIR_LEN > sizeof(buf)) {
+ continue;
+ }
+ snprintf(buf, sizeof(buf), "%.*s%s%s}", (int)(pl - SKIP_ONE), empty ? "{" : props,
+ empty ? "" : ",", MARK);
+ (void)cbm_gbuf_node_set_properties_json(f, buf);
+ }
+}
+
void cbm_pipeline_pass_shell(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files,
int file_count) {
sh_index_t ix = {0};
sh_hits_t hits = {0};
cbm_arena_init(&hits.arena);
+ hits.declarers = cbm_ht_create(0);
sh_pass_t p = {.ix = &ix, .hits = &hits, .parser = NULL};
CBMHashTable *prow = sh_prow_files(ctx);
+ CBMHashTable *seen = ctx->build_decl_count > 0 ? cbm_ht_create(0) : NULL;
for (int i = 0; i < file_count; i++) {
- CBMLanguage lang = files[i].language;
- bool ci = cbm_ci_path(files[i].rel_path) || (prow && cbm_ht_get(prow, files[i].rel_path));
- if (lang != CBM_LANG_BASH && lang != CBM_LANG_MAKEFILE && !ci) {
- continue;
+ if (seen) {
+ cbm_ht_set(seen, files[i].rel_path, (void *)files[i].rel_path);
}
- size_t len = 0;
- char *src = sh_read(files[i].path, &len);
- if (!src) {
+ sh_pass_file(&p, ctx, prow, files[i].rel_path, files[i].path, files[i].language);
+ }
+ /* An incremental run recomputes every image's build edges: the unchanged
+ * files that declared builds before are read again (pipeline_incremental.c). */
+ for (int i = 0; seen && ctx->repo_path && i < ctx->build_decl_count; i++) {
+ const char *rel = ctx->build_decl_files[i];
+ if (!rel || cbm_ht_get(seen, rel)) {
continue;
}
- if (!p.ix_ready) {
- p.ix_ready = true;
- p.ix_ok = sh_index_build(&ix, ctx);
- p.parser = ts_parser_new();
- }
- if (p.ix_ok && p.parser) {
- if (ci) {
- sh_ci_file(&p, files[i].rel_path, src, len);
- } else if (lang == CBM_LANG_BASH) {
- sh_script(&p, files[i].rel_path, src, len);
- } else {
- sh_makefile(&p, files[i].rel_path, src, len);
- }
- }
- cbm_free(CBM_MEM_CLASS_OTHER, src);
+ cbm_ht_set(seen, rel, (void *)rel);
+ const char *slash = strrchr(rel, '/');
+ CBMLanguage lang = cbm_language_for_filename(slash ? slash + SKIP_ONE : rel);
+ char path[CBM_SZ_4K];
+ snprintf(path, sizeof(path), "%s/%s", ctx->repo_path, rel);
+ sh_pass_file(&p, ctx, prow, rel, path,
+ lang == CBM_LANG_MAKEFILE ? CBM_LANG_MAKEFILE : CBM_LANG_BASH);
+ }
+ if (seen) {
+ cbm_ht_free(seen);
}
if (prow) {
cbm_ht_free(prow);
@@ -3474,6 +3741,10 @@ void cbm_pipeline_pass_shell(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *fil
sh_make_resolve(&p, ctx, &hits);
}
int edges = hits.count > 0 ? sh_link(ctx, &hits) : 0;
+ if (hits.declarers) {
+ sh_mark_declarers(ctx, hits.declarers);
+ cbm_ht_free(hits.declarers);
+ }
if (p.parser) {
ts_parser_delete(p.parser);
}
diff --git a/src/pipeline/pipeline_delta.c b/src/pipeline/pipeline_delta.c
index 48f5eb638..9d6a1b86b 100644
--- a/src/pipeline/pipeline_delta.c
+++ b/src/pipeline/pipeline_delta.c
@@ -283,6 +283,48 @@ int cbm_delta_drop_unreferenced(cbm_store_t *store, const char *project, const c
return rc == SQLITE_DONE ? sqlite3_changes(db) : CBM_NOT_FOUND;
}
+int cbm_delta_build_decl_files(cbm_store_t *store, const char *project, CBMArena *a,
+ const char ***out) {
+ *out = NULL;
+ sqlite3 *db = cbm_store_get_db(store);
+ if (!db) {
+ return CBM_NOT_FOUND;
+ }
+ sqlite3_stmt *stmt = NULL;
+ if (sqlite3_prepare_v2(db,
+ "SELECT file_path FROM nodes WHERE project = ?1 AND label = 'File'"
+ " AND json_extract(properties, '$.declares_build') = 1",
+ 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 cap = 0;
+ const char **items = NULL;
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ const char *f = (const char *)sqlite3_column_text(stmt, 0);
+ if (!f) {
+ continue;
+ }
+ if (n == cap) {
+ int ncap = cap ? cap * PAIR_LEN : CBM_SZ_16;
+ const char **grown = cbm_arena_alloc(a, (size_t)ncap * sizeof(*grown));
+ if (!grown) {
+ break;
+ }
+ if (n) {
+ memcpy((void *)grown, items, (size_t)n * sizeof(*grown));
+ }
+ items = grown;
+ cap = ncap;
+ }
+ items[n++] = cbm_arena_strdup(a, f);
+ }
+ sqlite3_finalize(stmt);
+ *out = items;
+ return n;
+}
+
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);
diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c
index f4134be00..104f8d7fa 100644
--- a/src/pipeline/pipeline_incremental.c
+++ b/src/pipeline/pipeline_incremental.c
@@ -1164,6 +1164,28 @@ static bool incr_edge_type_is_recomputed(const char *type) {
strcmp(type, "FILE_CHANGES_WITH") == 0 || strcmp(type, "DATA_FLOWS") == 0);
}
+/* The files whose File node the shell pass marked as running a container
+ * build (pass_shell.c), from the full-load buffer, into arena `a`; returns
+ * how many. */
+static int incr_build_decl_files(const cbm_gbuf_t *gb, CBMArena *a, const char ***out) {
+ const cbm_gbuf_node_t **nodes = NULL;
+ int n = 0;
+ *out = NULL;
+ if (cbm_gbuf_find_by_label(gb, "File", &nodes, &n) != 0 || n == 0) {
+ return 0;
+ }
+ const char **items = cbm_arena_alloc(a, (size_t)n * sizeof(*items));
+ int k = 0;
+ for (int i = 0; items && i < n; i++) {
+ const char *p = nodes[i]->properties_json;
+ if (nodes[i]->file_path && p && strstr(p, "\"declares_build\":true")) {
+ items[k++] = cbm_arena_strdup(a, nodes[i]->file_path);
+ }
+ }
+ *out = items;
+ return k;
+}
+
/* cbm_gbuf_foreach_edge visitor: snapshot inbound cross-file edges into
* changed files so they survive the purge and can be re-linked afterward. */
static void incr_capture_inbound_edge(const cbm_gbuf_edge_t *edge, void *userdata) {
@@ -2857,6 +2879,10 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
cbm_doc_link_row_t *doc_rows = NULL;
int doc_row_count = 0;
bool doc_failed = false;
+ 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 = 0;
cbm_clock_gettime(CLOCK_MONOTONIC, &t);
if (cbm_delta_stage_clone(db_path, &stage) != 0) {
@@ -2869,6 +2895,9 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
goto out;
}
cbm_log_info("delta.staging_open", "elapsed_ms", itoa_buf((int)elapsed_ms(t)));
+ /* before the purge drops any of their edges */
+ decl_count = cbm_delta_build_decl_files(staging, project, &decl_arena, &decl_files);
+ decl_count = decl_count > 0 ? decl_count : 0;
/* Purge set = closure files + deleted files. */
int purge_count = ci + deleted_count;
@@ -3052,6 +3081,8 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char
.excluded_count = excluded_count,
.doc_link_base = doc_base,
.doc_link_base_count = doc_base_count,
+ .build_decl_files = decl_files,
+ .build_decl_count = decl_count,
};
for (int i = 0; i < ci; i++) {
char *file_qn = cbm_pipeline_fqn_compute(project, changed_files[i].rel_path, "__file__");
@@ -3306,6 +3337,7 @@ out:
#endif
}
cbm_delta_free_snapshot(snapshot, snapshot_count);
+ cbm_arena_destroy(&decl_arena);
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);
@@ -3641,6 +3673,10 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
* 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). */
+ 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);
/* Snapshot inbound cross-file edges into changed files BEFORE purging, so
@@ -3734,6 +3770,8 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
}
}
+ ctx.build_decl_files = decl_files;
+ ctx.build_decl_count = decl_count;
int phase_rc = run_extract_resolve(&ctx, changed_files, ci, NULL);
if (phase_rc == 0) {
phase_rc = cbm_pipeline_pass_k8s(&ctx, changed_files, ci);
@@ -3780,6 +3818,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free_mode_skipped(mode_skipped, mode_skipped_count);
free(saved_adr);
cbm_gbuf_free(existing);
+ cbm_arena_destroy(&decl_arena);
legacy_doc_free(&legacy_doc);
return CBM_PIPELINE_ABORT_PRESERVE_DB;
}
@@ -3892,6 +3931,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
free_mode_skipped(mode_skipped, mode_skipped_count);
free(saved_adr);
cbm_gbuf_free(existing);
+ cbm_arena_destroy(&decl_arena);
cbm_doclinks_free_rows(doc_rows, doc_row_count);
return manifest_rc == CBM_DISCOVER_LIMIT_EXCEEDED ? CBM_PIPELINE_RESOURCE_LIMIT
: CBM_PIPELINE_ABORT_PRESERVE_DB;
@@ -3931,6 +3971,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil
cbm_store_free_coverage(old_cov, old_cov_count);
free_mode_skipped(mode_skipped, mode_skipped_count);
cbm_gbuf_free(existing);
+ cbm_arena_destroy(&decl_arena);
cbm_log_info("incremental.done", "elapsed_ms", itoa_buf((int)elapsed_ms(t0)));
return persist_rc;
diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h
index 34a3a42dc..0284d2185 100644
--- a/src/pipeline/pipeline_internal.h
+++ b/src/pipeline/pipeline_internal.h
@@ -314,6 +314,11 @@ void cbm_pkg_entries_free(cbm_pkg_entries_t *e);
typedef struct {
const char *project_name; /* borrowed from pipeline */
const char *repo_path; /* borrowed from pipeline */
+ /* An incremental run: the unchanged files whose earlier run declared
+ * image builds (docker build ...), read again by the shell pass, as
+ * those edges are recomputed whole. Borrowed; NULL in a full run. */
+ const char *const *build_decl_files;
+ int build_decl_count;
cbm_gbuf_t *gbuf; /* owned by pipeline */
cbm_registry_t *registry; /* owned by pipeline */
atomic_int *cancelled; /* pointer to pipeline's cancelled flag */
@@ -1354,6 +1359,12 @@ int cbm_delta_drop_unreferenced(cbm_store_t *store, const char *project, const c
#define CBM_BUILD_EDGE_TYPE "DEPENDS_ON"
#define CBM_BUILD_EDGE_SOURCE "Image"
+/* 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
+ * on failure. */
+int cbm_delta_build_decl_files(cbm_store_t *store, const char *project, CBMArena *a,
+ const char ***out);
+
/* Delete the project's `type` edges whose source node has label `label`;
* returns how many, or -1 on failure. */
int cbm_delta_drop_edges_from_label(cbm_store_t *store, const char *project, const char *type,
diff --git a/tests/test_grammar_probe_f.c b/tests/test_grammar_probe_f.c
index b00d31481..2ff52501f 100644
--- a/tests/test_grammar_probe_f.c
+++ b/tests/test_grammar_probe_f.c
@@ -2176,6 +2176,35 @@ TEST(probe_compose_services_are_resources) {
PASS();
}
+/* A container build command (docker / podman [buildx] build, in a script or
+ * a Make recipe) builds each tagged image from the Dockerfile -f names
+ * (against the cwd), else <context>/Dockerfile. A tag of unknown value keeps
+ * its repository ($(TAG), $VERSION; a registry port is no tag); an unknown
+ * context, or another docker command, is no build. */
+TEST(probe_shell_docker_builds_build_images) {
+ static const GpfFile files[] = {
+ {"Dockerfile", "FROM alpine:3.20\n"},
+ {"svc/Dockerfile", "FROM golang:1.22\n"},
+ {"Makefile", "DOCKER_REPO ?= quay.io/acme\nTAG ?= $(shell git describe --tags)\n\n"
+ "image:\n\tdocker build --label a=b -t $(DOCKER_REPO)/app:$(TAG) .\n"},
+ {"build.sh",
+ "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
+ "docker buildx build --platform linux/amd64 --tag ghcr.io/acme/svc:1 -f svc/Dockerfile .\n"
+ "podman build -t \"localhost:5000/team/x:$VERSION\" svc\n"
+ "docker build -t unknown-ctx $CTX\ndocker run -t nope .\n"}};
+ char *got = gpf_builds(files, 4);
+ static const char want[] = "ghcr.io/acme/svc -> svc/Dockerfile (docker-build)\n"
+ "localhost:5000/team/x -> svc/Dockerfile (docker-build)\n"
+ "quay.io/acme/app -> Dockerfile (docker-build)";
+ if (!got || strcmp(got, want) != 0) {
+ fprintf(stderr, "builds got:\n%s\nwant:\n%s\n", got ? got : "(null)", want);
+ free(got);
+ FAIL("docker build DEPENDS_ON {via:build} 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"
@@ -4445,6 +4474,7 @@ SUITE(grammar_probe_f) {
RUN_TEST(probe_k8s_container_images_are_images);
RUN_TEST(probe_build_configs_build_images_from_dockerfiles);
RUN_TEST(probe_compose_services_are_resources);
+ RUN_TEST(probe_shell_docker_builds_build_images);
RUN_TEST(probe_gitops_objects_deploy_their_units);
RUN_TEST(probe_shell_kubectl_deploys_what_it_applies);
RUN_TEST(probe_shell_helm_deploys_its_chart);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index 1bb965a3d..16fe909a6 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -3698,6 +3698,97 @@ TEST(incremental_compose_dotenv_change_rebuilds) {
PASS();
}
+/* Editing a Dockerfile keeps the build edge an unchanged script declares
+ * for it: image build edges are recomputed whole, so the files that declared
+ * builds before are read again. `legacy`: through the full-load route, else
+ * the closure-repair route. Returns "full=N inc=M", or NULL. */
+static char *incremental_docker_build_keep(bool legacy, cbm_incremental_route_t *route) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_dockerbuild_XXXXXX");
+ if (!cbm_mkdtemp(tmp)) {
+ return NULL;
+ }
+ write_temp_file(tmp, "svc/Dockerfile", "FROM golang:1.22\n");
+ write_temp_file(tmp, "build.sh",
+ "#!/bin/sh\ncd \"$(dirname \"$0\")\"\ndocker build -t ghcr.io/acme/svc:1 svc\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/dockerbuild.db", tmp);
+ static const char builds[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id WHERE e.type='DEPENDS_ON' "
+ "AND s.label='Image' AND json_extract(e.properties,'$.tool')='docker-build'";
+ int full = k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, builds) : -1;
+ write_temp_file(tmp, "svc/Dockerfile", "FROM golang:1.22\n# edited\n");
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ if (legacy) {
+ cbm_pipeline_incremental_test_force_legacy_partial_once();
+ }
+#else
+ (void)legacy;
+#endif
+ char *out = NULL;
+ if (k8s_inc_index(tmp, db_path) == 0) {
+ char buf[64];
+ snprintf(buf, sizeof(buf), "full=%d inc=%d", full, k8s_inc_count(db_path, builds));
+ out = strdup(buf);
+ }
+ *route = cbm_pipeline_incremental_test_last_route();
+ th_rmtree(tmp);
+ return out;
+}
+
+TEST(incremental_dockerfile_edit_keeps_docker_build_edges) {
+ cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
+ char *got = incremental_docker_build_keep(false, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "full=1 inc=1");
+ free(got);
+#if defined(CBM_INCREMENTAL_TEST_API) && CBM_INCREMENTAL_TEST_API
+ got = incremental_docker_build_keep(true, &route);
+ ASSERT_EQ(route, CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL);
+ ASSERT_NOT_NULL(got);
+ ASSERT_STR_EQ(got, "full=1 inc=1");
+ free(got);
+#endif
+ PASS();
+}
+
+/* Two files build the same image from one Dockerfile: deleting the one
+ * the edge happened to record keeps the edge -- the other file still
+ * declares it, and it is read again (its File node carries the mark). */
+TEST(incremental_second_build_declarer_keeps_the_edge) {
+ char tmp[256];
+ snprintf(tmp, sizeof(tmp), "/tmp/cbm_twodecl_XXXXXX");
+ ASSERT_NOT_NULL(cbm_mkdtemp(tmp));
+ write_temp_file(tmp, "svc/Dockerfile", "FROM golang:1.22\n");
+ write_temp_file(tmp, "a.sh", "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
+ "docker build -t ghcr.io/acme/svc:1 svc\n");
+ write_temp_file(tmp, "b.sh", "#!/bin/sh\ncd \"$(dirname \"$0\")\"\n"
+ "docker build -t ghcr.io/acme/svc:2 svc\n");
+ char db_path[512];
+ snprintf(db_path, sizeof(db_path), "%s/twodecl.db", tmp);
+ static const char builds[] =
+ "SELECT COUNT(*) FROM edges e JOIN nodes s ON s.id=e.source_id WHERE e.type='DEPENDS_ON' "
+ "AND s.label='Image' AND json_extract(e.properties,'$.tool')='docker-build'";
+ int full = k8s_inc_index(tmp, db_path) == 0 ? k8s_inc_count(db_path, builds) : -1;
+ char path[512];
+ snprintf(path, sizeof(path), "%s/a.sh", tmp);
+ (void)remove(path);
+ int rc_a = k8s_inc_index(tmp, db_path);
+ int after_a = k8s_inc_count(db_path, builds);
+ snprintf(path, sizeof(path), "%s/b.sh", tmp);
+ (void)remove(path);
+ int rc_b = k8s_inc_index(tmp, db_path);
+ int after_b = k8s_inc_count(db_path, builds);
+ th_rmtree(tmp);
+ ASSERT_EQ(full, 1);
+ ASSERT_EQ(rc_a, 0);
+ ASSERT_EQ(after_a, 1);
+ ASSERT_EQ(rc_b, 0);
+ ASSERT_EQ(after_b, 0);
+ PASS();
+}
+
TEST(incremental_deleted_dockerfile_drops_its_image) {
cbm_incremental_route_t route = CBM_INCREMENTAL_ROUTE_NONE;
int left = incremental_dockerfile_delete(false, &route);
@@ -19341,6 +19432,8 @@ SUITE(pipeline_semantic_manifest_repro) {
RUN_TEST(incremental_dockerfile_edit_keeps_build_edges);
RUN_TEST(incremental_compose_edit_replaces_build_edges);
RUN_TEST(incremental_compose_dotenv_change_rebuilds);
+ RUN_TEST(incremental_dockerfile_edit_keeps_docker_build_edges);
+ RUN_TEST(incremental_second_build_declarer_keeps_the_edge);
RUN_TEST(pipeline_sql_lineage_and_relation_isolation);
RUN_TEST(pipeline_incremental_sql_table_rename_drops_stale_lineage);
RUN_TEST(pipeline_dbt_jinja_lineage);