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): a symlink must not abort a bounded count

Martin Vogelaa95352bd1e3
diff --git a/src/discover/discover.c b/src/discover/discover.c
index 134dd18c8..aaa604771 100644
--- a/src/discover/discover.c
+++ b/src/discover/discover.c
@@ -1197,7 +1197,12 @@ static void walk_dir_process_entry(cbm_dirent_t *entry, const walk_frame_t *fram
struct stat st;
bool is_symlink = false;
if (safe_stat(abs_path, &st, &is_symlink) != 0) {
- if (out->count_only) {
+ /* A count aborts when it cannot observe an entry, because its caller
+ * (auto-index admission) treats an unreliable count as a refusal. A
+ * symlink is not that case: it is skipped deliberately, on every walk.
+ * Failing here made one symlink anywhere in a tree refuse the whole
+ * repo, and the refusal surfaced as "unsafe_or_unavailable_path". */
+ if (out->count_only && !is_symlink) {
out->failed = true;
} else if (is_symlink) {
/* Deliberately not indexed (#963): record so callers can
diff --git a/tests/test_discover.c b/tests/test_discover.c
index 3b1c5a213..c98ec26a8 100644
--- a/tests/test_discover.c
+++ b/tests/test_discover.c
@@ -2122,6 +2122,34 @@ static CBMLanguage discover_lang_of(const cbm_file_info_t *files, int count, con
return CBM_LANG_COUNT;
}
+/* #1803: a symlink is skipped by policy on every walk. A bounded count must
+ * skip it too, not abort: aborting made auto-index admission refuse every
+ * repository containing any symlink ("unsafe_or_unavailable_path"), while
+ * an explicit index of the same tree succeeded. */
+TEST(discover_count_bounded_survives_a_symlink) {
+#ifdef _WIN32
+ SKIP_PLATFORM("Windows: symlinks need admin / symlink() unavailable");
+#else
+ char *base = th_mktempdir("cbm_disc_link");
+ ASSERT(base != NULL);
+
+ th_mkdir_p(TH_PATH(base, ".git"));
+ th_write_file(TH_PATH(base, "main.go"), "package main\n");
+ th_write_file(TH_PATH(base, "sub/helper.go"), "package sub\n");
+ ASSERT_EQ(symlink("sub", TH_PATH(base, "linkdir")), 0);
+
+ cbm_discover_opts_t opts = {0};
+ int count = -1;
+ /* deadline 0 = no deadline: only the symlink can decide this count. */
+ cbm_discover_status_t status = cbm_discover_count_bounded(base, &opts, 50000, 0, &count);
+ ASSERT_EQ(status, CBM_DISCOVER_OK);
+ ASSERT_EQ(count, 2);
+
+ th_cleanup(base);
+ PASS();
+#endif
+}
+
/* Write raw bytes (may contain embedded NUL) to base/rel. Parent must exist. */
static int write_bytes_file(const char *path, const void *data, size_t n) {
FILE *f = cbm_fopen(path, "wb");
@@ -2507,4 +2535,7 @@ SUITE(discover) {
RUN_TEST(discover_nested_gitignore_below_ancestor_gitignore_issue1973);
RUN_TEST(discover_nested_gitignore_negation_overrides_ancestor_pattern);
RUN_TEST(discover_nested_gitignore_three_levels_deeper_file_wins);
+
+ /* A symlink must not abort a bounded count (auto-index admission, #1803) */
+ RUN_TEST(discover_count_bounded_survives_a_symlink);
}