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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
doc-links(msbuild): a group's condition is evaluated once, not per child (S1, evaluator side)
Martin Vogele3c1ea384b7f
diff --git a/src/pipeline/doc_links_msbuild.c b/src/pipeline/doc_links_msbuild.cindex ec3375709..cf17503dc 100644--- a/src/pipeline/doc_links_msbuild.c+++ b/src/pipeline/doc_links_msbuild.c@@ -557,6 +557,12 @@ typedef struct {st_component_t *owner;msb_tri_t group; /* the condition of the <PropertyGroup> being read */const msb_rec_t *hgroup; /* the <ItemGroup> being read */+ /* The <ImportGroup> being read: its condition's text (one string the+ * group's imports share) and what it came to. MSBuild evaluates a+ * group's condition once, where the group stands; evaluating it again+ * for every import cost imports x condition length. */+ const char *igroup_text;+ msb_tri_t igroup;} msb_frame_t;enum { MSB_SEEDS = 5, MSB_LIVE_OWNERS = 5 };@@ -2609,9 +2615,10 @@ static void msb_add_item(msb_eval_t *ev, const msb_rec_t *group, const msb_rec_tmsb_work(sizeof(msb_item_t));}-/* An <Import> standing in the file on top of the frame stack. */-static void msb_import(msb_eval_t *ev, int file, const msb_rec_t *r) {- msb_tri_t c = tri_and(msb_cond(ev, file, r->f[0]), msb_cond(ev, file, r->f[1]));+/* An <Import> standing in the file on top of the frame stack, under its+ * <ImportGroup>'s condition `group` (MSB_TRUE outside a group). */+static void msb_import(msb_eval_t *ev, int file, const msb_rec_t *r, msb_tri_t group) {+ msb_tri_t c = tri_and(group, msb_cond(ev, file, r->f[1]));if (c == MSB_FALSE || r->f[3]) {return; /* not taken; or an SDK's file, which is no file of the repository */}@@ -2704,9 +2711,20 @@ static void msb_pass1(msb_eval_t *ev, int file) {case 'C':ev->unevaluable++;break;- case 'I':- msb_import(ev, at, r);+ case 'I': {+ /* the group's condition once per group: its imports share the+ * text (cbm_msb_add); a legacy blob's copies are evaluated each */+ msb_tri_t group = MSB_TRUE;+ if (r->f[0]) {+ if (fr->igroup_text != r->f[0]) {+ fr->igroup_text = r->f[0];+ fr->igroup = msb_cond(ev, at, r->f[0]);+ }+ group = fr->igroup;+ }+ msb_import(ev, at, r, group); /* may push a frame: fr is not used after it */break;+ }default:break;}@@ -2846,12 +2864,32 @@ static int target_cmp(const void *a, const void *b) {return strcmp(((const cbm_msb_using_t *)a)->target, ((const cbm_msb_using_t *)b)->target);}+/* An <ItemGroup>'s condition for the items of one pass over them. MSBuild+ * evaluates it once, where the group stands, and the final properties the+ * items see do not change while they are read: evaluating it again for+ * every item cost items x condition length. */+typedef struct {+ const msb_rec_t *group;+ msb_tri_t value;+} msb_group_memo_t;++static msb_tri_t item_group_cond(msb_eval_t *ev, const msb_item_t *it, msb_group_memo_t *memo) {+ if (!it->group) {+ return MSB_TRUE;+ }+ if (memo->group != it->group) {+ memo->group = it->group;+ memo->value = msb_cond(ev, it->file, it->group->f[0]);+ }+ return memo->value;+}+/* One <Using> item with the final properties: into `inc` or `rem`. */-static void msb_using(msb_eval_t *ev, const msb_item_t *it, msb_ulist_t *inc, msb_ulist_t *rem) {+static void msb_using(msb_eval_t *ev, const msb_item_t *it, msb_ulist_t *inc, msb_ulist_t *rem,+ msb_group_memo_t *memo) {const msb_rec_t *r = it->item;msb_record();- msb_tri_t c = tri_and(msb_cond(ev, it->file, it->group ? it->group->f[0] : NULL),- msb_cond(ev, it->file, r->f[0]));+ msb_tri_t c = tri_and(item_group_cond(ev, it, memo), msb_cond(ev, it->file, r->f[0]));if (c == MSB_FALSE) {return;}@@ -3111,8 +3149,9 @@ bool cbm_msb_eval(const cbm_msb_t *m, const char *project_rel, cbm_msb_result_tfor (int i = 0; implicit && implicit[i]; i++) {ulist_add(&ev, &inc, 'n', "", implicit[i]);}+ msb_group_memo_t memo = {0};for (int i = 0; i < ev.nitems; i++) {- msb_using(&ev, &ev.items[i], &inc, &rem);+ msb_using(&ev, &ev.items[i], &inc, &rem, &memo);/* All four expansions remain live until aliases and targets* have been copied into the persistent evaluation arena. */cbm_arena_reset(&ev.scratch);@@ -3435,8 +3474,9 @@ static bool apply_targets(cbm_msb_eval_context_t *context, msb_eval_t *ev, int rstatic void eval_items(msb_eval_t *ev, const msb_eval_t *owner, msb_ulist_t *inc,msb_ulist_t *rem) {+ msb_group_memo_t memo = {0};for (int i = 0; !ev->oom && i < owner->nitems; i++) {- msb_using(ev, &owner->items[i], inc, rem);+ msb_using(ev, &owner->items[i], inc, rem, &memo);cbm_arena_reset(&ev->scratch);}}@@ -3682,6 +3722,7 @@ bool cbm_msb_eval_context_eval(cbm_msb_eval_context_t *context, const char *projstatic void st_eval_rope(msb_eval_t *ev, st_rope_t *rope, msb_ulist_t *inc, msb_ulist_t *rem) {st_rope_t *stack[CBM_SZ_64];int depth = 0;+ msb_group_memo_t memo = {0};while (!ev->oom && (rope || depth)) {if (!rope) {rope = stack[--depth];@@ -3689,7 +3730,7 @@ static void st_eval_rope(msb_eval_t *ev, st_rope_t *rope, msb_ulist_t *inc, msb_}msb_work(1);if (!rope->left) {- msb_using(ev, &rope->item, inc, rem);+ msb_using(ev, &rope->item, inc, rem, &memo);cbm_arena_reset(&ev->scratch);rope = NULL;} else {diff --git a/tests/test_doc_mentions.c b/tests/test_doc_mentions.cindex bcad1ed4f..e5fb2024a 100644--- a/tests/test_doc_mentions.c+++ b/tests/test_doc_mentions.c@@ -3483,6 +3483,140 @@ TEST(doc_mentions_msbuild_import_group_roundtrip) {PASS();}+/* The evaluator side of the same product: an <ImportGroup>'s condition is+ * evaluated once, where the group stands, not again for every import in it.+ * Doubling both the imports and the condition's length at most doubles the+ * evaluator's work; evaluated per import, it would quadruple. */+TEST(doc_mentions_msbuild_import_group_condition_work) {+ enum { IMPORTS = 100, TERMS = 100, CAP = 64 * 1024 };+ /* Flag is defined (an undefined property is unknown); no term holds */+ static const char term[] = "'$(Flag)' == 'aaaaaaaaaaaaaaaa'";+ uint64_t work[2] = {0};+ char *xml = malloc(CAP);+ ASSERT_NOT_NULL(xml);+ for (int sample = 0; sample < 2; sample++) {+ int imports = IMPORTS << sample;+ int terms = TERMS << sample;+ size_t used = (size_t)snprintf(xml, CAP,+ "<Project><PropertyGroup><Flag>b</Flag></PropertyGroup>"+ "<ImportGroup Condition=\"");+ for (int i = 0; i < terms; i++) {+ used += (size_t)snprintf(xml + used, CAP - used, "%s%s", i ? " or " : "", term);+ }+ used += (size_t)snprintf(xml + used, CAP - used, "\">");+ for (int i = 0; i < imports; i++) {+ used += (size_t)snprintf(xml + used, CAP - used, "<Import Project=\"X.props\"/>");+ }+ used += (size_t)snprintf(xml + used, CAP - used,+ "</ImportGroup><ItemGroup><Using Include=\"Kept\"/>"+ "</ItemGroup></Project>");+ ASSERT_LT(used, (size_t)CAP);+ cbm_msb_t *m = cbm_msb_new();+ ASSERT_NOT_NULL(m);+ ASSERT_TRUE(dm_msb_add_xml(m, "App.csproj", xml));+ ASSERT_TRUE(dm_msb_add_xml(+ m, "X.props",+ "<Project><ItemGroup><Using Include=\"Not.Taken\"/></ItemGroup></Project>"));+ cbm_msb_test_cost_reset();+ cbm_msb_result_t r;+ ASSERT_TRUE(cbm_msb_eval(m, "App.csproj", &r));+ work[sample] = cbm_msb_test_work();+ ASSERT_TRUE(dm_has_using(&r, 'n', "Kept"));+ ASSERT_FALSE(dm_has_using(&r, 'n', "Not.Taken"));+ ASSERT_EQ(r.count, 1);+ ASSERT_FALSE(r.open);+ ASSERT_EQ(r.unevaluable, 0);+ cbm_msb_result_free(&r);+ cbm_msb_free(m);+ }+ free(xml);+ fprintf(stderr, "msbuild import group condition work: %llu -> %llu\n",+ (unsigned long long)work[0], (unsigned long long)work[1]);+ ASSERT_GT(work[0], 0);+ ASSERT_LTE(work[1], 2 * work[0]);+ PASS();+}++/* The same for an <ItemGroup>: its condition is evaluated once for the+ * items of a pass, not again for every <Using> in it. */+TEST(doc_mentions_msbuild_item_group_condition_work) {+ enum { USINGS = 100, TERMS = 100, CAP = 64 * 1024 };+ static const char term[] = "'$(Flag)' == 'aaaaaaaaaaaaaaaa'";+ uint64_t work[2] = {0};+ char *xml = malloc(CAP);+ ASSERT_NOT_NULL(xml);+ for (int sample = 0; sample < 2; sample++) {+ int usings = USINGS << sample;+ int terms = TERMS << sample;+ size_t used = (size_t)snprintf(xml, CAP,+ "<Project><PropertyGroup><Flag>b</Flag></PropertyGroup>"+ "<ItemGroup Condition=\"");+ for (int i = 0; i < terms; i++) {+ used += (size_t)snprintf(xml + used, CAP - used, "%s%s", i ? " or " : "", term);+ }+ used += (size_t)snprintf(xml + used, CAP - used, "\">");+ for (int i = 0; i < usings; i++) {+ used +=+ (size_t)snprintf(xml + used, CAP - used, "<Using Include=\"Not.Taken%04d\"/>", i);+ }+ used += (size_t)snprintf(xml + used, CAP - used,+ "</ItemGroup><ItemGroup><Using Include=\"Kept\"/>"+ "</ItemGroup></Project>");+ ASSERT_LT(used, (size_t)CAP);+ cbm_msb_t *m = cbm_msb_new();+ ASSERT_NOT_NULL(m);+ ASSERT_TRUE(dm_msb_add_xml(m, "App.csproj", xml));+ cbm_msb_test_cost_reset();+ cbm_msb_result_t r;+ ASSERT_TRUE(cbm_msb_eval(m, "App.csproj", &r));+ work[sample] = cbm_msb_test_work();+ ASSERT_TRUE(dm_has_using(&r, 'n', "Kept"));+ ASSERT_EQ(r.count, 1);+ ASSERT_FALSE(r.open);+ ASSERT_EQ(r.unevaluable, 0);+ cbm_msb_result_free(&r);+ cbm_msb_free(m);+ }+ free(xml);+ fprintf(stderr, "msbuild item group condition work: %llu -> %llu\n",+ (unsigned long long)work[0], (unsigned long long)work[1]);+ ASSERT_GT(work[0], 0);+ ASSERT_LTE(work[1], 2 * work[0]);+ PASS();+}++/* MSBuild evaluates an <ImportGroup>'s condition once, before its imports:+ * a property the group's first import sets does not take the second import+ * away. */+TEST(doc_mentions_msbuild_import_group_condition_once) {+ const dm_project_file_t files[] = {+ /* a property no file defines is unknown (the SDK or the+ * environment may set it): Stop is defined first */+ {"App.csproj", "<Project><PropertyGroup><Stop>no</Stop></PropertyGroup>"+ "<ImportGroup Condition=\"'$(Stop)' == 'no'\">"+ "<Import Project=\"First.props\"/><Import Project=\"Second.props\"/>"+ "</ImportGroup>"+ "<ImportGroup Condition=\"'$(Stop)' == 'no'\">"+ "<Import Project=\"Third.props\"/></ImportGroup></Project>"},+ {"First.props", "<Project><PropertyGroup><Stop>yes</Stop></PropertyGroup>"+ "<ItemGroup><Using Include=\"From.First\"/></ItemGroup></Project>"},+ {"Second.props",+ "<Project><ItemGroup><Using Include=\"From.Second\"/></ItemGroup></Project>"},+ {"Third.props",+ "<Project><ItemGroup><Using Include=\"From.Third\"/></ItemGroup></Project>"},+ };+ cbm_msb_result_t r;+ ASSERT_TRUE(dm_msb_eval(files, 4, "App.csproj", &r));+ ASSERT_TRUE(dm_has_using(&r, 'n', "From.First"));+ ASSERT_TRUE(dm_has_using(&r, 'n', "From.Second"));+ /* a later group with the same text is evaluated where it stands */+ ASSERT_FALSE(dm_has_using(&r, 'n', "From.Third"));+ ASSERT_EQ(r.count, 2);+ ASSERT_FALSE(r.open);+ cbm_msb_result_free(&r);+ PASS();+}+TEST(doc_mentions_msbuild_legacy_import_blob) {cbm_msb_t *m = cbm_msb_new();ASSERT_NOT_NULL(m);@@ -9837,6 +9971,9 @@ SUITE(doc_mentions) {RUN_TEST(doc_mentions_msbuild_blob);RUN_TEST(doc_mentions_msbuild_import_group_blob_growth);RUN_TEST(doc_mentions_msbuild_import_group_roundtrip);+ RUN_TEST(doc_mentions_msbuild_import_group_condition_work);+ RUN_TEST(doc_mentions_msbuild_item_group_condition_work);+ RUN_TEST(doc_mentions_msbuild_import_group_condition_once);RUN_TEST(doc_mentions_msbuild_legacy_import_blob);RUN_TEST(doc_mentions_msbuild_bad_import_group_blob);RUN_TEST(doc_mentions_msbuild_imports);