Repository: pypa/pipenv

Issue:
Empty .venv file causes entire virtualenvs directory to be deleted
### Summary

if `.venv` is an empty file and I run `pipenv --rm`, the entire `~/.local/share/virtualenvs` directory is deleted. This happens even when there's no `Pipfile` present.

This is on macOS, pipenv version 2021.5.29.

### Issue description

I was messing around with my project environment, and tried to tell my editor where to find the environment by putting it in `.venv`. Turns out that running `pipenv --venv > .venv` fails because it first touches `.venv`, and then the actual command looks at the empty `.venv` and doesn't find an environment. This causes the file to remain empty. (The command did print an error message in STDERR, but somehow I didn't notice it immediately.)

So now with an empty `.venv` in my project folder, I ran `pipenv install <some package>` and wondered why it said it had to create a new virtualenv. I looked at the output more closely and realized that the new env was created in `/Users/myname/.local/share/virtualenvs` (not in a subdirectory).

I got curious about what would happen if I now tried to scrap the thing, and as I feared, running `pipenv --rm` deleted that entire directory, which contained all the other virtualenvs that I had made with Pipenv for other projects earlier. It wasn't a big loss or I wouldn't have tried, but still not something I like to see happening.

### Steps to replicate

In an empty directory:

```
touch .venv
pipenv --rm
```

Now the entire `~/.local/share/virtualenvs` directory is deleted.

The way I ended up in this situation was actually essentially the following:

```
pipenv --python 3.9
pipenv --venv > .venv
pipenv install requests
pipenv --rm
```




Discussion:
This is because it cannot handle empty .venv file and then returns incorrect virtual environment path which is workon home.

Resolving patch:
diff --git a/pipenv/project.py b/pipenv/project.py
--- a/pipenv/project.py
+++ b/pipenv/project.py
@@ -270,6 +270,10 @@ def get_location_for_virtualenv(self):
         with io.open(dot_venv) as f:
             name = f.read().strip()
 
+        # If .venv file is empty, set location based on config.
+        if not name:
+            return str(get_workon_home().joinpath(self.virtualenv_name))
+
         # If content looks like a path, use it as a relative path.
         # Otherwise use directory named after content in WORKON_HOME.
         if looks_like_dir(name):


Test patch:
diff --git a/tests/integration/test_dot_venv.py b/tests/integration/test_dot_venv.py
--- a/tests/integration/test_dot_venv.py
+++ b/tests/integration/test_dot_venv.py
@@ -76,6 +76,36 @@ def test_venv_file(venv_name, PipenvInstance):
             assert venv_path == normalize_drive(venv_expected_path)
 
 
+@pytest.mark.dotvenv
+def test_empty_venv_file(PipenvInstance):
+    """Tests virtualenv creation when a empty .venv file exists at the project root
+    """
+    with PipenvInstance(chdir=True) as p:
+        file_path = os.path.join(p.path, '.venv')
+        with open(file_path, 'w') as f:
+            pass
+
+        with temp_environ(), TemporaryDirectory(
+            prefix='pipenv-', suffix='temp_workon_home'
+        ) as workon_home:
+            os.environ['WORKON_HOME'] = workon_home.name
+            if 'PIPENV_VENV_IN_PROJECT' in os.environ:
+                del os.environ['PIPENV_VENV_IN_PROJECT']
+
+            c = p.pipenv('install')
+            assert c.returncode == 0
+
+            c = p.pipenv('--venv')
+            assert c.returncode == 0
+            venv_loc = Path(c.stdout.strip()).absolute()
+            assert venv_loc.exists()
+            assert venv_loc.joinpath('.project').exists()
+            from pathlib import PurePosixPath
+            venv_path = normalize_drive(venv_loc.as_posix())
+            venv_path_parent = str(PurePosixPath(venv_path).parent)
+            assert venv_path_parent == Path(workon_home.name).absolute().as_posix()
+
+
 @pytest.mark.dotvenv
 def test_venv_file_with_path(PipenvInstance):
     """Tests virtualenv creation when a .venv file exists at the project root