Skip to content

Commit c1e2414

Browse files
committed
fix(pipeline): resolve parent-relative non-code imports
Signed-off-by: lorenzozanee <wyz0707@proton.me>
1 parent 879c11e commit c1e2414

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎src/pipeline/fqn.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ static char *resolve_js_relative(char *buf, size_t buf_size, const char *module_
317317
continue;
318318
}
319319
if (seg_len == FQN_DOTDOT_LEN && seg_start[0] == '.' && seg_start[FQN_SEP_LEN] == '.') {
320+
if (!buf[0]) {
321+
return NULL;
322+
}
320323
path_pop_segment(buf);
321324
continue;
322325
}

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,6 +2305,22 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
23052305
}
23062306
}
23072307

2308+
/* Relative non-module files can have a File QN that does not match the
2309+
* module QN above. Resolve the exact file after module and sibling lookup. */
2310+
if (imp->module_path[0] == '.' &&
2311+
(imp->module_path[1] == '/' ||
2312+
(imp->module_path[1] == '.' && imp->module_path[2] == '/'))) {
2313+
char *resolved = cbm_pipeline_resolve_relative_import(source_rel, imp->module_path);
2314+
if (resolved) {
2315+
const cbm_gbuf_node_t *file =
2316+
cbm_pipeline_file_node(ctx->gbuf, ctx->project_name, resolved);
2317+
free(resolved);
2318+
if (file) {
2319+
return file;
2320+
}
2321+
}
2322+
}
2323+
23082324
/* Strategy 2: namespace map. `using App.Utils`, `import com.example.Foo`,
23092325
* `use App\Utils\Helper` name a NAMESPACE (or a member of it) that the
23102326
* path-based QN cannot express. Try the full module path and progressively

‎tests/test_edge_imports.c‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,102 @@ static int ei_edge_present(const EILangFile *files, int nfiles, const char *edge
266266
return got >= floor;
267267
}
268268

269+
typedef struct {
270+
const char *importer;
271+
const char *target;
272+
} EIFileEdgeExpect;
273+
274+
static int ei_file_edges_match(const EILangFile *files, int nfiles, const EIFileEdgeExpect *want,
275+
int nwant, int expected_count) {
276+
EILangProj lp;
277+
cbm_store_t *store = ei_index_files(&lp, files, nfiles);
278+
int ok = store && cbm_store_count_edges_by_type(store, lp.project, "IMPORTS") == expected_count;
279+
if (store && !ok) {
280+
int got = cbm_store_count_edges_by_type(store, lp.project, "IMPORTS");
281+
fprintf(stderr, " [IMPORTS] FAIL count=%d expected==%d\n", got, expected_count);
282+
}
283+
for (int i = 0; store && i < nwant; i++) {
284+
int64_t source_id = ei_node_id_for_file_label(store, lp.project, want[i].importer, "File");
285+
cbm_edge_t *edges = NULL;
286+
int edge_count = 0;
287+
int found = 0;
288+
if (source_id > 0 &&
289+
cbm_store_find_edges_by_source_type(store, source_id, "IMPORTS", &edges, &edge_count) ==
290+
CBM_STORE_OK) {
291+
for (int j = 0; j < edge_count; j++) {
292+
cbm_node_t target;
293+
memset(&target, 0, sizeof(target));
294+
if (cbm_store_find_node_by_id(store, edges[j].target_id, &target) == CBM_STORE_OK) {
295+
found = target.file_path && strcmp(target.file_path, want[i].target) == 0;
296+
cbm_node_free_fields(&target);
297+
}
298+
if (found) {
299+
break;
300+
}
301+
}
302+
}
303+
cbm_store_free_edges(edges, edge_count);
304+
if (!found) {
305+
fprintf(stderr, " [IMPORTS] FAIL %s -> %s\n", want[i].importer, want[i].target);
306+
ok = 0;
307+
}
308+
}
309+
ei_cleanup(&lp, store);
310+
return ok;
311+
}
312+
313+
TEST(ei_relative_astro_css_imports_issue2558) {
314+
static const EILangFile f[] = {
315+
{"Badge.astro", "<span>outside</span>\n"},
316+
{"src/components/shared/Button.astro",
317+
"---\nexport function buttonLabel(t: string): string { return t; }\n---\n<button />\n"},
318+
{"src/components/Badge.astro", "<span>badge</span>\n"},
319+
{"src/components/Card.astro",
320+
"---\nimport Badge from './Badge.astro';\n"
321+
"import Button from './shared/Button.astro';\n---\n<div><Badge /><Button /></div>\n"},
322+
{"src/layouts/local.css", "body { margin: 0; }\n"},
323+
{"src/styles/global.css", "body { color: black; }\n"},
324+
{"src/layouts/Layout.astro",
325+
"---\nimport '../styles/global.css';\nimport './local.css';\n---\n"
326+
"<html><body><slot /></body></html>\n"},
327+
{"src/lib/format.ts", "export function formatTitle(t: string): string { return t.trim(); }\n"},
328+
{"src/lib/registry.ts",
329+
"import Badge from '../components/Badge.astro';\n"
330+
"import '../styles/global.css';\n"
331+
"import { formatTitle } from './format';\n"
332+
"export function registry(): unknown[] { return [Badge, formatTitle]; }\n"},
333+
{"src/lib/sub/deep.ts",
334+
"import { formatTitle } from '../format';\n"
335+
"import { registry } from '../registry.ts';\n"
336+
"export function deep(): string { registry(); return formatTitle('x'); }\n"},
337+
{"src/pages/index.astro",
338+
"---\nimport Layout from '../layouts/Layout.astro';\n"
339+
"import Card from '../components/Card.astro';\n"
340+
"import Outside from '../../../Badge.astro';\n"
341+
"import { formatTitle } from '../lib/format';\n"
342+
"const title = formatTitle(' Home ');\n---\n"
343+
"<Layout><h1>{title}</h1><Card /></Layout>\n"},
344+
};
345+
static const EIFileEdgeExpect want[] = {
346+
{"src/components/Card.astro", "src/components/Badge.astro"},
347+
{"src/components/Card.astro", "src/components/shared/Button.astro"},
348+
{"src/layouts/Layout.astro", "src/styles/global.css"},
349+
{"src/layouts/Layout.astro", "src/layouts/local.css"},
350+
{"src/lib/registry.ts", "src/components/Badge.astro"},
351+
{"src/lib/registry.ts", "src/styles/global.css"},
352+
{"src/lib/registry.ts", "src/lib/format.ts"},
353+
{"src/lib/sub/deep.ts", "src/lib/format.ts"},
354+
{"src/lib/sub/deep.ts", "src/lib/registry.ts"},
355+
{"src/pages/index.astro", "src/layouts/Layout.astro"},
356+
{"src/pages/index.astro", "src/components/Card.astro"},
357+
{"src/pages/index.astro", "src/lib/format.ts"},
358+
};
359+
360+
ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want,
361+
(int)(sizeof(want) / sizeof(want[0])), 12));
362+
PASS();
363+
}
364+
269365
/* ═══════════════════════════════════════════════════════════════════════════
270366
* GREEN GUARD — Python
271367
*
@@ -1604,6 +1700,8 @@ TEST(ei_php_psr4_missing_class_file_unresolved_issue1186) {
16041700
* ═══════════════════════════════════════════════════════════════════════════ */
16051701

16061702
SUITE(edge_imports) {
1703+
RUN_TEST(ei_relative_astro_css_imports_issue2558);
1704+
16071705
/* ── GREEN GUARDS — Python (must stay passing) ── */
16081706
RUN_TEST(ei_python_relative_from_import);
16091707
RUN_TEST(ei_python_absolute_import);

0 commit comments

Comments
 (0)