@@ -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
21232136static 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 ];
0 commit comments