Repository: pypa/pipenv

Issue:
2023.8.19 through to 2023.8.22 break editable package installation
### Issue description

Editable package installation appears to be broken from pipenv 2023.8.19 onwards.
For example, given a package in the directory `test-hello` under the pipenv root installing the package with `pipenv install -e ./test-hello` fails.

### Expected result

pipenv 2023.7.23 and prior versions would install editable packages in Pipfile and in the venv.

### Actual result

Here's a log of package installation across recent versions of pipenv.
[try_pipenv_versions.log](https://github.com/pypa/pipenv/files/12411124/try_pipenv_versions.log)

For example:
```
+ pipx install pipenv==2023.8.19
creating virtual environment...
installing pipenv from spec 'pipenv==2023.8.19'...
done! ✨ 🌟 ✨
  installed package pipenv 2023.8.19, installed using Python 3.8.10
  These apps are now globally available
    - pipenv
    - pipenv-resolver
+ git clean -dfx
Removing .venv/
Removing applications/test-hello/test_hello.egg-info/
Removing test-hello/test_hello.egg-info/
+ echo '=== Using pipenv version 2023.8.19 ==='
=== Using pipenv version 2023.8.19 ===
+ pipenv sync
Creating a virtualenv for this project...
Pipfile: /home/stewart/src/pipenv_editable_install_repro/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
created virtual environment CPython3.8.10.final.0-64 in 48ms
  creator CPython3Posix(dest=/home/stewart/src/pipenv_editable_install_repro/.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/stewart/.local/share/virtualenv)
    added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /home/stewart/src/pipenv_editable_install_repro/.venv
Installing dependencies from Pipfile.lock (db4242)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!
+ install_test_and_uninstall_package ./test-hello
+ path=./test-hello
+ pipenv run pipenv install -e ./test-hello
Installing -e ./test-hello...
Resolving -e ./test-hello...
[31m[1mError:[0m Traceback (most recent call last):
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/routines/install.py", line 
279, in do_install
    added, cat, normalized_name = project.add_package_to_pipfile(
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/project.py", line 1159, in 
add_package_to_pipfile
    name, normalized_name, entry = self.generate_package_pipfile_entry(
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/project.py", line 1110, in 
generate_package_pipfile_entry
    req_name = determine_package_name(package)
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/utils/dependencies.py", line 
745, in determine_package_name
    raise ValueError(f"Could not determine package name from {package}")
ValueError: Could not determine package name from 
file:///file:///home/stewart/src/pipenv_editable_install_repro/test-hello

✘ Failed adding package to Pipfile
✔ Installation Succeeded
```

It's also interesting that installation fails, due to the mangled URI that starts with `file:///file:///`, but the final message is that installation succeeded.

### Steps to replicate

https://github.com/stewartmiles/pipenv_editable_install_repro contains a minimal test environment.
I generated the log file using:

```shell
git clone https://github.com/stewartmiles/pipenv_editable_install_repro
cd pipenv_editable_install_repro
./try_pipenv_versions.sh 2>&1 | tee ../try_pipenv_versions.log
```

-------------------------------------------------------------------------------

Please run `$ pipenv --support`, and paste the results here. Don't put backticks (`` ` ``) around it! The output already contains Markdown formatting.

<details><summary>$ pipenv --support</summary>

Pipenv version: `'2023.8.22'`

Pipenv location: `'/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv'`

Python location: `'/home/stewart/.local/pipx/venvs/pipenv/bin/python'`

OS Name: `'posix'`

User pip version: `'23.2.1'`

user Python installations found:


PEP 508 Information:

```
{'implementation_name': 'cpython',
 'implementation_version': '3.8.10',
 'os_name': 'posix',
 'platform_machine': 'x86_64',
 'platform_python_implementation': 'CPython',
 'platform_release': '5.15.0-69-generic',
 'platform_system': 'Linux',
 'platform_version': '#76~20.04.1-Ubuntu SMP Mon Mar 20 15:54:19 UTC 2023',
 'python_full_version': '3.8.10',
 'python_version': '3.8',
 'sys_platform': 'linux'}
```

System environment variables:

  - `SHELL`
  - `PIPENV_VENV_IN_PROJECT`
  - `TERMCAP`
  - `SSH_AUTH_SOCK`
  - `WINDOW`
  - `SSH_AGENT_PID`
  - `PWD`
  - `LOGNAME`
  - `XDG_SESSION_TYPE`
  - `MOTD_SHOWN`
  - `HOME`
  - `LANG`
  - `LS_COLORS`
  - `SSH_CONNECTION`
  - `LESSCLOSE`
  - `XDG_SESSION_CLASS`
  - `TERM`
  - `LESSOPEN`
  - `USER`
  - `XLA_FLAGS`
  - `SHLVL`
  - `XDG_SESSION_ID`
  - `KUBECONFIG`
  - `LD_LIBRARY_PATH`
  - `XDG_RUNTIME_DIR`
  - `SSH_CLIENT`
  - `XDG_DATA_DIRS`
  - `PATH`
  - `STY`
  - `DBUS_SESSION_BUS_ADDRESS`
  - `SSH_TTY`
  - `USE_GKE_GCLOUD_AUTH_PLUGIN`
  - `OLDPWD`
  - `_`
  - `PIP_DISABLE_PIP_VERSION_CHECK`
  - `PYTHONDONTWRITEBYTECODE`
  - `PYTHONFINDER_IGNORE_UNSUPPORTED`

Pipenv–specific environment variables:

 - `PIPENV_VENV_IN_PROJECT`: `true`

Debug–specific environment variables:

  - `PATH`: `~/.local/bin:/home/stewart/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin`
  - `SHELL`: `/bin/bash`
  - `LANG`: `en_US.UTF-8`
  - `PWD`: `/home/stewart/src/pipenv_editable_install_repro`


---------------------------

Contents of `Pipfile` ('/home/stewart/src/pipenv_editable_install_repro/Pipfile'):

```toml
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.8"

```


Contents of `Pipfile.lock` ('/home/stewart/src/pipenv_editable_install_repro/Pipfile.lock'):

```json
{
    "_meta": {
        "hash": {
            "sha256": "7f7606f08e0544d8d012ef4d097dabdd6df6843a28793eb6551245d4b2db4242"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.8"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {},
    "develop": {}
}

```
</details>



Discussion:
Same here, I am working in Amplify and had to specify `pipenv==2023.7.23` in amplify.yml to make Amplify build work, otherwise (without specifying the version) it will raise 'Failed to install dependencies in ... Error: Command failed with exit code 1: pipenv install)'
Same issue here. Also breaking for `pipenv install "git+..."`.
@lewiswolf I believe that is a separate issue, there are a couple reports about the git+ vcs issue and an open PR that some have reported fix it. 

Regarding  this actual issue report, I think part of the problem (maybe not the whole problem) is the ast parser logic for the old setup.py file logic is not accounting for all cases.   I tried to correct for that some locally over lunch in the reproduction link (super helpful by the way) and was able to get it to install for the top level test-hello, but when I try the one in the applications sub-dir, I get a pathing issue.   

I'll be able to cut a release going  into late evening for the PR I have open, but I am not sure I have an immediate fix for what's going on with editable file installs in this issue report.
So I opened https://github.com/pypa/pipenv/pull/5861 which I did some testing on windows and linux and it appears to be past this issue, but what I noticed is on windows it will lock a relative path fine, but on linux in this branch its adding the full path to the Pipfile, and then locking that.  🤔 

`test-hello = {file = "file:/home/matteius/pipenv_editable_install_repro/applications/test-hello", editable=true}`
Good news (I think) -- this branch works for your reproduction examples that I tried on Windows and the changes are still passing the existing pipenv tests.  Please have a look and help me verify if it resolves the issues or if there is still something to address regarding local file installs:  https://github.com/pypa/pipenv/pull/5870
@matteius thanks for the patch. However, it looks like things still aren't working correctly.

Here's a log of a new test run with 2023.8.26:
[try_pipenv_versions.log](https://github.com/pypa/pipenv/files/12456664/try_pipenv_versions.log)

Or even easier after cloning https://github.com/stewartmiles/pipenv_editable_install_repro:
```shell
pipenv sync && pipenv install -e ./test-hello
```

which errors out with:
```
Creating a virtualenv for this project...
Pipfile: /home/stewart/src/pipenv_editable_install_repro/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
⠋ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 51ms
  creator CPython3Posix(dest=/home/stewart/src/pipenv_editable_install_repro/.venv, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/stewart/.local/share/virtualenv)
    added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.2
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /home/stewart/src/pipenv_editable_install_repro/.venv
Installing dependencies from Pipfile.lock (db4242)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!
Installing -e ./test-hello...
Resolving -e ./test-hello...
⠋ Installing...Error: Traceback (most recent call last):
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/routines/install.py",
line 255, in do_install
    added, cat, normalized_name = project.add_package_to_pipfile(
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/project.py", line
1167, in add_package_to_pipfile
    name, normalized_name, entry = self.generate_package_pipfile_entry(
  File "/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/project.py", line
1114, in generate_package_pipfile_entry
    req_name = determine_package_name(package)
  File
"/home/stewart/.local/pipx/venvs/pipenv/lib/python3.8/site-packages/pipenv/utils/dependencies.py", line
744, in determine_package_name
    raise ValueError(f"Could not determine package name from {package}")
ValueError: Could not determine package name from
file:///home/stewart/src/pipenv_editable_install_repro/test-hello

✘ Failed adding package to Pipfile
✔ Installation Succeeded
Installing dependencies from Pipfile.lock (db4242)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
```
@stewartmiles That reproduction repository it was I was testing the fix against.  I tried both windows, and here is my ubuntu:

```
matteius@matteius-VirtualBox:~/pipenv_editable_install_repro$ pipenv install -e ./test-hello/
Creating a virtualenv for this project...
Pipfile: /home/matteius/pipenv_editable_install_repro/Pipfile
Using default python from /usr/bin/python (3.10.12) to create virtualenv...
⠼ Creating virtual environment...created virtual environment CPython3.10.12.final.0-64 in 337ms
  creator CPython3Posix(dest=/home/matteius/.virtualenvs/pipenv_editable_install_repro-3TYtUsNd, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/matteius/.local/share/virtualenv)
    added seed packages: pip==23.2.1, setuptools==68.0.0, wheel==0.41.2
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

✔ Successfully created virtual environment!
Virtualenv location: /home/matteius/.virtualenvs/pipenv_editable_install_repro-3TYtUsNd
Creating a Pipfile for this project...
Installing -e ./test-hello/...
Resolving -e ./test-hello/...
Added test-hello to Pipfile's [packages] ...
✔ Installation Succeeded
Pipfile.lock not found, creating...
Locking [packages] dependencies...
Building requirements...
Resolving dependencies...
✔ Success!
⠸ Locking...Warning: INFO:pipenv.patched.pip._internal.operations.prepare:Obtaining file:///home/matteius/pipenv_editable_install_repro/test-hello (from -r /tmp/pipenv-8i8n7f5s-requirements/pipenv-e9l02qrp-constraints.txt (line 2))
INFO:pipenv.patched.pip._internal.cli.spinners:Installing build dependencies: started
INFO:pipenv.patched.pip._internal.cli.spinners:Installing build dependencies: finished with status 'done'
INFO:pipenv.patched.pip._internal.cli.spinners:Checking if build backend supports build_editable: started
INFO:pipenv.patched.pip._internal.cli.spinners:Checking if build backend supports build_editable: finished with status 'done'
INFO:pipenv.patched.pip._internal.cli.spinners:Getting requirements to build wheel: started
INFO:pipenv.patched.pip._internal.cli.spinners:Getting requirements to build wheel: finished with status 'done'
INFO:pipenv.patched.pip._internal.cli.spinners:Preparing metadata (pyproject.toml): started
INFO:pipenv.patched.pip._internal.cli.spinners:Preparing metadata (pyproject.toml): finished with status 'done'
Locking [dev-packages] dependencies...
Updated Pipfile.lock (a528f480c44a1940d1f9897c854058593109192be0c4f125d8e2497338d05198)!
Installing dependencies from Pipfile.lock (d05198)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
```


🤔 that's odd. My environment uses pipenv installed via pipx and I'm using Python 3.8.

Here's my attempt with the Python docker image:

```shell
docker run -it python:3.10.12 /bin/sh
# pip install pipenv
# pipenv --version
pipenv, version 2023.8.26
# git clone https://github.com/stewartmiles/pipenv_editable_install_repro
# cd pipenv_editable_install_repro
```

First attempt:
```
# pipenv sync && pipenv install -e ./test-hello
Warning: Python 3.8 was not found on your system...
Neither 'pyenv' nor 'asdf' could be found to install Python.
You can specify specific versions of Python with:
$ pipenv --python path/to/python
```

ok so I need to update the Pipfile etc.:
```
# rm Pipfile*
# pipenv --python 3.10
# pipenv lock
Locking [packages] dependencies...
Locking [dev-packages] dependencies...
Updated Pipfile.lock (8e6547742479a83bb67555446f723053c2894d670dcc89b1a6e1e07cab885a1b)!
```

Finally, try again:
```
# pipenv install -e ./test-hello
Installing -e ./test-hello...
Resolving -e ./test-hello...
⠋ Installing...Error: Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/pipenv/routines/install.py", line 255, in do_install
    added, cat, normalized_name = project.add_package_to_pipfile(
  File "/usr/local/lib/python3.10/site-packages/pipenv/project.py", line 1167, in
add_package_to_pipfile
    name, normalized_name, entry = self.generate_package_pipfile_entry(
  File "/usr/local/lib/python3.10/site-packages/pipenv/project.py", line 1114, in
generate_package_pipfile_entry
    req_name = determine_package_name(package)
  File "/usr/local/lib/python3.10/site-packages/pipenv/utils/dependencies.py", line 744, in
determine_package_name
    raise ValueError(f"Could not determine package name from {package}")
ValueError: Could not determine package name from file:///pipenv_editable_install_repro/test-hello

✘ Failed adding package to Pipfile
✔ Installation Succeeded
Installing dependencies from Pipfile.lock (885a1b)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
```

So it doesn't seem related to Python version or the fact I usually install pipenv with pipx.
Maybe the ast parser for the setup.py to determine the package name isn't working right for lower versions of python?
I reproduced with 3.10.12 which is the same version that you're using AFAIK
This maybe a different issue as I'm hitting a private repo but I wanted to add my notes in case its related or it may help others since this thread helped me.

My github build started failing on 8/16 I see that a passing build that day had pipenv version `2023.7.23` and the very next build had `2023.8.22` and I started getting the following error

```
CRITICAL:pipenv.patched.pip._internal.resolution.resolvelib.factory:Could not find a version that satisfies the requirement dkany (from versions: none)
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\resolver.py", line 811, in _main
[ResolutionFailure]:       resolve_packages(
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\resolver.py", line 759, in resolve_packages
[ResolutionFailure]:       results, resolver = resolve(
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\resolver.py", line 738, in resolve
[ResolutionFailure]:       return resolve_deps(
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\utils\resolver.py", line 1096, in resolve_deps
[ResolutionFailure]:       results, hashes, markers_lookup, resolver, skipped = actually_resolve_deps(
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\utils\resolver.py", line 895, in actually_resolve_deps
[ResolutionFailure]:       resolver.resolve()
[ResolutionFailure]:   File "c:\users\bhunscher\appdata\local\programs\python\python38\lib\site-packages\pipenv\utils\resolver.py", line 684, in resolve
[ResolutionFailure]:       raise ResolutionFailure(message=str(e))
[pipenv.exceptions.ResolutionFailure]: Warning: Your dependencies could not be resolved. You likely have a mismatch in your sub-dependencies.
  You can use $ pipenv install --skip-lock to bypass this mechanism, then run $ pipenv graph to inspect the situation.
  Hint: try $ pipenv lock --pre if it is a pre-release dependency.
ERROR: No matching distribution found for dkany
```

```
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[[source]]
url = "https://my-repository/simple/"
verify_ssl = true
name = "mine"

[packages]
dataclasses-json = "*"
pandas = ">0.20.1"
strenum = "*"
pyyaml = "*"
dkany = {version="0.0.13", index="mine"}
....

```

This may be obvious but I was wondering for a bit so if I help someone great.

@stbth01 that issue may be what is tracked with #5878 and #5880
@matteius not sure whether you saw https://github.com/pypa/pipenv/issues/5858#issuecomment-1696116439 but as I mentioned I reproduced the problem with `2023.8.26` with python `3.10.12` (your test version) in a Python docker container.
@stewartmiles I determined it to be a limitation of the prior ast parser on python 3.10 version -- hopefully latest pipenv `2023.8.28` works for you.   Please try it out and close the issue if it is satisfactory.
@matteius what is the minimum Python version now required to use pipenv? It used to support all of 3.x but now it sounds like if it doesn't work with 3.10 then all users using editable packages (at least) will need to upgrade.
@stewartmiles python 3.7 thru 3.11 are still supported -- at some point later this year we will drop support for 3.7.   I patched the ast parser logic for setup.py; unless you are reporting a new issue with this update?  I still recommend folks migrate away from putting the library name in the setup.py because parsing it can be problematic, but it is expected to work in cases where the string is present in the setup.py
@matteius I misread the version number you mentioned in https://github.com/pypa/pipenv/issues/5858#issuecomment-1696627911 i.e `2023.8.28` not `26`. In https://github.com/pypa/pipenv/issues/5858#issuecomment-1696073704 I tested `2023.8.26` and Python 3.10.12 things did not work, editable package installation fails.

With the _latest_ version `2023.8.28` using https://github.com/stewartmiles/pipenv_editable_install_repro @ https://github.com/stewartmiles/pipenv_editable_install_repro/commit/1926b97d0e6bfb65c8090845dadec43f55ddcb4b things work fine.

However, now when installing editable packages from parent directories outside the venv (a behavior that used to work), package installation embeds the absolute path of the editable package in Pipfile and Pipfile.lock. This is reproducible with `2023.8.28` with https://github.com/stewartmiles/pipenv_editable_install_repro/commit/86ed880900ee95eb1419bb3aab928a94ca00759c (sorry the shell is becoming a mess).

i.e running:
```
git clone https://github.com/stewartmiles/pipenv_editable_install_repro
cd pipenv_editable_install_repro
./try_pipenv_versions.sh 2>&1 | tee ../try_pipenv_versions.log
```

results in:
```
+ echo './../test-hello and ../test-hello '
./../test-hello and ../test-hello
+ echo 'not found in {filename}'
not found in {filename}
+ echo '--- Pipfile ---'
--- Pipfile ---
+ cat Pipfile
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
test-hello = {file = "/home/stewart/src/pipenv_editable_install_repro/test-hello", editable = true}

[dev-packages]

[requires]
python_version = "3.8"
+ error=1
+ for filename in Pipfile Pipfile.lock
++ grep -F '"./../test-hello' Pipfile.lock
+ local found_relative_package_path=
++ grep -F '"../test-hello' Pipfile.lock
+ local found_alternative_package_path=
+ [[ -z '' ]]
+ [[ -z '' ]]
+ echo './../test-hello and ../test-hello '
./../test-hello and ../test-hello
+ echo 'not found in {filename}'
not found in {filename}
+ echo '--- Pipfile.lock ---'
--- Pipfile.lock ---
+ cat Pipfile.lock
{
    "_meta": {
        "hash": {
            "sha256": "08d8cd2781157cf3ebafbeba84526e1c8ed2450b91b614e70ea3e09e24c9ebaa"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.8"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "test-hello": {
            "editable": true,
            "file": "/home/stewart/src/pipenv_editable_install_repro/test-hello"
        }
    },
    "develop": {}
}
+ error=1
+ return 1
+ error=1
+ popd
~/src/pipenv_editable_install_repro
+ return 1
+ echo '--- pipenv version 2023.8.28 is broken ---'
--- pipenv version 2023.8.28 is broken ---
```

The full log is [try_pipenv_versions.log](https://github.com/pypa/pipenv/files/12467831/try_pipenv_versions.log)

@stewartmiles Ah, absolute path bites again -- ok, well I believe this is the area where that is happening:  https://github.com/pypa/pipenv/blob/main/pipenv/utils/dependencies.py#L648-L651

I am fully open to code patches that will further figure out how to keep the file paths relative -- I am just not sure the exact direction to head.

Resolving patch:
diff --git a/pipenv/utils/dependencies.py b/pipenv/utils/dependencies.py
--- a/pipenv/utils/dependencies.py
+++ b/pipenv/utils/dependencies.py
@@ -645,10 +645,29 @@ def determine_path_specifier(package: InstallRequirement):
         if package.link.scheme in ["http", "https"]:
             return package.link.url_without_fragment
         if package.link.scheme == "file":
+            abs_path = Path(package.link.file_path).resolve()
+            current_dir = Path.cwd()
+
             try:
-                return Path(package.link.file_path).relative_to(Path.cwd()).as_posix()
+                relative_path = abs_path.relative_to(current_dir)
+                return relative_path.as_posix()
             except ValueError:
-                return Path(package.link.file_path).as_posix()
+                # If the direct relative_to fails, manually compute the relative path
+                common_parts = 0
+                for part_a, part_b in zip(abs_path.parts, current_dir.parts):
+                    if part_a == part_b:
+                        common_parts += 1
+                    else:
+                        break
+
+                # Number of ".." needed are the extra parts in the current directory
+                # beyond the common parts
+                up_levels = [".."] * (len(current_dir.parts) - common_parts)
+                # The relative path is constructed by going up as needed and then
+                # appending the non-common parts of the absolute path
+                rel_parts = up_levels + list(abs_path.parts[common_parts:])
+                relative_path = Path(*rel_parts)
+                return relative_path.as_posix()
 
 
 def determine_vcs_specifier(package: InstallRequirement):


Test patch: