All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
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.
<!-- Changes to how Black can be configured -->
- Black now uses the presence of debug f-strings to detect target version. (#3215)
<!-- 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)
out(main.get_usage(ctx) + "\n\nOne of 'SRC' or 'code' is required.")
ctx.exit(1)
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:
ctx.obj["root"] = root
if verbose:
- (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(
for source in src
]
srcs_string = ", ".join(
-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
"""Return a directory containing .git, .hg, or pyproject.toml.
That directory will be a common parent of all files and directories
the second element as a string describing the method by which the
project root was discovered.
"""
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())]
if not srcs:
srcs = [str(Path.cwd().resolve())]
(src_dir.resolve(), "pyproject.toml"),
)
(src_dir.resolve(), "pyproject.toml"),
)
+ with change_directory(test_dir):
+ self.assertEqual(
+ black.find_project_root(("-",), stdin_filename="../src/a.py"),
+ (src_dir.resolve(), "pyproject.toml"),
+ )
+
@patch(
"black.files.find_user_pyproject_toml",
)
@patch(
"black.files.find_user_pyproject_toml",
)