This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 'venv --clear' should prompt user before nuking entire directory
Type: behavior Stage:
Components: Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: alimpfard, eryksun, vinay.sajip
Priority: normal Keywords:

Created on 2022-01-10 09:01 by alimpfard, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg410192 - (view) Author: Ali Mohammad Pur (alimpfard) Date: 2022-01-10 09:01
`virtualenv --clear` is extremely eager to delete the passed directory without
any sort of confirmation, leading to possible data-loss
(e.g. with a mistyped command, or a misunderstanding of what it actually does).

Simply deleting an entire directory tree with a command that's extremely
prone to misunderstanding should not be virtualenv's job, but as it has
decided to make this its job, then it should take proper precautions to avoid
unintentional `rm -fr some-directory` by *at least* asking for confirmation.
The previous behaviour can of course, stay behind a `--force` flag for uses in
CI or similar, but this should *not* be the default behaviour.

related: https://github.com/pypa/virtualenv/issues/1890 mentions that
"[it is not] good practice to put non-virtual environment-related files into a
virtual environment", which is sensible, and so virtualenv should then simply
refuse to be instantiated in a non-empty directory.

On a more subjetive note, `virtualenv --clear <dir>` sounds like a way to purge the virtual environment files from a directory (yes, I understand what the help says, but that hardly changes anything), so perhaps a more verbose flag like "--clear-contents" would be more appropriate, but I digress.
msg410223 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2022-01-10 15:49
It appears you are describing behavior in the third-party virtualenv project; if so, you should report the problem to that project (https://pypi.org/project/virtualenv/). If similar behavior is also an issue with the Python standard library venv module (which is a derivative of virtualenv), please update this issue accordingly. Thanks!

https://docs.python.org/3/library/venv.html
msg410228 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-10 16:31
Ned, the CLI of venv has a `--clear` option, which invokes EnvBuilder [1] with clear=True. It's implemented by EnvBuilder.clear_directory(). Ali wants this method to prompt for confirmation before deleting the directory contents and also proposes the addition of a new `--force` option to skip the prompt.

---
[1] https://docs.python.org/3/library/venv.html#api
msg410229 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2022-01-10 16:35
> Ned, the CLI of venv has a `--clear` option, which invokes EnvBuilder [1] with clear=True.

Thanks, I saw that. However, the issue as written does not mention venv at all, only virtualenv. I wanted the OP to verify that it does apply to venv and, if the problem exists both places, to update this issue and to open an issue against virtualenv.
msg410230 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-01-10 16:53
The implementation of `--clear` in virtualenv apparently was changed to align with venv. On the linked virtualenv issue, Bernát Gábor suggested creating a bpo issue for venv to determine how the issue should be resolved for virtualenv.
msg410282 - (view) Author: Ali Mohammad Pur (alimpfard) Date: 2022-01-11 09:09
> If similar behavior is also an issue with the Python standard library venv module (which is a derivative of virtualenv), please update this issue accordingly

Yes, my bad, I should have mentioned that this applies to venv, I will update the issue title.

Not sure if I can modify my earlier comment however (I don't see any way to).
History
Date User Action Args
2022-04-11 14:59:54adminsetgithub: 90484
2022-01-13 15:29:41ned.deilysetnosy: - ned.deily
2022-01-11 09:09:46alimpfardsettitle: 'virtualenv --clear' should prompt user before nuking entire directory -> 'venv --clear' should prompt user before nuking entire directory
2022-01-11 09:09:38alimpfardsetmessages: + msg410282
2022-01-10 16:53:56eryksunsetnosy: + vinay.sajip
messages: + msg410230
2022-01-10 16:35:56ned.deilysetmessages: + msg410229
2022-01-10 16:31:27eryksunsetstatus: pending -> open
nosy: + eryksun
messages: + msg410228

2022-01-10 15:49:20ned.deilysetstatus: open -> pending
nosy: + ned.deily
messages: + msg410223

2022-01-10 09:01:46alimpfardcreate