@@ -1035,6 +1035,168 @@ static const cbm_gbuf_node_t *calls_field_target(cbm_pipeline_ctx_t *ctx, const
10351035 return target_node ;
10361036}
10371037
1038+ /* ── Java build roots (see pipeline_internal.h) ─────────────────────
1039+ *
1040+ * This thread's answers, keyed by the repository-relative directory and
1041+ * valued by its build root ("" = the repository root, or no build file
1042+ * anywhere above). tl_br_repo names the repository they belong to. */
1043+ static CBM_TLS CBMHashTable * tl_br_cache ;
1044+ static CBM_TLS char * tl_br_repo ;
1045+
1046+ static void br_free_entry (const char * key , void * value , void * ud ) {
1047+ (void )ud ;
1048+ free ((void * )key );
1049+ free (value );
1050+ }
1051+
1052+ void cbm_pipeline_build_root_cache_clear (void ) {
1053+ if (tl_br_cache ) {
1054+ cbm_ht_foreach (tl_br_cache , br_free_entry , NULL );
1055+ cbm_ht_free (tl_br_cache );
1056+ tl_br_cache = NULL ;
1057+ }
1058+ free (tl_br_repo );
1059+ tl_br_repo = NULL ;
1060+ }
1061+
1062+ static bool br_dir_has_build_file (const char * repo_path , const char * dir ) {
1063+ static const char * const files [] = {"settings.gradle" , "settings.gradle.kts" , "build.gradle" ,
1064+ "build.gradle.kts" , "pom.xml" };
1065+ char path [CBM_SZ_4K ];
1066+ for (size_t i = 0 ; i < sizeof (files ) / sizeof (files [0 ]); i ++ ) {
1067+ int n = snprintf (path , sizeof (path ), "%s%s%s/%s" , repo_path , dir [0 ] ? "/" : "" , dir ,
1068+ files [i ]);
1069+ if (n <= 0 || (size_t )n >= sizeof (path )) {
1070+ continue ;
1071+ }
1072+ cbm_path_info_t info ;
1073+ if (cbm_path_info_utf8 (path , & info ) == CBM_PATH_INFO_OK && info .is_regular ) {
1074+ return true;
1075+ }
1076+ }
1077+ return false;
1078+ }
1079+
1080+ /* The build root of repository-relative directory `dir` (cached). NULL only
1081+ * when memory runs out. */
1082+ static const char * br_root_of_dir (const char * repo_path , const char * dir ) {
1083+ if (!tl_br_repo || strcmp (tl_br_repo , repo_path ) != 0 ) {
1084+ cbm_pipeline_build_root_cache_clear ();
1085+ tl_br_repo = strdup (repo_path );
1086+ tl_br_cache = cbm_ht_create (CBM_SZ_256 );
1087+ if (!tl_br_repo || !tl_br_cache ) {
1088+ cbm_pipeline_build_root_cache_clear ();
1089+ return NULL ;
1090+ }
1091+ }
1092+ const char * hit = cbm_ht_get (tl_br_cache , dir );
1093+ if (hit ) {
1094+ return hit ;
1095+ }
1096+ const char * root = NULL ;
1097+ char * owned = NULL ;
1098+ if (br_dir_has_build_file (repo_path , dir )) {
1099+ owned = strdup (dir );
1100+ } else if (dir [0 ]) {
1101+ char parent [CBM_SZ_4K ];
1102+ snprintf (parent , sizeof (parent ), "%s" , dir );
1103+ char * slash = strrchr (parent , '/' );
1104+ if (slash ) {
1105+ * slash = '\0' ;
1106+ } else {
1107+ parent [0 ] = '\0' ;
1108+ }
1109+ root = br_root_of_dir (repo_path , parent );
1110+ owned = root ? strdup (root ) : NULL ;
1111+ } else {
1112+ owned = strdup ("" );
1113+ }
1114+ char * key = strdup (dir );
1115+ if (!owned || !key ) {
1116+ free (owned );
1117+ free (key );
1118+ return NULL ;
1119+ }
1120+ cbm_ht_set (tl_br_cache , key , owned );
1121+ return owned ;
1122+ }
1123+
1124+ /* Build root of a repository-relative file path, copied into out. */
1125+ static bool br_root_of_file (const char * repo_path , const char * rel , char * out , size_t out_size ) {
1126+ char dir [CBM_SZ_4K ];
1127+ int n = snprintf (dir , sizeof (dir ), "%s" , rel );
1128+ if (n < 0 || (size_t )n >= sizeof (dir )) {
1129+ return false;
1130+ }
1131+ char * slash = strrchr (dir , '/' );
1132+ if (slash ) {
1133+ * slash = '\0' ;
1134+ } else {
1135+ dir [0 ] = '\0' ;
1136+ }
1137+ const char * root = br_root_of_dir (repo_path , dir );
1138+ if (!root ) {
1139+ return false;
1140+ }
1141+ n = snprintf (out , out_size , "%s" , root );
1142+ return n >= 0 && (size_t )n < out_size ;
1143+ }
1144+
1145+ const cbm_gbuf_node_t * cbm_pipeline_java_same_build_root (const cbm_gbuf_t * gbuf ,
1146+ const char * project ,
1147+ const char * repo_path , CBMLanguage lang ,
1148+ const char * caller_rel ,
1149+ const cbm_gbuf_node_t * target ) {
1150+ if (lang != CBM_LANG_JAVA || !gbuf || !project || !repo_path || !repo_path [0 ] ||
1151+ !caller_rel || !target || !target -> file_path || !target -> qualified_name ||
1152+ !target -> name || !target -> label ) {
1153+ return target ;
1154+ }
1155+ char croot [CBM_SZ_4K ];
1156+ char troot [CBM_SZ_4K ];
1157+ if (!br_root_of_file (repo_path , caller_rel , croot , sizeof (croot )) ||
1158+ !br_root_of_file (repo_path , target -> file_path , troot , sizeof (troot )) ||
1159+ strcmp (croot , troot ) == 0 ) {
1160+ return target ;
1161+ }
1162+ /* The target's path below its build root, and its QN below the root's
1163+ * folder QN: both must be moved together. */
1164+ size_t tlen = strlen (troot );
1165+ const char * rest = target -> file_path + tlen ;
1166+ if (tlen > 0 ) {
1167+ if (strncmp (target -> file_path , troot , tlen ) != 0 || * rest != '/' ) {
1168+ return target ;
1169+ }
1170+ rest ++ ;
1171+ }
1172+ char * tprefix = cbm_pipeline_fqn_folder (project , troot );
1173+ char * cprefix = cbm_pipeline_fqn_folder (project , croot );
1174+ const cbm_gbuf_node_t * better = target ;
1175+ size_t plen = tprefix ? strlen (tprefix ) : 0 ;
1176+ if (tprefix && cprefix && plen > 0 && strncmp (target -> qualified_name , tprefix , plen ) == 0 &&
1177+ target -> qualified_name [plen ] == '.' ) {
1178+ const char * tail = target -> qualified_name + plen ;
1179+ size_t need = strlen (cprefix ) + strlen (tail ) + SKIP_ONE ;
1180+ char * qn = malloc (need );
1181+ if (qn ) {
1182+ snprintf (qn , need , "%s%s" , cprefix , tail );
1183+ const cbm_gbuf_node_t * cand = cbm_gbuf_find_by_qn (gbuf , qn );
1184+ char want [CBM_SZ_4K ];
1185+ int n = snprintf (want , sizeof (want ), "%s%s%s" , croot , croot [0 ] ? "/" : "" , rest );
1186+ if (cand && cand != target && n > 0 && (size_t )n < sizeof (want ) && cand -> file_path &&
1187+ strcmp (cand -> file_path , want ) == 0 && cand -> name &&
1188+ strcmp (cand -> name , target -> name ) == 0 && cand -> label &&
1189+ strcmp (cand -> label , target -> label ) == 0 ) {
1190+ better = cand ;
1191+ }
1192+ free (qn );
1193+ }
1194+ }
1195+ free (tprefix );
1196+ free (cprefix );
1197+ return better ;
1198+ }
1199+
10381200static bool calls_emit_lsp_target (cbm_pipeline_ctx_t * ctx , const CBMCall * call ,
10391201 const CBMResolvedCall * lsp , bool allow_tail , CBMLanguage lang ,
10401202 const CBMImportArray * imports , const cbm_gbuf_node_t * source_node ,
@@ -1051,6 +1213,9 @@ static bool calls_emit_lsp_target(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
10511213 lsp -> callee_qn , allow_tail )
10521214 : cbm_pipeline_lsp_target_node (ctx -> gbuf , ctx -> project_name ,
10531215 lsp -> callee_qn , allow_tail );
1216+ /* The caller's own build module first. MUST match pass_parallel.c. */
1217+ target_node = cbm_pipeline_java_same_build_root (ctx -> gbuf , ctx -> project_name , ctx -> repo_path ,
1218+ lang , source_node -> file_path , target_node );
10541219 if (!target_node || source_node -> id == target_node -> id ) {
10551220 return false;
10561221 }
@@ -1370,6 +1535,12 @@ static int resolve_single_call(cbm_pipeline_ctx_t *ctx, CBMCall *call, const CBM
13701535 }
13711536
13721537 const cbm_gbuf_node_t * target_node = cbm_gbuf_find_by_qn (ctx -> gbuf , res .qualified_name );
1538+ /* The caller's own build module first. MUST match pass_parallel.c. */
1539+ target_node = cbm_pipeline_java_same_build_root (ctx -> gbuf , ctx -> project_name , ctx -> repo_path ,
1540+ lang , source_node -> file_path , target_node );
1541+ if (target_node ) {
1542+ res .qualified_name = target_node -> qualified_name ;
1543+ }
13731544 if (!target_node || source_node -> id == target_node -> id ) {
13741545 return 0 ;
13751546 }
@@ -1523,6 +1694,8 @@ static CBMFileResult *calls_get_or_extract(cbm_pipeline_ctx_t *ctx, int idx,
15231694
15241695int cbm_pipeline_pass_calls (cbm_pipeline_ctx_t * ctx , const cbm_file_info_t * files , int file_count ) {
15251696 cbm_log_info ("pass.start" , "pass" , "calls" , "files" , itoa_log (file_count ));
1697+ /* Build files may have changed since the last run on this thread. */
1698+ cbm_pipeline_build_root_cache_clear ();
15261699
15271700 /* ObjectScript: build the method-return-type table from the definitions
15281701 * already in the graph buffer so `Set x = obj.Method()` can resolve x's
0 commit comments