graphrag icon indicating copy to clipboard operation
graphrag copied to clipboard

[Bug]: FileStorage.find returns matching directories as file keys

Open Linxiushen opened this issue 2 months ago • 1 comments

Describe the bug

FileStorage.find() yields directories when their paths match file_pattern, although its contract is to find files. A directory named archive.txt is returned by the usual text-file pattern; passing that returned key to get() raises PermissionError on Windows. Broad patterns such as .* also return ordinary directories.

The input reader catches read errors and skips these entries, so this does not necessarily abort indexing. It does cause unnecessary failed reads/warnings and inflated file counts, and direct storage consumers receive keys that are not readable files.

Steps to reproduce

Run from an environment with the current graphrag-storage package:

import asyncio
import re
from pathlib import Path
from tempfile import TemporaryDirectory

from graphrag_storage.file_storage import FileStorage


async def main():
    with TemporaryDirectory() as directory:
        archive = Path(directory) / "archive.txt"
        archive.mkdir()
        (archive / "document.txt").write_text("real document", encoding="utf-8")
        storage = FileStorage(directory)
        keys = list(storage.find(re.compile(r".*\.txt$")))
        print(keys)
        for key in keys:
            print(key, await storage.get(key))


asyncio.run(main())

On Windows the keys are ['archive.txt', 'archive.txt\\document.txt'], and the first read raises PermissionError: [Errno 13] Permission denied for the directory. Reading the nested document directly succeeds.

Expected behavior

Return only the matching file archive.txt/document.txt (using the platform's path separator), retaining recursive discovery of files inside matching directories. Empty directories should not be returned either.

Configuration and environment

  • GraphRAG storage 3.2.0, upstream commit 769542fbf1d8e5b4c6a8677fefc34621c87894c5.
  • Windows, Python 3.12.13; repository-locked storage dependencies installed with uv sync --frozen --package graphrag-storage --no-dev --python 3.12.
  • No model hosting, credentials, inference, or settings.yaml is involved; this is a local storage API issue.
  • Searched open and closed issues/PRs for FileStorage, directories, file patterns, and is_file; no matching fix found.

I can contribute a focused fix and real-filesystem regression coverage.

Linxiushen avatar Sep 27 '26 19:09 Linxiushen

@Linxiushen Thanks for the detailed bug report and repro script.

@dayesouza I saw you were the last to make updates to the file storage logic. Since PRs are currently locked to non-collaborators, I've drafted a patch on my fork to help get this resolved.

It adds the missing .is_file() check and refactors the manual string-slicing to use native pathlib via file.relative_to(self._base_dir) for safer cross-platform path resolution. It also includes the async pytest covering this exact edge case.

The commit is here if you'd like to cherry-pick it or use it to speed up the fix: https://github.com/A-P-Shukla/graphrag/commit/7b556f9

A-P-Shukla avatar Sep 28 '26 09:09 A-P-Shukla