fix: resolve CWD before lru_cache key in find_project_root#5152
Open
anisia19 wants to merge 2 commits into
Open
fix: resolve CWD before lru_cache key in find_project_root#5152anisia19 wants to merge 2 commits into
anisia19 wants to merge 2 commits into
Conversation
When srcs is empty (e.g. black --code), find_project_root fell back to os.getcwd() inside the lru_cache-decorated function. The working directory was never part of the cache key — only (srcs, stdin_filename) was — so two calls from different directories with srcs=() would share the same cache entry and pick up the wrong project root and pyproject.toml. Split the function into two parts: find_project_root (public, no cache) resolves the CWD fallback upfront and normalises all srcs to absolute paths before handing off, and _find_project_root_cached (private, lru_cache) only ever receives fully-resolved paths so its key is stable regardless of where the process is running from. This fixes test_code_option_config and test_code_option_parent_config, which were failing because of cache poisoning between test cases.
50d41b6 to
73b2ca3
Compare
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.
Description
When
srcsis empty (e.g.black --code),find_project_rootfell back toos.getcwd()inside the@lru_cache-decorated function. The working directory was never part of the cache key — only(srcs, stdin_filename)was — so two calls from different directories withsrcs=()would share the same cache entry and return the wrong project root andpyproject.toml.This was observable in the test suite:
test_code_option_configandtest_code_option_parent_configboth callblack --codefrom different directories usingchange_directory. Whichever ran second would hit the stale cache entry from the first and load the wrong config.Fix: split the function into two parts:
find_project_root(public, no cache) resolves the CWD fallback upfront and normalises allsrcsto absolute paths before handing off._find_project_root_cached(private,@lru_cache) only ever receives fully-resolved paths, so its cache key is stable regardless of where the process is running from.Checklist - did you ...
--previewstyle, following the stability policy? — N/A, no formatting changes.CHANGES.mdif necessary? — Yes, added under Configuration.test_code_option_configandtest_code_option_parent_configtests cover this exactly; they were failing before the fix.