Skip to content

Commit c349357

Browse files
authored
Fix an error in env use if the virtualenvs.in-project setting is activated (#1682)
1 parent c78503e commit c349357

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

poetry/utils/env.py

+18
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,24 @@ def activate(self, python, io): # type: (str, IO) -> Env
208208
patch = python_version.text
209209

210210
create = False
211+
is_root_venv = self._poetry.config.get("virtualenvs.in-project")
212+
# If we are required to create the virtual environment in the root folder,
213+
# create or recreate it if needed
214+
if is_root_venv:
215+
create = False
216+
venv = self._poetry.file.parent / ".venv"
217+
if venv.exists():
218+
# We need to check if the patch version is correct
219+
_venv = VirtualEnv(venv)
220+
current_patch = ".".join(str(v) for v in _venv.version_info[:3])
221+
222+
if patch != current_patch:
223+
create = True
224+
225+
self.create_venv(io, executable=python, force=create)
226+
227+
return self.get(reload=True)
228+
211229
envs = tomlkit.document()
212230
base_env_name = self.generate_env_name(self._poetry.package.name, str(cwd))
213231
if envs_file.exists():

tests/utils/test_env.py

+35
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,38 @@ def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(
661661

662662
assert expected_message == str(e.value)
663663
assert 0 == m.call_count
664+
665+
666+
def test_activate_with_in_project_setting_does_not_fail_if_no_venvs_dir(
667+
manager, poetry, config, tmp_dir, mocker
668+
):
669+
if "VIRTUAL_ENV" in os.environ:
670+
del os.environ["VIRTUAL_ENV"]
671+
672+
config.merge(
673+
{
674+
"virtualenvs": {
675+
"path": str(Path(tmp_dir) / "virtualenvs"),
676+
"in-project": True,
677+
}
678+
}
679+
)
680+
681+
mocker.patch(
682+
"poetry.utils._compat.subprocess.check_output",
683+
side_effect=check_output_wrapper(),
684+
)
685+
mocker.patch(
686+
"poetry.utils._compat.subprocess.Popen.communicate",
687+
side_effect=[("/prefix", None), ("/prefix", None)],
688+
)
689+
m = mocker.patch("poetry.utils.env.EnvManager.build_venv")
690+
691+
manager.activate("python3.7", NullIO())
692+
693+
m.assert_called_with(
694+
os.path.join(str(poetry.file.parent), ".venv"), executable="python3.7"
695+
)
696+
697+
envs_file = TomlFile(Path(tmp_dir) / "virtualenvs" / "envs.toml")
698+
assert not envs_file.exists()

0 commit comments

Comments
 (0)