graphify icon indicating copy to clipboard operation
graphify copied to clipboard

File node takes the label of a module-level variable when a same-stem sibling file exists (x.mjs + x.sql, const sql)

Open brogollack opened this issue 1 month ago • 3 comments

Summary

When a JS file and a file with another extension share the same stem in one directory (x.mjs + x.sql), the .mjs file node gets the disambiguated id <dir>_<stem>_mjs_<dir>_<stem>. If the .mjs also declares a module-level variable whose name equals the sibling's extension (const sql = ...), that variable is minted with the same id. Dedup keeps the variable and drops the file node, so the file node ends up labelled sql and cannot be found by its filename.

Reproduction (graphify 0.9.33 and 0.9.90, --code-only, with [sql] extra)

mkdir -p repro/esquema && cd repro && git init -q
printf 'import postgres from "postgres";\nconst sql = postgres(process.env.U);\nasync function prueba(){ return sql`select 1` }\n' > esquema/x.mjs
echo 'create table t(a int);' > esquema/x.sql
graphify extract . --code-only

Output:

[graphify] note: node 'esquema_x_mjs_esquema_x' was extracted twice from 'esquema/x.mjs' under different labels — keeping 'sql', dropping 'x.mjs'.

Resulting nodes:

esquema_x_mjs_esquema_x | sql      <- should be the file node "x.mjs"
esquema_x_prueba        | prueba()
esquema_x_sql_esquema_x | x.sql
esquema_x_t             | t

graphify explain "x.mjs" → No node matching 'x.mjs' found.

Controls

Case Result
x.mjs with const db = postgres(...) + x.sql OK: file node esquema_x_mjs_esquema_x labelled x.mjs
x.mjs with const sql = postgres(...), no x.sql OK: file node esquema_x (x.mjs), variable esquema_x_sql (sql)
x.mjs with const sql = postgres(...) + y.sql OK, same as above
x.mjs with const sql = 1 (literal) + x.sql OK: the literal is not emitted as a node

So the trigger is: a same-stem sibling file (which turns on the extension-tagged id scheme) plus a module-level symbol named like the sibling's extension.

Expected

The file node keeps its own label (x.mjs) and the variable gets a distinct id.

Impact

This is common in database repos, where a migration runner foo.mjs sits next to foo.sql and uses the postgres client conventionally named sql. In our repo it hits 3 of 3 such pairs.

brogollack avatar Oct 14 '26 05:10 brogollack

Thanks for opening this issue, @brogollack. A maintainer will take a look soon.

If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.

To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.

github-actions[bot] avatar Oct 14 '26 05:10 github-actions[bot]

I reproduced this on v8 (0.9.90), using your steps and then by calling extract() directly.

What happens is not a clash between two ids inside the .mjs. The sql variable and the file node start out with different ids, esquema_x_sql and esquema_x_mjs. Later, extract() renames each file's old id, which has the extension in it, to its final id. The old id of x.sql is esquema_x_sql, the same string the variable has, so the rename meant for x.sql also hits the variable. Now the variable and the file node share an id, and dedup keeps one of them.

That also means the sibling doesn't have to produce nodes. An x.ts with const css next to an x.css gives the same label swap. Edges that end on the variable get renamed wrongly too.

I opened #4452 with a fix. A file's old id is now renamed only on items from the file that made it. The tests cover your four controls, plus the x.ts and x.css case.

Deepjyoti-Sarmah avatar Oct 14 '26 11:10 Deepjyoti-Sarmah

This looks fixed on the v8 branch by c29950f8 (fix(extract): keep a symbol that spells a sibling's old file id on its own node (#4449)). Could a maintainer confirm and close?

omsenjalia avatar Oct 18 '26 08:10 omsenjalia