Repository navigation
Fail cache verification when an imported grammar file can no longer be loaded - #1705
Open
skylersemailaddress-debug wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 hitverify_used_filesre-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:strpath that no longer exists fails theos.path.existsguard, so neither branch assignstext;PackageResourcewhosepkgutil.get_dataraisesIOErrorhas the error swallowed bysuppress(IOError), sotextlikewise stays unassigned;and both fall into the
if text is Nonebranch 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 raisesFileNotFoundErrorinstead. Reproducer shape: buildLark(grammar_with_import, parser='lalr', cache=fn)once, delete the imported.larkfile, then construct again with the same cache — the cached construction succeeds and parses with the deleted file's terminal, wherecache=Falseraises.This change makes a recorded file of a kind the verifier knows how to load — a
strpath or aPackageResource— fail verification when it can no longer be loaded:Returning False treats the cache as stale, so construction rebuilds through the identical fresh path — which raises the same
FileNotFoundErrora 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 recordedused_files.Behaviour after the change, all verified against the pinned tree (commit
9a4fb9c):FileNotFoundErrornaming 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 aPackageResource);verify_used_filesreturns False for both unloadable kinds (missingstrpath, missingPackageResource);Tests: three new
TestCachetests intests/test_cache.py—test_cache_missing_importandtest_verify_used_files_missing(the two deciding cases; both fail on the unpatched tree) andtest_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.