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 when --stdin-filename filepath is outside the current working directory #3208

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def normalize_path_maybe_ignore(
abspath = path if path.is_absolute() else Path.cwd() / path
normalized_path = abspath.resolve()
try:
root_relative_path = normalized_path.relative_to(root).as_posix()
if not str(normalized_path).startswith(str(root)):
root_relative_path = os.path.join(Path.cwd(), path)
else:
root_relative_path = normalized_path.relative_to(root).as_posix()
except ValueError:
if report:
report.path_ignored(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,18 @@ def test_normalize_path_ignore_windows_junctions_outside_of_root(self) -> None:

self.assertEqual(normalized_path, None)

def test_normalize_path_outside_of_root(self) -> None:

with TemporaryDirectory() as workspace:
root = Path(workspace)
target_path = root / ".."
report = black.Report(verbose=True)
normalized_path = black.normalize_path_maybe_ignore(
target_path, root, report
)

self.assertNotEqual(normalized_path, None)

def test_newline_comment_interaction(self) -> None:
source = "class A:\\\r\n# type: ignore\n pass\n"
output = black.format_str(source, mode=DEFAULT_MODE)
Expand Down