Skip to content

Commit 83109dd

Browse files
authored
Merge pull request DeusData#2802 from harshitaajoshi/fix/2797-relative-import-exact-path
fix(pipeline): never guess a same-named sibling for explicit ./ and ../ imports (DeusData#2797)
2 parents e73eb9d + 2d06fe3 commit 83109dd

2 files changed

Lines changed: 93 additions & 34 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,49 @@ static const cbm_gbuf_node_t *resolve_header_include(const cbm_pipeline_ctx_t *c
20372037
return NULL;
20382038
}
20392039

2040+
static bool explicit_relative_path(const char *p) {
2041+
return strncmp(p, "./", PAIR_LEN) == 0 || strncmp(p, "../", PAIR_LEN + SKIP_ONE) == 0;
2042+
}
2043+
2044+
enum { SIBLING_MAX_CANDS = 4 };
2045+
2046+
/* The paths resolve_sibling_file tries for module_path, relative to the
2047+
* importer's directory `dir`, in priority order. Returns how many it wrote. */
2048+
static int sibling_file_candidates(const char *dir, const char *module_path,
2049+
char cands[SIBLING_MAX_CANDS][PKGMAP_PATH_BUF]) {
2050+
int ncand = 0;
2051+
const char *sep = dir[0] ? "/" : "";
2052+
const char *base = module_path;
2053+
/* Skip a leading "./". */
2054+
if (strncmp(base, "./", PAIR_LEN) == 0) {
2055+
base += PAIR_LEN;
2056+
}
2057+
const char *slash = strrchr(base, '/');
2058+
/* 1. Direct sibling: dir/<module_path>. */
2059+
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, sep, base);
2060+
/* 2. SCSS partial: dir/[subdir/]_<basename>.scss (underscore-prefixed). */
2061+
{
2062+
const char *bn = slash ? slash + SKIP_ONE : base;
2063+
char *dpart = slash ? cbm_strndup(base, (size_t)(slash - base)) : strdup("");
2064+
if (dpart && bn[0] != '_') {
2065+
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s%s_%s.scss", dir, sep, dpart,
2066+
dpart[0] ? "/" : "", bn);
2067+
}
2068+
free(dpart);
2069+
}
2070+
/* 3. Meson subdir: dir/<module_path>/meson.build. */
2071+
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s/meson.build", dir, sep, base);
2072+
/* 4. Basename sibling: dir/<basename(module_path)>. Covers include paths
2073+
* that carry a non-relative prefix (Hyprlang `source = ~/.config/.../x.conf`,
2074+
* absolute include paths) but reference a file sitting beside the importer.
2075+
* An explicit `./` or `../` path names one location, which Strategy 1c
2076+
* resolves exactly; its basename beside the importer is another file (#2797). */
2077+
if (slash && slash[SKIP_ONE] && !explicit_relative_path(module_path)) {
2078+
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, sep, slash + SKIP_ONE);
2079+
}
2080+
return ncand;
2081+
}
2082+
20402083
/* Resolve a sibling-file import: a bare path/name (no leading "./") that names
20412084
* a file relative to the importer's directory. This covers build/markup
20422085
* grammars whose import string is a sibling filename or directory rather than a
@@ -2063,38 +2106,8 @@ static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx
20632106
return NULL;
20642107
}
20652108

2066-
/* Candidate relative paths, in priority order. */
2067-
char cands[5][PKGMAP_PATH_BUF];
2068-
int ncand = 0;
2069-
const char *base = module_path;
2070-
/* Skip a leading "./". */
2071-
if (base[0] == '.' && base[1] == '/') {
2072-
base += 2;
2073-
}
2074-
/* 1. Direct sibling: dir/<module_path>. */
2075-
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, dir[0] ? "/" : "", base);
2076-
/* 2. SCSS partial: dir/[subdir/]_<basename>.scss (underscore-prefixed). */
2077-
{
2078-
const char *slash = strrchr(base, '/');
2079-
const char *bn = slash ? slash + 1 : base;
2080-
char *dpart = slash ? cbm_strndup(base, (size_t)(slash - base)) : strdup("");
2081-
if (dpart && bn[0] != '_') {
2082-
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s%s_%s.scss", dir, dir[0] ? "/" : "",
2083-
dpart[0] ? dpart : "", dpart[0] ? "/" : "", bn);
2084-
}
2085-
free(dpart);
2086-
}
2087-
/* 3. Meson subdir: dir/<module_path>/meson.build. */
2088-
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s/meson.build", dir, dir[0] ? "/" : "", base);
2089-
/* 4. Basename sibling: dir/<basename(module_path)>. Covers include paths
2090-
* that carry a non-relative prefix (Hyprlang `source = ~/.config/.../x.conf`,
2091-
* absolute include paths) but reference a file sitting beside the importer. */
2092-
{
2093-
const char *slash = strrchr(base, '/');
2094-
if (slash && slash[1]) {
2095-
snprintf(cands[ncand++], PKGMAP_PATH_BUF, "%s%s%s", dir, dir[0] ? "/" : "", slash + 1);
2096-
}
2097-
}
2109+
char cands[SIBLING_MAX_CANDS][PKGMAP_PATH_BUF];
2110+
int ncand = sibling_file_candidates(dir, module_path, cands);
20982111

