codebase-memory-mcp icon indicating copy to clipboard operation
codebase-memory-mcp copied to clipboard

Nix: `import ../path` resolves to a same-named file in the importer's directory (or is dropped)

Open achimcc opened this issue 1 month ago • 3 comments

Summary

In Nix files, an import whose path climbs with ../ doesn't resolve to the right file. If the importing file's directory happens to contain a file with the same name, the IMPORTS edge points to that file instead, which is a wrong edge. If there is no such file, the edge is missing.

Imports with ./ resolve correctly.

Version

codebase-memory-mcp 0.12.1 (release binary linux-amd64-portable, Linux x86_64)

Reproduction

mkdir -p repro/lib repro/checks && cd repro
printf '{ port = 1; }\n'          > lib/values.nix
printf '{ x }: { id = x; }\n'     > lib/helper.nix
printf '{ unrelated = true; }\n'  > checks/values.nix
cat > checks/user.nix <<'EOF'
let
  inherit (import ../lib/helper.nix { x = 1; }) id;
  port = (import ../lib/values.nix).port;
in
{ inherit id port; }
EOF
printf '{ v = (import ./lib/values.nix).port; }\n' > top.nix
git init -q && git add lib checks top.nix && git commit -qm init

codebase-memory-mcp cli index_repository --repo-path "$PWD"
codebase-memory-mcp cli query_graph --project <project from list_projects> \
  --query "MATCH (a)-[:IMPORTS]->(b) RETURN a.file_path, b.name"

Actual

checks/user.nix  checks/values.nix
top.nix          lib/values.nix

Expected

checks/user.nix  lib/helper.nix
checks/user.nix  lib/values.nix
top.nix          lib/values.nix
  • ../lib/values.nix resolves to checks/values.nix, a different, unrelated file in the importer's own directory.
  • ../lib/helper.nix gets no edge at all, because there is no checks/helper.nix.

It looks as if the ../ segments (and possibly the lib/ part) are dropped, and the basename is then looked up relative to the importing file's directory. parse_nix_imports in internal/cbm/extract_imports.c pushes the raw path text through push_path_import, so the normalization probably happens wherever that path is resolved. I haven't traced it further.

Impact

On a real NixOS flake repo with about 1,800 indexed files, laid out like this (checks/*.nix, hosts/**.nix importing ../lib/*.nix), one shared module is imported by 110 files according to grep. The graph found 9 of those imports, the 9 that use ./. It also added wrong edges wherever a same-named file existed next to the importer. Overall there were 295 IMPORTS edges for 896 import ./…/import ../… expressions.

Call edges (apply_expression) looked good in the same repo: 94 of 98 calling files were found for a widely used helper function.

achimcc avatar Oct 25 '26 15:10 achimcc

Thank you for the small Nix fixture and the ./ control. The report distinguishes a wrong same-name target from a missing import edge, which is useful for investigating path resolution. I’ll follow up after maintainer review; this acknowledgment does not confirm the suspected normalization mechanism.

DeusData avatar Oct 25 '26 17:10 DeusData

Hi! I'd like to work on this.

I traced it to the import resolver: a fallback that matches a same-named file next to the importer runs before the exact relative path lookup, so ../lib/values.nix lands on checks/values.nix. It isn't Nix-specific; a Thrift include "../shared/base.thrift" hits the same wrong edge.

I have a fix with your fixture as a regression test. On current main the ../lib/helper.nix edge already resolves, so the wrong target is the part I'm fixing. I'll open a PR shortly.

harshitaajoshi avatar Oct 25 '26 21:10 harshitaajoshi

Fixed on main by #2802 (merge 83109dde52b3). An explicit ./ or ../ import no longer binds to a same-named sibling when its real target is missing. Thank you for the precise report!

DeusData avatar Oct 27 '26 18:10 DeusData