Skip to content

Raise UsageError when sources share no common project root - #5385

Closed
Dmao233 wants to merge 2 commits into
psf:mainfrom
Dmao233:fix-5379-project-root-drives
Closed

Dmao233 wants to merge 2 commits into
psf:mainfrom
Dmao233:fix-5379-project-root-drives

Conversation

@Dmao233

@Dmao233 Dmao233 commented Sep 3, 2026

Copy link
Copy Markdown

Description

Fixes #5379.

find_project_root passed an empty parent-path intersection to max(), which raises ValueError when sources live on different Windows drives. It now raises click.UsageError instead. Same-drive and POSIX paths that share a parent are unchanged.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@shashb27

shashb27 commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Verified this on Windows, where the crash lives — thanks for picking it up. The crash-to-usage-error conversion works:

>>> from black.files import find_project_root
>>> find_project_root(("C:/work/app/a.py", "D:/tmp/b.py"), None)
UsageError: Sources on different drives share no common project root.

One gap against the requirement @cobaltt7 stated for this route ("passing --config should still allow formatting to continue"): main() calls find_project_root(src, stdin_filename) unconditionally whenever code is None (src/black/__init__.py:616-618), before and independent of the --config parameter, so black C:\...\a.py D:\...\b.py --config my.toml would still raise. (I couldn't produce a literal two-drive CLI run — only one drive with media on this machine — but the call flow is unconditional; the --stdin-filename path doesn't hit this since - is substituted with the single filename, so that intersection is never empty.)

There's also a second consumer of the root that either route needs to deal with for formatting to continue: get_sources/gen_python_files use it for gitignore handling, exclude matching, and the symlink-outside-root guard, and child.relative_to(root) can't work across drives with a single root.

I've opened #5386 implementing the other route from cobaltt7's comment (treat it as no config found, warn, use defaults), with a per-source-root fallback that covers both points — feel free to use whatever parts of it are useful, or close it if the error route is preferred and this PR gets amended instead. Happy to test any revision on Windows.

@Dmao233

Dmao233 commented Sep 3, 2026

Copy link
Copy Markdown
Author

@shashb27 thanks for the Windows check. You're right that --config still hits find_project_root unconditionally, so this UsageError route doesn't meet cobaltt7's requirement.

Closing in favor of #5386 (no-root → defaults + warn), which matches the preferred option.

@Dmao233

Dmao233 commented Sep 3, 2026

Copy link
Copy Markdown
Author

Superseded by #5386.

@Dmao233 Dmao233 closed this Sep 3, 2026
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.

find_project_root raises ValueError for sources on different Windows drives

2 participants