20992112
const cbm_gbuf_node_t *found = NULL;
21002113
for (int i = 0; i < ncand; i++) {
@@ -2123,8 +2136,7 @@ static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx
21232136
static const cbm_gbuf_node_t *resolve_relative_file(const cbm_pipeline_ctx_t *ctx,
21242137
const char *source_rel, const char *module_path,
21252138
bool php_only) {
2126-
if (!source_rel || !module_path || module_path[0] != '.' ||
2127-
!(module_path[1] == '/' || (module_path[1] == '.' && module_path[2] == '/'))) {
2139+
if (!source_rel || !module_path || !explicit_relative_path(module_path)) {
21282140
return NULL;
21292141
}
21302142
char dir[PKGMAP_PATH_BUF];

‎tests/test_edge_imports.c‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,51 @@ TEST(ei_relative_astro_css_imports_issue2558) {
363363
PASS();
364364
}
365365

366+
/* #2797: an explicit `../` path names one file. A same-named file beside the
367+
* importer (checks/values.nix) must not stand in for it, whether the named
368+
* file exists (lib/values.nix) or not (gone/values.nix). */
369+
TEST(ei_nix_parent_relative_import_issue2797) {
370+
static const EILangFile f[] = {
371+
{"lib/values.nix", "{ port = 1; }\n"},
372+
{"lib/helper.nix", "{ x }: { id = x; }\n"},
373+
{"checks/values.nix", "{ unrelated = true; }\n"},
374+
{"checks/user.nix", "let\n"
375+
" inherit (import ../lib/helper.nix { x = 1; }) id;\n"
376+
" port = (import ../lib/values.nix).port;\n"
377+
" gone = import ../gone/values.nix;\n"
378+
"in\n"
379+
"{ inherit id port; }\n"},
380+
{"top.nix", "{ v = (import ./lib/values.nix).port; }\n"},
381+
};
382+
static const EIFileEdgeExpect want[] = {
383+
{"checks/user.nix", "lib/helper.nix"},
384+
{"checks/user.nix", "lib/values.nix"},
385+
{"top.nix", "lib/values.nix"},
386+
};
387+
388+
ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want,
389+
(int)(sizeof(want) / sizeof(want[0])), 3));
390+
PASS();
391+
}
392+
393+
/* #2797: the same basename guess applies to any grammar whose import is a
394+
* file path, not only Nix. */
395+
TEST(ei_thrift_parent_relative_import_issue2797) {
396+
static const EILangFile f[] = {
397+
{"shared/base.thrift", "struct Base { 1: i32 id }\n"},
398+
{"service/base.thrift", "struct Unrelated { 1: bool flag }\n"},
399+
{"service/api.thrift", "include \"../shared/base.thrift\"\n"
400+
"service Api { base.Base get() }\n"},
401+
};
402+
static const EIFileEdgeExpect want[] = {
403+
{"service/api.thrift", "shared/base.thrift"},
404+
};
405+
406+
ASSERT_TRUE(ei_file_edges_match(f, (int)(sizeof(f) / sizeof(f[0])), want,
407+
(int)(sizeof(want) / sizeof(want[0])), 1));
408+
PASS();
409+
}
410+
366411
/* ═══════════════════════════════════════════════════════════════════════════
367412
* GREEN GUARD — Python
368413
*
@@ -2396,6 +2441,8 @@ TEST(ei_php_psr4_missing_class_leaves_type_evidence_unknown) {
23962441

23972442
SUITE(edge_imports) {
23982443
RUN_TEST(ei_relative_astro_css_imports_issue2558);
2444+
RUN_TEST(ei_nix_parent_relative_import_issue2797);
2445+
RUN_TEST(ei_thrift_parent_relative_import_issue2797);
23992446

24002447
/* ── GREEN GUARDS — Python (must stay passing) ── */
24012448
RUN_TEST(ei_python_relative_from_import);

0 commit comments

Comments
 (0)