From 6e4b7e51f12f21ce22132ac434abb0e9bb5bfebd Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 9 Aug 2022 23:49:28 -0700 Subject: [PATCH 1/5] Fix misdetection of project root with `--stdin-filename` 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. Fixes #3207 --- src/black/__init__.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 2a5c750a583..637875dc073 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -468,7 +468,13 @@ 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) + if stdin_filename is not None: + src_with_stdin_filename = tuple(stdin_filename if s == "-" else s for s in src) + else: + src_with_stdin_filename = src + root, method = ( + find_project_root(src_with_stdin_filename) if code is None else (None, None) + ) ctx.obj["root"] = root if verbose: From 3d01880ff6945f77724f99e82cbe4d73072f200f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 11 Aug 2022 11:51:06 -0700 Subject: [PATCH 2/5] make "sources to be formatted" more accurate --- src/black/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 637875dc073..1eec22ec62b 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -485,7 +485,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( From 679207e9fb1473dd9b7d070f966fb0c4cdd1fb51 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Fri, 12 Aug 2022 15:11:14 -0700 Subject: [PATCH 3/5] changelog --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 1fc8c65d6d8..d9496d31ef0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -34,6 +34,8 @@ - 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 From 6453abf87c52df2237ada98dd380220a88ec2388 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Wed, 24 Aug 2022 14:49:15 -0700 Subject: [PATCH 4/5] add a test --- src/black/__init__.py | 6 +----- src/black/files.py | 6 +++++- tests/test_black.py | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/black/__init__.py b/src/black/__init__.py index 678ec949b69..a0c1ad4b416 100644 --- a/src/black/__init__.py +++ b/src/black/__init__.py @@ -469,12 +469,8 @@ def main( # noqa: C901 out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.") ctx.exit(1) - if stdin_filename is not None: - src_with_stdin_filename = tuple(stdin_filename if s == "-" else s for s in src) - else: - src_with_stdin_filename = src root, method = ( - find_project_root(src_with_stdin_filename) if code is None else (None, None) + find_project_root(src, stdin_filename) if code is None else (None, None) ) ctx.obj["root"] = root diff --git a/src/black/files.py b/src/black/files.py index 17515d52b57..d51c1bc7a90 100644 --- a/src/black/files.py +++ b/src/black/files.py @@ -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 @@ -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())] diff --git a/tests/test_black.py b/tests/test_black.py index 81e7a9a7d0d..1e9c20dc183 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -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", ) From 5a8dcfec310ab9d749bd8b8c97a81fa4fceb285b Mon Sep 17 00:00:00 2001 From: Richard Si <63936253+ichard26@users.noreply.github.com> Date: Fri, 26 Aug 2022 16:34:42 -0400 Subject: [PATCH 5/5] Fix test that passes on main anyway Black searches parent directories if the common base of the given sources doesn't contain a "project root marker" so simply setting --stdin-filename to `../whatever.py` won't cause any problems. Instead let's set --stdin-filename to `../src/a.py` as `src` has its own pyproject.toml file. Currently on main, the detected root will be the parent directory of `src` but with this PR it'll be `src` as expected. --- tests/test_black.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_black.py b/tests/test_black.py index 1e9c20dc183..c76b3faddf5 100644 --- a/tests/test_black.py +++ b/tests/test_black.py @@ -1398,8 +1398,8 @@ def test_find_project_root(self) -> None: with change_directory(test_dir): self.assertEqual( - black.find_project_root(("-",), stdin_filename="../whatever.py"), - (root.resolve(), "pyproject.toml"), + black.find_project_root(("-",), stdin_filename="../src/a.py"), + (src_dir.resolve(), "pyproject.toml"), ) @patch(