Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix misdetection of project root with --stdin-filename #3216

Merged
merged 6 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<!-- Changes to how Black can be configured -->

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

### Documentation

Expand Down
8 changes: 6 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ def main( # noqa: C901
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
ctx.exit(1)

root, method = find_project_root(src) if code is None else (None, None)
root, method = (
find_project_root(src, stdin_filename) if code is None else (None, None)
)
ctx.obj["root"] = root

if verbose:
Expand All @@ -480,7 +482,9 @@ def main( # noqa: C901
)

normalized = [
(normalize_path_maybe_ignore(Path(source), root), source)
(source, source)
if source == "-"
else (normalize_path_maybe_ignore(Path(source), root), source)
for source in src
]
srcs_string = ", ".join(
Expand Down
6 changes: 5 additions & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@


@lru_cache()
def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
def find_project_root(
srcs: Sequence[str], stdin_filename: Optional[str] = None
) -> Tuple[Path, str]:
"""Return a directory containing .git, .hg, or pyproject.toml.

That directory will be a common parent of all files and directories
Expand All @@ -52,6 +54,8 @@ def find_project_root(srcs: Sequence[str]) -> Tuple[Path, str]:
the second element as a string describing the method by which the
project root was discovered.
"""
if stdin_filename is not None:
srcs = tuple(stdin_filename if s == "-" else s for s in srcs)
if not srcs:
srcs = [str(Path.cwd().resolve())]

Expand Down
6 changes: 6 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,12 @@ def test_find_project_root(self) -> None:
(src_dir.resolve(), "pyproject.toml"),
)

with change_directory(test_dir):
self.assertEqual(
black.find_project_root(("-",), stdin_filename="../whatever.py"),
(root.resolve(), "pyproject.toml"),
)

@patch(
"black.files.find_user_pyproject_toml",
)
Expand Down