dazzling-elephant-33766
10/03/2023, 8:30 PM[pytest]
extra_requirements.add = ["ipdb"]
Is scheduled to be dropped in 2.18
What’s the recommended way for using ipdb
in tests moving forward?
I’ve got the following in my pants.toml
[test]
extra_env_vars = ["PYTHONBREAKPOINT=ipdb.set_trace"]
And this in my BUILD
python_tests(
name="tests0",
dependencies=["//:reqs0#ipdb"]
)
In my dummy example I can see ipdb
is a transitive dependency, so it’s included in the pex build
➜ pants-test pants dependencies --transitive test_app.py
21:28:55.35 [INFO] Initializing scheduler...
21:28:58.44 [INFO] Scheduler initialized.
//:reqs0#Flask
//:reqs0#ipdb
//:reqs0#pytest
//main.py:root
//requirements.txt:reqs0
But when I set a breakpoint, it just gives me plain old pdb
even though the PYTHONBREAKPOINT
environment is present and ipdb
is available
pants test --debug :: -- -s
<OUTPUT REDACTED>
(Pdb) os.environ.get("PYTHONBREAKPOINT")
'ipdb.set_trace'
What am I missingenough-analyst-54434
10/03/2023, 8:44 PMCustom tool versions are now installed from named resolves, as described at [Lockfiles](doc:python-lockfiles).
should point to https://www.pantsbuild.org/docs/python-lockfiles#lockfiles-for-toolsdazzling-elephant-33766
10/03/2023, 8:53 PMipdb
should be working.
The absence of explicitly doing something like:
[python.resolves]
pytest = "3rdparty/python/pytest.lock"
[pytest]
install_from_resolve = "pytest" # Use this resolve's lockfiles.
requirements =["//3rdparty/python:pytest"] # Use these requirements from the lockfile.
Means it should use my default lockfile right? (where ipdb is present)enough-analyst-54434
10/03/2023, 8:55 PMenough-analyst-54434
10/03/2023, 8:55 PMenough-analyst-54434
10/03/2023, 8:57 PM//3rdparty/python:pytest
looks like a bad target address - surely missing a #.
+ You're missing a requirement in that list on ipdb.enough-analyst-54434
10/03/2023, 9:00 PMdazzling-elephant-33766
10/03/2023, 9:04 PMpants.toml
[pytest]
install_from_resolve = "python-default"
requirements = [
"//:reqs0#pytest",
"//:reqs0#ipdb",
]
Which is present in my BUILD
python_requirements(
name="reqs0",
)
Setting a breakpoint in my test still gives me the same pdb debugger, but ipdb is importable:
(Pdb) import ipdb
(Pdb) import os
(Pdb) os.environ.get("PYTHONBREAKPOINT")
'ipdb.set_trace'
(Pdb)
This is after running
pants generate-lockfiles ::
enough-analyst-54434
10/03/2023, 9:08 PMsys.version_info
say?dazzling-elephant-33766
10/03/2023, 9:09 PM[python]
interpreter_constraints = ['==3.11.*']
(Pdb) import sys
(Pdb) sys.version_info
sys.version_info(major=3, minor=11, micro=4, releaselevel='final', serial=0)
enough-analyst-54434
10/03/2023, 9:10 PMenough-analyst-54434
10/03/2023, 9:12 PMenough-analyst-54434
10/03/2023, 9:13 PM-E
.enough-analyst-54434
10/03/2023, 9:13 PMdazzling-elephant-33766
10/03/2023, 9:13 PMpants export ::
and running pytest manually inside the virtualenv
PYTHONBREAKPOINT=ipdb.set_trace pytest -s test_app.py
And this doesn’t work either, so I’m stumped.
I don’t think this is a pants issue.enough-analyst-54434
10/03/2023, 9:14 PM-E
?
I bet the pytest
script uses -E
- check the shebang.enough-analyst-54434
10/03/2023, 9:16 PMpex_binary
targets you can control this aspect of the shebang via: https://www.pantsbuild.org/docs/reference-pex_binary#codevenv_hermetic_scriptscode but I don't think this has ever been plumbed for Pants internal venvs, like the one it uses to run pytest
. @dazzling-elephant-33766 you're claiming this used to work for you in Pants version X? What is X?dazzling-elephant-33766
10/03/2023, 9:18 PM[pytest]
extra_requirements.add = ["ipdb"]
worked in 2.16 but I’d have to double check.
I’m currently on 2.17enough-analyst-54434
10/03/2023, 9:19 PMhead -1 $(which pytest)
in the exported + activated venv, it would be nice to confirm -E
in the shebang.dazzling-elephant-33766
10/03/2023, 9:24 PM(3.11.4) ➜ pants-test git:(main) ✗ head -1 $(which pytest)
#!/Users/jack/code/pants-test/dist/export/python/virtualenvs/python-default/3.11.4/bin/python3.11 -sE
Yeaaah …
You nailed itenough-analyst-54434
10/03/2023, 9:26 PMenough-analyst-54434
10/03/2023, 9:28 PMPEX_SCRIPT=pytest python the.internal.pex ...
maybe? The internal way pytest is launched may or may not have changed. I've not been hacking on that stuff for a few years.enough-analyst-54434
10/03/2023, 9:30 PMpytest
venv console script).enough-analyst-54434
10/03/2023, 9:30 PMenough-analyst-54434
10/03/2023, 9:35 PMdazzling-elephant-33766
10/03/2023, 9:43 PMentry_point
of pytest to from your suggestion https://www.pantsbuild.org/docs/reference-pytest#entry_point
iPart of me thinks I’ve misremembered this working, but then the docs have lots of references to usingenough-analyst-54434
10/03/2023, 9:45 PMpytest
console script here?enough-analyst-54434
10/03/2023, 9:46 PMBut the docs do have a lot of references to using ipdb with pytest, so makes me think this has worked at some point after 2.4.0, I’m unsure.That could definitely be true. It may be that the way the pytest PEX is executed behind the scenes has changed. I've not been involved recently, but I know enough to know there has been some shuffling in that general area.
enough-analyst-54434
10/03/2023, 9:48 PMpytest
set up using nox in a porject and I find:
$ cat .nox/test-3-12/bin/pytest
#!/home/jsirois/dev/a-scie/lift/.nox/test-3-12/bin/python
# -*- coding: utf-8 -*-
import re
import sys
from pytest import console_main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(console_main())
So the entrypoint is pytest:console_main
for that pytest
(version 7.4.2).enough-analyst-54434
10/03/2023, 9:48 PM$ cat .nox/test-3-12/lib/python3.12/site-packages/pytest-7.4.2.dist-info/entry_points.txt
[console_scripts]
py.test = pytest:console_main
pytest = pytest:console_main
dazzling-elephant-33766
10/03/2023, 10:40 PM#!/Users/jack/code/pants-test/dist/export/python/virtualenvs/python-default/3.11.4/bin/python3.11 -sE
# -*- coding: utf-8 -*-
import re
import sys
from pytest import console_main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(console_main())
But altering
[pytest]
install_from_resolve = "python-default"
entry_point="pytest:console_main"
requirements = [
"//:reqs0#pytest",
"//:reqs0#ipdb",
]
doesn’t appear to change the behaviourenough-analyst-54434
10/03/2023, 10:51 PMhappy-kitchen-89482
10/04/2023, 3:58 AM[pytest]
install_from_resolve = "python-default"
But also note that if you do this naively it means that your tests will depend on the entire lockfile, and be invalidated more often than you'd like. So you may also want to set requirements =[...]
to just the actual needed requirement targets (pytest, ipdb and anything pytest plugins, for example)happy-kitchen-89482
10/04/2023, 4:01 AM-E
problem, filing an issue would be great, thanks.dazzling-elephant-33766
10/04/2023, 12:02 PM