pypa / virtualenv

Virtual Python Environment builder
https://virtualenv.pypa.io
MIT License
4.78k stars 1.02k forks source link

Fixed a case when template variable is WindowsPath #2707

Closed NtWriteCode closed 4 months ago

NtWriteCode commented 4 months ago

Thanks for contributing, make sure you address all the checklists (for details on how see development documentation)

I think the fix is trivial enough not to create new tests on them. Also I'm unsure if it's going to be a new release, hence I didn't change the changelog.

maresb commented 4 months ago

I'm hitting this the corresponding issue also on Linux when running pre-commit:

[INFO] Installing environment for https://github.com/astral-sh/ruff-pre-commit.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
An unexpected error has occurred: CalledProcessError: command: ('/opt/conda/bin/python3.11', '-mvirtualenv', '/home/mambauser/.cache/pre-commit/repopp_kosvk/py_env-python3')
return code: 1
stdout:
    AttributeError: 'PosixPath' object has no attribute 'endswith'
stderr: (none)
$ python -mvirtualenv --with-traceback /tmp/x
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/__main__.py", line 70, in <module>
    run_with_catch()  # pragma: no cov
    ^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/__main__.py", line 56, in run_with_catch
    run(args, options, env)
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/__main__.py", line 18, in run
    session = cli_run(args, options, env)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/run/__init__.py", line 33, in cli_run
    of_session.run()
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/run/session.py", line 46, in run
    self._activate()
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/run/session.py", line 65, in _activate
    activator.generate(self.creator)
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/activation/via_template.py", line 27, in generate
    generated = self._generate(replacements, self.templates(), dest_folder, creator)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/conda/lib/python3.11/site-packages/virtualenv/activation/via_template.py", line 51, in _generate
    encoding = "utf-8-sig" if template.endswith(".ps1") else "utf-8"
                              ^^^^^^^^^^^^^^^^^
AttributeError: 'PosixPath' object has no attribute 'endswith'

Glad to see this merged, it'd be great to get this released soon.