graphify icon indicating copy to clipboard operation
graphify copied to clipboard

Terraform/HCL node ids embed the absolute build directory when the AST cache is reused from another location (0.9.92)

Open utkusafak opened this issue 1 month ago • 4 comments

Version: graphifyy 0.9.92 (macOS, Python 3.13)

Summary

A Terraform/HCL file's node ids depend on the absolute directory where its AST cache entry was first written. If a project (or its graphify-out/cache) is reused from another location, graphify update emits ids that contain the old absolute path. They differ from the ids a clean build produces for the same content.

Reproduce

mkdir -p /tmp/a/repo/deploy && cd /tmp/a/repo
printf 'variable "REGISTRY" {\n  default = "example"\n}\n' > deploy/bake.hcl
git init -q && git add . && git commit -qm x
graphify update . --force                       # clean build in /tmp/a

cp -R /tmp/a/repo /tmp/b/repo && cd /tmp/b/repo  # same content, cache copied
graphify update . --force

cp -R /tmp/a/repo /tmp/c/repo && rm -rf /tmp/c/repo/graphify-out && cd /tmp/c/repo
graphify update . --force                       # same content, no cache

Ids of var.REGISTRY (absolute part shortened):

  • /tmp/a (clean): terraform_deploy_<hash>_var_registry
  • /tmp/c (clean, copied): terraform_deploy_<hash>_var_registry, the same as /tmp/a
  • /tmp/b (cache reused): terraform_<…>_tmp_a_repo_deploy_<other hash>_var_registry, with the old absolute path inside the id

Python and the other extractors we use showed no such difference.

Cause (from reading the 0.9.92 source)

  • extract_terraform scopes ids with _scope_id(path.parent.resolve().as_posix()), i.e. the absolute directory. That result is stored in the per-file AST cache, which is keyed on content plus tool, schema and grammar versions, not on the path.
  • prepare_terraform (called for every per-file result, cached or fresh) rewrites only ids that start with _scope_id(<current absolute directory>).
  • A cache entry written under another root carries that root's scope, so nid.startswith(old + "_") is false and the absolute id is kept.
  • The docstring says "Make cached directory-scoped IDs portable"; this holds only when the cache was written at the same absolute path.

Suggested fixes (any one)

  • Store the portable form in the cache: run the relative re-scoping before save_cached, or scope by the path relative to the scan root from the start.
  • In prepare_terraform, recognise any absolute scope by storing the absolute directory (or its scope id) in the cached result, instead of recomputing it from the current location.
  • Include the scan-root path in the Terraform cache key.

Impact

  • Builds that run in a fresh directory each time (CI, blue-green build directories) get ids that change between runs and differ from a clean build.
  • Lookups by id then miss the node.
  • No crash, so it goes unnoticed.

utkusafak avatar Oct 29 '26 13:10 utkusafak

Thanks for opening this issue, @utkusafak. 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 29 '26 13:10 github-actions[bot]

Hi! I'd like to work on this issue if no one is already working on it.

I've reproduced the mismatch using the Terraform extractor and cache functions on v8: a fresh extraction produces stable IDs across directories, while reusing a cache copied from another directory retains the original absolute scope.

My initial plan is to add a regression test that copies the AST cache between project roots, then make a focused fix while preserving cross-file references and module isolation.

Would you be happy for me to take this on?

yUm1k4 avatar Oct 29 '26 19:10 yUm1k4

I've opened #4861 with a fix and regression tests for Terraform/HCL AST caches reused across project directories. The reproduction now produces matching IDs for clean and relocated-cache builds. Feedback is welcome!

yUm1k4 avatar Oct 29 '26 20:10 yUm1k4

Fixed in v0.9.100 via #4861 (pip install graphifyy==0.9.100). Terraform node ids stay stable across a cached-project directory move. Thanks for the report, @utkusafak.

safishamsi avatar Nov 01 '26 00:11 safishamsi