Repository: pex-tool/pex

Issue:
Using `--target-system linux --target-system mac` can still lead to failed attempts to lock Windows requirements.
This works:
```
$ rm -rf ~/.pex && time pex --python python3.7 pex==2.1.99 -cpex3 -- lock create --style universal --resolver-version pip-2020-resolver 'pywinpty>=1.1.0; os_name == "nt"' --interpreter-constraint "CPython<4,>=3.7.5" --indent 2 -o lock-2.1.99-no-ts.json

real	0m6.230s
user	0m2.608s
sys	0m0.262s
```

With a lock of:
```json
{
  "allow_builds": true,
  "allow_prereleases": false,
  "allow_wheels": true,
  "build_isolation": true,
  "constraints": [],
  "locked_resolves": [
    {
      "locked_requirements": [
        {
          "artifacts": [
            {
              "algorithm": "sha256",
              "hash": "f7ae5d29f1c3d028e06032f8d267b51fd72ea219b9bba3e2a972a7bc26a25a87",
              "url": "https://files.pythonhosted.org/packages/b2/f1/e47644d2097be5b927e11d69efdf3c194b280e97976c0c0612641b085699/pywinpty-2.0.6-cp39-none-win_amd64.whl"
            },
            {
              "algorithm": "sha256",
              "hash": "a91a77d23f29a58b44f62a9474a31ed67df1277cddb69665275f8d22429046ac",
              "url": "https://files.pythonhosted.org/packages/2e/a6/50815a4aeff3946c5723bcf3363980f98461fa01b8fa125c41fbe852540a/pywinpty-2.0.6.tar.gz"
            },
            {
              "algorithm": "sha256",
              "hash": "5e4b2167e813575bf495b46adb2d88be5c470d9daf49d488900350853e95248f",
              "url": "https://files.pythonhosted.org/packages/63/40/54b64836e0ac993a4df91e27e0ff9ee7399cf72a3eec892ec9d66ccc4263/pywinpty-2.0.6-cp38-none-win_amd64.whl"
            },
            {
              "algorithm": "sha256",
              "hash": "7fadc5265484c7d7c84554b9f1cfd7acf6383a877c1cfb3ee77d51179145b3ce",
              "url": "https://files.pythonhosted.org/packages/6f/9c/63f7e90094307b97e57c9ec8a384e360bd289d2b1252cbccbab812f7f852/pywinpty-2.0.6-cp310-none-win_amd64.whl"
            },
            {
              "algorithm": "sha256",
              "hash": "906a3048ecfec6ece1b141594ebbbcd5c4751960714c50524e8e907bb77c9207",
              "url": "https://files.pythonhosted.org/packages/a8/43/d73c866d6b50ac86ec16c4e70465d4fe1f7aa405f59361cdc6970809444b/pywinpty-2.0.6-cp37-none-win_amd64.whl"
            }
          ],
          "project_name": "pywinpty",
          "requires_dists": [],
          "requires_python": ">=3.7",
          "version": "2.0.6"
        }
      ],
      "platform_tag": null
    }
  ],
  "path_mappings": {},
  "pex_version": "2.1.99",
  "prefer_older_binary": false,
  "requirements": [
    "pywinpty>=1.1.0; os_name == \"nt\""
  ],
  "requires_python": [
    "<4,>=3.7.5"
  ],
  "resolver_version": "pip-2020-resolver",
  "style": "universal",
  "target_systems": [],
  "transitive": true,
  "use_pep517": null
}
```

