Skip to content

fix: resolve CWD before lru_cache key in find_project_root#5152

Open
anisia19 wants to merge 2 commits into
psf:mainfrom
anisia19:fix/find-project-root-lru-cache-cwd
Open

fix: resolve CWD before lru_cache key in find_project_root#5152
anisia19 wants to merge 2 commits into
psf:mainfrom
anisia19:fix/find-project-root-lru-cache-cwd

Conversation

@anisia19
Copy link
Copy Markdown

@anisia19 anisia19 commented May 25, 2026

Description

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 return the wrong project root and pyproject.toml.

This was observable in the test suite: test_code_option_config and test_code_option_parent_config both call black --code from different directories using change_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 all srcs to 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 ...

  • Implement any code style changes under the --preview style, following the stability policy? — N/A, no formatting changes.
  • Add an entry in CHANGES.md if necessary? — Yes, added under Configuration.
  • Add / update tests if necessary? — The existing test_code_option_config and test_code_option_parent_config tests cover this exactly; they were failing before the fix.
  • Add new / update outdated documentation? — N/A, no user-visible API or formatting behaviour changed.

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.
@anisia19 anisia19 force-pushed the fix/find-project-root-lru-cache-cwd branch from 50d41b6 to 73b2ca3 Compare May 25, 2026 22:13
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