Skip to content

Commit 12cf1e2

Browse files
committed
fix: write to stderr in utils.env
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.
1 parent 1206d3e commit 12cf1e2

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

src/poetry/utils/env.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,15 @@ def _detect_active_python(self, io: IO) -> str | None:
536536
executable = None
537537

538538
try:
539-
io.write_line(
539+
io.write_error_line(
540540
"Trying to detect current active python executable as specified in the"
541541
" config.",
542542
verbosity=Verbosity.VERBOSE,
543543
)
544544
executable = self._full_python_path("python")
545-
io.write_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
545+
io.write_error_line(f"Found: {executable}", verbosity=Verbosity.VERBOSE)
546546
except CalledProcessError:
547-
io.write_line(
547+
io.write_error_line(
548548
"Unable to detect the current active python executable. Falling back to"
549549
" default.",
550550
verbosity=Verbosity.VERBOSE,
@@ -651,7 +651,9 @@ def deactivate(self, io: IO) -> None:
651651
env = envs.get(name)
652652
if env is not None:
653653
venv = venv_path / f"{name}-py{env['minor']}"
654-
io.write_line(f"Deactivating virtualenv: <comment>{venv}</comment>")
654+
io.write_error_line(
655+
f"Deactivating virtualenv: <comment>{venv}</comment>"
656+
)
655657
del envs[name]
656658

657659
envs_file.write(envs)
@@ -911,7 +913,7 @@ def create_venv(
911913
python = "python" + python_to_try
912914

913915
if io.is_debug():
914-
io.write_line(f"<debug>Trying {python}</debug>")
916+
io.write_error_line(f"<debug>Trying {python}</debug>")
915917

916918
try:
917919
python_patch = decode(
@@ -930,7 +932,7 @@ def create_venv(
930932
continue
931933

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

956958
if not venv.exists():
957959
if create_venv is False:
958-
io.write_line(
960+
io.write_error_line(
959961
"<fg=black;bg=yellow>"
960962
"Skipping virtualenv creation, "
961963
"as specified in config file."
@@ -964,7 +966,7 @@ def create_venv(
964966

965967
return self.get_system_env()
966968

967-
io.write_line(
969+
io.write_error_line(
968970
f"Creating virtualenv <c1>{name}</> in"
969971
f" {venv_path if not WINDOWS else get_real_windows_path(venv_path)!s}"
970972
)
@@ -976,11 +978,11 @@ def create_venv(
976978
f"<warning>The virtual environment found in {env.path} seems to"
977979
" be broken.</warning>"
978980
)
979-
io.write_line(f"Recreating virtualenv <c1>{name}</> in {venv!s}")
981+
io.write_error_line(f"Recreating virtualenv <c1>{name}</> in {venv!s}")
980982
self.remove_venv(venv)
981983
create_venv = True
982984
elif io.is_very_verbose():
983-
io.write_line(f"Virtualenv <c1>{name}</> already exists.")
985+
io.write_error_line(f"Virtualenv <c1>{name}</> already exists.")
984986

985987
if create_venv:
986988
self.build_venv(
@@ -1917,14 +1919,14 @@ def build_environment(
19171919

19181920
if io:
19191921
if not overwrite:
1920-
io.write_line("")
1922+
io.write_error_line("")
19211923

19221924
requires = [
19231925
f"<c1>{requirement}</c1>"
19241926
for requirement in poetry.pyproject.build_system.requires
19251927
]
19261928

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

19391941
if overwrite:
19401942
assert io is not None
1941-
io.write_line("")
1943+
io.write_error_line("")
19421944

19431945
yield venv
19441946
else:

0 commit comments

Comments
 (0)