But this fails:
```
$ rm -rf ~/.pex && time pex --python python3.7 pex==2.1.99 -cpex3 -- lock create --style universal --resolver-version pip-2020-resolver 'pywinpty>=1.1.0; os_name == "nt"' --interpreter-constraint "CPython<4,>=3.7.5" --indent 2 -o lock-2.1.99-ts.json --target-system linux --target-system mac
pid 97975 -> /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python -sE /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/pex --disable-pip-version-check --no-python-version-warning --exists-action a --no-input --isolated -q --cache-dir /home/jsirois/.pex/pip_cache --log /tmp/pex-pip-log.cxvjjl_g/pip.log download --dest /tmp/tmp2f9h93cj/home.jsirois..pyenv.versions.3.7.13.bin.python3.7 pywinpty>=1.1.0; os_name == "nt" --index-url https://pypi.org/simple --retries 5 --timeout 15 exited with 1 and STDERR:
Ignoring the following environment variables in Pex venv mode:
_PEX_TARGET_SYSTEMS_FILE=/tmp/tmpdc9xie_9/target_systems.json

WARNING: Discarding https://files.pythonhosted.org/packages/2e/a6/50815a4aeff3946c5723bcf3363980f98461fa01b8fa125c41fbe852540a/pywinpty-2.0.6.tar.gz#sha256=a91a77d23f29a58b44f62a9474a31ed67df1277cddb69665275f8d22429046ac (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpdqepxkv3 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/02/29/27d6ab0b1566db75cce1fde817cb2cf327066fc545c08ceb4e61a77a07de/pywinpty-2.0.5.tar.gz#sha256=e125d3f1804d8804952b13e33604ad2ca8b9b2cac92b27b521c005d1604794f8 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpoo97hbn5 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/b2/44/c4b593af3c5e54bfb191f5fcb7232ec66d945a1ef632bbef4befacd50047/pywinpty-2.0.4.tar.gz#sha256=a5002c74afc1ddcc85fca25de58ce9cef1a22957582981418c81aaee218675ad (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp_r1o2xnf Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/c3/61/bda90dba80bc6cb905acebd0bf0710777ab04feb29d0f438202da0e82d72/pywinpty-2.0.3.tar.gz#sha256=6b29a826e896105370c38d53904c3aaac6c36146a50448fc0ed5082cf9d092bc (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpqgnww4ie Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/43/88/f7d9b3b45a8b6109f60702cfa3b011edf7cfd8d2680f738bc4d1cb120897/pywinpty-2.0.2.tar.gz#sha256=20ec117183f79642eff555ce0dd1823f942618d65813fb6122d14b6e34b5d05a (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpt2camlqh Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/b0/85/4a563e195fd5de30e4e18c51a4c398adbcbf656ca9166182ee44e2454719/pywinpty-2.0.1.tar.gz#sha256=14e7321c6d43743af0de175fca9f111c5cc8d0a9f7c608c9e1cc69ec0d6ac146 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpc5xuqbzv Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/3d/1d/b884c586cb4ff53da97f9c9e177dd73e74a5d848e2954492341f118413fc/pywinpty-1.1.6.tar.gz#sha256=8808f07350c709119cc4464144d6e749637f98e15acc1e5d3c37db1953d2eebc (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpz6rtlnb1 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/01/c1/c6c7bf4851d78d71c1859e124626cf53d48638f3907ca80731579382a1b3/pywinpty-1.1.5.tar.gz#sha256=92125f0f8e4e64bb5f3bf270a182c9206dc1765542c59bc07441908a9db17504 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpb7dvli8n Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/1f/86/8c320359c04a85084f416bc01e265d09384655c5643953c0eaea2c5c7636/pywinpty-1.1.4.tar.gz#sha256=cc700c9d5a9fcebf677ac93a4943ca9a24db6e2f11a5f0e7e8e226184c5036f7 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpu0b03in1 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/07/e0/a26c9ac4e16aefc0b66a203f0140373245b6298443fa35978a3c680c8726/pywinpty-1.1.3.tar.gz#sha256=3a1d57b338390333812a5eed31c93c7d8ba82b131078063703e731946d90c9f2 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpuh3ujnoe Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/fb/dc/06ed1e684dfba3929a985f1845eb86f8480f0418293fc9402526e4b8bf10/pywinpty-1.1.2.tar.gz#sha256=f1718838e1c7c700e5f0b79d5d5e05243ff583313ff88e47bb94318ba303e565 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpm0qlopz5 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/40/ed/694e4a642e3b8afac6d6e70538a3684e1049f2020a33002b58e1ac5cd4b3/pywinpty-1.1.1.tar.gz#sha256=4a3ffa2444daf15c5f65a76b5b2864447cc915564e41e2876816b9e4fe849070 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmprofuzeve Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/46/02/aeb98dc7ac48e3fc6ba13159be5c6fbd0d19e2d11edf996e96d031fa86ca/pywinpty-1.1.0.tar.gz#sha256=f60ed4947fe0841e7d7cafae4885a0040d4994a4647dd21be4ed4266789885c9 (from https://pypi.org/simple/pywinpty/). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpunglzrug Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement pywinpty>=1.1.0
ERROR: No matching distribution found for pywinpty>=1.1.0


real	0m14.592s
user	0m9.529s
sys	0m1.048s
```

