graphify icon indicating copy to clipboard operation
graphify copied to clipboard

C++ declaration and definition reconciliation fails when cache and source are on different Windows drives

Open sub4biz opened this issue 3 weeks ago • 3 comments

Summary

On clean graphify v0.9.106 on Windows, supplying source files on a different drive from cache_root prevents a C++ method declaration and its definition from being reconciled. With the same files and cache, supplying their source directory as root reconciles them. The two existing cpp_paired tests also fail when the checkout and the system temporary directory are on different drives.

Reproduction

Run this from a clean v0.9.106 checkout on Windows with the checkout and the system temporary directory on different drives:

import os
from pathlib import Path
from tempfile import TemporaryDirectory
from contextlib import redirect_stdout, redirect_stderr
from io import StringIO
from graphify.extract import extract

with TemporaryDirectory(dir=Path.cwd()) as src_dir, TemporaryDirectory() as cache_dir:
    source, cache = Path(src_dir), Path(cache_dir)
    if os.path.splitdrive(str(source))[0].lower() == os.path.splitdrive(str(cache))[0].lower():
        raise SystemExit('Run with the checkout and the temporary directory on different drives')
    (source / 'Foo.h').write_text('class Foo { public: void bar(); };\n', encoding='utf-8')
    (source / 'Foo.cpp').write_text('#include "Foo.h"\nvoid Foo::bar() {}\n', encoding='utf-8')
    paths = [source / 'Foo.h', source / 'Foo.cpp']
    for label, options in [('cache only', {}), ('explicit root', {'root': source})]:
        with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
            graph = extract(paths, cache_root=cache / label.replace(' ', '-'), parallel=False, **options)
        bars = [n for n in graph['nodes'] if n['label'] in ('.bar()', 'Foo::bar()')]
        print(label, len(bars))

Observed:

cache only 2
explicit root 1

Expected: the explicit files and their include relationship are recognized in both cases. Supplying root is a workaround.

Related

#5117 added declaration/definition reconciliation and support for explicitly supplied files outside the cache anchor.

sub4biz avatar Nov 11 '26 09:11 sub4biz

Thanks for opening this issue, @sub4biz. 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 Nov 11 '26 09:11 github-actions[bot]

I’d like to work on this. I’ll reproduce the cross-drive case on Windows, trace how explicitly supplied files are matched to their includes, and add a regression test that fails before the fix. I’ll keep the change focused on preserving declaration/definition reconciliation without weakening the existing include and corpus-boundary checks.

Swarneil1905 avatar Nov 12 '26 04:11 Swarneil1905

Thanks for the fix! I verified on clean v0.9.113 on Windows that the original reproduction now merges the declaration and definition when the cache and sources are on different drives.

I also checked the open question in #5302 about absolute paths in persisted reconciliation fields. When extract() is called with a cross-drive cache_root and no explicit root, _cpp_reconcile_files contains absolute paths. In a subsequent test, I saved the graph, copied it with the source tree, changed a method declaration in the copied header, and ran an incremental code rebuild. The rebuild omitted the now-separate definition node, while a fresh extraction retained it. Passing the source directory explicitly as root made the incremental and fresh results agree.

Here is a standalone reproduction on clean v0.9.113. Run it on Windows from a checkout on a different drive from the system temporary directory; it creates and cleans up its own files. It uses _rebuild_code with an explicit changed path to exercise the incremental rebuild without depending on filesystem event delivery.

import json
import shutil
from contextlib import redirect_stderr, redirect_stdout
from io import StringIO
from pathlib import Path
from tempfile import TemporaryDirectory

from graphify.build import build_from_json
from graphify.export import to_json
from graphify.extract import extract
from graphify.watch import _rebuild_code


def members(nodes):
    return sorted(
        (n.get("label"), bool(n.get("definition_file")))
        for n in nodes
        if n.get("label") in (".bar()", "Foo::bar()")
    )


with TemporaryDirectory(dir=Path.cwd()) as source_temp, TemporaryDirectory() as cache_temp:
    source = Path(source_temp) / "original"
    source.mkdir()
    cache = Path(cache_temp)
    if source.drive.lower() == cache.drive.lower():
        raise SystemExit("Run on Windows with the checkout and system temp on different drives")

    (source / "Foo.h").write_text("class Foo { public: void bar(); };\n", encoding="utf-8")
    (source / "Foo.cpp").write_text('#include "Foo.h"\nvoid Foo::bar() {}\n', encoding="utf-8")
    paths = [source / "Foo.h", source / "Foo.cpp"]

    for mode in ("cache_only", "explicit_root"):
        options = {"root": source} if mode == "explicit_root" else {}
        with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
            initial = extract(paths, cache_root=cache / mode, parallel=False, **options)
            graph = build_from_json(initial, root=source)
            out = source / "graphify-out"
            out.mkdir()
            assert to_json(graph, {}, str(out / "graph.json"), force=True)

        moved = Path(source_temp) / mode
        shutil.copytree(source, moved)
        (moved / "Foo.h").write_text(
            "class Foo { public: void bar() const; };\n", encoding="utf-8"
        )
        with redirect_stdout(StringIO()), redirect_stderr(StringIO()):
            ok = _rebuild_code(
                moved, changed_paths=[moved / "Foo.h"], no_cluster=True,
                force=True, acquire_lock=False,
            )
            updated = json.loads((moved / "graphify-out" / "graph.json").read_text(encoding="utf-8"))
            fresh = extract(
                [moved / "Foo.h", moved / "Foo.cpp"], root=moved,
                cache_root=cache / (mode + "_fresh"), parallel=False,
            )
        assert ok
        print(mode, "incremental:", members(updated["nodes"]))
        print(mode, "full:", members(fresh["nodes"]))
        shutil.rmtree(out)

Observed:

cache_only incremental: [('.bar()', False)]
cache_only full: [('.bar()', False), ('Foo::bar()', False)]
explicit_root incremental: [('.bar()', False), ('Foo::bar()', False)]
explicit_root full: [('.bar()', False), ('Foo::bar()', False)]

The boolean indicates whether the node has definition_file. The declaration is changed to bar() const, so it no longer matches the non-const definition and a fresh extraction retains both nodes. The absolute proof paths are also retained in the saved graph.

Would it make sense to make these persisted paths portable so the proof files can be rechecked after moving the graph?

sub4biz avatar Nov 20 '26 06:11 sub4biz