Repository: pdm-project/pdm

Issue:
Environment detection fails for Conda environments
- [x] I have searched the issue tracker and believe that this is not a duplicate.

**Make sure you run commands with `-v` flag before pasting the output.**

## Steps to reproduce

- Enable `python.use_venv`
- Run `pdm install` in a project using an activated Conda environment

## Actual behavior

PDM tries to create a virtual environment.

## Expected behavior

PDM uses the existing Conda environment

## Environment Information
First call to `pdm info`

```bash
PDM version:
  2.0.2
Python Interpreter:
  /home/angus/.mambaforge/envs/texat/bin/python (3.10)
Project Root:
  /home/angus/Git/texat
Project Packages:
  None
{
  "implementation_name": "cpython",
  "implementation_version": "3.10.5",
  "os_name": "posix",
  "platform_machine": "x86_64",
  "platform_release": "5.15.0-41-generic",
  "platform_system": "Linux",
  "platform_version": "#44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022",
  "python_full_version": "3.10.5",
  "platform_python_implementation": "CPython",
  "python_version": "3.10",
  "sys_platform": "linux"
}
```

Second call
```bash
PDM version:
  2.0.2
Python Interpreter:
  /home/angus/Git/texat/.venv/bin/python (3.10)
Project Root:
  /home/angus/Git/texat
Project Packages:
  None
{
  "implementation_name": "cpython",
  "implementation_version": "3.10.5",
  "os_name": "posix",
  "platform_machine": "x86_64",
  "platform_release": "5.15.0-41-generic",
  "platform_system": "Linux",
  "platform_version": "#44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022",
  "python_full_version": "3.10.5",
  "platform_python_implementation": "CPython",
  "python_version": "3.10",
  "sys_platform": "linux"
}
```


Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
> Merging [#1254](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) (ee860ca) into [main](https://codecov.io/gh/pdm-project/pdm/commit/bfc827927cb78843dd7fa1bfa4a396351bc6c85a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) (bfc8279) will **increase** coverage by `0.01%`.
> The diff coverage is `50.00%`.

```diff
@@            Coverage Diff             @@
##             main    #1254      +/-   ##
==========================================
+ Coverage   81.27%   81.29%   +0.01%     
==========================================
  Files          93       93              
  Lines        7643     7643              
  Branches     1785     1785              
==========================================
+ Hits         6212     6213       +1     
+ Misses       1069     1068       -1     
  Partials      362      362              
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `81.18% <50.00%> (+0.01%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [pdm/project/core.py](https://codecov.io/gh/pdm-project/pdm/pull/1254/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#diff-cGRtL3Byb2plY3QvY29yZS5weQ==) | `89.42% <50.00%> (+0.24%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project). Last update [bfc8279...ee860ca](https://codecov.io/gh/pdm-project/pdm/pull/1254?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).


Patch:
diff --git a/news/1253.bugfix.md b/news/1253.bugfix.md
new file mode 100644
index 00000000..8e91ec5b
--- /dev/null
+++ b/news/1253.bugfix.md
@@ -0,0 +1,1 @@
+Support Conda environments when detecting the project environment.
diff --git a/news/1256.bugfix.md b/news/1256.bugfix.md
new file mode 100644
index 00000000..c111cf60
--- /dev/null
+++ b/news/1256.bugfix.md
@@ -0,0 +1,1 @@
+Stabilize sorting of URLs in `metadata.files` in `pdm.lock`.
diff --git a/pdm/cli/utils.py b/pdm/cli/utils.py
index e822f7fc..725d0c94 100644
--- a/pdm/cli/utils.py
+++ b/pdm/cli/utils.py
@@ -465,7 +465,7 @@ def format_lockfile(
                 continue
             array = tomlkit.array().multiline(True)
             for link, hash_value in sorted(
-                v.hashes.items(), key=lambda l: l[0].filename
+                v.hashes.items(), key=lambda l_h: (l_h[0].url_without_fragment, l_h[1])
             ):
                 inline = make_inline_table(
                     {"url": link.url_without_fragment, "hash": hash_value}
diff --git a/pdm/project/core.py b/pdm/project/core.py
index 5fd54c5f..1a52a369 100644
--- a/pdm/project/core.py
+++ b/pdm/project/core.py
@@ -259,8 +259,8 @@ class Project:
                 if get_venv_like_prefix(self.python.executable) is not None
                 else Environment(self)
             )
-        if os.getenv("VIRTUAL_ENV"):
-            venv = cast(str, os.getenv("VIRTUAL_ENV"))
+        venv = os.getenv("VIRTUAL_ENV", os.getenv("CONDA_PREFIX"))
+        if venv is not None:
             self.core.ui.echo(
                 f"Detected inside an active virtualenv [green]{venv}[/], reuse it.",
                 style="yellow",
diff --git a/pdm/resolver/core.py b/pdm/resolver/core.py
index 4a6993c2..195af3bc 100644
--- a/pdm/resolver/core.py
+++ b/pdm/resolver/core.py
@@ -20,11 +20,10 @@ def resolve(
     max_rounds: int = 10000,
 ) -> tuple[dict[str, Candidate], dict[str, list[Requirement]]]:
     """Core function to perform the actual resolve process.
-    Return a tuple containing 3 items:
+    Return a tuple containing 2 items:
 
         1. A map of pinned candidates
         2. A map of resolved dependencies for each dependency group
-        3. A map of package descriptions fetched from PyPI source
     """
     requirements.append(PythonRequirement.from_pyspec_set(requires_python))
     provider = cast(BaseProvider, resolver.provider)


Test patch:
diff --git a/tests/cli/test_lock.py b/tests/cli/test_lock.py
index 071ab9ff..80a10cf9 100644
--- a/tests/cli/test_lock.py
+++ b/tests/cli/test_lock.py
@@ -30,24 +30,23 @@ def test_lock_refresh(invoke, project, repository):
     assert project.is_lockfile_hash_match()
     assert not project.lockfile["metadata"]["files"].get("requests 2.19.1")
     project.add_dependencies({"requests": parse_requirement("requests>=2.0")})
-    repository.get_hashes = (
-        lambda c: {
-            Link(
-                "http://example.com/requests-2.19.1-py3-none-any.whl"
-            ): "sha256:abcdef123456"
-        }
+    url_hashes = {
+        "http://example.com/requests-2.19.1-py3-none-any.whl": "sha256:abcdef123456",
+        "http://example2.com/requests-2.19.1-py3-none-AMD64.whl": "sha256:abcdef123456",
+        "http://example1.com/requests-2.19.1-py3-none-any.whl": "sha256:abcdef123456",
+    }
+    repository.get_hashes = lambda c: (
+        {Link(url): hash for url, hash in url_hashes.items()}
         if c.identify() == "requests"
         else {}
     )
-    print(project.lockfile)
     assert not project.is_lockfile_hash_match()
     result = invoke(["lock", "--refresh", "-v"], obj=project)
     assert result.exit_code == 0
     assert project.is_lockfile_hash_match()
-    assert project.lockfile["metadata"]["files"]["requests 2.19.1"][0] == {
-        "url": "http://example.com/requests-2.19.1-py3-none-any.whl",
-        "hash": "sha256:abcdef123456",
-    }
+    assert project.lockfile["metadata"]["files"]["requests 2.19.1"] == [
+        {"url": url, "hash": hash} for url, hash in sorted(url_hashes.items())
+    ]
 
 
 def test_lock_refresh_keep_consistent(invoke, project, repository):