Pants uses the latter (the difference is adding `--target-system linux --target-system mac`) to avoid locking Windows requirements.


Discussion:
Noting that the failure case above does seem to also hinge on not having `cargo` available. The `pywinpty>=1.1.0` series (Really 1.0.0: https://github.com/andfoy/pywinpty/releases/tag/v1.0.0) moved from an old `setup.py` based build to a `maturin` / Rust PEP-517 / PEP-518 build of Python native extensions. More careful repro steps will be needed to narrow this all the way down to a stable integration test case that reliably fails before the fix, which is to add `os_name` patching to the `sys_platform` and `platform_system` patching already done surrounding `--target-system {linux,mac,windows}`.
Ok, I had in fact permanently altered my `PATH` removing the cargo entry. That confounded things for a bit. Now with that entry re-established, I can consistently flip back and forth and repro with:

Good - cargo / rustc, etc on PATH:
```
$ which cargo rustc
/home/jsirois/.cargo/bin/cargo
/home/jsirois/.cargo/bin/rustc
$ rm -rf ~/.pex && time pex --python python3.7 pex==2.1.99 -cpex3 -- lock create --style universal --resolver-version pip-2020-resolver 'pywinpty>=1.1.0; os_name == "nt"' --interpreter-constraint "CPython<4,>=3.7.5" --indent 2 -o lock-2.1.99-ts.json --target-system linux --target-system mac

real	0m15.465s
user	0m49.172s
sys	0m4.222s
```

Bad - cargo on PATH still but rustc over-ridden via an [env var cargo respects](https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-reads):
```
$ rm -rf ~/.pex && time RUSTC=/bin/false pex --python python3.7 pex==2.1.99 -cpex3 -- lock create --style universal --resolver-version pip-2020-resolver 'pywinpty>=1.1.0; os_name == "nt"' --interpreter-constraint "CPython<4,>=3.7.5" --indent 2 -o lock-2.1.99-ts.json --target-system linux --target-system mac
pid 134717 -> /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python -sE /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/pex --disable-pip-version-check --no-python-version-warning --exists-action a --no-input --isolated -q --cache-dir /home/jsirois/.pex/pip_cache --log /tmp/pex-pip-log.qy7hc84q/pip.log download --dest /tmp/tmp_n25t1n9/home.jsirois..pyenv.versions.3.7.13.bin.python3.7 pywinpty>=1.1.0; os_name == "nt" --index-url https://pypi.org/simple --retries 5 --timeout 15 exited with 1 and STDERR:
Ignoring the following environment variables in Pex venv mode:
_PEX_TARGET_SYSTEMS_FILE=/tmp/tmp444i61ak/target_systems.json

WARNING: Discarding https://files.pythonhosted.org/packages/2e/a6/50815a4aeff3946c5723bcf3363980f98461fa01b8fa125c41fbe852540a/pywinpty-2.0.6.tar.gz#sha256=a91a77d23f29a58b44f62a9474a31ed67df1277cddb69665275f8d22429046ac (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp0vj402at Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/02/29/27d6ab0b1566db75cce1fde817cb2cf327066fc545c08ceb4e61a77a07de/pywinpty-2.0.5.tar.gz#sha256=e125d3f1804d8804952b13e33604ad2ca8b9b2cac92b27b521c005d1604794f8 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpgqfkzwq_ Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/b2/44/c4b593af3c5e54bfb191f5fcb7232ec66d945a1ef632bbef4befacd50047/pywinpty-2.0.4.tar.gz#sha256=a5002c74afc1ddcc85fca25de58ce9cef1a22957582981418c81aaee218675ad (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.7). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp2lkowry4 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/c3/61/bda90dba80bc6cb905acebd0bf0710777ab04feb29d0f438202da0e82d72/pywinpty-2.0.3.tar.gz#sha256=6b29a826e896105370c38d53904c3aaac6c36146a50448fc0ed5082cf9d092bc (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmplsgb3rac Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/43/88/f7d9b3b45a8b6109f60702cfa3b011edf7cfd8d2680f738bc4d1cb120897/pywinpty-2.0.2.tar.gz#sha256=20ec117183f79642eff555ce0dd1823f942618d65813fb6122d14b6e34b5d05a (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpp63ve9ip Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/b0/85/4a563e195fd5de30e4e18c51a4c398adbcbf656ca9166182ee44e2454719/pywinpty-2.0.1.tar.gz#sha256=14e7321c6d43743af0de175fca9f111c5cc8d0a9f7c608c9e1cc69ec0d6ac146 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpy1_9yb73 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/3d/1d/b884c586cb4ff53da97f9c9e177dd73e74a5d848e2954492341f118413fc/pywinpty-1.1.6.tar.gz#sha256=8808f07350c709119cc4464144d6e749637f98e15acc1e5d3c37db1953d2eebc (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp6oi5_jpi Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/01/c1/c6c7bf4851d78d71c1859e124626cf53d48638f3907ca80731579382a1b3/pywinpty-1.1.5.tar.gz#sha256=92125f0f8e4e64bb5f3bf270a182c9206dc1765542c59bc07441908a9db17504 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmplan3qrus Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/1f/86/8c320359c04a85084f416bc01e265d09384655c5643953c0eaea2c5c7636/pywinpty-1.1.4.tar.gz#sha256=cc700c9d5a9fcebf677ac93a4943ca9a24db6e2f11a5f0e7e8e226184c5036f7 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp0p6ursr1 Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/07/e0/a26c9ac4e16aefc0b66a203f0140373245b6298443fa35978a3c680c8726/pywinpty-1.1.3.tar.gz#sha256=3a1d57b338390333812a5eed31c93c7d8ba82b131078063703e731946d90c9f2 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpk89lfood Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/fb/dc/06ed1e684dfba3929a985f1845eb86f8480f0418293fc9402526e4b8bf10/pywinpty-1.1.2.tar.gz#sha256=f1718838e1c7c700e5f0b79d5d5e05243ff583313ff88e47bb94318ba303e565 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmph4uzubgm Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/40/ed/694e4a642e3b8afac6d6e70538a3684e1049f2020a33002b58e1ac5cd4b3/pywinpty-1.1.1.tar.gz#sha256=4a3ffa2444daf15c5f65a76b5b2864447cc915564e41e2876816b9e4fe849070 (from https://pypi.org/simple/pywinpty/) (requires-python:>=3.6). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmpelndk9pe Check the logs for full command output.
WARNING: Discarding https://files.pythonhosted.org/packages/46/02/aeb98dc7ac48e3fc6ba13159be5c6fbd0d19e2d11edf996e96d031fa86ca/pywinpty-1.1.0.tar.gz#sha256=f60ed4947fe0841e7d7cafae4885a0040d4994a4647dd21be4ed4266789885c9 (from https://pypi.org/simple/pywinpty/). Command errored out with exit status 1: /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/bin/python /home/jsirois/.pex/venvs/1a2d739a57fbf583e32383354f8b55a71f546f2a/fd300d5feeace385d1195e058d7c9082241e354a/lib/python3.7/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /home/jsirois/.pex/pip_cache/.tmp/tmp2cqkag7g Check the logs for full command output.
ERROR: Could not find a version that satisfies the requirement pywinpty>=1.1.0
ERROR: No matching distribution found for pywinpty>=1.1.0


real	0m15.698s
user	0m10.123s
sys	0m1.206s
```

So, there are two conditions needed to hit this particular repro case:
1. Use the ~new `--target-system` option.
2. Don't have `cargo` installed and operating normally.

If 2 is not satisfied, cargo successfully builds a wheel:
```
$ rm -rf ~/.pex && pex --python python3.7 pex==2.1.99 -cpex -- --resolver-version pip-2020-resolver 'pywinpty>=1.1.0' -o cargo-built-it.pex
$ pex-tools cargo-built-it.pex info -i2
{
  "bootstrap_hash": "39a985b567c2cacb49b4aa15d2933b4021f8a786",
  "build_properties": {
    "pex_version": "2.1.99"
  },
  "code_hash": "267e95d985d67e861e4ce1bcabb672837b7add44",
  "distributions": {
    "pywinpty-2.0.6-cp37-cp37m-linux_x86_64.whl": "a98be1fa08fa80c5d2d3f1368d2c10173427c18122932ebbe62495ef7dba8d4b"
  },
  "emit_warnings": true,
  "ignore_errors": false,
  "includes_tools": false,
  "inherit_path": "false",
  "interpreter_constraints": [],
  "pex_hash": "fd04a290d146b5d3a03c33229a5dad26ce3cc399",
  "pex_path": null,
  "requirements": [
    "pywinpty>=1.1.0"
  ],
  "strip_pex_env": true,
  "venv": false,
  "venv_bin_path": "false",
  "venv_copies": false,
  "venv_site_packages_copies": false,
  "pex_root": "/home/jsirois/.pex"
}
$ ls -1 ~/.pex/installed_wheels/a98be1fa08fa80c5d2d3f1368d2c10173427c18122932ebbe62495ef7dba8d4b/pywinpty-2.0.6-cp37-cp37m-linux_x86_64.whl/winpty/
enums.py
__init__.py
ptyprocess.py
tests
winpty.cpython-37m-x86_64-linux-gnu.so
winpty.pyi
$ file ~/.pex/installed_wheels/a98be1fa08fa80c5d2d3f1368d2c10173427c18122932ebbe62495ef7dba8d4b/pywinpty-2.0.6-cp37-cp37m-linux_x86_64.whl/winpty/winpty.cpython-37m-x86_64-linux-gnu.so 
/home/jsirois/.pex/installed_wheels/a98be1fa08fa80c5d2d3f1368d2c10173427c18122932ebbe62495ef7dba8d4b/pywinpty-2.0.6-cp37-cp37m-linux_x86_64.whl/winpty/winpty.cpython-37m-x86_64-linux-gnu.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=c5e8837504471b12ab82f8542fa8623201f022d2, with debug_info, not stripped
$ nm ~/.pex/installed_wheels/a98be1fa08fa80c5d2d3f1368d2c10173427c18122932ebbe62495ef7dba8d4b/pywinpty-2.0.6-cp37-cp37m-linux_x86_64.whl/winpty/winpty.cpython-37m-x86_64-linux-gnu.so | grep -i rust | head -5
00000000000720d0 d DW.ref.rust_eh_personality
0000000000014ba0 t __rust_alloc
0000000000014be0 t __rust_alloc_error_handler
0000000000072150 b __rust_alloc_error_handler_should_panic
0000000000014bd0 t __rust_alloc_zeroed
```

Resolving patch:
diff --git a/pex/resolve/locker.py b/pex/resolve/locker.py
--- a/pex/resolve/locker.py
+++ b/pex/resolve/locker.py
@@ -309,6 +309,11 @@ def lock_result(self):
 
 
 # See https://www.python.org/dev/peps/pep-0508/#environment-markers for more about these values.
+_OS_NAME = {
+    TargetSystem.LINUX: "posix",
+    TargetSystem.MAC: "posix",
+    TargetSystem.WINDOWS: "nt",
+}
 _PLATFORM_SYSTEM = {
     TargetSystem.LINUX: "Linux",
     TargetSystem.MAC: "Darwin",
@@ -364,6 +369,9 @@ def patch(
             TargetSystem.values()
         ):
             target_systems = {
+                "os_names": [
+                    _OS_NAME[target_system] for target_system in lock_configuration.target_systems
+                ],
                 "platform_systems": [
                     _PLATFORM_SYSTEM[target_system]
                     for target_system in lock_configuration.target_systems
diff --git a/pex/resolve/locker_patches.py b/pex/resolve/locker_patches.py
--- a/pex/resolve/locker_patches.py
+++ b/pex/resolve/locker_patches.py
@@ -9,6 +9,7 @@
 python_versions = []
 python_majors = []
 
+os_names = []
 platform_systems = []
 sys_platforms = []
 platform_tag_regexps = []
@@ -31,6 +32,7 @@
 
     with open(target_systems_file) as fp:
         target_systems = json.load(fp)
+    os_names = target_systems["os_names"]
     platform_systems = target_systems["platform_systems"]
     sys_platforms = target_systems["sys_platforms"]
     platform_tag_regexps = target_systems["platform_tag_regexps"]
@@ -56,6 +58,7 @@ def versions_to_string(versions):
 
     python_versions_strings = versions_to_string(python_versions) or skip
     python_full_versions_strings = versions_to_string(python_full_versions) or skip
+    os_names_strings = os_names or skip
     platform_systems_strings = platform_systems or skip
     sys_platforms_strings = sys_platforms or skip
 
@@ -66,6 +69,8 @@ def _get_env(environment, name):
             return python_versions_strings
         if name == "python_full_version":
             return python_full_versions_strings
+        if name == "os_name":
+            return os_names_strings
         if name == "platform_system":
             return platform_systems_strings
         if name == "sys_platform":
diff --git a/pex/venv/pex.py b/pex/venv/pex.py
--- a/pex/venv/pex.py
+++ b/pex/venv/pex.py
@@ -418,6 +418,7 @@ def sys_executable_paths():
                     "_PEX_PATCHED_TAGS_FILE",
                     # These are used by Pex's Pip venv to implement universal locks.
                     "_PEX_PYTHON_VERSIONS_FILE",
+                    "_PEX_TARGET_SYSTEMS_FILE",
                 )
             ]
             if ignored_pex_env_vars:


Test patch:
diff --git a/tests/integration/test_issue_1856.py b/tests/integration/test_issue_1856.py
new file mode 100644
--- /dev/null
+++ b/tests/integration/test_issue_1856.py
@@ -0,0 +1,64 @@
+# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
+# Licensed under the Apache License, Version 2.0 (see LICENSE).
+
+import os
+import sys
+
+from pex.cli.testing import run_pex3
+from pex.resolve.lockfile import json_codec
+from pex.testing import PY37, ensure_python_interpreter, make_env
+from pex.typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+    from typing import Any
+
+
+def test_os_name_spoofing(tmpdir):
+    # type: (Any) -> None
+
+    lock = os.path.join(str(tmpdir), "lock.json")
+
+    create_lock_args = [
+        "lock",
+        "create",
+        "--style",
+        "universal",
+        "--resolver-version",
+        "pip-2020-resolver",
+        "pywinpty==2.0.6; os_name == 'nt'",
+        "--interpreter-constraint",
+        "CPython<4,>=3.7.5",
+        "--indent",
+        "2",
+        "-o",
+        lock,
+        "--target-system",
+        "linux",
+        "--target-system",
+        "mac",
+    ]
+
+    # This ensures that, even if the machine has a functioning cargo / rust toolchain on the PATH,
+    # it becomes non-functioning without altering the system permanently. This is needed to ensure
+    # we can't build the pywinpty sdist to extract needed resolve metadata from it.
+    #
+    # For more info on this corner to reproducing the prior failure, see:
+    #   https://github.com/pantsbuild/pex/issues/1856#issuecomment-1193054493
+    env = make_env(RUSTC=os.devnull)
+
+    # The above attempt to get pywinpty dependency metadata by building the sdist requires a
+    # CPython>=3.7.5 on the PATH which we arrange for if not already present here.
+    if sys.version_info[:3] < (3, 7, 5):
+        python_path = os.environ["PATH"].split(os.pathsep) + [ensure_python_interpreter(PY37)]
+        create_lock_args.extend(["--python-path", ":".join(python_path)])
+
+    run_pex3(*create_lock_args, env=env).assert_success()
+
+    lockfile = json_codec.load(lockfile_path=lock)
+    assert 1 == len(lockfile.locked_resolves)
+
+    locked_resolve = lockfile.locked_resolves[0]
+    assert 0 == len(locked_resolve.locked_requirements), (
+        "Since we're not running on `os_name == 'nt'`, we shouldn't have been able to lock any "
+        "requirements at all."
+    )