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"
}
```


Comments:

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):