diff --git a/internal/cbm/extract_imports.c b/internal/cbm/extract_imports.c index 62985df7c7..548d786fdc 100644 --- a/internal/cbm/extract_imports.c +++ b/internal/cbm/extract_imports.c @@ -27,6 +27,7 @@ static void parse_go_imports(CBMExtractCtx *ctx); static void parse_python_imports(CBMExtractCtx *ctx); static void parse_es_imports(CBMExtractCtx *ctx); static void parse_java_imports(CBMExtractCtx *ctx); +static void parse_scala_imports(CBMExtractCtx *ctx); static void parse_rust_imports(CBMExtractCtx *ctx); static void parse_c_imports(CBMExtractCtx *ctx); static void parse_ruby_imports(CBMExtractCtx *ctx); @@ -2458,6 +2459,146 @@ static void parse_generic_imports(CBMExtractCtx *ctx, const char *node_type) { ts_tree_cursor_delete(&cursor); } +// --- Scala imports --- + +static char *scala_trim_span(CBMArena *a, const char *start, const char *end) { + while (start < end && isspace((unsigned char)*start)) { + start++; + } + while (end > start && isspace((unsigned char)end[-1])) { + end--; + } + return cbm_arena_strndup(a, start, (size_t)(end - start)); +} + +static void scala_emit_import(CBMExtractCtx *ctx, const char *path, const char *local) { + if (!path || !path[0] || !local || !local[0]) { + return; + } + CBMImport imp = {.local_name = cbm_arena_strdup(ctx->arena, local), + .module_path = cbm_arena_strdup(ctx->arena, path)}; + cbm_imports_push(&ctx->result->imports, ctx->arena, imp); +} + +static void scala_parse_selector(CBMExtractCtx *ctx, const char *prefix, const char *start, + const char *end) { + char *selector = scala_trim_span(ctx->arena, start, end); + if (!selector || !selector[0] || + (strncmp(selector, "given", 5) == 0 && + (selector[5] == '\0' || isspace((unsigned char)selector[5])))) { + return; + } + if (strcmp(selector, "*") == 0 || strcmp(selector, "_") == 0) { + scala_emit_import(ctx, prefix, "*"); + return; + } + + char *rename = strstr(selector, "=>"); + size_t op_len = 2; + if (!rename) { + rename = strstr(selector, " as "); + op_len = 4; + } + char *name = selector; + char *local = selector; + if (rename) { + local = scala_trim_span(ctx->arena, rename + op_len, selector + strlen(selector)); + name = scala_trim_span(ctx->arena, selector, rename); + if (strcmp(local, "_") == 0 || strcmp(local, "*") == 0) { + return; // Scala 2 hidden selector: `Name => _` + } + } + if (!name[0] || !local[0]) { + return; + } + const char *path = + prefix && prefix[0] ? cbm_arena_sprintf(ctx->arena, "%s.%s", prefix, name) : name; + scala_emit_import(ctx, path, local); +} + +static void scala_parse_import_expr(CBMExtractCtx *ctx, const char *start, const char *end) { + char *expr = scala_trim_span(ctx->arena, start, end); + if (!expr || !expr[0]) { + return; + } + char *open = strchr(expr, '{'); + char *close = strrchr(expr, '}'); + if (open && close && close > open) { + char *prefix = scala_trim_span(ctx->arena, expr, open); + size_t plen = strlen(prefix); + while (plen > 0 && (prefix[plen - 1] == '.' || isspace((unsigned char)prefix[plen - 1]))) { + prefix[--plen] = '\0'; + } + const char *part = open + 1; + for (const char *p = part;; p++) { + if (p == close || *p == ',') { + scala_parse_selector(ctx, prefix, part, p); + if (p == close) { + break; + } + part = p + 1; + } + } + return; + } + + char *rename = strstr(expr, " as "); + if (rename) { + char *path = scala_trim_span(ctx->arena, expr, rename); + char *local = scala_trim_span(ctx->arena, rename + 4, expr + strlen(expr)); + scala_emit_import(ctx, path, local); + return; + } + size_t len = strlen(expr); + if ((len > 2 && strcmp(expr + len - 2, ".*") == 0) || + (len > 2 && strcmp(expr + len - 2, "._") == 0)) { + expr[len - 2] = '\0'; + scala_emit_import(ctx, expr, "*"); + return; + } + if (len > 6 && strcmp(expr + len - 6, ".given") == 0) { + expr[len - 6] = '\0'; + scala_emit_import(ctx, expr, "*"); + return; + } + scala_emit_import(ctx, expr, path_last(ctx->arena, expr)); +} + +static void parse_scala_imports(CBMExtractCtx *ctx) { + TSTreeCursor cursor = ts_tree_cursor_new(ctx->root); + if (!ts_tree_cursor_goto_first_child(&cursor)) { + ts_tree_cursor_delete(&cursor); + return; + } + do { + TSNode node = ts_tree_cursor_current_node(&cursor); + if (strcmp(ts_node_type(node), "import_declaration") != 0) { + continue; + } + char *text = cbm_node_text(ctx->arena, node, ctx->source); + if (!text || strncmp(text, "import", 6) != 0) { + continue; + } + const char *part = text + 6; + int brace_depth = 0; + for (const char *p = part;; p++) { + if (*p == '{') { + brace_depth++; + } else if (*p == '}') { + brace_depth--; + } + if (*p == '\0' || (*p == ',' && brace_depth == 0)) { + scala_parse_import_expr(ctx, part, p); + if (*p == '\0') { + break; + } + part = p + 1; + } + } + } while (ts_tree_cursor_goto_next_sibling(&cursor)); + ts_tree_cursor_delete(&cursor); +} + // --- Kotlin imports --- // tree-sitter-kotlin nests imports: source_file -> import_list -> import_header*. // parse_generic_imports only scans the DIRECT children of root, and "import" is @@ -3133,6 +3274,37 @@ static void parse_embedded_imports(CBMExtractCtx *ctx) { } } +// Scala chains braceless clauses: `package a` followed by `package b` puts the +// file's symbols in a.b. Append every directly following braceless +// package_clause to the namespace already recorded from the first one. A +// braced clause (`package b { ... }`) scopes only its body and is left alone. +static void join_chained_scala_packages(CBMExtractCtx *ctx, TSTreeCursor *cursor) { + while (ts_tree_cursor_goto_next_sibling(cursor)) { + TSNode next = ts_tree_cursor_current_node(cursor); + const char *next_kind = ts_node_type(next); + if (strcmp(next_kind, "comment") == 0 || strcmp(next_kind, "block_comment") == 0) { + continue; + } + if (strcmp(next_kind, "package_clause") != 0) { + return; + } + TSNode body = next; + if (find_first_descendant_of(next, "template_body", &body)) { + return; + } + TSNode name_node = next; + if (!find_first_descendant_of(next, "package_identifier", &name_node)) { + return; + } + char *segment = cbm_node_text(ctx->arena, name_node, ctx->source); + if (!segment || !segment[0]) { + return; + } + ctx->result->namespace_name = + cbm_arena_sprintf(ctx->arena, "%s.%s", ctx->result->namespace_name, segment); + } +} + // --- Namespace / package declaration capture --- // Java/Kotlin/C#/PHP put the file's symbols inside a namespace/package whose // name is NOT reflected in the path-based QN scheme. Capturing it lets the @@ -3144,6 +3316,7 @@ static void capture_namespace_decl(CBMExtractCtx *ctx) { "file_scoped_namespace_declaration", // C# 10 "package_declaration", // Java / Kotlin "package_header", // Kotlin + "package_clause", // Scala "namespace_definition", // PHP NULL}; TSTreeCursor cursor = ts_tree_cursor_new(ctx->root); @@ -3160,6 +3333,7 @@ static void capture_namespace_decl(CBMExtractCtx *ctx) { // The namespace name is the first qualified_name / scoped_identifier / // namespace_name / identifier descendant. static const char *name_kinds[] = {"qualified_name", + "package_identifier", "scoped_identifier", "namespace_name", "identifier", @@ -3177,6 +3351,9 @@ static void capture_namespace_decl(CBMExtractCtx *ctx) { } } if (ctx->result->namespace_name) { + if (strcmp(kind, "package_clause") == 0) { + join_chained_scala_packages(ctx, &cursor); + } break; } } while (ts_tree_cursor_goto_next_sibling(&cursor)); @@ -4492,6 +4669,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) { switch (ctx->language) { case CBM_LANG_JAVA: case CBM_LANG_KOTLIN: + case CBM_LANG_SCALA: case CBM_LANG_CSHARP: case CBM_LANG_PHP: capture_namespace_decl(ctx); @@ -4519,7 +4697,7 @@ void cbm_extract_imports(CBMExtractCtx *ctx) { parse_kotlin_imports(ctx); break; case CBM_LANG_SCALA: - parse_generic_imports(ctx, "import_declaration"); + parse_scala_imports(ctx); break; case CBM_LANG_CSHARP: parse_csharp_imports(ctx); diff --git a/scripts/memory-core-baseline.txt b/scripts/memory-core-baseline.txt index 67c3980c3f..23f4744af9 100644 --- a/scripts/memory-core-baseline.txt +++ b/scripts/memory-core-baseline.txt @@ -60,7 +60,7 @@ src/pipeline/pass_calls.c 22 src/pipeline/pass_compile_commands.c 17 src/pipeline/pass_configlink.c 3 src/pipeline/pass_cross_repo.c 8 -src/pipeline/pass_definitions.c 11 +src/pipeline/pass_definitions.c 9 src/pipeline/pass_enrichment.c 15 src/pipeline/pass_ensemble_routing.c 10 src/pipeline/pass_githistory.c 27 @@ -68,7 +68,7 @@ src/pipeline/pass_importance.c 4 src/pipeline/pass_infrascan.c 2 src/pipeline/pass_k8s.c 6 src/pipeline/pass_lsp_cross.c 49 -src/pipeline/pass_parallel.c 31 +src/pipeline/pass_parallel.c 29 src/pipeline/pass_pkgmap.c 80 src/pipeline/pass_semantic.c 3 src/pipeline/pass_tests.c 3 diff --git a/src/pipeline/pass_calls.c b/src/pipeline/pass_calls.c index 4430e9d440..2f914d0744 100644 --- a/src/pipeline/pass_calls.c +++ b/src/pipeline/pass_calls.c @@ -133,7 +133,7 @@ static const cbm_gbuf_node_t *pc_find_by_computed_qn(const cbm_gbuf_t *gbuf, con return n; } -static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path, +static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path, CBMLanguage language, const CBMFileResult *result, const char ***out_keys, const char ***out_vals, int *out_count) { *out_keys = NULL; @@ -157,7 +157,7 @@ static int build_import_map(cbm_pipeline_ctx_t *ctx, const char *rel_path, continue; } const cbm_gbuf_node_t *target = - cbm_pipeline_resolve_import_node(ctx, rel_path, file_qn, imp, NULL); + cbm_pipeline_resolve_import_node(ctx, rel_path, file_qn, language, imp, NULL, NULL); if (!target) { continue; } @@ -1614,7 +1614,7 @@ int cbm_pipeline_pass_calls(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *file const char **imp_keys = NULL; const char **imp_vals = NULL; int imp_count = 0; - build_import_map(ctx, rel, result, &imp_keys, &imp_vals, &imp_count); + build_import_map(ctx, rel, files[i].language, result, &imp_keys, &imp_vals, &imp_count); cbm_php_vendor_names_t php_vendor; cbm_pipeline_php_vendor_names_build(ctx->gbuf, ctx->project_name, rel, result, &php_vendor); @@ -1790,7 +1790,8 @@ void cbm_pipeline_pass_fastapi_depends(cbm_pipeline_ctx_t *ctx, const cbm_file_i const char **imp_keys = NULL; const char **imp_vals = NULL; int imp_count = 0; - build_import_map(ctx, files[i].rel_path, result, &imp_keys, &imp_vals, &imp_count); + build_import_map(ctx, files[i].rel_path, files[i].language, result, &imp_keys, &imp_vals, + &imp_count); for (int d = 0; d < result->defs.count; d++) { CBMDefinition *def = &result->defs.items[d]; diff --git a/src/pipeline/pass_definitions.c b/src/pipeline/pass_definitions.c index 11f851fbff..ae0cf88cbe 100644 --- a/src/pipeline/pass_definitions.c +++ b/src/pipeline/pass_definitions.c @@ -687,7 +687,9 @@ int cbm_pipeline_create_env_configures_for_file(cbm_pipeline_ctx_t *ctx, /* Create IMPORTS edges for one file's imports. Mirrors the resolution * logic in pass_parallel.c register_and_link_def — keep the two in sync. */ static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result, - const char *rel, CBMHashTable *namespace_map) { + const char *rel, CBMLanguage language, + CBMHashTable *namespace_map, + const cbm_scala_index_t *scala_index) { int count = 0; char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__"); const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn); @@ -704,8 +706,8 @@ static int create_import_edges_for_file(cbm_pipeline_ctx_t *ctx, const CBMFileRe if (!imp->module_path) { continue; } - const cbm_gbuf_node_t *target = - cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map); + const cbm_gbuf_node_t *target = cbm_pipeline_resolve_import_node( + ctx, rel, file_qn, language, imp, namespace_map, scala_index); if (target && target->id != source_node->id) { char esc_ln[CBM_SZ_128]; cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : ""); @@ -1182,7 +1184,8 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t * resolve to defs already in the graph, but the file's * own defs are now persisted before the lookup. No namespace * map is available without the cache (single-file scope). */ - total_imports += create_import_edges_for_file(ctx, result, rel, NULL); + total_imports += + create_import_edges_for_file(ctx, result, rel, files[i].language, NULL, NULL); create_channel_edges_for_file(ctx, result, rel); cbm_pipeline_diagram_arrows(ctx, result); cbm_pipeline_create_env_configures_for_file(ctx, result, rel); @@ -1204,16 +1207,19 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t if (local_cache) { /* Build a namespace/package → File-QN map so that namespace imports * (C# `using`, Java/Kotlin `import`, PHP `use`) resolve to the file - * that declares the namespace. */ - const char **rels = (const char **)calloc((size_t)file_count, sizeof(char *)); - if (rels) { + * that declares the namespace, and the Scala top-level index. */ + CBMHashTable *namespace_map = NULL; + cbm_scala_index_t *scala_index = NULL; + const char **namespaces = + cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)file_count * sizeof(char *)); + if (namespaces) { for (int i = 0; i < file_count; i++) { - rels[i] = files[i].rel_path; + namespaces[i] = local_cache[i] ? local_cache[i]->namespace_name : NULL; } + cbm_pipeline_import_maps_build(ctx, files, file_count, namespaces, &namespace_map, + &scala_index); + cbm_free(CBM_MEM_CLASS_OTHER, namespaces); } - CBMHashTable *namespace_map = - cbm_pipeline_namespace_map_build(ctx->project_name, local_cache, rels, file_count); - free(rels); for (int i = 0; i < file_count; i++) { if (cbm_pipeline_check_cancel(ctx)) { break; @@ -1222,13 +1228,15 @@ int cbm_pipeline_pass_definitions(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t if (!result) { continue; } - total_imports += - create_import_edges_for_file(ctx, result, files[i].rel_path, namespace_map); + total_imports += create_import_edges_for_file( + ctx, result, files[i].rel_path, files[i].language, namespace_map, scala_index); create_channel_edges_for_file(ctx, result, files[i].rel_path); cbm_pipeline_diagram_arrows(ctx, result); cbm_pipeline_create_env_configures_for_file(ctx, result, files[i].rel_path); } - /* Publish instead of free — see the twin in pass_parallel.c (#1355). */ + /* Publish instead of free — see the twin in pass_parallel.c (#1355). + * Only the Scala index is pass-local. */ + cbm_pipeline_import_maps_free(NULL, scala_index); cbm_pipeline_namespace_map_free(cbm_pipeline_get_nsmap()); cbm_pipeline_set_nsmap(namespace_map); if (owns_local_cache) { diff --git a/src/pipeline/pass_parallel.c b/src/pipeline/pass_parallel.c index 71b5b684ff..21204224a7 100644 --- a/src/pipeline/pass_parallel.c +++ b/src/pipeline/pass_parallel.c @@ -1900,7 +1900,8 @@ static void pp_add_file_doc(const cbm_gbuf_node_t *file_node, const char *doc) { /* Create IMPORTS edges for one file's imports (parallel path). */ static int create_imports_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *result, - const char *rel, CBMHashTable *namespace_map) { + const char *rel, CBMLanguage language, CBMHashTable *namespace_map, + const cbm_scala_index_t *scala_index) { int count = 0; char *file_qn = cbm_pipeline_fqn_compute(ctx->project_name, rel, "__file__"); const cbm_gbuf_node_t *source_node = cbm_gbuf_find_by_qn(ctx->gbuf, file_qn); @@ -1917,8 +1918,8 @@ static int create_imports_edges(cbm_pipeline_ctx_t *ctx, const CBMFileResult *re if (!imp->module_path) { continue; } - const cbm_gbuf_node_t *target = - cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map); + const cbm_gbuf_node_t *target = cbm_pipeline_resolve_import_node( + ctx, rel, file_qn, language, imp, namespace_map, scala_index); if (target && target->id != source_node->id) { char esc_ln[CBM_SZ_128]; cbm_json_escape(esc_ln, sizeof(esc_ln), imp->local_name ? imp->local_name : ""); @@ -1986,36 +1987,30 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t int imports_edges = 0; /* Namespace/package → File-QN map for namespace imports (C# `using`, - * Java/Kotlin `import`, PHP `use`). Built from the full result cache so - * every declaring file is visible regardless of loop order. */ - const char **rels = (const char **)calloc((size_t)file_count, sizeof(char *)); - if (rels) { - for (int i = 0; i < file_count; i++) { - rels[i] = files[i].rel_path; - } - } - /* Built from every file, including the ones already parked on disk: their - * results are NULL in result_cache, and a file absent from this map does - * not fail to resolve, it resolves through the looser fallback. Spilling - * therefore used to CHANGE the graph rather than merely delay it -- php - * measured 57,182 edges in memory against 59,379 while spilling, the same - * binary and corpus (2026-09-18), differing in both directions. */ + * Java/Kotlin `import`, PHP `use`) and the Scala top-level index. Built + * from the full result cache so every declaring file is visible + * regardless of loop order, including the files already parked on disk: + * their results are NULL in result_cache, and a file absent from this map + * does not fail to resolve, it resolves through the looser fallback. + * Spilling therefore used to CHANGE the graph rather than merely delay it + * -- php measured 57,182 edges in memory against 59,379 while spilling, + * the same binary and corpus (2026-09-18), differing in both directions. */ const char **namespaces = cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)file_count * sizeof(char *)); CBMHashTable *namespace_map = NULL; + cbm_scala_index_t *scala_index = NULL; if (namespaces) { for (int i = 0; i < file_count; i++) { namespaces[i] = result_cache[i] ? result_cache[i]->namespace_name : cbm_result_spill_namespace(ctx->spill, i); } - namespace_map = - cbm_pipeline_namespace_map_build_names(ctx->project_name, namespaces, rels, file_count); + cbm_pipeline_import_maps_build(ctx, files, file_count, namespaces, &namespace_map, + &scala_index); cbm_free(CBM_MEM_CLASS_OTHER, namespaces); } - free(rels); for (int i = 0; i < file_count; i++) { if (cbm_pipeline_check_cancel(ctx)) { - cbm_pipeline_namespace_map_free(namespace_map); + cbm_pipeline_import_maps_free(namespace_map, scala_index); return CBM_NOT_FOUND; } @@ -2026,7 +2021,7 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t loaded = result != NULL; if (!cbm_pipeline_test_extraction_ok(ctx, files[i].language, result)) { cbm_pipeline_result_release(result, loaded); - cbm_pipeline_namespace_map_free(namespace_map); + cbm_pipeline_import_maps_free(namespace_map, scala_index); return CBM_PIPELINE_ABORT_PRESERVE_DB; } } @@ -2036,7 +2031,7 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t if (!cbm_pipeline_test_result_ok(ctx, result)) { cbm_pipeline_result_release(result, loaded); - cbm_pipeline_namespace_map_free(namespace_map); + cbm_pipeline_import_maps_free(namespace_map, scala_index); return CBM_PIPELINE_ABORT_PRESERVE_DB; } @@ -2060,7 +2055,8 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t } } - imports_edges += create_imports_edges(ctx, result, rel, namespace_map); + imports_edges += + create_imports_edges(ctx, result, rel, files[i].language, namespace_map, scala_index); create_channel_edges(ctx, result, rel); cbm_pipeline_diagram_arrows(ctx, result); cbm_pipeline_create_env_configures_for_file(ctx, result, rel); @@ -2071,7 +2067,9 @@ int cbm_build_registry_from_cache(cbm_pipeline_ctx_t *ctx, const cbm_file_info_t /* Publish instead of free: the call pass needs the same declared-package * set to tell an in-tree package path from a third-party one (#1355). - * The pipeline owns it from here and frees it in its cleanup. */ + * The pipeline owns it from here and frees it in its cleanup. Only the + * Scala index is pass-local. */ + cbm_pipeline_import_maps_free(NULL, scala_index); cbm_pipeline_namespace_map_free(cbm_pipeline_get_nsmap()); cbm_pipeline_set_nsmap(namespace_map); diff --git a/src/pipeline/pass_pkgmap.c b/src/pipeline/pass_pkgmap.c index 05d024b50e..f50478b7b5 100644 --- a/src/pipeline/pass_pkgmap.c +++ b/src/pipeline/pass_pkgmap.c @@ -14,6 +14,8 @@ #include "pipeline/pipeline_internal.h" #include "pipeline/pass_lsp_cross.h" #include "discover/discover.h" +#include "helpers.h" /* cbm_fqn_module_source_lang */ +#include "foundation/arena.h" #include "foundation/compat.h" #include "foundation/compat_fs.h" #include "foundation/constants.h" @@ -2426,6 +2428,152 @@ static psr4_outcome_t resolve_php_psr4_class(const cbm_gbuf_t *gbuf, const char return covered ? PSR4_UNRESOLVED : PSR4_NOT_APPLICABLE; } +/* ── Scala top-level index ───────────────────────────────────────── */ + +/* The two File nodes with the lexicographically smallest QNs declaring a + * package, borrowed from the graph buffer. The smallest is the `import pkg._` + * target on every platform regardless of directory-walk order; the runner-up + * serves when the smallest is the importing file itself. */ +typedef struct { + const cbm_gbuf_node_t *first; + const cbm_gbuf_node_t *second; /* NULL for a single-file package */ +} scala_package_files_t; + +struct cbm_scala_index { + CBMHashTable *symbols; /* "pkg.Name" → node, or &scala_toplevel_ambiguous */ + CBMHashTable *packages; /* "pkg" → scala_package_files_t */ +}; + +/* Marker value in the Scala top-level index: two files of a split package + * declare different top-level symbols under the same name, so an import of + * that name is ambiguous and must not bind. */ +static const cbm_gbuf_node_t scala_toplevel_ambiguous; + +static const cbm_gbuf_node_t *scala_toplevel_lookup(const cbm_scala_index_t *scala_index, + const char *package, const char *name) { + if (!scala_index || !scala_index->symbols || !package || !name || !name[0]) { + return NULL; + } + char key[PKGMAP_PATH_BUF]; + if (snprintf(key, sizeof(key), "%s.%s", package, name) >= (int)sizeof(key)) { + return NULL; + } + const cbm_gbuf_node_t *node = (const cbm_gbuf_node_t *)cbm_ht_get(scala_index->symbols, key); + return node == &scala_toplevel_ambiguous ? NULL : node; +} + +/* `import pkg._` binds the package itself: its smallest declaring file, or + * the runner-up when that is the importer. */ +static const cbm_gbuf_node_t *scala_package_file(const cbm_scala_index_t *scala_index, + const char *package, const char *source_file_qn) { + if (!scala_index || !scala_index->packages) { + return NULL; + } + const scala_package_files_t *pf = cbm_ht_get(scala_index->packages, package); + if (!pf) { + return NULL; + } + const cbm_gbuf_node_t *pick = pf->first; + if (source_file_qn && pick && strcmp(pick->qualified_name, source_file_qn) == 0) { + pick = pf->second; + } + return pick; +} + +/* Scala source packages are independent of the repository layout. Resolve the + * longest declared package prefix first, then the imported top-level symbol + * through the (package, name) index, then an optional member under that + * owner's QN. Every step fails closed: generic suffix fallback is unsafe for + * JVM FQNs because common names such as api/get/app occur throughout a + * monorepo, and a member import that binds the owner instead would turn + * `member()` into a CALLS edge to the owner class via resolve_import_map. */ +static const cbm_gbuf_node_t *resolve_scala_namespace_import(const cbm_pipeline_ctx_t *ctx, + const char *source_file_qn, + const CBMImport *imp, + const CBMHashTable *namespace_map, + const cbm_scala_index_t *scala_index) { + if (!namespace_map || !imp || !imp->module_path || !imp->module_path[0]) { + return NULL; + } + char full[PKGMAP_PATH_BUF]; + snprintf(full, sizeof(full), "%s", imp->module_path); + for (char *p = full; *p; p++) { + if (*p == '\\' || *p == ':' || *p == '/') { + *p = '.'; + } + } + size_t full_len = strlen(full); + while (full_len > 0 && (full[full_len - 1] == '*' || full[full_len - 1] == '.')) { + full[--full_len] = '\0'; + } + + bool wildcard = imp->local_name && strcmp(imp->local_name, "*") == 0; + if (wildcard && cbm_ht_has(namespace_map, full)) { + return scala_package_file(scala_index, full, source_file_qn); + } + + char package[PKGMAP_PATH_BUF]; + snprintf(package, sizeof(package), "%s", full); + for (;;) { + char *dot = strrchr(package, '.'); + if (!dot) { + break; + } + *dot = '\0'; + if (!cbm_ht_has(namespace_map, package)) { + continue; + } + const char *remainder = full + strlen(package) + 1; + char top_level[CBM_SZ_256]; + size_t top_len = strcspn(remainder, "."); + if (top_len == 0 || top_len >= sizeof(top_level)) { + return NULL; + } + memcpy(top_level, remainder, top_len); + top_level[top_len] = '\0'; + /* Only a top-level symbol of a file declaring this package qualifies; + * a nested class or method of the same name is not importable by + * `pkg.Name` and an ambiguous split-package name yields no edge. */ + const cbm_gbuf_node_t *owner = scala_toplevel_lookup(scala_index, package, top_level); + if (!owner || !owner->qualified_name) { + return NULL; + } + if (wildcard || !strchr(remainder, '.')) { + return owner; + } + /* `import pkg.Owner.member`: the member must exist under the owner's + * QN. Binding the owner instead would mislabel the edge. */ + char member_qn[CBM_SZ_512]; + if (snprintf(member_qn, sizeof(member_qn), "%s.%s", owner->qualified_name, + remainder + top_len + 1) >= (int)sizeof(member_qn)) { + return NULL; + } + const cbm_gbuf_node_t *scoped = cbm_gbuf_find_by_qn(ctx->gbuf, member_qn); + if (scoped && scoped->label && import_targetable_label(scoped->label)) { + return scoped; + } + return NULL; + } + return NULL; +} + +/* Scala `package` clauses are resolved only by the fail-closed resolver above. + * Letting them feed the generic first-declaring-file walk would bind Java or + * Kotlin imports of a shared package prefix to an arbitrary Scala file. */ +static bool path_is_scala(const char *path) { + const char *base = path; + for (const char *p = base; *p; p++) { + if (*p == '/' || *p == '\\') { + base = p + SKIP_ONE; + } + } + return cbm_language_for_filename(base) == CBM_LANG_SCALA; +} + +static bool declaring_file_is_scala(const cbm_gbuf_node_t *node) { + return node && node->file_path && path_is_scala(node->file_path); +} + static bool psr4_class_label(const char *label) { return label && (strcmp(label, "Class") == 0 || strcmp(label, "Interface") == 0 || strcmp(label, "Trait") == 0 || strcmp(label, "Enum") == 0); @@ -3046,7 +3194,8 @@ static const cbm_gbuf_node_t *rust_use_resolve(const cbm_pipeline_ctx_t *ctx, * fallbacks (#1934). */ static const cbm_gbuf_node_t *resolve_import_strategies( const cbm_pipeline_ctx_t *ctx, const char *source_rel, const char *source_file_qn, - const CBMImport *imp, CBMHashTable *namespace_map, CBMLanguage src_lang, bool php_only) { + const CBMImport *imp, CBMHashTable *namespace_map, CBMLanguage src_lang, bool php_only, + CBMLanguage language, const cbm_scala_index_t *scala_index) { /* Strategy 1: module-path resolution → existing node (Python/TS/Go). * No label filter here: directory-module languages (Go/Java packages) * legitimately resolve straight to a Folder node -- that's the intended, @@ -3127,6 +3276,10 @@ static const cbm_gbuf_node_t *resolve_import_strategies( return rel_file; } + if (language == CBM_LANG_SCALA) { + return resolve_scala_namespace_import(ctx, source_file_qn, imp, namespace_map, scala_index); + } + /* Strategy 2: namespace map. `using App.Utils`, `import com.example.Foo`, * `use App\Utils\Helper` name a NAMESPACE (or a member of it) that the * path-based QN cannot express. Try the full module path and progressively @@ -3181,7 +3334,8 @@ static const cbm_gbuf_node_t *resolve_import_strategies( qbuf[len] = '\0'; const cbm_gbuf_node_t *n = cbm_gbuf_find_by_qn(ctx->gbuf, qbuf); if (n && (!php_only || node_in_php_file(n)) && - (!source_file_qn || strcmp(n->qualified_name, source_file_qn) != 0)) { + (!source_file_qn || strcmp(n->qualified_name, source_file_qn) != 0) && + !declaring_file_is_scala(n)) { return n; } } @@ -3506,8 +3660,9 @@ static const cbm_gbuf_node_t *resolve_php_declared_use(const cbm_pipeline_ctx_t const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx, const char *source_rel, const char *source_file_qn, - const CBMImport *imp, - CBMHashTable *namespace_map) { + CBMLanguage language, const CBMImport *imp, + CBMHashTable *namespace_map, + const cbm_scala_index_t *scala_index) { if (!ctx || !imp || !imp->module_path) { return NULL; } @@ -3547,7 +3702,7 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t const php_use_shape_t php_use = php_use_shape(source_rel, imp); if (php_use == PHP_USE_NONE) { return resolve_import_strategies(ctx, source_rel, source_file_qn, imp, namespace_map, - src_lang, false); + src_lang, false, language, scala_index); } /* Any other PHP namespaced `use` resolves only through declared namespaces. * A global-namespace `use Closure;` and the CALLS import map (which passes @@ -3557,7 +3712,7 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t return resolve_php_declared_use(ctx, source_file_qn, imp, namespace_map); } return resolve_import_strategies(ctx, source_rel, source_file_qn, imp, namespace_map, src_lang, - true); + true, language, scala_index); } /* PHP binding evidence is per File. The first field is canonical so its @@ -3840,7 +3995,8 @@ int cbm_pipeline_php_create_import_edges(cbm_pipeline_ctx_t *ctx, const CBMFileR for (int i = 0; i < count; i++) { const CBMImport *imp = &result->imports.items[i]; targets[i] = imp->module_path - ? cbm_pipeline_resolve_import_node(ctx, rel, file_qn, imp, namespace_map) + ? cbm_pipeline_resolve_import_node(ctx, rel, file_qn, CBM_LANG_PHP, imp, + namespace_map, NULL) : NULL; if (targets[i] && targets[i]->id == file->id) targets[i] = NULL; @@ -4013,6 +4169,40 @@ bool cbm_pipeline_php_vendor_usage(const cbm_php_vendor_names_t *value, const CB return php_vendor_kind(value, usage->ref_name, kind); } +/* ── File node package property ──────────────────────────────────── */ + +/* Persist a file's package clause on its File node so an incremental run + * can rebuild the namespace map and the Scala index for the files it does + * not re-extract (see incr_load_stored_namespaces). */ +static void file_node_set_package(const cbm_pipeline_ctx_t *ctx, const char *file_qn, + const char *namespace_name) { + cbm_gbuf_node_t *node = (cbm_gbuf_node_t *)cbm_gbuf_find_by_qn(ctx->gbuf, file_qn); + if (!node || !node->label || strcmp(node->label, "File") != 0) { + return; + } + /* Append to the existing object ({"extension":".scala"}); the previous + * generation's value is gone with the purge of a changed file, so a + * `package` key already present means this pass set it, or the file is + * one the pass does not re-extract. */ + const char *props = node->properties_json ? node->properties_json : "{}"; + if (strstr(props, "\"package\"")) { + return; + } + size_t props_len = strlen(props); + if (props_len < 2 || props[props_len - 1] != '}') { + return; + } + char escaped[PKGMAP_PATH_BUF]; + cbm_json_escape(escaped, sizeof(escaped), namespace_name); + char json[PKGMAP_PATH_BUF * 2]; + int n = snprintf(json, sizeof(json), "%.*s%s\"package\":\"%s\"}", (int)props_len - 1, props, + props_len > 2 ? "," : "", escaped); + if (n < 0 || n >= (int)sizeof(json)) { + return; + } + cbm_gbuf_node_set_properties_json(node, json); +} + /* ── Namespace map ───────────────────────────────────────────────── */ /* The namespace names themselves, so a caller that has parked some results on @@ -4021,9 +4211,9 @@ bool cbm_pipeline_php_vendor_usage(const cbm_php_vendor_names_t *value, const CB * resolve -- it resolves DIFFERENTLY, through the looser fallback, which is why * an incomplete map changed edge counts in both directions rather than only * losing edges. */ -CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name, - const char *const *namespaces, - const char *const *rels, int count) { +static CBMHashTable *namespace_map_build(const cbm_pipeline_ctx_t *ctx, const char *project_name, + const char *const *namespaces, const char *const *rels, + int count) { /* NULL means no namespace information (e.g. the CALLS import map), while * an empty map means no project file declares a namespace. A partial map * cannot prove a PHP import absent, so every construction failure returns @@ -4048,6 +4238,9 @@ CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name, if (!file_qn) { goto item_done; } + if (ctx) { + file_node_set_package(ctx, file_qn, namespace_name); + } /* Normalize the namespace key to dot-separated form so it matches the * dot-normalized lookups in cbm_pipeline_resolve_import_node (PHP uses * '\\', some grammars '::' or '/'). */ @@ -4114,6 +4307,12 @@ CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name, return map; } +CBMHashTable *cbm_pipeline_namespace_map_build_names(const char *project_name, + const char *const *namespaces, + const char *const *rels, int count) { + return namespace_map_build(NULL, project_name, namespaces, rels, count); +} + /* Convenience for callers whose results are all in memory (the sequential * definitions pass). A caller that can SPILL must use the _names variant and * fill the parked slots from cbm_result_spill_namespace, or its map silently @@ -4151,3 +4350,250 @@ void cbm_pipeline_namespace_map_free(CBMHashTable *map) { cbm_ht_foreach(map, ns_map_free_entry, NULL); cbm_ht_free(map); } + +/* ── Scala top-level index ───────────────────────────────────────── */ + +typedef struct { + char *namespace_name; /* dot-normalized package clause */ + char *module_qn; /* language-aware module QN of the declaring file (qn_arena) */ +} scala_file_entry_t; + +typedef struct { + CBMHashTable *files; /* rel path -> scala_file_entry_t (build-time only) */ + cbm_scala_index_t *index; +} scala_index_build_t; + +/* The language whose module QN a file's top-level symbols hang off. + * Directory-module languages (Java, Go) put `class Outer` in Outer.java at + * .Outer, so is the module and Outer's methods and nested types + * sit two segments below it; stem-module languages (Scala `object Foo` in + * Foo.scala is Foo.Foo) use the file stem. The module QN itself comes from + * cbm_fqn_module_source_lang, the helper the extractor's definition QNs are + * built with, so both sides apply the same rule. */ +static CBMLanguage pkgmap_file_language(const char *rel) { + const char *base = rel; + for (const char *p = rel; *p; p++) { + if (*p == '/' || *p == '\\') { + base = p + SKIP_ONE; + } + } + return cbm_language_for_filename(base); +} + +/* Keep the two smallest File nodes of a package (see scala_package_files_t). */ +static void scala_index_note_package_file(cbm_scala_index_t *index, const char *package, + const cbm_gbuf_node_t *file_node) { + if (!index->packages) { + index->packages = cbm_ht_create(CBM_SZ_64); + if (!index->packages) { + return; + } + } + scala_package_files_t *pf = cbm_ht_get(index->packages, package); + if (!pf) { + pf = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*pf)); + char *key = pf ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, package) : NULL; + if (!key) { + cbm_free(CBM_MEM_CLASS_OTHER, pf); + return; + } + pf->first = file_node; + cbm_ht_set(index->packages, key, pf); /* index owns key + pf */ + return; + } + int c = strcmp(file_node->qualified_name, pf->first->qualified_name); + if (c < 0) { + pf->second = pf->first; + pf->first = file_node; + } else if (c > 0 && + (!pf->second || strcmp(file_node->qualified_name, pf->second->qualified_name) < 0)) { + pf->second = file_node; + } +} + +/* A node is top-level when its QN is exactly ., one segment + * below the declaring file's module. Nested classes and methods carry deeper + * QNs. Module and File nodes share those QN shapes but declare nothing + * importable; a File node of a declaring file is instead a candidate for the + * package's `import pkg._` target. */ +static void scala_index_visit_node(const cbm_gbuf_node_t *node, void *ud) { + scala_index_build_t *b = ud; + if (!node || !node->qualified_name || !node->file_path || !node->name || + !import_targetable_label(node->label) || strcmp(node->label, "Module") == 0) { + return; + } + const scala_file_entry_t *fe = + (const scala_file_entry_t *)cbm_ht_get(b->files, node->file_path); + if (!fe) { + return; + } + if (strcmp(node->label, "File") == 0) { + scala_index_note_package_file(b->index, fe->namespace_name, node); + return; + } + size_t mod_len = strlen(fe->module_qn); + const char *qn = node->qualified_name; + if (strncmp(qn, fe->module_qn, mod_len) != 0 || qn[mod_len] != '.' || !qn[mod_len + 1] || + strchr(qn + mod_len + 1, '.')) { + return; + } + const char *tail = qn + mod_len + 1; + char key[PKGMAP_PATH_BUF]; + if (snprintf(key, sizeof(key), "%s.%s", fe->namespace_name, tail) >= (int)sizeof(key)) { + return; + } + if (!b->index->symbols) { + b->index->symbols = cbm_ht_create(CBM_SZ_256); + if (!b->index->symbols) { + return; + } + } + const char *stored_key = cbm_ht_get_key(b->index->symbols, key); + if (!stored_key) { + char *owned = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key); + if (owned) { + cbm_ht_set(b->index->symbols, owned, (void *)node); /* index owns key */ + } + return; + } + if (cbm_ht_get(b->index->symbols, stored_key) != node) { + cbm_ht_set(b->index->symbols, stored_key, (void *)&scala_toplevel_ambiguous); + } +} + +static void scala_index_free_file_entry(const char *key, void *value, void *ud) { + (void)key; /* borrowed from the caller's rels array */ + (void)ud; + scala_file_entry_t *fe = value; + cbm_free(CBM_MEM_CLASS_OTHER, fe->namespace_name); /* module_qn lives in qn_arena */ + cbm_free(CBM_MEM_CLASS_OTHER, fe); +} + +static void scala_index_free_symbol(const char *key, void *value, void *ud) { + (void)value; /* borrowed gbuf node or the ambiguity marker */ + (void)ud; + cbm_free(CBM_MEM_CLASS_OTHER, (void *)key); /* cbm_mem_strdup'd in scala_index_visit_node */ +} + +static void scala_index_free_package(const char *key, void *value, void *ud) { + (void)ud; + cbm_free(CBM_MEM_CLASS_OTHER, value); /* File nodes are borrowed */ + cbm_free(CBM_MEM_CLASS_OTHER, (void *)key); +} + +static void scala_index_free(cbm_scala_index_t *index) { + if (!index) { + return; + } + if (index->symbols) { + cbm_ht_foreach(index->symbols, scala_index_free_symbol, NULL); + cbm_ht_free(index->symbols); + } + if (index->packages) { + cbm_ht_foreach(index->packages, scala_index_free_package, NULL); + cbm_ht_free(index->packages); + } + cbm_free(CBM_MEM_CLASS_OTHER, index); +} + +/* Every file declaring a namespace contributes, not only Scala ones: a Scala + * import routinely names a Java class of the same (mixed) package. */ +static cbm_scala_index_t *scala_index_build(const cbm_pipeline_ctx_t *ctx, + const char *const *namespaces, const char *const *rels, + int count) { + scala_index_build_t b = {0}; + b.index = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*b.index)); + if (!b.index) { + return NULL; + } + CBMArena qn_arena; /* the declaring files' module QNs, build-time only */ + cbm_arena_init(&qn_arena); + for (int i = 0; i < count; i++) { + if (!namespaces[i] || !namespaces[i][0] || !rels[i]) { + continue; + } + if (!b.files) { + b.files = cbm_ht_create(CBM_SZ_64); + if (!b.files) { + break; + } + } + scala_file_entry_t *fe = cbm_calloc(CBM_MEM_CLASS_OTHER, sizeof(*fe)); + if (!fe) { + continue; + } + fe->namespace_name = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, namespaces[i]); + fe->module_qn = cbm_fqn_module_source_lang(&qn_arena, ctx->project_name, rels[i], + pkgmap_file_language(rels[i])); + if (!fe->namespace_name || !fe->module_qn) { + scala_index_free_file_entry(rels[i], fe, NULL); + continue; + } + for (char *p = fe->namespace_name; *p; p++) { + if (*p == '\\' || *p == ':' || *p == '/') { + *p = '.'; + } + } + void *prev = cbm_ht_set(b.files, rels[i], fe); + if (prev) { + scala_index_free_file_entry(rels[i], prev, NULL); + } + } + if (!b.files) { + cbm_arena_destroy(&qn_arena); + scala_index_free(b.index); + return NULL; + } + cbm_gbuf_foreach_node(ctx->gbuf, scala_index_visit_node, &b); + cbm_ht_foreach(b.files, scala_index_free_file_entry, NULL); + cbm_ht_free(b.files); + cbm_arena_destroy(&qn_arena); + return b.index; +} + +/* ── Import maps ─────────────────────────────────────────────────── */ + +void cbm_pipeline_import_maps_build(const cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, + int file_count, const char *const *namespaces, + CBMHashTable **namespace_map, cbm_scala_index_t **scala_index) { + *namespace_map = NULL; + *scala_index = NULL; + if (!ctx || !ctx->gbuf || !files || !namespaces) { + return; + } + /* The fresh files first, then the previous generation's clauses for the + * files this pass does not re-extract (incremental routes only; the + * caller guarantees the two sets are disjoint). */ + int stored = ctx->stored_namespaces ? ctx->stored_namespace_count : 0; + size_t total = (size_t)file_count + (size_t)stored; + const char **all_ns = cbm_calloc(CBM_MEM_CLASS_OTHER, (total ? total : 1) * sizeof(char *)); + const char **all_rels = cbm_calloc(CBM_MEM_CLASS_OTHER, (total ? total : 1) * sizeof(char *)); + if (!all_ns || !all_rels) { + cbm_free(CBM_MEM_CLASS_OTHER, all_ns); + cbm_free(CBM_MEM_CLASS_OTHER, all_rels); + return; + } + bool scala_importer = false; + for (int i = 0; i < file_count; i++) { + all_ns[i] = namespaces[i]; + all_rels[i] = files[i].rel_path; + scala_importer = scala_importer || files[i].language == CBM_LANG_SCALA; + } + for (int i = 0; i < stored; i++) { + all_ns[file_count + i] = ctx->stored_namespaces[i].namespace_name; + all_rels[file_count + i] = ctx->stored_namespaces[i].rel_path; + } + *namespace_map = namespace_map_build(ctx, ctx->project_name, all_ns, all_rels, (int)total); + /* Only the Scala resolver reads the index; a corpus without a Scala + * importer pays nothing for it. */ + if (scala_importer) { + *scala_index = scala_index_build(ctx, all_ns, all_rels, (int)total); + } + cbm_free(CBM_MEM_CLASS_OTHER, all_ns); + cbm_free(CBM_MEM_CLASS_OTHER, all_rels); +} + +void cbm_pipeline_import_maps_free(CBMHashTable *namespace_map, cbm_scala_index_t *scala_index) { + scala_index_free(scala_index); + cbm_pipeline_namespace_map_free(namespace_map); +} diff --git a/src/pipeline/pipeline_incremental.c b/src/pipeline/pipeline_incremental.c index d0ecbbb21b..40a425ea64 100644 --- a/src/pipeline/pipeline_incremental.c +++ b/src/pipeline/pipeline_incremental.c @@ -3430,6 +3430,88 @@ static int closure_try_plan(cbm_pipeline_t *p, cbm_store_t *store, const char *p return 1; } +/* ── Stored package clauses ────────────────────────────────────── + * + * Both incremental routes hand the passes only the changed files, and the + * passes build the import maps (namespace map, Scala top-level index) from + * the files they are handed. The maps therefore knew only the changed files' + * packages: an import into an unchanged package resolved through the looser + * symbol fallback, or with the fail-closed Scala resolver not at all, until + * the next full index. The definitions/registry pass persists each file's + * clause on its File node (`package` property); read those back for every + * file that stays, so the passes see the whole project as a full build does. + * `skip` holds the rel paths whose rows are still present but about to be + * replaced or removed (legacy route); NULL once the purge already ran. */ +static void incr_free_stored_namespaces(cbm_file_namespace_t *ns, int count) { + for (int i = 0; i < count; i++) { + cbm_free(CBM_MEM_CLASS_OTHER, ns[i].rel_path); + cbm_free(CBM_MEM_CLASS_OTHER, ns[i].namespace_name); + } + cbm_free(CBM_MEM_CLASS_OTHER, ns); +} + +static int incr_load_stored_namespaces(cbm_store_t *store, const char *project, + const CBMHashTable *skip, cbm_file_namespace_t **out, + int *out_count) { + *out = NULL; + *out_count = 0; + sqlite3 *db = cbm_store_get_db(store); + if (!db) { + return -1; + } + /* LIKE keeps the JSON parse off the File nodes without a clause; + * json_valid() guards it because json_extract() raises on malformed + * properties (same shape as the store's FTS body expression). */ + sqlite3_stmt *stmt = NULL; + if (sqlite3_prepare_v2(db, + "SELECT file_path, json_extract(properties, '$.package') FROM nodes" + " WHERE project = ?1 AND label = 'File'" + " AND properties LIKE '%\"package\"%' AND json_valid(properties)", + CBM_NOT_FOUND, &stmt, NULL) != SQLITE_OK) { + return -1; + } + sqlite3_bind_text(stmt, 1, project, CBM_NOT_FOUND, SQLITE_TRANSIENT); + cbm_file_namespace_t *rows = NULL; + int count = 0; + int cap = 0; + int step_rc; + while ((step_rc = sqlite3_step(stmt)) == SQLITE_ROW) { + const char *rel = (const char *)sqlite3_column_text(stmt, 0); + const char *ns = (const char *)sqlite3_column_text(stmt, 1); + if (!rel || !rel[0] || !ns || !ns[0] || (skip && cbm_ht_has(skip, rel))) { + continue; + } + if (count == cap) { + int new_cap = cap ? cap * 2 : CBM_SZ_64; + cbm_file_namespace_t *grown = cbm_realloc( + CBM_MEM_CLASS_OTHER, rows, (size_t)new_cap * sizeof(cbm_file_namespace_t)); + if (!grown) { + step_rc = SQLITE_NOMEM; + break; + } + rows = grown; + cap = new_cap; + } + rows[count].rel_path = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, rel); + rows[count].namespace_name = cbm_mem_strdup(CBM_MEM_CLASS_OTHER, ns); + if (!rows[count].rel_path || !rows[count].namespace_name) { + cbm_free(CBM_MEM_CLASS_OTHER, rows[count].rel_path); + cbm_free(CBM_MEM_CLASS_OTHER, rows[count].namespace_name); + step_rc = SQLITE_NOMEM; + break; + } + count++; + } + sqlite3_finalize(stmt); + if (step_rc != SQLITE_DONE) { + incr_free_stored_namespaces(rows, count); + return -1; + } + *out = rows; + *out_count = count; + return 0; +} + static bool incremental_metadata_current(int meta_rc, const cbm_coverage_meta_t *meta, const char *mode_name) { return meta_rc == CBM_STORE_OK && meta->coverage_version == CBM_SEMANTIC_INDEX_VERSION && @@ -3696,6 +3778,16 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char cbm_log_error("delta.err", "phase", "doc_link_scopes"); goto out; } + /* The purge already removed the closure and deleted files, so every File + * row left in the stage is a file the passes will not see. A failed read + * is a delta-route failure: the maps would silently diverge from a full + * build. */ + cbm_file_namespace_t *stored_ns = NULL; + int stored_ns_count = 0; + if (incr_load_stored_namespaces(staging, project, NULL, &stored_ns, &stored_ns_count) != 0) { + cbm_log_error("delta.err", "phase", "stored_namespaces"); + goto out; + } cbm_pipeline_ctx_t ctx = { .project_name = project, .repo_path = cbm_pipeline_repo_path(p), @@ -3711,6 +3803,8 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char .path_aliases = path_aliases, .excluded_dirs = excluded_dirs, .excluded_count = excluded_count, + .stored_namespaces = stored_ns, + .stored_namespace_count = stored_ns_count, .doc_link_base = doc_base, .doc_link_base_count = doc_base_count, .build_decl_files = decl_files, @@ -3752,6 +3846,7 @@ static int run_closure_delta(cbm_pipeline_t *p, const char *db_path, const char if (ctx.macro_table) { free((void *)ctx.macro_table); } + incr_free_stored_namespaces(stored_ns, stored_ns_count); cbm_pkgmap_free(cbm_pipeline_get_pkgmap()); cbm_pipeline_set_pkgmap(NULL); cbm_pipeline_namespace_map_free(cbm_pipeline_get_nsmap()); @@ -4343,8 +4438,29 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil itoa_buf(cbm_gbuf_edge_count(existing)), "elapsed_ms", itoa_buf((int)elapsed_ms(t))); - if (load_rc != 0) { - cbm_log_error("incremental.err", "msg", "load_db_failed"); + /* Package clauses of the files that stay: the changed and deleted files + * are still in the database here, so they are skipped explicitly. A + * failed read aborts the route and shares the failed load's cleanup. */ + cbm_file_namespace_t *stored_ns = NULL; + int stored_ns_count = 0; + int ns_rc = 0; + if (load_rc == 0) { + CBMHashTable *skip = cbm_ht_create((size_t)(ci + deleted_count) * PAIR_LEN + CBM_SZ_64); + for (int i = 0; skip && i < ci; i++) { + cbm_ht_set(skip, changed_files[i].rel_path, &changed_files[i]); + } + for (int i = 0; skip && i < deleted_count; i++) { + cbm_ht_set(skip, deleted[i], deleted[i]); + } + ns_rc = + skip ? incr_load_stored_namespaces(store, project, skip, &stored_ns, &stored_ns_count) + : -1; + cbm_ht_free(skip); /* keys borrowed */ + } + + if (load_rc != 0 || ns_rc != 0) { + cbm_log_error("incremental.err", "msg", + load_rc != 0 ? "load_db_failed" : "stored_namespaces_failed"); cbm_gbuf_free(existing); free(changed_files); for (int i = 0; i < deleted_count; i++) { @@ -4355,7 +4471,7 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil cbm_store_free_coverage(old_cov, old_cov_count); cbm_store_close(store); closure_plan_free(&closure_plan); - return CBM_NOT_FOUND; + return load_rc != 0 ? CBM_NOT_FOUND : CBM_PIPELINE_ABORT_PRESERVE_DB; } /* ADRs live in the ".adr.db" sidecar, which the delta clone/patch/rename @@ -4446,6 +4562,8 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil .path_aliases = path_aliases, .excluded_dirs = excluded_dirs, .excluded_count = excluded_count, + .stored_namespaces = stored_ns, + .stored_namespace_count = stored_ns_count, .doc_link_base = legacy_doc.scopes, .doc_link_base_count = legacy_doc.scope_count, }; @@ -4503,6 +4621,9 @@ int cbm_pipeline_run_incremental(cbm_pipeline_t *p, const char *db_path, cbm_fil free((void *)ctx.macro_table); ctx.macro_table = NULL; } + incr_free_stored_namespaces(stored_ns, stored_ns_count); + ctx.stored_namespaces = NULL; + ctx.stored_namespace_count = 0; /* Parallel extraction builds the process-global package map. Match the * full pipeline's ownership boundary on both success and failure. */ diff --git a/src/pipeline/pipeline_internal.h b/src/pipeline/pipeline_internal.h index ba1dc75a12..475f233a20 100644 --- a/src/pipeline/pipeline_internal.h +++ b/src/pipeline/pipeline_internal.h @@ -402,6 +402,16 @@ typedef struct { void cbm_pkg_entries_init(cbm_pkg_entries_t *e); void cbm_pkg_entries_free(cbm_pkg_entries_t *e); +/* A file's declared namespace/package clause, as persisted on its File node + * (`package` property) by the previous index generation. */ +typedef struct { + char *rel_path; /* heap */ + char *namespace_name; /* heap, raw (un-normalized) clause */ +} cbm_file_namespace_t; + +/* Scala top-level index (pass_pkgmap.c); see cbm_pipeline_import_maps_build. */ +typedef struct cbm_scala_index cbm_scala_index_t; + /* The env join's inputs held by the unchanged files of an incremental * repair (pass_envjoin.c), read from the store before the K8s pass; ids are * the stored rows, i.e. the repair buffer's proxies. */ @@ -488,6 +498,16 @@ typedef struct { char **excluded_dirs; int excluded_count; + /* Package clauses of the files this run does NOT re-extract, read back + * from their File nodes by the incremental routes. The import maps + * (namespace map, Scala index) are built from the files handed to a pass, + * and an incremental pass receives only the changed files; without these + * the maps would know only the changed files' packages and an import into + * an unchanged package would resolve differently than in a full build. + * NULL/0 on a full build. Owned by pipeline_incremental.c. */ + const cbm_file_namespace_t *stored_namespaces; + int stored_namespace_count; + /* Stem collisions of the discovered files: which Module definitions take * `qn#ext`. Borrowed for the extraction phase; NULL elsewhere. */ const cbm_stem_marks_t *stem_marks; @@ -910,19 +930,48 @@ void cbm_pipeline_emit_spawn(cbm_gbuf_t *gbuf, const cbm_gbuf_node_t *source, co * 1. Module-path resolution (relative / pkgmap / fqn_module) → existing node. * This preserves the behavior for Python/TS/Go whose module path maps * directly to a sibling Module/File QN. - * 2. namespace_map[module_path-prefix] → File node QN (Java/Kotlin/C#/PHP + * 2. namespace_map[module_path-prefix] → File/symbol node QN (Java/Kotlin/Scala/C#/PHP * `using`/`import` of a NAMESPACE that the path-based QN cannot express). * 3. Symbol-name fallback: the import's last path segment matched against an * in-graph definition node of the same simple name in a different file * (Rust `use crate::util::helper`, Java `import com.example.Util`, ...). * - * `namespace_map` may be NULL (skips step 2). `source_file_qn` is the importing - * file's __file__ QN, used to avoid self-imports in step 3. */ + * `namespace_map` may be NULL (skips step 2). `scala_index` is the Scala + * top-level index from cbm_pipeline_import_maps_build (NULL when no Scala + * file is among the importers). `source_file_qn` is the importing file's + * __file__ QN, used to avoid self-imports in step 3. */ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t *ctx, const char *source_rel, const char *source_file_qn, - const CBMImport *imp, - CBMHashTable *namespace_map); + CBMLanguage language, const CBMImport *imp, + CBMHashTable *namespace_map, + const cbm_scala_index_t *scala_index); + +/* The import-resolution maps of one definitions/registry pass, built after + * all definition nodes are in the graph buffer: + * + * namespace_map: namespace → '\n'-separated File-node QNs of every file + * declaring it (e.g. "App.Utils", "com.example"), for C# `using`, Java/ + * Kotlin `import`, PHP `use`. NULL when no file declares a namespace. + * scala_index: "." → the node declaring a top-level `Name` + * in a file whose package clause is ``, plus the package's + * wildcard target, so the Scala resolver does every step in O(1). A name + * declared at top level by two files of a split package is recorded as + * ambiguous and never binds. NULL unless a Scala file is among `files` + * (only the Scala resolver reads it). + * + * `namespaces[i]` is the clause of files[i] or NULL — a caller that spilled + * results must still supply it (cbm_result_spill_namespace): a file missing + * from the map does not fail to resolve, it resolves through the looser + * fallback, so an incomplete map CHANGES the graph instead of shrinking it. + * ctx->stored_namespaces complete the maps on an incremental run. Each + * declaring file's clause is also recorded as the `package` property of its + * File node, which is where a later incremental run reads it from. Frees via + * cbm_pipeline_import_maps_free. */ +void cbm_pipeline_import_maps_build(const cbm_pipeline_ctx_t *ctx, const cbm_file_info_t *files, + int file_count, const char *const *namespaces, + CBMHashTable **namespace_map, cbm_scala_index_t **scala_index); +void cbm_pipeline_import_maps_free(CBMHashTable *namespace_map, cbm_scala_index_t *scala_index); /* PHP binding arrays borrow current result and graph strings. Free before * releasing/reloading the result; never cache this state across files. */ diff --git a/src/pipeline/registry.c b/src/pipeline/registry.c index 73c2a41481..8c636ddf3c 100644 --- a/src/pipeline/registry.c +++ b/src/pipeline/registry.c @@ -2156,6 +2156,14 @@ static cbm_resolution_t resolve_import_map(const cbm_registry_t *r, const char * if (!keys || !vals || map_count <= 0) { return empty_result(); } + /* A wildcard import (`import pkg._`, `import pkg.*`, `from m import *`) + * binds no local name: its "*" entry only makes the package + * import-reachable for the strategies below. Scala operators are method + * calls, so `a * b` has the callee "*" and must not bind to the + * wildcard's target (#2360). */ + if (strcmp(prefix, "*") == 0) { + return empty_result(); + } /* Find prefix in import map keys. Prefer the per-file TLS hash * cache (O(1)) over the linear scan when available. */ diff --git a/src/store/store.h b/src/store/store.h index 87237c4ac3..c94add8ed4 100644 --- a/src/store/store.h +++ b/src/store/store.h @@ -35,8 +35,12 @@ typedef struct cbm_store_read_scope cbm_store_read_scope_t; * __image__docker.io/[library/]. * 4 #1913: Go method QNs are receiver-qualified (pkg.Recv.method, generic * receivers unwrapped); free functions stay pkg.func. - * 5 #2061: Swift declaration identities and uncapped call selectors. */ -#define CBM_INDEX_FORMAT_VERSION 5 + * 5 #2061: Swift declaration identities and uncapped call selectors. + * 6 #2360: File nodes record their declared package clause (`package` + * property); an incremental run reads it back to build the namespace + * map and the Scala top-level index for the files it does not + * re-extract. */ +#define CBM_INDEX_FORMAT_VERSION 6 #define CBM_STORE_CANCELLED (-3) #define CBM_STORE_SCAN_LIMIT (-4) #define CBM_STORE_CALLBACK_ERR (-5) diff --git a/tests/test_edge_imports.c b/tests/test_edge_imports.c index 582ab53333..36b57eea94 100644 --- a/tests/test_edge_imports.c +++ b/tests/test_edge_imports.c @@ -139,6 +139,28 @@ static cbm_store_t *ei_index_files(EILangProj *lp, const EILangFile *files, int return cbm_store_open_path(lp->dbpath); } +/* Rewrite one already-indexed fixture file and index the same repository + * again, closing `store` first. One changed file and nothing new keeps the + * second run on an incremental route. Returns the reopened store. */ +static cbm_store_t *ei_rewrite_and_reindex(EILangProj *lp, cbm_store_t *store, + const EILangFile *file) { + cbm_store_close(store); + char path[700]; + snprintf(path, sizeof(path), "%s/%s", lp->tmpdir, file->name); + FILE *f = fopen(path, "wb"); + if (!f) + return NULL; + fputs(file->content, f); + fclose(f); + + char args[700]; + snprintf(args, sizeof(args), "{\"repo_path\":\"%s\"}", lp->tmpdir); + char *resp = cbm_mcp_handle_tool(lp->srv, "index_repository", args); + if (resp) + free(resp); + return cbm_store_open_path(lp->dbpath); +} + static char *ei_slurp_file(const char *path) { FILE *f = fopen(path, "rb"); if (!f) { @@ -1123,6 +1145,433 @@ TEST(ei_cpp_header_include_targets_header_file) { PASS(); } +/* Scala packages do not have to mirror the repository path. Concrete imports + * must resolve to the symbol declared in the source package, while package + * wildcards may only resolve inside that exact package. External wildcards + * must fail closed instead of selecting an unrelated same-name node. */ +TEST(ei_scala_package_imports_ignore_physical_layout) { + static const EILangFile f[] = { + {"modules/model/src/odd/layout/Target.scala", + "package com.example.model\nobject Target { def build(): Int = 1 }\n"}, + {"modules/model/src/another/layout/Aux.scala", + "package com.example.model\nobject Aux { def value: Int = 2 }\n"}, + {"modules/decoy/src/App.scala", "package unrelated\nobject App\n"}, + {"modules/client/src/Consumer.scala", + "package com.example.client\n" + "import com.example.model.Target\n" + "import com.example.model._\n" + "import outside.library._\n" + "object Consumer { def run(): Int = Target.build() }\n"}}; + + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, 4); + ASSERT_NOT_NULL(store); + int64_t source_id = + ei_node_id_for_file_label(store, lp.project, "modules/client/src/Consumer.scala", "File"); + ASSERT_GT(source_id, 0); + + cbm_edge_t *edges = NULL; + int edge_count = 0; + ASSERT_EQ(cbm_store_find_edges_by_source_type(store, source_id, "IMPORTS", &edges, &edge_count), + CBM_STORE_OK); + ASSERT_EQ(edge_count, 2); + + bool saw_target_symbol = false; + for (int i = 0; i < edge_count; i++) { + cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + ASSERT_NOT_NULL(target); + ASSERT_EQ(cbm_store_find_node_by_id(store, edges[i].target_id, target), CBM_STORE_OK); + ASSERT_NOT_NULL(target->file_path); + ASSERT_TRUE(strstr(target->file_path, "modules/model/src/") == target->file_path); + if (target->name && strcmp(target->name, "Target") == 0 && + strstr(target->file_path, "Target.scala") != NULL) { + saw_target_symbol = true; + } + cbm_store_free_nodes(target, 1); + } + cbm_store_free_edges(edges, edge_count); + ASSERT_TRUE(saw_target_symbol); + ei_cleanup(&lp, store); + PASS(); +} + +/* Scala package clauses feed only the Scala resolver. A Java import whose + * package is external must not bind to a Scala file that merely declares a + * shared prefix of that package. */ +TEST(ei_java_import_ignores_scala_package_prefix) { + static const EILangFile f[] = { + {"core/src/main/scala/com/acme/package.scala", + "package com.acme\npackage object core { val marker: Int = 1 }\n"}, + {"client/src/main/java/com/acme/client/Consumer.java", + "package com.acme.client;\n" + "import com.acme.util.Future;\n" + "public class Consumer { Future pending; }\n"}}; + + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, 2); + ASSERT_NOT_NULL(store); + int64_t source_id = ei_node_id_for_file_label( + store, lp.project, "client/src/main/java/com/acme/client/Consumer.java", "File"); + ASSERT_GT(source_id, 0); + + cbm_edge_t *edges = NULL; + int edge_count = 0; + ASSERT_EQ(cbm_store_find_edges_by_source_type(store, source_id, "IMPORTS", &edges, &edge_count), + CBM_STORE_OK); + ASSERT_EQ(edge_count, 0); + cbm_store_free_edges(edges, edge_count); + ei_cleanup(&lp, store); + PASS(); +} + +/* Index `files`, return the IMPORTS edges leaving `source_file` and, when + * exactly one is expected, the name and file of its target. */ +static int ei_scala_single_import_target(const EILangFile *files, int nfiles, + const char *source_file, char *target_name, size_t name_sz, + char *target_file, size_t file_sz) { + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, files, nfiles); + if (!store) { + return -1; + } + int64_t source_id = ei_node_id_for_file_label(store, lp.project, source_file, "File"); + cbm_edge_t *edges = NULL; + int edge_count = 0; + if (source_id <= 0 || cbm_store_find_edges_by_source_type(store, source_id, "IMPORTS", &edges, + &edge_count) != CBM_STORE_OK) { + ei_cleanup(&lp, store); + return -1; + } + if (edge_count == 1) { + cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + if (target && + cbm_store_find_node_by_id(store, edges[0].target_id, target) == CBM_STORE_OK) { + snprintf(target_name, name_sz, "%s", target->name ? target->name : ""); + snprintf(target_file, file_sz, "%s", target->file_path ? target->file_path : ""); + } + cbm_store_free_nodes(target, 1); + } + cbm_store_free_edges(edges, edge_count); + ei_cleanup(&lp, store); + return edge_count; +} + +/* The most common mixed-package case: a Scala file imports a Java class that + * lives in the same source package but a different source root. */ +TEST(ei_scala_imports_java_class_from_mixed_package) { + static const EILangFile f[] = { + {"core/src/main/java/com/acme/core/Codec.java", + "package com.acme.core;\npublic class Codec { public static int size() { return 1; } }\n"}, + {"core/src/main/scala/com/acme/core/Util.scala", "package com.acme.core\nobject Util\n"}, + {"client/src/main/scala/com/acme/client/Consumer.scala", + "package com.acme.client\n" + "import com.acme.core.Codec\n" + "object Consumer { def run(): Int = Codec.size() }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 3, + "client/src/main/scala/com/acme/client/Consumer.scala", + name, sizeof(name), file, sizeof(file)), + 1); + ASSERT_STR_EQ(name, "Codec"); + ASSERT_TRUE(strstr(file, "Codec.java") != NULL); + PASS(); +} + +/* A package declared by both Java and Scala files: a Java import still binds + * the Java declaring file exactly as on main, never a Scala sibling. */ +TEST(ei_java_import_of_mixed_package_binds_java_file) { + static const EILangFile f[] = {{"a/src/main/scala/com/acme/shared/Helper.scala", + "package com.acme.shared\nobject Helper\n"}, + {"b/src/main/java/com/acme/shared/Shared.java", + "package com.acme.shared;\npublic class Shared {}\n"}, + {"c/src/main/java/com/acme/app/App.java", + "package com.acme.app;\nimport com.acme.shared.Shared;\npublic " + "class App { Shared s; }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 3, "c/src/main/java/com/acme/app/App.java", name, + sizeof(name), file, sizeof(file)), + 1); + ASSERT_TRUE(strstr(file, "Shared.java") != NULL); + PASS(); +} + +/* Two files of a split package both declare a top-level `Codec`: the import is + * ambiguous and must produce no edge rather than pick one. */ +TEST(ei_scala_split_package_same_name_top_level_is_ambiguous) { + static const EILangFile f[] = {{"main/src/com/acme/model/Codec.scala", + "package com.acme.model\nobject Codec { def a(): Int = 1 }\n"}, + {"bench/src/com/acme/model/Codec.scala", + "package com.acme.model\nobject Codec { def b(): Int = 2 }\n"}, + {"client/src/Consumer.scala", + "package com.acme.client\n" + "import com.acme.model.Codec\n" + "object Consumer { def run(): Int = Codec.a() }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 3, "client/src/Consumer.scala", name, sizeof(name), + file, sizeof(file)), + 0); + PASS(); +} + +/* `import pkg.Name` matches only a top-level Name. A nested object of the same + * name neither becomes the target nor makes the real top-level one ambiguous. */ +TEST(ei_scala_import_ignores_nested_same_name) { + static const EILangFile f[] = { + {"model/src/Codec.scala", "package com.acme.model\nobject Codec { def a(): Int = 1 }\n"}, + {"model/src/Outer.scala", + "package com.acme.model\nobject Outer { object Codec { def b(): Int = 2 } }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.model.Codec\n" + "object Consumer { def run(): Int = Codec.a() }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 3, "client/src/Consumer.scala", name, sizeof(name), + file, sizeof(file)), + 1); + ASSERT_STR_EQ(name, "Codec"); + ASSERT_TRUE(strstr(file, "model/src/Codec.scala") != NULL); + PASS(); +} + +/* `import pkg.Owner.member` binds the member under the owner's QN. When the + * member does not exist the import fails closed: no IMPORTS edge, and the bare + * `missing()` call must not become a CALLS edge to the `Config` class. */ +TEST(ei_scala_unresolvable_member_import_yields_no_owner_edge) { + static const EILangFile f[] = { + {"model/src/Config.scala", + "package com.acme.model\nobject Config { def load(): Int = 1 }\n"}, + {"client/src/Loader.scala", "package com.acme.client\n" + "import com.acme.model.Config.load\n" + "object Loader { def run(): Int = load() }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.model.Config.missing\n" + "object Consumer { def run(): Int = missing() }\n"}}; + + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, 3); + ASSERT_NOT_NULL(store); + + int64_t loader_id = + ei_node_id_for_file_label(store, lp.project, "client/src/Loader.scala", "File"); + ASSERT_GT(loader_id, 0); + cbm_edge_t *edges = NULL; + int edge_count = 0; + ASSERT_EQ(cbm_store_find_edges_by_source_type(store, loader_id, "IMPORTS", &edges, &edge_count), + CBM_STORE_OK); + ASSERT_EQ(edge_count, 1); + cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + ASSERT_NOT_NULL(target); + ASSERT_EQ(cbm_store_find_node_by_id(store, edges[0].target_id, target), CBM_STORE_OK); + ASSERT_STR_EQ(target->name, "load"); + cbm_store_free_nodes(target, 1); + cbm_store_free_edges(edges, edge_count); + + int64_t consumer_id = + ei_node_id_for_file_label(store, lp.project, "client/src/Consumer.scala", "File"); + ASSERT_GT(consumer_id, 0); + edges = NULL; + edge_count = 0; + ASSERT_EQ( + cbm_store_find_edges_by_source_type(store, consumer_id, "IMPORTS", &edges, &edge_count), + CBM_STORE_OK); + ASSERT_EQ(edge_count, 0); + cbm_store_free_edges(edges, edge_count); + + cbm_node_t *configs = NULL; + int config_count = 0; + ASSERT_EQ(cbm_store_find_nodes_by_name(store, lp.project, "Config", &configs, &config_count), + CBM_STORE_OK); + ASSERT_GT(config_count, 0); + for (int i = 0; i < config_count; i++) { + cbm_edge_t *calls = NULL; + int call_count = 0; + ASSERT_EQ( + cbm_store_find_edges_by_target_type(store, configs[i].id, "CALLS", &calls, &call_count), + CBM_STORE_OK); + ASSERT_EQ(call_count, 0); + cbm_store_free_edges(calls, call_count); + } + cbm_store_free_nodes(configs, config_count); + ei_cleanup(&lp, store); + PASS(); +} + +/* Consecutive `package a` / `package b` clauses declare a.b. */ +TEST(ei_scala_chained_package_clauses_resolve) { + static const EILangFile f[] = { + {"model/src/Target.scala", + "package com.acme\npackage model\nobject Target { def build(): Int = 1 }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.model.Target\n" + "object Consumer { def run(): Int = Target.build() }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 2, "client/src/Consumer.scala", name, sizeof(name), + file, sizeof(file)), + 1); + ASSERT_STR_EQ(name, "Target"); + ASSERT_TRUE(strstr(file, "Target.scala") != NULL); + PASS(); +} + +/* Java class QNs are directory-based (`Request` in Request.java is + * .Request), so its nested `Builder` sits one segment below the stem + * module QN. It is not a top-level symbol of the package and must neither + * bind `import pkg.Builder` nor make the real top-level Scala `Builder` + * ambiguous. */ +TEST(ei_scala_import_ignores_java_nested_type_of_same_name) { + static const EILangFile f[] = { + {"core/src/main/java/com/acme/core/Request.java", + "package com.acme.core;\n" + "public class Request {\n" + " public static class Builder { public Request build() { return null; } }\n" + "}\n"}, + {"core/src/main/scala/com/acme/core/Builder.scala", + "package com.acme.core\nobject Builder { def make(): Int = 1 }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.core.Builder\n" + "object Consumer { def run(): Int = Builder.make() }\n"}}; + char name[64] = ""; + char file[256] = ""; + ASSERT_EQ(ei_scala_single_import_target(f, 3, "client/src/Consumer.scala", name, sizeof(name), + file, sizeof(file)), + 1); + ASSERT_STR_EQ(name, "Builder"); + ASSERT_TRUE(strstr(file, "Builder.scala") != NULL); + PASS(); +} + +/* The Scala IMPORTS of client/src/Consumer.scala in the incremental fixture + * below: exactly the symbol import of `Target` and the wildcard bound to the + * package's smallest declaring file, plus the `run() -> build()` CALLS edge + * the import map yields. */ +static int ei_scala_incr_consumer_edges_ok(cbm_store_t *store, const char *project) { + int64_t source_id = + ei_node_id_for_file_label(store, project, "client/src/Consumer.scala", "File"); + if (source_id <= 0) { + fprintf(stderr, " no File node for Consumer.scala\n"); + return 0; + } + cbm_edge_t *edges = NULL; + int edge_count = 0; + if (cbm_store_find_edges_by_source_type(store, source_id, "IMPORTS", &edges, &edge_count) != + CBM_STORE_OK) { + return 0; + } + bool saw_target = false; + bool saw_wildcard = false; + for (int i = 0; i < edge_count; i++) { + cbm_node_t *target = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + if (!target || + cbm_store_find_node_by_id(store, edges[i].target_id, target) != CBM_STORE_OK) { + free(target); + continue; + } + if (target->name && strcmp(target->name, "Target") == 0 && target->file_path && + strstr(target->file_path, "model/src/Target.scala")) { + saw_target = true; + } else if (target->label && strcmp(target->label, "File") == 0 && target->file_path && + strcmp(target->file_path, "model/src/Aux.scala") == 0) { + saw_wildcard = true; + } + cbm_store_free_nodes(target, 1); + } + cbm_store_free_edges(edges, edge_count); + if (edge_count != 2 || !saw_target || !saw_wildcard) { + fprintf(stderr, " Consumer IMPORTS: count=%d target=%d wildcard=%d\n", edge_count, + saw_target, saw_wildcard); + return 0; + } + + cbm_node_t *runs = NULL; + int run_count = 0; + if (cbm_store_find_nodes_by_name(store, project, "run", &runs, &run_count) != CBM_STORE_OK) { + return 0; + } + bool saw_call = false; + for (int i = 0; i < run_count && !saw_call; i++) { + cbm_edge_t *calls = NULL; + int call_count = 0; + if (cbm_store_find_edges_by_source_type(store, runs[i].id, "CALLS", &calls, &call_count) != + CBM_STORE_OK) { + continue; + } + for (int j = 0; j < call_count && !saw_call; j++) { + cbm_node_t *callee = (cbm_node_t *)calloc(1, sizeof(cbm_node_t)); + if (callee && + cbm_store_find_node_by_id(store, calls[j].target_id, callee) == CBM_STORE_OK && + callee->name && strcmp(callee->name, "build") == 0) { + saw_call = true; + } + if (callee) { + cbm_store_free_nodes(callee, 1); + } + } + cbm_store_free_edges(calls, call_count); + } + cbm_store_free_nodes(runs, run_count); + if (!saw_call) { + fprintf(stderr, " no CALLS run -> build\n"); + } + return saw_call; +} + +/* Graph content must not depend on whether the last index was full or + * incremental. An incremental run hands the passes only the changed file, so + * the namespace map and the Scala index must be completed from the package + * clauses persisted for the unchanged files; otherwise the fail-closed Scala + * resolver drops every import into an unchanged package, and the CALLS built + * on them, until the next full index. Exercised on both incremental routes + * (closure repair, then the legacy partial fallback), each proven by the + * route probe so a silent fall-back to a full reindex cannot pass this. */ +TEST(ei_scala_incremental_reindex_keeps_imports_into_unchanged_packages) { + static const EILangFile f[] = { + {"model/src/Target.scala", + "package com.acme.model\nobject Target { def build(): Int = 1 }\n"}, + {"model/src/Aux.scala", "package com.acme.model\nobject Aux { def value: Int = 2 }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.model.Target\n" + "import com.acme.model._\n" + "object Consumer { def run(): Int = Target.build() }\n"}}; + /* Body-only edits: a new definition name makes the closure route decline + * (another file might reference it), which would fall back to a full + * reindex and void the route proof below. */ + static const EILangFile edit1 = {"client/src/Consumer.scala", + "package com.acme.client\n" + "import com.acme.model.Target\n" + "import com.acme.model._\n" + "object Consumer { def run(): Int = Target.build() + 1 }\n"}; + static const EILangFile edit2 = {"client/src/Consumer.scala", + "package com.acme.client\n" + "import com.acme.model.Target\n" + "import com.acme.model._\n" + "object Consumer { def run(): Int = Target.build() + 2 }\n"}; + + EILangProj lp; + cbm_store_t *store = ei_index_files(&lp, f, 3); + ASSERT_NOT_NULL(store); + ASSERT_TRUE(ei_scala_incr_consumer_edges_ok(store, lp.project)); + + store = ei_rewrite_and_reindex(&lp, store, &edit1); + ASSERT_NOT_NULL(store); + ASSERT_EQ((int)cbm_pipeline_incremental_test_last_route(), + (int)CBM_INCREMENTAL_ROUTE_CLOSURE_REPAIR); + ASSERT_TRUE(ei_scala_incr_consumer_edges_ok(store, lp.project)); + + cbm_pipeline_incremental_test_force_legacy_partial_once(); + store = ei_rewrite_and_reindex(&lp, store, &edit2); + ASSERT_NOT_NULL(store); + ASSERT_EQ((int)cbm_pipeline_incremental_test_last_route(), + (int)CBM_INCREMENTAL_ROUTE_LEGACY_PARTIAL); + ASSERT_TRUE(ei_scala_incr_consumer_edges_ok(store, lp.project)); + + ei_cleanup(&lp, store); + PASS(); +} + /* ═══════════════════════════════════════════════════════════════════════════ * RED REPRODUCTION — Rust * @@ -1970,8 +2419,8 @@ static int ei_php_null_map_candidate(bool direct_collision) { .local_name = "fmt", .module_path = "Lib\\render", .kind = CBM_IMPORT_KIND_FUNCTION}; CBMHashTable *saved_pkgmap = cbm_pipeline_get_pkgmap(); cbm_pipeline_set_pkgmap(NULL); - const cbm_gbuf_node_t *target = - cbm_pipeline_resolve_import_node(&ctx, "main.php", "phpimports.main.__file__", &imp, NULL); + const cbm_gbuf_node_t *target = cbm_pipeline_resolve_import_node( + &ctx, "main.php", "phpimports.main.__file__", CBM_LANG_PHP, &imp, NULL, NULL); int ok = decoy > 0 && valid > 0 && target && target->id == valid; cbm_pipeline_set_pkgmap(saved_pkgmap); cbm_gbuf_free(gbuf); @@ -1996,8 +2445,8 @@ TEST(ei_php_require_path_keeps_generic_resolution_issue1186) { CBMHashTable *map = cbm_pipeline_namespace_map_build_names("phpimports", NULL, NULL, 0); CBMHashTable *saved_pkgmap = cbm_pipeline_get_pkgmap(); cbm_pipeline_set_pkgmap(NULL); - const cbm_gbuf_node_t *target = - cbm_pipeline_resolve_import_node(&ctx, "main.php", "phpimports.main.__file__", &imp, map); + const cbm_gbuf_node_t *target = cbm_pipeline_resolve_import_node( + &ctx, "main.php", "phpimports.main.__file__", CBM_LANG_PHP, &imp, map, NULL); int ok = map && valid > 0 && target && target->id == valid; cbm_pipeline_set_pkgmap(saved_pkgmap); cbm_pipeline_namespace_map_free(map); @@ -2641,6 +3090,18 @@ SUITE(edge_imports) { RUN_TEST(ei_py_external_base_never_inherits_project_class); RUN_TEST(ei_cpp_header_include_targets_header_file); + /* ── GREEN GUARDS — Scala package-aware resolution ── */ + RUN_TEST(ei_scala_package_imports_ignore_physical_layout); + RUN_TEST(ei_java_import_ignores_scala_package_prefix); + RUN_TEST(ei_scala_imports_java_class_from_mixed_package); + RUN_TEST(ei_java_import_of_mixed_package_binds_java_file); + RUN_TEST(ei_scala_split_package_same_name_top_level_is_ambiguous); + RUN_TEST(ei_scala_import_ignores_nested_same_name); + RUN_TEST(ei_scala_unresolvable_member_import_yields_no_owner_edge); + RUN_TEST(ei_scala_chained_package_clauses_resolve); + RUN_TEST(ei_scala_import_ignores_java_nested_type_of_same_name); + RUN_TEST(ei_scala_incremental_reindex_keeps_imports_into_unchanged_packages); + /* ── RED REPRODUCTIONS — Rust (expected to FAIL until pipeline fixed) ── */ RUN_TEST(ei_rust_mod_plus_use); RUN_TEST(ei_rust_use_crate_path); diff --git a/tests/test_extraction.c b/tests/test_extraction.c index 2cb9fcffc4..4d1ac49dea 100644 --- a/tests/test_extraction.c +++ b/tests/test_extraction.c @@ -67,6 +67,17 @@ static int __attribute__((unused)) has_import(CBMFileResult *r, const char *path return 0; } +static const CBMImport *find_import(CBMFileResult *r, const char *local, const char *path) { + for (int i = 0; i < r->imports.count; i++) { + const CBMImport *imp = &r->imports.items[i]; + if (imp->local_name && imp->module_path && strcmp(imp->local_name, local) == 0 && + strcmp(imp->module_path, path) == 0) { + return imp; + } + } + return NULL; +} + /* Count definitions with a given label. */ /* Check for a definition with the given qualified name. Distinct from * find_def_by_name, which returns the first match by NAME and so cannot tell two @@ -10315,6 +10326,65 @@ TEST(extract_ts_member_call_flags_is_method) { PASS(); } +TEST(extract_scala_import_selectors_and_aliases) { + CBMFileResult *r = extract("import foo.Direct\n" + "import foo.{Selected, Original => Alias, Hidden => _, _}\n" + "import modern.{Source as ModernAlias, *}\n" + "import alpha.One, beta.Two\n", + CBM_LANG_SCALA, "t", "Imports.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + ASSERT_NOT_NULL(find_import(r, "Direct", "foo.Direct")); + ASSERT_NOT_NULL(find_import(r, "Selected", "foo.Selected")); + ASSERT_NOT_NULL(find_import(r, "Alias", "foo.Original")); + ASSERT_NULL(find_import(r, "Hidden", "foo.Hidden")); + ASSERT_NOT_NULL(find_import(r, "*", "foo")); + ASSERT_NOT_NULL(find_import(r, "ModernAlias", "modern.Source")); + ASSERT_NOT_NULL(find_import(r, "*", "modern")); + ASSERT_NOT_NULL(find_import(r, "One", "alpha.One")); + ASSERT_NOT_NULL(find_import(r, "Two", "beta.Two")); + cbm_free_result(r); + PASS(); +} + +TEST(extract_scala_package_namespace) { + CBMFileResult *r = extract("package com.example.app.account\n" + "import com.example.app.model.Target\n" + "object Consumer\n", + CBM_LANG_SCALA, "t", "modules/client/Consumer.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + ASSERT_NOT_NULL(r->namespace_name); + ASSERT_STR_EQ(r->namespace_name, "com.example.app.account"); + cbm_free_result(r); + PASS(); +} + +TEST(extract_scala_chained_package_clauses) { + CBMFileResult *r = extract("package com.example\n" + "// chained clause below\n" + "package app.account\n" + "import com.example.app.model.Target\n" + "object Consumer\n", + CBM_LANG_SCALA, "t", "modules/client/Consumer.scala"); + ASSERT_NOT_NULL(r); + ASSERT_FALSE(r->has_error); + ASSERT_NOT_NULL(r->namespace_name); + ASSERT_STR_EQ(r->namespace_name, "com.example.app.account"); + cbm_free_result(r); + + /* A braced clause scopes only its body; the file namespace stays the outer one. */ + r = extract("package com.example\n" + "package braced { object Inner }\n" + "object Outer\n", + CBM_LANG_SCALA, "t", "Outer.scala"); + ASSERT_NOT_NULL(r); + ASSERT_NOT_NULL(r->namespace_name); + ASSERT_STR_EQ(r->namespace_name, "com.example"); + cbm_free_result(r); + PASS(); +} + /* this/super receivers keep the enclosing-class target, where a weak * namespace-proximity match is usually correct — so they are NOT flagged. A * new_expression has no member receiver and is never flagged either. */ @@ -15771,6 +15841,9 @@ SUITE(extraction) { RUN_TEST(extract_templ_method_qn_matches_call_scope); RUN_TEST(extract_go_multiple_init_disambiguated); RUN_TEST(extract_ts_member_call_flags_is_method); + RUN_TEST(extract_scala_import_selectors_and_aliases); + RUN_TEST(extract_scala_package_namespace); + RUN_TEST(extract_scala_chained_package_clauses); RUN_TEST(extract_ts_this_super_receiver_not_flagged); RUN_TEST(extract_js_member_call_flags_is_method); diff --git a/tests/test_index_format.c b/tests/test_index_format.c index a7c2e1a053..b94467b01d 100644 --- a/tests/test_index_format.c +++ b/tests/test_index_format.c @@ -459,10 +459,120 @@ TEST(index_format_v2_images_rebuild_without_invented_host) { PASS(); } +/* Format 5 stored no `package` property on File nodes (#2360). An incremental + * refresh reads the declared packages of the files it does not re-extract back + * from that property, so a format-5 index refreshed in place would resolve a + * Scala import into an unchanged package against an incomplete package map and + * the fail-closed resolver would drop the edge until the next full index. */ +static const RFile k_scala_files[] = { + {"model/src/Target.scala", "package com.acme.model\nobject Target { def build(): Int = 1 }\n"}, + {"model/src/Aux.scala", "package com.acme.model\nobject Aux { def value: Int = 2 }\n"}, + {"client/src/Consumer.scala", "package com.acme.client\n" + "import com.acme.model.Target\n" + "object Consumer { def run(): Int = Target.build() }\n"}, +}; + +/* IMPORTS edges from Consumer.scala's File node to a node named Target. */ +static int scala_consumer_target_imports(cbm_store_t *s, const char *project) { + cbm_node_t *files = NULL; + int nfiles = 0; + if (cbm_store_find_nodes_by_name(s, project, "Consumer.scala", &files, &nfiles) != + CBM_STORE_OK) { + return -1; + } + int hits = 0; + for (int f = 0; f < nfiles && hits >= 0; f++) { + if (!files[f].label || strcmp(files[f].label, "File") != 0) { + continue; + } + cbm_edge_t *edges = NULL; + int n = 0; + if (cbm_store_find_edges_by_source_type(s, files[f].id, "IMPORTS", &edges, &n) != + CBM_STORE_OK) { + hits = -1; + break; + } + for (int e = 0; e < n; e++) { + cbm_node_t t = {0}; + if (cbm_store_find_node_by_id(s, edges[e].target_id, &t) == CBM_STORE_OK && t.name && + strcmp(t.name, "Target") == 0) { + hits++; + } + cbm_node_free_fields(&t); + } + cbm_store_free_edges(edges, n); + } + cbm_store_free_nodes(files, nfiles); + return hits; +} + +static int scala_files_with_package(cbm_store_t *s) { + sqlite3_stmt *st = NULL; + if (sqlite3_prepare_v2(cbm_store_get_db(s), + "SELECT COUNT(*) FROM nodes WHERE label='File' AND json_valid(properties)" + " AND json_extract(properties, '$.package') IS NOT NULL", + -1, &st, NULL) != SQLITE_OK) { + return -1; + } + int count = sqlite3_step(st) == SQLITE_ROW ? sqlite3_column_int(st, 0) : -1; + sqlite3_finalize(st); + return count; +} + +TEST(index_format_v5_scala_packages_rebuild) { + RProj lp; + cbm_store_t *q0 = rh_index_files(&lp, k_scala_files, 3); + ASSERT_NOT_NULL(q0); + ASSERT_EQ(scala_consumer_target_imports(q0, lp.project), 1); + cbm_store_close(q0); + + /* The old shape: no File node carries its package clause. */ + cbm_store_t *w = cbm_store_open_path(lp.dbpath); + ASSERT_NOT_NULL(w); + ASSERT_EQ(sqlite3_exec(cbm_store_get_db(w), + "UPDATE nodes SET properties = json_remove(properties, '$.package') " + "WHERE label='File' AND json_valid(properties) " + "AND json_extract(properties, '$.package') IS NOT NULL", + NULL, NULL, NULL), + SQLITE_OK); + ASSERT_EQ(sqlite3_changes(cbm_store_get_db(w)), 3); + ASSERT_EQ(cbm_store_set_format_version(w, 5), CBM_STORE_OK); + cbm_store_close(w); + + /* A body-only edit of the importer: refreshed in place, only Consumer.scala + * would be re-resolved, against the clauses stored on the other files. */ + char path[700]; + snprintf(path, sizeof(path), "%s/client/src/Consumer.scala", lp.tmpdir); + FILE *fp = fopen(path, "wb"); + ASSERT_NOT_NULL(fp); + fputs("package com.acme.client\n" + "import com.acme.model.Target\n" + "object Consumer { def run(): Int = Target.build() + 1 }\n", + fp); + fclose(fp); + + char *resp = index_capture(&lp); + ASSERT_NOT_NULL(resp); + free(resp); + cbm_store_t *r = cbm_store_open_path(lp.dbpath); + ASSERT_NOT_NULL(r); + ASSERT_EQ(scala_consumer_target_imports(r, lp.project), 1); + if (!strstr(g_log_buf, "format_change_reindex")) { + FAIL("a format-5 index was not routed through the full-reindex path"); + } + ASSERT_EQ(scala_files_with_package(r), 3); + int format = -1; + ASSERT_EQ(cbm_store_get_format_version(r, &format), CBM_STORE_OK); + ASSERT_EQ(format, CBM_INDEX_FORMAT_VERSION); + rh_cleanup(&lp, r); + PASS(); +} + SUITE(index_format) { RUN_TEST(index_format_siblings_distinct_and_searchable); RUN_TEST(index_format_legacy_index_rebuilds_and_repairs); RUN_TEST(index_format_version_one_rebuilds); RUN_TEST(index_format_swift_collisions_and_stale_calls_rebuild); RUN_TEST(index_format_v2_images_rebuild_without_invented_host); + RUN_TEST(index_format_v5_scala_packages_rebuild); } diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c index c1e6d01855..124a1514d1 100644 --- a/tests/test_pipeline.c +++ b/tests/test_pipeline.c @@ -2075,6 +2075,44 @@ static bool cross_file_call_exists(cbm_store_t *s, const char *project, const ch return cross_file_edge_exists(s, project, src_name, tgt_name, "CALLS"); } +static bool cross_file_call_to_owner_has_strategy(cbm_store_t *s, const char *project, + const char *src_name, const char *tgt_name, + const char *owner_fragment, + const char *strategy_fragment) { + cbm_node_t *srcs = NULL; + cbm_node_t *tgts = NULL; + int sc = 0; + int tc = 0; + cbm_store_find_nodes_by_name(s, project, src_name, &srcs, &sc); + cbm_store_find_nodes_by_name(s, project, tgt_name, &tgts, &tc); + bool found = false; + for (int i = 0; i < sc && !found; i++) { + cbm_edge_t *edges = NULL; + int ec = 0; + cbm_store_find_edges_by_source_type(s, srcs[i].id, "CALLS", &edges, &ec); + for (int j = 0; j < ec && !found; j++) { + for (int k = 0; k < tc; k++) { + if (edges[j].target_id == tgts[k].id && tgts[k].qualified_name && + strstr(tgts[k].qualified_name, owner_fragment) && edges[j].properties_json && + strstr(edges[j].properties_json, strategy_fragment)) { + found = true; + break; + } + } + } + if (edges) { + cbm_store_free_edges(edges, ec); + } + } + if (srcs) { + cbm_store_free_nodes(srcs, sc); + } + if (tgts) { + cbm_store_free_nodes(tgts, tc); + } + return found; +} + /* True iff the exact named CALLS edge exists and its serialized strategy * contains `strategy_fragment`. Parallel synthetic-carrier regressions use * this on a separate ordinary-call control: it proves the cross-file LSP ran @@ -14873,6 +14911,280 @@ TEST(pipeline_c_member_access_binds_field_by_object_type_parallel) { PASS(); } +static void write_scala_import_alias_fixture(const char *tmp) { + write_temp_file(tmp, "alias_targets.scala", + "package alias_targets\n" + "object CorrectObject { def execute(): Int = 1 }\n" + "object WrongObject { def execute(): Int = 2 }\n" + "object ExternalStateRef { def make(): Int = 3 }\n"); + write_temp_file(tmp, "alias_caller.scala", + "package alias_caller\n" + "import alias_targets.{CorrectObject => ImportedAlias}\n" + "import outside.library.ExternalStateRef\n" + "object AliasCalls {\n" + " def invokeAlias(): Int = ImportedAlias.execute()\n" + " def invokeExternal(): Int = ExternalStateRef.make()\n" + "}\n"); +} + +static bool scala_import_alias_edge_is_exact(cbm_store_t *s, const char *project) { + cbm_edge_t *imports = NULL; + int import_count = 0; + bool exact_call = cross_file_call_to_owner_has_strategy(s, project, "invokeAlias", "execute", + "CorrectObject.execute", "import_map"); + cbm_store_find_edges_by_type(s, project, "IMPORTS", &imports, &import_count); + if (imports) { + cbm_store_free_edges(imports, import_count); + } + return exact_call && import_count == 1; +} + +TEST(pipeline_scala_import_alias_resolves_exact_method) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_alias_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_scala_import_alias_fixture(tmp); + + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_alias.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + ASSERT_TRUE(scala_import_alias_edge_is_exact(s, cbm_pipeline_project_name(p))); + + cbm_store_close(s); + cbm_pipeline_free(p); + th_rmtree(tmp); + PASS(); +} + +/* `import pkg.Owner.member` when the package is split across modules and a + * second file in the same package declares a same-named member: the import + * must bind to Owner.member, not degrade to the owner (which would drop the + * unqualified `member()` call). */ +TEST(pipeline_scala_member_import_in_split_package_binds_owner_member) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_member_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_temp_file(tmp, "main_parser.scala", + "package split_pkg\n" + "object Parser { def check(s: String): Boolean = true }\n"); + write_temp_file(tmp, "bench_parser.scala", + "package split_pkg\n" + "class ParserBench { def check(s: String): Boolean = false }\n"); + write_temp_file(tmp, "member_caller.scala", + "package member_caller\n" + "import split_pkg.Parser.check\n" + "object Decoder { def validate(s: String): Boolean = check(s) }\n"); + + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_member.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + ASSERT_TRUE(cross_file_call_to_owner_has_strategy(s, cbm_pipeline_project_name(p), "validate", + "check", "Parser.check", "import_map")); + + cbm_store_close(s); + cbm_pipeline_free(p); + th_rmtree(tmp); + PASS(); +} + +/* Only the Scala resolver reads the top-level index, so a pass whose files + * hold no Scala importer must not pay the node walk and per-symbol keys for + * it: Java/Kotlin/C#/PHP corpora get exactly the namespace map, as before. + * One Scala file among the importers builds it, whether or not that file + * declares a package (a default-package Scala file still imports). */ +TEST(pipeline_scala_index_built_only_for_scala_importers) { + cbm_gbuf_t *gb = cbm_gbuf_new("proj", "/tmp/proj"); + ASSERT_NOT_NULL(gb); + ASSERT_GT(cbm_gbuf_upsert_node(gb, "File", "Codec.java", "proj.core.Codec.__file__", + "core/Codec.java", 0, 0, "{}"), + 0); + ASSERT_GT(cbm_gbuf_upsert_node(gb, "Class", "Codec", "proj.core.Codec", "core/Codec.java", 1, 3, + "{}"), + 0); + ASSERT_GT(cbm_gbuf_upsert_node(gb, "File", "App.scala", "proj.app.App.__file__", + "app/App.scala", 0, 0, "{}"), + 0); + atomic_int cancelled = 0; + cbm_pipeline_ctx_t ctx = { + .project_name = "proj", + .repo_path = "/tmp/proj", + .gbuf = gb, + .cancelled = &cancelled, + }; + + cbm_file_info_t java_only[] = {{.rel_path = "core/Codec.java", .language = CBM_LANG_JAVA}}; + const char *java_ns[] = {"com.acme.core"}; + CBMHashTable *namespace_map = NULL; + cbm_scala_index_t *scala_index = NULL; + cbm_pipeline_import_maps_build(&ctx, java_only, 1, java_ns, &namespace_map, &scala_index); + ASSERT_NOT_NULL(namespace_map); + ASSERT_NULL(scala_index); + cbm_pipeline_import_maps_free(namespace_map, scala_index); + + cbm_file_info_t mixed[] = {{.rel_path = "core/Codec.java", .language = CBM_LANG_JAVA}, + {.rel_path = "app/App.scala", .language = CBM_LANG_SCALA}}; + const char *mixed_ns[] = {"com.acme.core", NULL}; + cbm_pipeline_import_maps_build(&ctx, mixed, 2, mixed_ns, &namespace_map, &scala_index); + ASSERT_NOT_NULL(namespace_map); + ASSERT_NOT_NULL(scala_index); + cbm_pipeline_import_maps_free(namespace_map, scala_index); + + cbm_gbuf_free(gb); + PASS(); +} + +TEST(pipeline_scala_import_alias_parallel_resolves_exact_method) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "/tmp/cbm_scala_alias_par_XXXXXX"); + if (!cbm_mkdtemp(tmp)) { + FAIL("tmpdir"); + } + write_scala_import_alias_fixture(tmp); + for (int i = 0; i < 52; i++) { + char name[64]; + char body[128]; + snprintf(name, sizeof(name), "alias_filler%d.scala", i); + snprintf(body, sizeof(body), "object AliasFiller%d { def value: Int = %d }\n", i, i); + write_temp_file(tmp, name, body); + } + + char *old_workers = getenv("CBM_WORKERS"); + char *saved = old_workers ? strdup(old_workers) : NULL; + cbm_setenv("CBM_WORKERS", "4", 1); + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_alias_par.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + ASSERT_NOT_NULL(p); + ASSERT_EQ(cbm_pipeline_run(p), 0); + cbm_store_t *s = cbm_store_open_path(db_path); + ASSERT_NOT_NULL(s); + ASSERT_TRUE(scala_import_alias_edge_is_exact(s, cbm_pipeline_project_name(p))); + + cbm_store_close(s); + cbm_pipeline_free(p); + if (saved) { + cbm_setenv("CBM_WORKERS", saved, 1); + free(saved); + } else { + cbm_unsetenv("CBM_WORKERS"); + } + th_rmtree(tmp); + PASS(); +} + +/* A Scala wildcard import (`import pkg._`, `import pkg.Owner._`) binds no + * local name: its IMPORTS edge carries the local name "*". Scala operators are + * method calls, so `h * width` is a call whose callee is "*" as well, and it + * must never bind to the wildcard's target through the import map. */ +static int scala_operator_calls(cbm_store_t *s) { + sqlite3_stmt *st = NULL; + if (sqlite3_prepare_v2(cbm_store_get_db(s), + "SELECT n.qualified_name, e.properties FROM edges e" + " JOIN nodes n ON n.id = e.target_id WHERE e.type = 'CALLS'" + " AND json_valid(e.properties)" + " AND json_extract(e.properties, '$.callee') = '*'", + -1, &st, NULL) != SQLITE_OK) { + return -1; + } + int n = 0; + while (sqlite3_step(st) == SQLITE_ROW) { + fprintf(stderr, " operator call bound: -> %s %s\n", sqlite3_column_text(st, 0), + sqlite3_column_text(st, 1)); + n++; + } + sqlite3_finalize(st); + return n; +} + +static int scala_wildcard_operator_case(const char *tmpl, bool parallel) { + char tmp[256]; + snprintf(tmp, sizeof(tmp), "%s", tmpl); + if (!cbm_mkdtemp(tmp)) { + return 0; + } + write_temp_file(tmp, "wild_model.scala", + "package wild_model\n" + "object Consts { val Width: Int = 10 }\n" + "object Helper { def run(): Int = 1 }\n"); + write_temp_file(tmp, "wild_caller.scala", + "package wild_caller\n" + "import wild_model._\n" + "import wild_model.Consts._\n" + "import wild_model.Helper\n" + "object Area {\n" + " def area(h: Int): Int = h * Width\n" + " def scaled(): Int = Helper.run() * 2\n" + "}\n"); + for (int i = 0; parallel && i < 52; i++) { + char name[64]; + char body[128]; + snprintf(name, sizeof(name), "wild_filler%d.scala", i); + snprintf(body, sizeof(body), "object WildFiller%d { def value: Int = %d }\n", i, i); + write_temp_file(tmp, name, body); + } + char *old_workers = getenv("CBM_WORKERS"); + char *saved = old_workers ? strdup(old_workers) : NULL; + if (parallel) { + cbm_setenv("CBM_WORKERS", "4", 1); + } + char db_path[512]; + snprintf(db_path, sizeof(db_path), "%s/scala_wild.db", tmp); + cbm_pipeline_t *p = cbm_pipeline_new(tmp, db_path, CBM_MODE_FULL); + int ok = p && cbm_pipeline_run(p) == 0; + cbm_store_t *s = ok ? cbm_store_open_path(db_path) : NULL; + if (s) { + const char *project = cbm_pipeline_project_name(p); + cbm_edge_t *imports = NULL; + int import_count = 0; + cbm_store_find_edges_by_type(s, project, "IMPORTS", &imports, &import_count); + if (imports) { + cbm_store_free_edges(imports, import_count); + } + /* Both wildcards and the named import keep their IMPORTS edges, and a + * named import still binds its calls through the import map. */ + bool named = cross_file_call_to_owner_has_strategy(s, project, "scaled", "run", + "Helper.run", "import_map"); + int operators = scala_operator_calls(s); + if (import_count != 3 || !named || operators != 0) { + fprintf(stderr, " wildcard case parallel=%d: imports=%d named=%d operator_calls=%d\n", + parallel, import_count, named, operators); + ok = 0; + } + cbm_store_close(s); + } else { + ok = 0; + } + if (p) { + cbm_pipeline_free(p); + } + if (saved) { + cbm_setenv("CBM_WORKERS", saved, 1); + free(saved); + } else if (parallel) { + cbm_unsetenv("CBM_WORKERS"); + } + th_rmtree(tmp); + return ok; +} + +TEST(pipeline_scala_wildcard_import_binds_no_operator_call) { + ASSERT_TRUE(scala_wildcard_operator_case("/tmp/cbm_scala_wild_XXXXXX", false)); + ASSERT_TRUE(scala_wildcard_operator_case("/tmp/cbm_scala_wild_par_XXXXXX", true)); + PASS(); +} + /* Count nodes with the given exact name in the project (e.g. a Route path). */ static int count_nodes_named(cbm_store_t *s, const char *project, const char *name) { cbm_node_t *ns = NULL; @@ -25684,8 +25996,8 @@ static const cbm_gbuf_node_t *resolve_device_h_from(cbm_gbuf_t *gb) { CBMImport imp = {0}; imp.module_path = "linux/device.h"; imp.local_name = "h"; - return cbm_pipeline_resolve_import_node(&ctx, "arch/x/bugs.c", "p.arch.x.bugs.c.__file__", &imp, - NULL); + return cbm_pipeline_resolve_import_node(&ctx, "arch/x/bugs.c", "p.arch.x.bugs.c.__file__", + CBM_LANG_C, &imp, NULL, NULL); } TEST(pipeline_header_include_target_is_independent_of_registration_order) { @@ -35305,6 +35617,11 @@ SUITE(pipeline) { RUN_TEST(pipeline_python_receiver_parallel_suppresses_weak_method_edges); RUN_TEST(pipeline_python_bare_local_binding_suppresses_weak_edge); RUN_TEST(pipeline_python_bare_local_binding_parallel_suppresses_weak_edge); + RUN_TEST(pipeline_scala_import_alias_resolves_exact_method); + RUN_TEST(pipeline_scala_member_import_in_split_package_binds_owner_member); + RUN_TEST(pipeline_scala_index_built_only_for_scala_importers); + RUN_TEST(pipeline_scala_import_alias_parallel_resolves_exact_method); + RUN_TEST(pipeline_scala_wildcard_import_binds_no_operator_call); RUN_TEST(pipeline_external_import_shadow_not_bound_to_local_homonym_issue1355); RUN_TEST(pipeline_external_import_shadow_keeps_workspace_sibling_issue1355); RUN_TEST(pipeline_external_import_shadow_keeps_declared_package_issue1355);