Skip to content

Commit c47b91f

Browse files
authored
Fix misdetection of project root with --stdin-filename (#3216)
There are a number of places this behaviour could be patched, for instance, it's quite tempting to patch it in `get_sources`. However I believe we generally have the invariant that project root contains all files we want to format, in which case it seems prudent to keep that invariant. This also improves the accuracy of the "sources to be formatted" log message with --stdin-filename. Fixes GH-3207.
1 parent a5fde8a commit c47b91f

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<!-- Changes to how Black can be configured -->
4141

4242
- Black now uses the presence of debug f-strings to detect target version. (#3215)
43+
- Fix misdetection of project root and verbose logging of sources in cases involving
44+
`--stdin-filename` (#3216)
4345

4446
### Documentation
4547

src/black/__init__.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,9 @@ def main( # noqa: C901
469469
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
470470
ctx.exit(1)
471471

472-
root, method = find_project_root(src) if code is None else (None, None)
472+
root, method = (
473+
find_project_root(src, stdin_filename) if code is None else (None, None)
474+
)
473475
ctx.obj["root"] = root
474476

475477
if verbose:
@@ -480,7 +482,9 @@ def main( # noqa: C901
480482
)
481483

482484
normalized = [
483-
(normalize_path_maybe_ignore(Path(source), root), source)
485+
(source, source)
486+
if source == "-"
487+
else (normalize_path_maybe_ignore(Path(source), root), source)
484488
for source in src
485489
]
486490
srcs_string = ", ".join(

src/black/files.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@
3939

4040

4141
@lru_cache()
42-
def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
42+
def find_project_root(
43+
srcs: Sequence[str], stdin_filename: Optional[str] = None
44+
) -> Tuple[Path, str]:
4345
"""Return a directory containing .git, .hg, or pyproject.toml.
4446
4547
That directory will be a common parent of all files and directories
@@ -52,6 +54,8 @@ def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
5254
the second element as a string describing the method by which the
5355
project root was discovered.
5456
"""
57+
if stdin_filename is not None:
58+
srcs = tuple(stdin_filename if s == "-" else s for s in srcs)
5559
if not srcs:
5660
srcs = [str(Path.cwd().resolve())]
5761

tests/test_black.py

+6
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,12 @@ def test_find_project_root(self) -> None:
13961396
(src_dir.resolve(), "pyproject.toml"),
13971397
)
13981398

1399+
with change_directory(test_dir):
1400+
self.assertEqual(
1401+
black.find_project_root(("-",), stdin_filename="../src/a.py"),
1402+
(src_dir.resolve(), "pyproject.toml"),
1403+
)
1404+
13991405
@patch(
14001406
"black.files.find_user_pyproject_toml",
14011407
)

0 commit comments

Comments
 (0)