Skip to content

Commit 0baad94

Browse files
committed
fix(pipeline): resolve relative import files without a raw allocation
Resolve the exact File an explicit relative import names through cbm_repo_rel_join into stack buffers instead of the allocating relative resolver, so the raw allocator count of pass_pkgmap.c does not grow (memory-core ratchet). The helper refuses a path above the repository root and honours php_only like the other strategies in resolve_import_strategies, which main gained after the base of this branch. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 55e8479 commit 0baad94

1 file changed

Lines changed: 38 additions & 14 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2115,6 +2115,37 @@ static const cbm_gbuf_node_t *resolve_sibling_file(const cbm_pipeline_ctx_t *ctx
21152115
return found;
21162116
}
21172117

2118+
/* The File an explicit relative import (`./x`, `../x`) names exactly, for
2119+
* files whose File QN differs from the module QN Strategy 1 tries
2120+
* (`../styles/global.css`, `../layouts/Layout.astro`, #2558). Resolved in
2121+
* stack buffers by cbm_repo_rel_join, which refuses a path above the
2122+
* repository root. */
2123+
static const cbm_gbuf_node_t *resolve_relative_file(const cbm_pipeline_ctx_t *ctx,
2124+
const char *source_rel, const char *module_path,
2125+
bool php_only) {
2126+
if (!source_rel || !module_path || module_path[0] != '.' ||
2127+
!(module_path[1] == '/' || (module_path[1] == '.' && module_path[2] == '/'))) {
2128+
return NULL;
2129+
}
2130+
char dir[PKGMAP_PATH_BUF];
2131+
int dir_len = snprintf(dir, sizeof(dir), "%s", source_rel);
2132+
if (dir_len < 0 || (size_t)dir_len >= sizeof(dir)) {
2133+
return NULL;
2134+
}
2135+
char *slash = strrchr(dir, '/');
2136+
if (slash) {
2137+
*slash = '\0';
2138+
} else {
2139+
dir[0] = '\0';
2140+
}
2141+
char path[PKGMAP_PATH_BUF];
2142+
if (!cbm_repo_rel_join(dir, module_path, path, sizeof(path))) {
2143+
return NULL;
2144+
}
2145+
const cbm_gbuf_node_t *file = cbm_pipeline_file_node(ctx->gbuf, ctx->project_name, path);
2146+
return file && (!php_only || node_in_php_file(file)) ? file : NULL;
2147+
}
2148+
21182149
/* ── PHP PSR-4 class imports (#1186) ──────────────────────────────── */
21192150

21202151
typedef enum {
@@ -2389,20 +2420,13 @@ static const cbm_gbuf_node_t *resolve_import_strategies(
23892420
}
23902421
}
23912422

2392-
/* Relative non-module files can have a File QN that does not match the
2393-
* module QN above. Resolve the exact file after module and sibling lookup. */
2394-
if (imp->module_path[0] == '.' &&
2395-
(imp->module_path[1] == '/' ||
2396-
(imp->module_path[1] == '.' && imp->module_path[2] == '/'))) {
2397-
char *resolved = cbm_pipeline_resolve_relative_import(source_rel, imp->module_path);
2398-
if (resolved) {
2399-
const cbm_gbuf_node_t *file =
2400-
cbm_pipeline_file_node(ctx->gbuf, ctx->project_name, resolved);
2401-
free(resolved);
2402-
if (file) {
2403-
return file;
2404-
}
2405-
}
2423+
/* Strategy 1c: relative non-module files can have a File QN that does not
2424+
* match the module QN above. Resolve the exact file after module and
2425+
* sibling lookup. */
2426+
const cbm_gbuf_node_t *rel_file =
2427+
resolve_relative_file(ctx, source_rel, imp->module_path, php_only);
2428+
if (rel_file) {
2429+
return rel_file;
24062430
}
24072431

24082432
/* Strategy 2: namespace map. `using App.Utils`, `import com.example.Foo`,

0 commit comments

Comments
 (0)