Skip to content

Commit

Permalink
fix: write to stderr in utils.env
Browse files Browse the repository at this point in the history
This is a quick fix to avoid polluting stdout unexpectedly when Poetry's
environment management comes into play.

It's apparent from how much the complexity of this file has grown that
this needs to be refactored moderately, as well as each major class
deserving its own source file.

Future work should also include a rethink of how IO objects are passed
around the codebase, how we reason about verbosity at a function level,
and how code is re-used -- one command may wish to output to stdout, but
if that code is reused by another command, the calculus of what is
command output and what is informative (or even needs to be hidden/shown
based on verbosity level) changes.

Work on output would likely have to be fairly comprehensive and
invasive, but things have grown complex enough that a top-down design
pass is likely the best route.

Regardless, this is a simple change today, and low risk. Resolves #6427.
  • Loading branch information
neersighted committed Sep 6, 2022
1 parent 1206d3e commit 12cf1e2
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/poetry/utils/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,15 @@ def _detect_active_python(self, io: IO) -> str | None:
executable = None

try:
io.write_line(
io.write_error_line(
"Trying to detect current active python executable as specified in the"
" config.",
verbosity=Verbosity.VERBOSE,
)
executable = self._full_python_path("python")
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
io.write_error_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
except CalledProcessError:
io.write_line(
io.write_error_line(
"Unable to detect the current active python executable. Falling back to"
" default.",
verbosity=Verbosity.VERBOSE,
Expand Down Expand Up @@ -651,7 +651,9 @@ def deactivate(self, io: IO) -> None:
env = envs.get(name)
if env is not None:
venv = venv_path / f"{name}-py{env['minor']}"
io.write_line(f"Deactivating virtualenv: <comment>{venv}</comment>")
io.write_error_line(
f"Deactivating virtualenv: <comment>{venv}</comment>"
)
del envs[name]

envs_file.write(envs)
Expand Down Expand Up @@ -911,7 +913,7 @@ def create_venv(
python = "python" + python_to_try

if io.is_debug():
io.write_line(f"<debug>Trying {python}</debug>")
io.write_error_line(f"<debug>Trying {python}</debug>")

try:
python_patch = decode(
Expand All @@ -930,7 +932,7 @@ def create_venv(
continue

if supported_python.allows(Version.parse(python_patch)):
io.write_line(f"Using <c1>{python}</c1> ({python_patch})")
io.write_error_line(f"Using <c1>{python}</c1> ({python_patch})")
executable = python
python_minor = ".".join(python_patch.split(".")[:2])
break
Expand All @@ -955,7 +957,7 @@ def create_venv(

if not venv.exists():
if create_venv is False:
io.write_line(
io.write_error_line(
"<fg=black;bg=yellow>"
"Skipping virtualenv creation, "
"as specified in config file."
Expand All @@ -964,7 +966,7 @@ def create_venv(

return self.get_system_env()

io.write_line(
io.write_error_line(
f"Creating virtualenv <c1>{name}</> in"
f" {venv_path if not WINDOWS else get_real_windows_path(venv_path)!s}"
)
Expand All @@ -976,11 +978,11 @@ def create_venv(
f"<warning>The virtual environment found in {env.path} seems to"
" be broken.</warning>"
)
io.write_line(f"Recreating virtualenv <c1>{name}</> in {venv!s}")
io.write_error_line(f"Recreating virtualenv <c1>{name}</> in {venv!s}")
self.remove_venv(venv)
create_venv = True
elif io.is_very_verbose():
io.write_line(f"Virtualenv <c1>{name}</> already exists.")
io.write_error_line(f"Virtualenv <c1>{name}</> already exists.")

if create_venv:
self.build_venv(
Expand Down Expand Up @@ -1917,14 +1919,14 @@ def build_environment(

if io:
if not overwrite:
io.write_line("")
io.write_error_line("")

requires = [
f"<c1>{requirement}</c1>"
for requirement in poetry.pyproject.build_system.requires
]

io.overwrite(
io.overwrite_error(
"<b>Preparing</b> build environment with build-system requirements"
f" {', '.join(requires)}"
)
Expand All @@ -1938,7 +1940,7 @@ def build_environment(

if overwrite:
assert io is not None
io.write_line("")
io.write_error_line("")

yield venv
else:
Expand Down

0 comments on commit 12cf1e2

Please sign in to comment.