Skip to content

Commit 9b7d63f

Browse files
authored
Merge pull request DeusData#1931 from ilyabrykau-orca/fix/go-cgo-pseudo-import
fix(extract): drop cgo's `import "C"` pseudo-package
2 parents 28b4093 + 5cb90f6 commit 9b7d63f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎internal/cbm/extract_imports.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ static void parse_go_import_spec(CBMExtractCtx *ctx, TSNode spec) {
133133
return;
134134
}
135135

136+
/* cgo: `import "C"` names the cgo PSEUDO-package, not a real one — "C" is
137+
* reserved by go/build and no package with that import path can exist.
138+
* Emitting it as an ordinary import makes the import resolver fall through
139+
* to its symbol-name fallback (cbm_pipeline_resolve_import_node Strategy 3,
140+
* pass_pkgmap.c), which matches the literal name "C" against every project
141+
* node called C and picks the lexicographically smallest: on a real Go repo
142+
* all 27 cgo files pointed their IMPORTS edge at the same unrelated `C`
143+
* member of a test helper. Drop it — the C side is not a graph package, and
144+
* the file's remaining imports are unaffected. */
145+
if (strcmp(path, "C") == 0) {
146+
return;
147+
}
148+
136149
TSNode name_node = ts_node_child_by_field_name(spec, TS_FIELD("name"));
137150
const char *local_name =
138151
!ts_node_is_null(name_node) ? cbm_node_text(a, name_node, ctx->source) : path_last(a, path);

‎tests/test_extraction.c‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,6 +3215,25 @@ TEST(go_imports) {
32153215
PASS();
32163216
}
32173217

3218+
/* cgo's `import "C"` is a pseudo-package, not a real import: keeping it lets the
3219+
* import resolver name-match "C" onto an arbitrary project symbol called C. The
3220+
* real imports of the same file must survive. */
3221+
TEST(go_cgo_pseudo_import_dropped) {
3222+
CBMFileResult *r = extract("package m\n\n/*\nstatic int helper(void) { return 1; }\n*/\n"
3223+
"import \"C\"\n\nimport \"fmt\"\n\n"
3224+
"func Run() { fmt.Println(C.helper()) }\n",
3225+
CBM_LANG_GO, "t", "cgo.go");
3226+
ASSERT_NOT_NULL(r);
3227+
ASSERT_FALSE(r->has_error);
3228+
ASSERT(has_import(r, "fmt"));
3229+
for (int i = 0; i < r->imports.count; i++) {
3230+
ASSERT_NOT_NULL(r->imports.items[i].module_path);
3231+
ASSERT_TRUE(strcmp(r->imports.items[i].module_path, "C") != 0);
3232+
}
3233+
cbm_free_result(r);
3234+
PASS();
3235+
}
3236+
32183237
/* #1935: Go struct fields were never extracted — find_class_body() returns the
32193238
* struct_type node, whose only named child is a field_declaration_list, so the
32203239
* member loop matched nothing and every field was silently skipped (0 Field
@@ -8422,6 +8441,7 @@ SUITE(extraction) {
84228441
RUN_TEST(python_imports);
84238442
RUN_TEST(js_imports);
84248443
RUN_TEST(go_imports);
8444+
RUN_TEST(go_cgo_pseudo_import_dropped);
84258445
RUN_TEST(extract_go_struct_fields_have_nodes);
84268446
RUN_TEST(java_imports);
84278447
RUN_TEST(rust_imports);

0 commit comments

Comments
 (0)