Skip to content

Commit c8e7a3f

Browse files
committed
fix(pipeline): a bare JS/TS import specifier never binds a project symbol by name
A JS/TS specifier names a module, not a symbol. When module resolution misses a bare package specifier (a Node built-in such as 'path', or a workspace package whose exports point at gitignored dist/), the symbol-name fallback matched a path segment against any project symbol, and the import map then sent every call of the imported name there at import_map confidence 0.95. Treat such a specifier as external, and resolve workspace packages through their TypeScript source condition and subpath exports (DeusData#1732).
1 parent 812d075 commit c8e7a3f

2 files changed

Lines changed: 180 additions & 4 deletions

File tree

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 111 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,50 @@ static char *extract_quoted(const char *p, const char *end) {
247247

248248
/* ── Language-specific manifest parsers ────────────────────────── */
249249

250-
/* Resolve JS/TS entry point from exports["."] object. */
250+
/* A TypeScript source file, not a declaration file (.d.ts / .d.mts / .d.cts). */
251+
static bool is_ts_source_path(const char *p) {
252+
if (!p) {
253+
return false;
254+
}
255+
size_t n = strlen(p);
256+
static const char *decl[] = {".d.ts", ".d.mts", ".d.cts", NULL};
257+
for (const char **d = decl; *d; d++) {
258+
size_t dl = strlen(*d);
259+
if (n >= dl && strcmp(p + n - dl, *d) == 0) {
260+
return false;
261+
}
262+
}
263+
static const char *src[] = {".ts", ".tsx", ".mts", ".cts", NULL};
264+
for (const char **s = src; *s; s++) {
265+
size_t sl = strlen(*s);
266+
if (n > sl && strcmp(p + n - sl, *s) == 0) {
267+
return true;
268+
}
269+
}
270+
return false;
271+
}
272+
273+
/* Resolve the JS/TS target of one exports entry (exports["."] or a subpath).
274+
* A condition that names a TypeScript source file wins: in a source-first
275+
* workspace "types" points at src/ while "import" points at gitignored build
276+
* output that has no graph node (#1732). Otherwise the runtime conditions. */
251277
static const char *resolve_exports_dot(yyjson_val *dot) {
252278
if (yyjson_is_str(dot)) {
253279
return yyjson_get_str(dot);
254280
}
255281
if (!yyjson_is_obj(dot)) {
256282
return NULL;
257283
}
284+
size_t ci;
285+
size_t cmax;
286+
yyjson_val *ck;
287+
yyjson_val *cv;
288+
yyjson_obj_foreach(dot, ci, cmax, ck, cv) {
289+
(void)ck;
290+
if (yyjson_is_str(cv) && is_ts_source_path(yyjson_get_str(cv))) {
291+
return yyjson_get_str(cv);
292+
}
293+
}
258294
static const char *keys[] = {"import", "default", "require"};
259295
for (int i = 0; i < (int)(sizeof(keys) / sizeof(keys[0])); i++) {
260296
yyjson_val *v = yyjson_obj_get(dot, keys[i]);
@@ -309,15 +345,43 @@ static void parse_package_json(const char *source, int source_len, const char *r
309345
}
310346

311347
const char *entry = resolve_pkg_entry(root);
312-
if (entry) {
313-
char *dir = path_dirname(rel_path);
348+
char *dir = path_dirname(rel_path);
349+
if (entry && dir) {
314350
char *resolved = join_and_strip(dir, entry);
315351
if (resolved) {
316352
pkg_entries_push(entries, strdup(name), resolved);
317353
}
318-
free(dir);
319354
}
320355

356+
/* Subpath exports: `"./xdg-paths": {...}` makes `<name>/xdg-paths` an
357+
* importable specifier with its own target. Without it the subpath was
358+
* glued onto the root entry's QN and named no node. Star patterns are
359+
* left to the existing prefix fallback. */
360+
yyjson_val *exports = yyjson_obj_get(root, "exports");
361+
if (dir && yyjson_is_obj(exports)) {
362+
size_t ei;
363+
size_t emax;
364+
yyjson_val *ek;
365+
yyjson_val *ev;
366+
yyjson_obj_foreach(exports, ei, emax, ek, ev) {
367+
const char *sub = yyjson_get_str(ek);
368+
if (!sub || sub[0] != '.' || sub[SKIP_ONE] != '/' || strchr(sub, '*')) {
369+
continue;
370+
}
371+
const char *target = resolve_exports_dot(ev);
372+
if (!target) {
373+
continue;
374+
}
375+
char key[PKGMAP_PATH_BUF];
376+
snprintf(key, sizeof(key), "%s%s", name, sub + SKIP_ONE);
377+
char *resolved = join_and_strip(dir, target);
378+
if (resolved) {
379+
pkg_entries_push(entries, strdup(key), resolved);
380+
}
381+
}
382+
}
383+
free(dir);
384+
321385
yyjson_doc_free(doc);
322386
}
323387

@@ -1819,6 +1883,44 @@ bool cbm_import_symbol_fallback_allowed(CBMLanguage lang) {
18191883
return lang != CBM_LANG_GO;
18201884
}
18211885

1886+
/* A JS/TS import specifier always names a module, never a symbol: the
1887+
* imported names live in local_name. A bare specifier (`path`, `node:fs`,
1888+
* `@scope/pkg/sub`) that Strategy 1 (relative path, tsconfig paths, package
1889+
* manifests) did not resolve is a Node built-in or a dependency outside the
1890+
* graph, and the correct result is NO edge. The name-guess fallbacks instead
1891+
* matched a path segment against any project symbol (`path` -> Router.path(),
1892+
* `@acme/config` -> a test's local `config`), and the import map then sent
1893+
* every call of the imported name there at import_map confidence. */
1894+
static bool js_external_bare_specifier(CBMLanguage lang, const char *module_path) {
1895+
if (lang != CBM_LANG_JAVASCRIPT && lang != CBM_LANG_TYPESCRIPT && lang != CBM_LANG_TSX) {
1896+
return false;
1897+
}
1898+
if (!module_path || !module_path[0]) {
1899+
return false;
1900+
}
1901+
if (strncmp(module_path, "node:", strlen("node:")) == 0) {
1902+
return true;
1903+
}
1904+
/* Only an npm package-name shape: `pkg[/sub]` or `@scope/pkg[/sub]`.
1905+
* Bundler aliases (`@/x`, `~/x`) and `#x` subpath imports keep the
1906+
* fallbacks, since no package name spells them. */
1907+
const char *s = module_path;
1908+
if (*s == '@') {
1909+
s++;
1910+
if (!isalnum((unsigned char)*s)) {
1911+
return false;
1912+
}
1913+
while (*s && *s != '/') {
1914+
s++;
1915+
}
1916+
if (*s != '/') {
1917+
return false;
1918+
}
1919+
s++;
1920+
}
1921+
return isalnum((unsigned char)*s) != 0;
1922+
}
1923+
18221924
/* A Python def or class declared inside a function body -- a closure, a
18231925
* decorator's `wrapper`, a test's local class -- is a local of that function:
18241926
* no import can name it. Its QN extends a Function/Method node's
@@ -2414,6 +2516,11 @@ static const cbm_gbuf_node_t *resolve_import_strategies(
24142516
return target;
24152517
}
24162518

2519+
/* A bare JS/TS specifier Strategy 1 missed is external: no edge. */
2520+
if (js_external_bare_specifier(src_lang, imp->module_path)) {
2521+
return NULL;
2522+
}
2523+
24172524
/* Name-guess fallbacks below (Strategy 1b sibling-file, Strategy 3
24182525
* symbol-name) are gated per importing-file language — see
24192526
* cbm_import_symbol_fallback_allowed (#1934). */

‎tests/test_edge_imports.c‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,74 @@ TEST(ei_py_external_import_never_binds_project_symbol) {
894894
PASS();
895895
}
896896

897+
/* A JS/TS import specifier names a module, never a symbol. A bare specifier
898+
* that module resolution misses (`path`, a dependency outside the graph) is
899+
* external: Strategy 3 bound it to any project symbol named like a path
900+
* segment (`path` -> Router.path(), `@acme/config` -> a function `config`),
901+
* and every call of the imported name became an import_map CALLS edge at
902+
* 0.95. A pnpm workspace package whose exports point at gitignored dist/
903+
* must resolve through its TypeScript source condition, subpaths included,
904+
* so the real function keeps both callers. */
905+
static int ei_ts_bare_specifier_case(int pad) {
906+
enum { EI_TSB_BASE = 10, EI_TSB_MAX = EI_TSB_BASE + 64 };
907+
static char names[EI_TSB_MAX][32];
908+
EILangFile f[EI_TSB_MAX];
909+
int n = 0;
910+
f[n++] = (EILangFile){"pnpm-workspace.yaml", "packages:\n - 'pkgs/*'\n"};
911+
f[n++] = (EILangFile){"package.json", "{\"name\":\"root\",\"private\":true}\n"};
912+
f[n++] = (EILangFile){
913+
"pkgs/config/package.json",
914+
"{\"name\":\"@acme/config\",\"type\":\"module\",\"exports\":{"
915+
"\".\":{\"types\":\"./src/index.ts\",\"import\":\"./dist/index.js\"},"
916+
"\"./xdg-paths\":{\"types\":\"./src/xdg-paths.ts\",\"import\":\"./dist/xdg-paths.js\"}}}\n"};
917+
f[n++] = (EILangFile){"pkgs/config/src/xdg-paths.ts",
918+
"export function getStatePath(): string { return '/state'; }\n"};
919+
f[n++] = (EILangFile){"pkgs/config/src/index.ts", "export { getStatePath } from './xdg-paths';\n"};
920+
f[n++] = (EILangFile){"pkgs/app/src/use-subpath.ts",
921+
"import { getStatePath } from '@acme/config/xdg-paths';\n"
922+
"export function viaSubpath(): string { return getStatePath(); }\n"};
923+
f[n++] = (EILangFile){"pkgs/app/src/use-root.ts",
924+
"import { getStatePath } from '@acme/config';\n"
925+
"export function viaRoot(): string { return getStatePath(); }\n"};
926+
f[n++] = (EILangFile){"pkgs/app/src/use-node.ts",
927+
"import { join } from 'path';\n"
928+
"export function joined(): string { return join('a', 'b'); }\n"};
929+
f[n++] = (EILangFile){"pkgs/other/src/decoy.ts",
930+
"export function config(flow: string): number { return flow.length; }\n"};
931+
f[n++] = (EILangFile){"pkgs/other/src/router.ts",
932+
"export class Router { path(): string { return '/'; } }\n"};
933+
for (int i = 0; i < pad && n < EI_TSB_MAX; i++) {
934+
snprintf(names[n], sizeof(names[n]), "pad/mod_%02d.ts", i);
935+
f[n] = (EILangFile){names[n], "export function filler(): number { return 0; }\n"};
936+
n++;
937+
}
938+
EILangProj lp;
939+
cbm_store_t *store = ei_index_files(&lp, f, n);
940+
int bad = store ? ei_inbound_edges_on(store, lp.project, "path", "Method", "IMPORTS") : -1;
941+
bad += store ? ei_inbound_edges_on(store, lp.project, "path", "Method", "CALLS") : 0;
942+
bad += store ? ei_inbound_edges_on(store, lp.project, "config", "Function", "IMPORTS") : 0;
943+
bad += store ? ei_inbound_edges_on(store, lp.project, "config", "Function", "CALLS") : 0;
944+
int good =
945+
store ? ei_inbound_edges_on(store, lp.project, "getStatePath", "Function", "CALLS") : -1;
946+
int ok = bad == 0 && good >= 2;
947+
if (!ok) {
948+
fprintf(stderr,
949+
" [ts bare specifier pad=%d] decoy IMPORTS+CALLS=%d (want 0); "
950+
"getStatePath CALLS=%d (want >=2)\n",
951+
pad, bad, good);
952+
}
953+
ei_cleanup(&lp, store);
954+
return ok;
955+
}
956+
957+
TEST(ei_ts_bare_specifier_never_binds_project_symbol) {
958+
int sequential_ok = ei_ts_bare_specifier_case(0);
959+
int parallel_ok = ei_ts_bare_specifier_case(60);
960+
ASSERT_TRUE(sequential_ok);
961+
ASSERT_TRUE(parallel_ok);
962+
PASS();
963+
}
964+
897965
/* Base-class helper: INHERITS edges out of the (single) Class named `src`
898966
* (return value), and how many land on a node whose QN ends with
899967
* `want_qn_suffix` (*to_want); -1 when the class is missing. */
@@ -2514,6 +2582,7 @@ SUITE(edge_imports) {
25142582
RUN_TEST(ei_go_two_consumers_same_package);
25152583
RUN_TEST(ei_go_import_never_binds_symbol);
25162584
RUN_TEST(ei_py_external_import_never_binds_project_symbol);
2585+
RUN_TEST(ei_ts_bare_specifier_never_binds_project_symbol);
25172586
RUN_TEST(ei_py_external_base_never_inherits_project_class);
25182587
RUN_TEST(ei_cpp_header_include_targets_header_file);
25192588

0 commit comments

Comments
 (0)