Skip to content

Fail cache verification when an imported grammar file can no longer be loaded - #1705

Open
skylersemailaddress-debug wants to merge 2 commits into
lark-parser:masterfrom
skylersemailaddress-debug:fix-verify-used-files-missing-import
Open

skylersemailaddress-debug wants to merge 2 commits into
lark-parser:masterfrom
skylersemailaddress-debug:fix-verify-used-files-missing-import

Conversation

@skylersemailaddress-debug

Copy link
Copy Markdown

When a parser is built with the cache option, the cache records every imported grammar file in used_files (path → sha256 of its contents), and on a cache hit verify_used_files re-loads each recorded file and compares hashes before the cached parser is trusted. A changed import is caught that way and the parser is rebuilt. A recorded import that can no longer be loaded at all, however, is silently skipped:

  • a str path that no longer exists fails the os.path.exists guard, so neither branch assigns text;
  • a PackageResource whose pkgutil.get_data raises IOError has the error swallowed by suppress(IOError), so text likewise stays unassigned;

and both fall into the if text is None branch written for path kinds the verifier does not know how to load — continue — so verification returns True and the stale cached parser is trusted, carrying the deleted file's old rules and terminals. A fresh build of the same grammar and options raises FileNotFoundError instead. Reproducer shape: build Lark(grammar_with_import, parser='lalr', cache=fn) once, delete the imported .lark file, then construct again with the same cache — the cached construction succeeds and parses with the deleted file's terminal, where cache=False raises.

This change makes a recorded file of a kind the verifier knows how to load — a str path or a PackageResource — fail verification when it can no longer be loaded:

if text is None:
    if isinstance(path, (str, PackageResource)):
        # The file was recorded when the cache was written, but it
        # can no longer be loaded (e.g. it was deleted), so the
        # cache cannot be verified against it. Treat the cache as
        # stale and rebuild, instead of trusting it silently.
        logger.info("File %r could not be loaded, rebuilding Parser" % (path,))
        return False
    # We don't know how to load the path. ignore it.
    continue

Returning False treats the cache as stale, so construction rebuilds through the identical fresh path — which raises the same FileNotFoundError a fresh build raises for the missing file. The ignore is kept, with its original comment, for path kinds the verifier genuinely does not know how to load. No other behavior changes: files that are present and readable are hash-compared exactly as before (cache hit on match, rebuild on mismatch), and no parallel manifest is introduced — validation runs against the cache's own recorded used_files.

Behaviour after the change, all verified against the pinned tree (commit 9a4fb9c):

  • cached construction after deleting an imported file raises FileNotFoundError naming that file — the same verdict and message as a fresh build — for a single import, for the second import of a multi-import grammar, and for a stdlib import (common.lark, recorded as a PackageResource);
  • verify_used_files returns False for both unloadable kinds (missing str path, missing PackageResource);
  • a present-and-unchanged import still serves from cache (the cache file is byte- and mtime-identical after the hit), a changed import still rebuilds, and a grammar with no imports is unaffected.

Tests: three new TestCache tests in tests/test_cache.py — test_cache_missing_import and test_verify_used_files_missing (the two deciding cases; both fail on the unpatched tree) and test_cache_changed_import (the already-enforced changed case, as a control; passes before and after). Full test suite: the patched tree's outcome set is identical to the pristine tree's except for exactly the three new tests passing (1,148 → 1,151 passed, 181 skipped, zero failures and zero errors on both).

Credit: Nexus Verify.


This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant