Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/2786.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Creating a virtual environment on a filesystem without symlink-support would fail even with `--copies`
Make `fs_supports_symlink` perform a real symlink creation check on all platforms.
Contributed by :user:`esafak`.
7 changes: 6 additions & 1 deletion src/virtualenv/create/via_global_ref/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ def add_parser_arguments(cls, parser, interpreter, meta, app_data):
help="give the virtual environment access to the system site-packages dir",
)
if not meta.can_symlink and not meta.can_copy:
msg = "neither symlink or copy method supported"
errors = []
if meta.symlink_error:
errors.append(f"symlink: {meta.symlink_error}")
if meta.copy_error:
errors.append(f"copy: {meta.copy_error}")
msg = f"neither symlink or copy method supported: {', '.join(errors)}"
raise RuntimeError(msg)
group = parser.add_mutually_exclusive_group()
if meta.can_symlink:
Expand Down
4 changes: 3 additions & 1 deletion src/virtualenv/discovery/py_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,9 @@ def is_venv(self):
return self.base_prefix is not None

def sysconfig_path(self, key, config_var=None, sep=os.sep):
pattern = self.sysconfig_paths[key]
pattern = self.sysconfig_paths.get(key)
if pattern is None:
return ""
if config_var is None:
config_var = self.sysconfig_vars
else:
Expand Down
26 changes: 14 additions & 12 deletions src/virtualenv/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ def fs_supports_symlink():
if _CAN_SYMLINK is None:
can = False
if hasattr(os, "symlink"):
if IS_WIN:
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
temp_dir = os.path.dirname(tmp_file.name)
dest = os.path.join(temp_dir, f"{tmp_file.name}-{'b'}")
try:
os.symlink(tmp_file.name, dest)
can = True
except (OSError, NotImplementedError):
pass
LOGGER.debug("symlink on filesystem does%s work", "" if can else " not")
else:
can = True
# Creating a symlink can fail for a variety of reasons, indicating that the filesystem does not support it.
# E.g. on Linux with a VFAT partition mounted.
with tempfile.NamedTemporaryFile(prefix="TmP") as tmp_file:
temp_dir = os.path.dirname(tmp_file.name)
dest = os.path.join(temp_dir, f"{tmp_file.name}-{'b'}")
try:
os.symlink(tmp_file.name, dest)
can = True
except (OSError, NotImplementedError):
pass # symlink is not supported
finally:
if os.path.lexists(dest):
os.remove(dest)
LOGGER.debug("symlink on filesystem does%s work", "" if can else " not")
_CAN_SYMLINK = can
return _CAN_SYMLINK

Expand Down
61 changes: 61 additions & 0 deletions tests/unit/create/test_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from virtualenv.__main__ import run, run_with_catch
from virtualenv.create.creator import DEBUG_SCRIPT, Creator, get_env_debug_info
from virtualenv.create.pyenv_cfg import PyEnvCfg
from virtualenv.create.via_global_ref import api
from virtualenv.create.via_global_ref.builtin.cpython.common import is_macos_brew
from virtualenv.create.via_global_ref.builtin.cpython.cpython3 import CPython3Posix
from virtualenv.discovery.py_info import PythonInfo
Expand Down Expand Up @@ -690,3 +691,63 @@ def func(self):

cmd = [str(tmp_path), "--seeder", "app-data", "--without-pip", "--creator", "venv"]
cli_run(cmd)


def test_fallback_to_copies_if_symlink_unsupported(tmp_path, python, mocker):
"""Test that creating a virtual environment falls back to copies when filesystem has no symlink support."""
if is_macos_brew(PythonInfo.from_exe(python)):
pytest.skip("brew python on darwin may not support copies, which is tested separately")

# Given a filesystem that does not support symlinks
mocker.patch("virtualenv.create.via_global_ref.api.fs_supports_symlink", return_value=False)

# When creating a virtual environment (no method specified)
cmd = [
"-v",
"-p",
str(python),
str(tmp_path),
"--without-pip",
"--activators",
"",
]
result = cli_run(cmd)

# Then the creation should succeed and the creator should report it used copies
assert result.creator is not None
assert result.creator.symlinks is False


def test_fail_gracefully_if_no_method_supported(tmp_path, python, mocker):
"""Test that virtualenv fails gracefully when no creation method is supported."""
# Given a filesystem that does not support symlinks
mocker.patch("virtualenv.create.via_global_ref.api.fs_supports_symlink", return_value=False)

# And a creator that does not support copying
if not is_macos_brew(PythonInfo.from_exe(python)):
original_init = api.ViaGlobalRefMeta.__init__

def new_init(self, *args, **kwargs):
original_init(self, *args, **kwargs)
self.copy_error = "copying is not supported"

mocker.patch("virtualenv.create.via_global_ref.api.ViaGlobalRefMeta.__init__", new=new_init)

# When creating a virtual environment
with pytest.raises(RuntimeError) as excinfo:
cli_run(
[
"-p",
str(python),
str(tmp_path),
"--without-pip",
],
)

# Then a RuntimeError should be raised with a detailed message
assert "neither symlink or copy method supported" in str(excinfo.value)
assert "symlink: the filesystem does not supports symlink" in str(excinfo.value)
if is_macos_brew(PythonInfo.from_exe(python)):
assert "copy: Brew disables copy creation" in str(excinfo.value)
else:
assert "copy: copying is not supported" in str(excinfo.value)
Loading