Skip to content

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

fix(discover): grow the walk stack instead of failing wide repositories

Martin Vogel03dc9c916a5b
diff --git a/src/discover/discover.c b/src/discover/discover.c
index 832cc1f5a..b2169f1aa 100644
--- a/src/discover/discover.c
+++ b/src/discover/discover.c
@@ -764,7 +764,17 @@ typedef struct {
cbm_gitignore_t *local_gi; /* nested .gitignore for this subtree */
char local_gi_prefix[CBM_SZ_4K]; /* rel_prefix when local_gi was loaded */
} walk_frame_t;
+/* Initial capacity only — the stack grows on demand. A single directory can
+ * hold more pending sibling frames than any fixed cap (dotnet/runtime has 855
+ * subdirs in one JIT regression dir), so a hard cap here means whole-repo
+ * discovery failure, not a depth guard. */
#define WALK_STACK_CAP 512
+
+typedef struct {
+ walk_frame_t *frames;
+ int top;
+ int cap;
+} walk_stack_t;
/* Build abs/rel paths and process one directory entry. */
/* Try to load a nested .gitignore from this directory. Returns owned pointer or NULL. */
static cbm_gitignore_t *try_load_nested_gitignore(const walk_frame_t *frame) {
@@ -780,35 +790,44 @@ static cbm_gitignore_t *try_load_nested_gitignore(const walk_frame_t *frame) {
return NULL;
}
-/* Push a subdirectory onto the walk stack, inheriting local gitignore context. */
-static void walk_push_subdir(walk_frame_t *stack, int *top, const char *abs_path,
- const char *rel_path, const walk_frame_t *parent, file_list_t *out) {
- if (*top >= WALK_STACK_CAP) {
- out->failed = true;
- return;
+/* Push a subdirectory onto the walk stack, inheriting local gitignore
+ * context. Grows the stack geometrically; the caller's `parent` must not
+ * point into the stack array (walk_dir pops into a local copy). */
+static void walk_push_subdir(walk_stack_t *ws, const char *abs_path, const char *rel_path,
+ const walk_frame_t *parent, file_list_t *out) {
+ if (ws->top >= ws->cap) {
+ int new_cap = ws->cap * 2;
+ walk_frame_t *grown = realloc(ws->frames, (size_t)new_cap * sizeof(*grown));
+ if (!grown) {
+ out->failed = true;
+ return;
+ }
+ ws->frames = grown;
+ ws->cap = new_cap;
}
- int directory_length = snprintf(stack[*top].dir, CBM_SZ_4K, "%s", abs_path);
- int prefix_length = snprintf(stack[*top].prefix, CBM_SZ_4K, "%s", rel_path);
+ walk_frame_t *slot = &ws->frames[ws->top];
+ int directory_length = snprintf(slot->dir, CBM_SZ_4K, "%s", abs_path);
+ int prefix_length = snprintf(slot->prefix, CBM_SZ_4K, "%s", rel_path);
if (directory_length <= 0 || directory_length >= CBM_SZ_4K || prefix_length < 0 ||
prefix_length >= CBM_SZ_4K) {
out->failed = true;
return;
}
- stack[*top].local_gi = parent->local_gi;
+ slot->local_gi = parent->local_gi;
int local_prefix_length =
- snprintf(stack[*top].local_gi_prefix, CBM_SZ_4K, "%s", parent->local_gi_prefix);
+ snprintf(slot->local_gi_prefix, CBM_SZ_4K, "%s", parent->local_gi_prefix);
if (local_prefix_length < 0 || local_prefix_length >= CBM_SZ_4K) {
out->failed = true;
return;
}
- (*top)++;
+ ws->top++;
}
static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *frame,
const cbm_discover_opts_t *opts,
const cbm_gitignore_t *gitignore,
const cbm_gitignore_t *global_gi,
- const cbm_gitignore_t *cbmignore, walk_frame_t *stack, int *top,
+ const cbm_gitignore_t *cbmignore, walk_stack_t *ws,
file_list_t *out) {
char abs_path[CBM_SZ_4K];
char rel_path[CBM_SZ_4K];
@@ -836,7 +855,7 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
if (S_ISDIR(st.st_mode)) {
if (!should_skip_directory(entry->name, rel_path, opts, gitignore, global_gi, cbmignore,
frame->local_gi, frame->local_gi_prefix)) {
- walk_push_subdir(stack, top, abs_path, rel_path, frame, out);
+ walk_push_subdir(ws, abs_path, rel_path, frame, out);
} else {
/* Record the excluded subtree root so callers can report it (#411). */
file_list_add_excluded(out, rel_path);
@@ -871,8 +890,9 @@ static bool walk_owned_gitignore_append(cbm_gitignore_t ***owned, size_t *count,
static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_discover_opts_t *opts,
const cbm_gitignore_t *gitignore, const cbm_gitignore_t *global_gi,
const cbm_gitignore_t *cbmignore, file_list_t *out) {
- walk_frame_t *stack = calloc(WALK_STACK_CAP, sizeof(walk_frame_t));
- if (!stack) {
+ walk_stack_t ws = {
+ .frames = calloc(WALK_STACK_CAP, sizeof(walk_frame_t)), .top = 0, .cap = WALK_STACK_CAP};
+ if (!ws.frames) {
out->failed = true;
return;
}
@@ -882,19 +902,18 @@ static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_dis
size_t owned_count = 0;
size_t owned_capacity = 0;
- int top = 0;
- int initial_directory_length = snprintf(stack[top].dir, CBM_SZ_4K, "%s", dir_path);
- int initial_prefix_length = snprintf(stack[top].prefix, CBM_SZ_4K, "%s", rel_prefix);
+ int initial_directory_length = snprintf(ws.frames[0].dir, CBM_SZ_4K, "%s", dir_path);
+ int initial_prefix_length = snprintf(ws.frames[0].prefix, CBM_SZ_4K, "%s", rel_prefix);
if (initial_directory_length <= 0 || initial_directory_length >= CBM_SZ_4K ||
initial_prefix_length < 0 || initial_prefix_length >= CBM_SZ_4K) {
out->failed = true;
- free(stack);
+ free(ws.frames);
return;
}
- top++;
+ ws.top++;
- while (top > 0 && !file_list_should_stop(out)) {
- walk_frame_t frame = stack[--top];
+ while (ws.top > 0 && !file_list_should_stop(out)) {
+ walk_frame_t frame = ws.frames[--ws.top];
cbm_gitignore_t *loaded = try_load_nested_gitignore(&frame);
if (loaded) {
@@ -920,8 +939,7 @@ static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_dis
cbm_dirent_t *entry;
while (!file_list_should_stop(out) && (entry = cbm_readdir(d)) != NULL) {
- walk_dir_process_entry(entry, &frame, opts, gitignore, global_gi, cbmignore, stack,
- &top, out);
+ walk_dir_process_entry(entry, &frame, opts, gitignore, global_gi, cbmignore, &ws, out);
}
cbm_closedir(d);
}
@@ -929,7 +947,7 @@ static void walk_dir(const char *dir_path, const char *rel_prefix, const cbm_dis
cbm_gitignore_free(owned_gis[i]);
}
free(owned_gis);
- free(stack);
+ free(ws.frames);
}
/* ── Public API ───────────────────────────────── */
diff --git a/tests/test_discover.c b/tests/test_discover.c
index 35c28f150..57cbefe75 100644
--- a/tests/test_discover.c
+++ b/tests/test_discover.c
@@ -328,6 +328,35 @@ TEST(discover_simple) {
PASS();
}
+/* A directory with more immediate subdirectories than the walk stack's
+ * initial capacity must still be fully discovered — dotnet/runtime's
+ * src/tests/JIT/Regression/JitBlue holds 855 sibling dirs and made the whole
+ * pipeline hard-fail with files=0 (walk_push_subdir treated the fixed cap as
+ * a hard error instead of growing). */
+TEST(discover_wide_sibling_fanout_exceeds_initial_walk_stack) {
+ char *base = th_mktempdir("cbm_disc_wide");
+ ASSERT(base != NULL);
+
+ enum { WIDE_SIBLINGS = 600 };
+ for (int i = 0; i < WIDE_SIBLINGS; i++) {
+ char rel[64];
+ (void)snprintf(rel, sizeof(rel), "wide/d%03d/f.py", i);
+ th_write_file(TH_PATH(base, rel), "x = 1\n");
+ }
+
+ cbm_discover_opts_t opts = {0};
+ cbm_file_info_t *files = NULL;
+ int count = 0;
+
+ int rc = cbm_discover(base, &opts, &files, &count);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(count, WIDE_SIBLINGS);
+
+ cbm_discover_free(files, count);
+ th_cleanup(base);
+ PASS();
+}
+
TEST(discover_bounded_count_is_allocation_free_and_limit_exact) {
char *base = th_mktempdir("cbm_disc_bounded");
ASSERT(base != NULL);
@@ -337,11 +366,11 @@ TEST(discover_bounded_count_is_allocation_free_and_limit_exact) {
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
int limited_count = -1;
- cbm_discover_status_t limited = cbm_discover_count_bounded(
- base, &opts, 1, cbm_now_ms() + 2000, &limited_count);
+ cbm_discover_status_t limited =
+ cbm_discover_count_bounded(base, &opts, 1, cbm_now_ms() + 2000, &limited_count);
int exact_count = -1;
- cbm_discover_status_t exact = cbm_discover_count_bounded(
- base, &opts, 2, cbm_now_ms() + 2000, &exact_count);
+ cbm_discover_status_t exact =
+ cbm_discover_count_bounded(base, &opts, 2, cbm_now_ms() + 2000, &exact_count);
th_cleanup(base);
ASSERT_EQ(limited, CBM_DISCOVER_LIMIT_EXCEEDED);
@@ -358,8 +387,7 @@ TEST(discover_bounded_count_fails_closed_after_deadline) {
cbm_discover_opts_t opts = {.mode = CBM_MODE_FULL};
int count = 99;
- cbm_discover_status_t status =
- cbm_discover_count_bounded(base, &opts, 100, 1, &count);
+ cbm_discover_status_t status = cbm_discover_count_bounded(base, &opts, 100, 1, &count);
th_cleanup(base);
ASSERT_EQ(status, CBM_DISCOVER_ERROR);
@@ -1130,8 +1158,10 @@ TEST(discover_git_info_exclude_stacks_with_gitignore) {
bool found_log = false;
bool found_scratch = false;
for (int i = 0; i < count; i++) {
- if (strstr(files[i].rel_path, ".log")) found_log = true;
- if (strstr(files[i].rel_path, "scratch")) found_scratch = true;
+ if (strstr(files[i].rel_path, ".log"))
+ found_log = true;
+ if (strstr(files[i].rel_path, "scratch"))
+ found_scratch = true;
}
ASSERT_FALSE(found_log);
ASSERT_FALSE(found_scratch);
@@ -1294,29 +1324,25 @@ TEST(discover_many_nested_gitignores_do_not_exhaust_matcher_ownership) {
for (int i = 0; i < PACKAGE_COUNT; i++) {
char ignore_path[1024];
char source_path[1024];
- int ignore_written = snprintf(ignore_path, sizeof(ignore_path),
- "%s/package-%02d/.gitignore", base, i);
- int source_written = snprintf(source_path, sizeof(source_path),
- "%s/package-%02d/source.go", base, i);
- fixture_ready =
- fixture_ready && ignore_written > 0 &&
- (size_t)ignore_written < sizeof(ignore_path) &&
- source_written > 0 &&
- (size_t)source_written < sizeof(source_path) &&
- th_write_file(ignore_path, "ignored.tmp\n") == 0 &&
- th_write_file(source_path, "package fixture\n") == 0;
+ int ignore_written =
+ snprintf(ignore_path, sizeof(ignore_path), "%s/package-%02d/.gitignore", base, i);
+ int source_written =
+ snprintf(source_path, sizeof(source_path), "%s/package-%02d/source.go", base, i);
+ fixture_ready = fixture_ready && ignore_written > 0 &&
+ (size_t)ignore_written < sizeof(ignore_path) && source_written > 0 &&
+ (size_t)source_written < sizeof(source_path) &&
+ th_write_file(ignore_path, "ignored.tmp\n") == 0 &&
+ th_write_file(source_path, "package fixture\n") == 0;
}
cbm_discover_opts_t opts = {0};
cbm_file_info_t *files = NULL;
int count = 0;
- int discover_rc =
- fixture_ready ? cbm_discover(base, &opts, &files, &count) : -1;
+ int discover_rc = fixture_ready ? cbm_discover(base, &opts, &files, &count) : -1;
int bounded_count = -1;
cbm_discover_status_t bounded_status =
fixture_ready
- ? cbm_discover_count_bounded(base, &opts, PACKAGE_COUNT + 1, 0,
- &bounded_count)
+ ? cbm_discover_count_bounded(base, &opts, PACKAGE_COUNT + 1, 0, &bounded_count)
: CBM_DISCOVER_ERROR;
cbm_discover_free(files, count);
@@ -1403,6 +1429,7 @@ SUITE(discover) {
/* Integration tests (cross-platform) */
RUN_TEST(discover_simple);
+ RUN_TEST(discover_wide_sibling_fanout_exceeds_initial_walk_stack);
RUN_TEST(discover_bounded_count_is_allocation_free_and_limit_exact);
RUN_TEST(discover_bounded_count_fails_closed_after_deadline);
RUN_TEST(discover_skips_git_dir);