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): keep make/cargo dep-info .d files out of the D grammar

Nelson Spencefd1b88374c0c
diff --git a/src/discover/discover.c b/src/discover/discover.c
index 0e5ae5e4f..f3518abb8 100644
--- a/src/discover/discover.c
+++ b/src/discover/discover.c
@@ -803,6 +803,11 @@ static CBMLanguage detect_file_language(const char *entry_name, const char *abs_
if (dot && strcmp(dot, ".res") == 0) {
lang = cbm_disambiguate_res(abs_path);
}
+ /* Special: .d is shared by D and make-style dep-info (cargo, gcc -MD).
+ * Probe only while .d maps to D, so a user override to another language wins. */
+ if (dot && strcmp(dot, ".d") == 0 && lang == CBM_LANG_DLANG) {
+ lang = cbm_disambiguate_d(abs_path);
+ }
/* Special: ObjectScript Studio Export XML (<Export generator="...">) is
* detected by content; otherwise .xml stays XML. */
if (lang == CBM_LANG_XML) {
diff --git a/src/discover/discover.h b/src/discover/discover.h
index 89ccca4f3..24a08d5f4 100644
--- a/src/discover/discover.h
+++ b/src/discover/discover.h
@@ -57,6 +57,12 @@ CBMLanguage cbm_disambiguate_frm(const char *path);
* CBM_LANG_RESCRIPT. On read failure, defaults to CBM_LANG_RESCRIPT. */
CBMLanguage cbm_disambiguate_res(const char *path);
+/* Disambiguate .d files by reading first 4KB of content.
+ * Returns CBM_LANG_COUNT (not source) for a make-style dependency file written
+ * by rustc/cargo, gcc/clang -MD or CMake ("target ...: prereq ..."), otherwise
+ * CBM_LANG_DLANG. On read failure or any doubt, defaults to CBM_LANG_DLANG. */
+CBMLanguage cbm_disambiguate_d(const char *path);
+
/* Disambiguate .inc files by reading first 4KB of content.
* Returns CBM_LANG_OBJECTSCRIPT_ROUTINE if it looks like an ObjectScript
* include (a "ROUTINE <Uppercase>" header), otherwise CBM_LANG_BITBAKE.
diff --git a/src/discover/language.c b/src/discover/language.c
index dcc41dafd..66de4f4df 100644
--- a/src/discover/language.c
+++ b/src/discover/language.c
@@ -1330,6 +1330,87 @@ CBMLanguage cbm_disambiguate_res(const char *path) {
return memchr(buf, '\0', n) ? CBM_LANG_COUNT : CBM_LANG_RESCRIPT;
}
+/* A make rule's targets end at a ':' followed by whitespace or the end of the
+ * file, which skips drive-letter colons ("C:/", "C:\"). */
+static bool is_rule_colon(const char *p, bool whole_file) {
+ char next = p[SKIP_ONE];
+ return *p == ':' && (next == ' ' || next == '\t' || next == '\r' || next == '\n' ||
+ (next == '\0' && whole_file));
+}
+
+/* True if the line at p is a make rule "target...: prereq..." whose targets all
+ * look like paths (contain '/', '\' or '.'); a backslash-escaped space stays
+ * inside its target. D source fails this: "public:", "@safe:", "extern(C):"
+ * and "import a.b : c" all have a non-path word or D punctuation before the
+ * colon. */
+static bool is_dep_rule_line(const char *p, bool whole_file) {
+ bool in_target = false;
+ bool path_like = false;
+ bool any_target = false;
+ for (; *p && *p != '\n'; p++) {
+ if (is_rule_colon(p, whole_file)) {
+ return in_target ? path_like : any_target;
+ }
+ if (*p == ' ' || *p == '\t' || *p == '\r') {
+ if (in_target && !path_like) {
+ return false;
+ }
+ in_target = false;
+ path_like = false;
+ continue;
+ }
+ if (strchr("(){};=\"',", *p)) {
+ return false;
+ }
+ in_target = true;
+ any_target = true;
+ path_like = path_like || *p == '/' || *p == '\\' || *p == '.';
+ if (*p == '\\' && p[SKIP_ONE] == ' ') {
+ p++; /* escaped space: part of this target */
+ }
+ }
+ return false;
+}
+
+/* Disambiguate .d files: shared by D source and make-style dependency files
+ * written by rustc/cargo (target/<profile>/deps/<crate>-<hash>.d), gcc/clang -MD
+ * and CMake (<object>.o.d). Those are "target: prereq ..." rules, often tens of
+ * KB on one line, which the D grammar parses slowly into nothing but a module
+ * node, so they are reported as unsupported (CBM_LANG_COUNT). Defaults to D on
+ * any doubt (preserves existing behaviour). */
+CBMLanguage cbm_disambiguate_d(const char *path) {
+ if (!path) {
+ return CBM_LANG_DLANG;
+ }
+
+ FILE *f = cbm_fopen(path, "r");
+ if (!f) {
+ return CBM_LANG_DLANG;
+ }
+
+ char buf[CBM_SZ_4K + SKIP_ONE];
+ size_t n = fread(buf, SKIP_ONE, CBM_SZ_4K, f);
+ buf[n] = '\0';
+ (void)fclose(f);
+ /* A colon at the end of buf only ends the rule if nothing was cut off. */
+ bool whole_file = n < CBM_SZ_4K && strlen(buf) == n;
+
+ const char *p = buf;
+ if (strncmp(p, "\xEF\xBB\xBF", SLEN("\xEF\xBB\xBF")) == 0) {
+ p += SLEN("\xEF\xBB\xBF");
+ }
+ while (*p && isspace((unsigned char)*p)) {
+ p++;
+ }
+ /* A shebang or a leading D comment (including the DUB single-file
+ * "/+ dub.sdl:" recipe) is D; dep-info never starts that way. */
+ if (*p == '#' ||
+ (p[0] == '/' && (p[SKIP_ONE] == '/' || p[SKIP_ONE] == '*' || p[SKIP_ONE] == '+'))) {
+ return CBM_LANG_DLANG;
+ }
+ return is_dep_rule_line(p, whole_file) ? CBM_LANG_COUNT : CBM_LANG_DLANG;
+}
+
/* Disambiguate .cls files: shared by InterSystems ObjectScript UDL, Salesforce
* Apex and Visual Basic 6 class modules (#721). ObjectScript class files begin
* with a line of the form "Class <UppercasePackage>..."; VB6 class modules
diff --git a/tests/test_discover.c b/tests/test_discover.c
index 5e9bdd8af..033eb10c9 100644
--- a/tests/test_discover.c
+++ b/tests/test_discover.c
@@ -6,6 +6,7 @@
#include "test_framework.h"
#include "test_helpers.h"
#include "discover/discover.h"
+#include "discover/userconfig.h"
#include "foundation/platform.h"
typedef struct {
@@ -1990,6 +1991,49 @@ TEST(shebang_oversized_first_line_unindexed) {
PASS();
}
+/* ── .d: D source vs make/cargo dep-info ───────────────────────── */
+
+/* rustc/cargo dep-info (and gcc -MD, CMake .o.d) files share .d with D source
+ * but are Makefile rules the D grammar parses slowly into nothing useful, so
+ * they must not be indexed as D. */
+static const char CARGO_DEP_INFO[] = "/home/u/proj/t/debug/deps/foo-abc123.d: src/lib.rs src/a.rs\n"
+ "\n"
+ "src/lib.rs:\n"
+ "src/a.rs:\n";
+
+TEST(discover_d_dep_info_not_indexed_as_dlang) {
+ CBMLanguage dep;
+ CBMLanguage src;
+ ASSERT(shebang_probe("cbm_disc_dotd", "t/debug/deps/foo-abc123.d", CARGO_DEP_INFO, &dep));
+ ASSERT(shebang_probe("cbm_disc_dotd", "app.d", "module app;\nimport std.stdio : writeln;\n",
+ &src));
+ ASSERT_EQ(dep, CBM_LANG_COUNT);
+ ASSERT_EQ(src, CBM_LANG_DLANG);
+ PASS();
+}
+
+/* The probe runs only while .d maps to D, so a user override to another
+ * language wins. */
+TEST(discover_d_user_override_skips_dep_info_probe) {
+ char *cfg_dir = th_mktempdir("cbm_disc_dotd_uc");
+ ASSERT(cfg_dir != NULL);
+ ASSERT_EQ(th_write_file(TH_PATH(cfg_dir, ".codebase-memory.json"),
+ "{\"extra_extensions\":{\".d\":\"makefile\"}}"),
+ 0);
+ cbm_userconfig_t *cfg = cbm_userconfig_load(cfg_dir);
+ th_cleanup(cfg_dir);
+ ASSERT_NOT_NULL(cfg);
+
+ cbm_set_user_lang_config(cfg);
+ CBMLanguage lang;
+ bool ok = shebang_probe("cbm_disc_dotd_uc", "foo-abc123.d", CARGO_DEP_INFO, &lang);
+ cbm_set_user_lang_config(NULL);
+ cbm_userconfig_free(cfg);
+ ASSERT(ok);
+ ASSERT_EQ(lang, CBM_LANG_MAKEFILE);
+ PASS();
+}
+
/* ── Suite ─────────────────────────────────────────────────────── */
SUITE(discover) {
@@ -2082,6 +2126,10 @@ SUITE(discover) {
RUN_TEST(shebang_embedded_nul_unindexed);
RUN_TEST(shebang_oversized_first_line_unindexed);
+ /* .d: D source vs make/cargo dep-info */
+ RUN_TEST(discover_d_dep_info_not_indexed_as_dlang);
+ RUN_TEST(discover_d_user_override_skips_dep_info_probe);
+
/* Integration tests (cross-platform) */
RUN_TEST(discover_simple);
RUN_TEST(discover_wide_sibling_fanout_exceeds_initial_walk_stack);
diff --git a/tests/test_language.c b/tests/test_language.c
index dc2bcf7d3..d9d38429f 100644
--- a/tests/test_language.c
+++ b/tests/test_language.c
@@ -835,6 +835,55 @@ TEST(lang_cfc_default_on_read_fail) {
PASS();
}
+/* ── .d: D source vs make/cargo dep-info ───────────────────────── */
+
+/* Write content to a temp .d file and return cbm_disambiguate_d() for it. A
+ * setup failure returns -1 so neither a D nor a COUNT expectation can pass
+ * vacuously. */
+static int disambiguate_d_content(const char *content) {
+ char path[256];
+ snprintf(path, sizeof(path), "%s/test_lang_dotd.d", cbm_tmpdir());
+ if (!write_probe_file(path, content)) {
+ return -1;
+ }
+ CBMLanguage lang = cbm_disambiguate_d(path);
+ remove(path);
+ return (int)lang;
+}
+
+TEST(lang_d_dep_info_unsupported) {
+ /* cargo (absolute target), gcc -MD continuation, CMake object rule, Windows
+ * drive letter (not the rule colon), escaped space (one target, not "app"),
+ * and the GNU make manual's "foo.o foo.d : ..." (two targets, spaced colon). */
+ ASSERT_EQ(disambiguate_d_content("/home/u/t/debug/deps/foo-abc123.d: src/lib.rs src/a.rs\n"
+ "\nsrc/lib.rs:\n"),
+ CBM_LANG_COUNT);
+ ASSERT_EQ(disambiguate_d_content("foo.o: foo.c /usr/include/stdio.h \\\n"
+ " /usr/include/features.h\n"),
+ CBM_LANG_COUNT);
+ ASSERT_EQ(disambiguate_d_content("CMakeFiles/x.dir/a.cpp.o: \\\n /src/a.cpp\n"),
+ CBM_LANG_COUNT);
+ ASSERT_EQ(disambiguate_d_content("C:/b/foo.o: C:/s/foo.c \\\r\n C:/s/foo.h\r\n"),
+ CBM_LANG_COUNT);
+ ASSERT_EQ(disambiguate_d_content("out/my\\ app: main.c\n"), CBM_LANG_COUNT);
+ ASSERT_EQ(disambiguate_d_content("foo.o foo.d : foo.c defs.h\n"), CBM_LANG_COUNT);
+ PASS();
+}
+
+TEST(lang_d_source_stays_dlang) {
+ ASSERT_EQ(disambiguate_d_content("module a;\n"), CBM_LANG_DLANG);
+ ASSERT_EQ(disambiguate_d_content("import std.stdio : writeln;\n"), CBM_LANG_DLANG);
+ ASSERT_EQ(disambiguate_d_content("@safe:\nvoid f() {}\n"), CBM_LANG_DLANG);
+ /* A leading comment is D, even DUB's "/+ dub.sdl:" single-file recipe. */
+ ASSERT_EQ(disambiguate_d_content("/+ dub.sdl:\n name \"hello\"\n+/\nvoid main() {}\n"),
+ CBM_LANG_DLANG);
+ ASSERT_EQ(disambiguate_d_content("enum E : ubyte {\n a,\n}\n"), CBM_LANG_DLANG);
+ /* Default on doubt: empty or unreadable. */
+ ASSERT_EQ(disambiguate_d_content(""), CBM_LANG_DLANG);
+ ASSERT_EQ(cbm_disambiguate_d("/tmp/nonexistent_file_12345.d"), CBM_LANG_DLANG);
+ PASS();
+}
+
/* --- New languages (auto-generated) --- */
TEST(lang_ext_solidity) {
ASSERT_EQ(cbm_language_for_extension(".sol"), CBM_LANG_SOLIDITY);
@@ -1488,6 +1537,8 @@ SUITE(language) {
RUN_TEST(lang_frm_form_stays_form);
RUN_TEST(lang_res_binary_resource_unsupported);
RUN_TEST(lang_res_rescript_stays_rescript);
+ RUN_TEST(lang_d_dep_info_unsupported);
+ RUN_TEST(lang_d_source_stays_dlang);
/* Go test ports */
/* New languages */