Repository: pdm-project/pdm
Pull request #3152 (2024-09-10 02:27:17)

fix: allow no-marker option to work in pdm export command
Signed-off-by: Frost Ming <me@frostming.com>

## Pull Request Checklist

- [ ] A news fragment is added in `news/` describing what is new.
- [ ] Test cases added for changed code.

## Describe what you have changed in this PR.


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pdm-project/pdm/pull/3152?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) Report
Attention: Patch coverage is `16.66667%` with `5 lines` in your changes missing coverage. Please review.
> Project coverage is 85.61%. Comparing base [(`cfa4913`)](https://app.codecov.io/gh/pdm-project/pdm/commit/cfa4913ec36922194f7543b3f80895fe32a4659f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) to head [(`ef80f57`)](https://app.codecov.io/gh/pdm-project/pdm/commit/ef80f57d3b7476a1f61913eb51ec3ffcadd5e701?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).

| [Files with missing lines](https://app.codecov.io/gh/pdm-project/pdm/pull/3152?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Patch % | Lines |
|---|---|---|
| [src/pdm/cli/commands/export.py](https://app.codecov.io/gh/pdm-project/pdm/pull/3152?src=pr&el=tree&filepath=src%2Fpdm%2Fcli%2Fcommands%2Fexport.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project#diff-c3JjL3BkbS9jbGkvY29tbWFuZHMvZXhwb3J0LnB5) | 16.66% | [4 Missing and 1 partial :warning: ](https://app.codecov.io/gh/pdm-project/pdm/pull/3152?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #3152      +/-   ##
==========================================
+ Coverage   85.60%   85.61%   +0.01%     
==========================================
  Files         109      109              
  Lines       11044    11049       +5     
  Branches     2413     2415       +2     
==========================================
+ Hits         9454     9460       +6     
- Misses       1092     1096       +4     
+ Partials      498      493       -5     
```

| [Flag](https://app.codecov.io/gh/pdm-project/pdm/pull/3152/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | Coverage Δ | |
|---|---|---|
| [unittests](https://app.codecov.io/gh/pdm-project/pdm/pull/3152/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project) | `85.41% <16.66%> (+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.

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pdm-project/pdm/pull/3152?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pdm-project).


Patch:
diff --git a/news/3152.bugfix.md b/news/3152.bugfix.md
new file mode 100644
index 00000000..159b3f14
--- /dev/null
+++ b/news/3152.bugfix.md
@@ -0,0 +1,1 @@
+Now when `--no-markers` is used, the exported requirements can only work on the current platform.
diff --git a/pdm.lock b/pdm.lock
index 6dc62015..e7f78773 100644
--- a/pdm.lock
+++ b/pdm.lock
@@ -5,7 +5,7 @@
 groups = ["default", "all", "cookiecutter", "copier", "doc", "keyring", "pytest", "template", "test", "tox", "workflow"]
 strategy = ["inherit_metadata"]
 lock_version = "4.5.0"
-content_hash = "sha256:ebd43a389ae69bf0178c67d859dc2de063316e6d9727265153393563f93ad317"
+content_hash = "sha256:bd8cd44205f546193ad7c31fd24ab10f181d68938d1e6149feaf908a2890fa7a"
 
 [[metadata.targets]]
 requires_python = ">=3.8"
@@ -2120,14 +2120,14 @@ files = [
 
 [[package]]
 name = "truststore"
-version = "0.8.0"
-requires_python = ">= 3.10"
+version = "0.9.2"
+requires_python = ">=3.10"
 summary = "Verify certificates using native system trust stores"
 groups = ["default"]
 marker = "python_version >= \"3.10\""
 files = [
-    {file = "truststore-0.8.0-py3-none-any.whl", hash = "sha256:e37a5642ae9fc48caa8f120b6283d77225d600d224965a672c9e8ef49ce4bb4c"},
-    {file = "truststore-0.8.0.tar.gz", hash = "sha256:dc70da89634944a579bfeec70a7a4523c53ffdb3cf52d1bb4a431fda278ddb96"},
+    {file = "truststore-0.9.2-py3-none-any.whl", hash = "sha256:04559916f8810cc1a5ecc41f215eddc988746067b754fc0995da7a2ceaf54735"},
+    {file = "truststore-0.9.2.tar.gz", hash = "sha256:a1dee0d0575ff22d2875476343783a5d64575419974e228f3248772613c3d993"},
 ]
 
 [[package]]
diff --git a/pyproject.toml b/pyproject.toml
index dcc98ee5..5761376c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -28,7 +28,7 @@ dependencies = [
     "python-dotenv>=0.15",
     "resolvelib>=1.0.1",
     "installer<0.8,>=0.7",
-    "truststore; python_version >= \"3.10\"",
+    "truststore>=0.9; python_version >= \"3.10\"",
     "tomli>=1.1.0; python_version < \"3.11\"",
     "importlib-resources>=5; python_version < \"3.9\"",
     "importlib-metadata>=3.6; python_version < \"3.10\"",
diff --git a/src/pdm/cli/commands/export.py b/src/pdm/cli/commands/export.py
index 6959ab8b..f3e53806 100644
--- a/src/pdm/cli/commands/export.py
+++ b/src/pdm/cli/commands/export.py
@@ -68,7 +68,9 @@ class Command(BaseCommand):
             options.hashes = False
         selection = GroupSelection.from_options(project, options)
         if options.markers is False:
-            project.core.ui.deprecated("The --no-markers option is deprecated and has no effect.")
+            project.core.ui.warn(
+                "The --no-markers option is on, the exported requirements can only work on the current platform"
+            )
         packages: Iterable[Requirement] | Iterable[Candidate]
         if options.pyproject:
             packages = [r for group in selection for r in project.get_dependencies(group)]
@@ -86,6 +88,7 @@ class Command(BaseCommand):
             groups = set(selection)
             packages = []
             seen_extras: set[str] = set()
+            this_spec = project.environment.spec
             for candidate in candidates:
                 if groups.isdisjoint(candidate.req.groups):
                     continue
@@ -97,6 +100,10 @@ class Command(BaseCommand):
                         continue
                 elif candidate.req.extras:
                     continue
+                if not options.markers and candidate.req.marker:
+                    if not candidate.req.marker.matches(this_spec):
+                        continue
+                    candidate.req.marker = None
                 packages.append(candidate)  # type: ignore[arg-type]
 
         content = FORMATS[options.format].export(project, packages, options)


Test patch:
diff --git a/tests/cli/test_others.py b/tests/cli/test_others.py
index 4bf1aa5d..14bfa47b 100644
--- a/tests/cli/test_others.py
+++ b/tests/cli/test_others.py
@@ -234,9 +234,20 @@ def test_show_update_hint(pdm, project, monkeypatch):
 
 @pytest.mark.usefixtures("repository")
 def test_export_with_platform_markers(pdm, project):
-    pdm(["add", "--no-sync", 'urllib3; sys_platform == "fake"'], obj=project, strict=True)
+    pdm(
+        ["add", "--no-sync", 'urllib3; sys_platform == "fake"', 'idna; python_version >= "3.7"'],
+        obj=project,
+        strict=True,
+    )
     result = pdm(["export", "--no-hashes"], obj=project, strict=True)
-    assert 'urllib3==1.22; sys_platform == "fake"' in result.output.splitlines()
+    result_lines = result.output.splitlines()
+    assert 'urllib3==1.22; sys_platform == "fake"' in result_lines
+    assert 'idna==2.7; python_version >= "3.7"' in result_lines
+
+    result = pdm(["export", "--no-hashes", "--no-markers"], obj=project, strict=True)
+    result_lines = result.output.splitlines()
+    assert not any(line.startswith("urllib3") for line in result_lines)
+    assert "idna==2.7" in result_lines
 
 
 @pytest.mark.usefixtures("repository", "vcs")