Skip to content

Commit b6e807d

Browse files
committed
fix(path_alias): read jsconfig.json paths when tsconfig.json has none
A folder with both files used only tsconfig.json. When that tsconfig.json declared no paths (only baseUrl or compiler flags), the jsconfig.json aliases in the same folder were never loaded, so alias imports did not resolve and their calls fell back to name guesses. Rule: per folder, tsconfig.json keeps precedence when it declares paths or has "extends" (its paths may come from the base config, which the loader does not follow). Otherwise, if jsconfig.json in the same folder declares paths, its paths are used. A baseUrl set by the tsconfig.json is kept, so the result never loses what the tsconfig.json alone gave. The scope records jsconfig.json as its source file. Tested: new path_alias_loader_jsconfig_fallback fails before and passes after. Suites path_alias, edge_imports, extraction_imports, lsp_resolution_probe, mcp, incremental: 786 passed, 4 skipped. Signed-off-by: chidev <[email protected]>
1 parent cf70a88 commit b6e807d

3 files changed

Lines changed: 145 additions & 12 deletions

File tree

‎src/pipeline/path_alias.c‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ static int cmp_scope_by_specificity(const void *a, const void *b) {
173173
/* Parse compilerOptions.paths and compilerOptions.baseUrl into an alias map.
174174
* dir_prefix is the directory of the config file relative to the repo root
175175
* (e.g. "apps/manager", or "" for repo root). Returns NULL if the file is
176-
* missing, malformed, or has neither a usable paths block nor a baseUrl. */
177-
static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char *dir_prefix) {
176+
* missing, malformed, or has neither a usable paths block nor a baseUrl.
177+
* When has_extends is non-NULL it is set to true if the file declares a
178+
* top-level "extends" (the loader does not follow it). */
179+
static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char *dir_prefix,
180+
bool *has_extends) {
181+
if (has_extends) {
182+
*has_extends = false;
183+
}
178184
FILE *f = cbm_fopen(abs_path, "r");
179185
if (!f) {
180186
return NULL;
@@ -202,6 +208,9 @@ static cbm_path_alias_map_t *load_tsconfig_file(const char *abs_path, const char
202208
return NULL;
203209
}
204210
yyjson_val *root = yyjson_doc_get_root(doc);
211+
if (has_extends && yyjson_obj_get(root, "extends")) {
212+
*has_extends = true;
213+
}
205214
yyjson_val *compiler_opts = yyjson_obj_get(root, "compilerOptions");
206215
if (!compiler_opts) {
207216
yyjson_doc_free(doc);
@@ -380,6 +389,7 @@ char *cbm_path_alias_resolve(const cbm_path_alias_map_t *map, const char *module
380389
typedef struct {
381390
char abs[CBM_SZ_512];
382391
char rel[CBM_SZ_256];
392+
char fallback_abs[CBM_SZ_512]; /* jsconfig.json beside a tsconfig.json, or "" */
383393
} alias_config_hit_t;
384394

385395
static const char *const TS_CONFIG_NAMES[] = {"tsconfig.json", "jsconfig.json"};
@@ -396,18 +406,28 @@ static void find_alias_files(const char *abs_dir, const char *rel_dir, alias_con
396406
return;
397407
}
398408

399-
/* One config file per directory: prefer tsconfig.json over jsconfig.json. */
409+
/* One config file per directory: prefer tsconfig.json over jsconfig.json.
410+
* A jsconfig.json beside the tsconfig.json is kept as the fallback the
411+
* loader uses when the tsconfig.json declares no paths. */
412+
bool found = false;
400413
for (int i = 0; i < TS_CONFIG_NAMES_COUNT && *count < max_count; i++) {
401414
char check[CBM_SZ_512];
402415
snprintf(check, sizeof(check), "%s/%s", abs_dir, TS_CONFIG_NAMES[i]);
403416
FILE *f = cbm_fopen(check, "r");
404-
if (f) {
405-
fclose(f);
406-
snprintf(out[*count].abs, sizeof(out[*count].abs), "%s", check);
407-
snprintf(out[*count].rel, sizeof(out[*count].rel), "%s", rel_dir);
408-
(*count)++;
417+
if (!f) {
418+
continue;
419+
}
420+
fclose(f);
421+
if (found) {
422+
snprintf(out[*count - 1].fallback_abs, sizeof(out[*count - 1].fallback_abs), "%s",
423+
check);
409424
break;
410425
}
426+
snprintf(out[*count].abs, sizeof(out[*count].abs), "%s", check);
427+
snprintf(out[*count].rel, sizeof(out[*count].rel), "%s", rel_dir);
428+
out[*count].fallback_abs[0] = '\0';
429+
(*count)++;
430+
found = true;
411431
}
412432

413433
cbm_dirent_t *ent;
@@ -481,12 +501,35 @@ cbm_path_alias_collection_t *cbm_load_path_aliases_excluded(const char *repo_pat
481501
}
482502

483503
for (int i = 0; i < count; i++) {
484-
cbm_path_alias_map_t *map = load_tsconfig_file(hits[i].abs, hits[i].rel);
504+
bool has_extends = false;
505+
cbm_path_alias_map_t *map = load_tsconfig_file(hits[i].abs, hits[i].rel, &has_extends);
506+
const char *selected_abs = hits[i].abs;
507+
/* jsconfig.json fallback. A tsconfig.json that declares no paths and
508+
* extends nothing gives the folder no aliases; a jsconfig.json in the
509+
* same folder that declares paths then supplies them. The tsconfig
510+
* baseUrl stays when it set one, so the result never loses what the
511+
* tsconfig alone gave. A tsconfig with "extends" may inherit paths
512+
* from its base, so it keeps precedence. */
513+
if (hits[i].fallback_abs[0] && !has_extends && (!map || map->count == 0)) {
514+
cbm_path_alias_map_t *js = load_tsconfig_file(hits[i].fallback_abs, hits[i].rel, NULL);
515+
if (js && js->count > 0) {
516+
if (map && map->base_url) {
517+
free(js->base_url);
518+
js->base_url = map->base_url;
519+
map->base_url = NULL;
520+
}
521+
free_alias_map(map);
522+
map = js;
523+
selected_abs = hits[i].fallback_abs;
524+
} else {
525+
free_alias_map(js);
526+
}
527+
}
485528
if (!map) {
486529
continue;
487530
}
488-
const char *base = strrchr(hits[i].abs, '/');
489-
base = base ? base + 1 : hits[i].abs;
531+
const char *base = strrchr(selected_abs, '/');
532+
base = base ? base + 1 : selected_abs;
490533
char selected_rel[CBM_SZ_512];
491534
if (hits[i].rel[0]) {
492535
snprintf(selected_rel, sizeof(selected_rel), "%s/%s", hits[i].rel, base);

‎src/pipeline/path_alias.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ typedef struct {
5555
* pick up the map of the nearest ancestor scope. */
5656
typedef struct {
5757
char *dir_prefix;
58-
char *source_rel_path; /* selected config file (tsconfig beats jsconfig) */
58+
char *source_rel_path; /* selected config file (tsconfig beats jsconfig
59+
* unless it has no paths and no extends) */
5960
cbm_path_alias_map_t *map;
6061
} cbm_path_alias_scope_t;
6162

‎tests/test_path_alias.c‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,94 @@ TEST(path_alias_loader_honors_discovery_exclusions) {
409409
PASS();
410410
}
411411

412+
/* ── jsconfig.json fallback beside a tsconfig.json without paths ── */
413+
414+
/* A folder holds both configs. The tsconfig.json keeps precedence when it
415+
* declares paths or extends another config; otherwise the jsconfig.json
416+
* paths are used. Made-up fixture: root (fallback), pkg-a (tsconfig paths
417+
* win), pkg-b (tsconfig extends, so its jsconfig is not read). */
418+
TEST(path_alias_loader_jsconfig_fallback) {
419+
char tmpl[256];
420+
snprintf(tmpl, sizeof(tmpl), "/tmp/cbm_palias_jsfb_XXXXXX");
421+
char *root = cbm_mkdtemp(tmpl);
422+
ASSERT_NOT_NULL(root);
423+
424+
char path[512];
425+
snprintf(path, sizeof(path), "%s/pkg-a", root);
426+
cbm_mkdir(path);
427+
snprintf(path, sizeof(path), "%s/pkg-b", root);
428+
cbm_mkdir(path);
429+
430+
static const struct {
431+
const char *rel;
432+
const char *body;
433+
} files[] = {
434+
{"tsconfig.json", "{ \"compilerOptions\": { \"baseUrl\": \".\", \"strict\": true } }\n"},
435+
{"jsconfig.json", "{ \"compilerOptions\": { \"baseUrl\": \".\",\n"
436+
" \"paths\": { \"@app/*\": [\"src/app/*\"] } } }\n"},
437+
{"pkg-a/tsconfig.json",
438+
"{ \"compilerOptions\": { \"paths\": { \"@a/*\": [\"./lib/*\"] } } }\n"},
439+
{"pkg-a/jsconfig.json",
440+
"{ \"compilerOptions\": { \"paths\": { \"@a/*\": [\"./other/*\"] } } }\n"},
441+
{"pkg-b/tsconfig.json", "{ \"extends\": \"../tsconfig.json\" }\n"},
442+
{"pkg-b/jsconfig.json",
443+
"{ \"compilerOptions\": { \"paths\": { \"@b/*\": [\"./src/*\"] } } }\n"},
444+
};
445+
enum { N_FILES = sizeof(files) / sizeof(files[0]) };
446+
for (int i = 0; i < N_FILES; i++) {
447+
snprintf(path, sizeof(path), "%s/%s", root, files[i].rel);
448+
ASSERT_EQ(write_file(path, files[i].body), 0);
449+
}
450+
451+
cbm_path_alias_collection_t *coll = cbm_load_path_aliases(root);
452+
ASSERT_NOT_NULL(coll);
453+
ASSERT_EQ(coll->count, 2);
454+
455+
/* Root: the tsconfig.json has no paths, so the jsconfig.json supplies them. */
456+
const cbm_path_alias_map_t *m = cbm_path_alias_find_for_file(coll, "src/feature/x.js");
457+
ASSERT_NOT_NULL(m);
458+
char *r = cbm_path_alias_resolve(m, "@app/util");
459+
ASSERT_NOT_NULL(r);
460+
ASSERT_STR_EQ(r, "src/app/util");
461+
free(r);
462+
bool root_source_is_jsconfig = false;
463+
for (int i = 0; i < coll->count; i++) {
464+
if (coll->scopes[i].dir_prefix[0] == '\0') {
465+
root_source_is_jsconfig =
466+
strcmp(coll->scopes[i].source_rel_path, "jsconfig.json") == 0;
467+
}
468+
}
469+
ASSERT_TRUE(root_source_is_jsconfig);
470+
471+
/* pkg-a: the tsconfig.json declares paths, so it wins. */
472+
m = cbm_path_alias_find_for_file(coll, "pkg-a/src/x.ts");
473+
ASSERT_NOT_NULL(m);
474+
r = cbm_path_alias_resolve(m, "@a/thing");
475+
ASSERT_NOT_NULL(r);
476+
ASSERT_STR_EQ(r, "pkg-a/lib/thing");
477+
free(r);
478+
479+
/* pkg-b: the tsconfig.json extends another config, so the jsconfig.json
480+
* beside it is not read; the file falls back to the root scope. */
481+
m = cbm_path_alias_find_for_file(coll, "pkg-b/src/x.ts");
482+
ASSERT_NOT_NULL(m);
483+
r = cbm_path_alias_resolve(m, "@b/thing");
484+
ASSERT_NULL(r);
485+
486+
cbm_path_alias_collection_free(coll);
487+
488+
for (int i = N_FILES - 1; i >= 0; i--) {
489+
snprintf(path, sizeof(path), "%s/%s", root, files[i].rel);
490+
unlink(path);
491+
}
492+
snprintf(path, sizeof(path), "%s/pkg-a", root);
493+
rmdir(path);
494+
snprintf(path, sizeof(path), "%s/pkg-b", root);
495+
rmdir(path);
496+
rmdir(root);
497+
PASS();
498+
}
499+
412500
/* ── Loader returns NULL when no configs found ─────────────────── */
413501

414502
TEST(path_alias_loader_no_configs) {
@@ -580,6 +668,7 @@ void suite_path_alias(void) {
580668
RUN_TEST(path_alias_loader_monorepo);
581669
RUN_TEST(path_alias_loader_monorepo_dotdot_climb);
582670
RUN_TEST(path_alias_loader_honors_discovery_exclusions);
671+
RUN_TEST(path_alias_loader_jsconfig_fallback);
583672
RUN_TEST(path_alias_loader_no_configs);
584673
RUN_TEST(path_alias_loader_skips_linked_directory);
585674
#ifdef _WIN32

0 commit comments

Comments
 (0)