hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Feature Request: Traceback Show Locals

Open juftin opened this issue 2 years ago • 10 comments

As a hatch plugin developer I'll occasionally come across an issue reported from a user that contains hatch traceback - the traceback provided by rich has been super helpful but there are situations where I'd like additional output. The show_locals option is very verbose but there are some situations where I'd like them to be included. The feature I'm suggesting is being able to toggle show_locals using the --verbose option (or maybe a new --debug flag?).

Rich API Docs: https://rich.readthedocs.io/en/stable/traceback.html?highlight=print_exception#printing-tracebacks

The show_locals=True parameter causes Rich to display the value of local variables for each frame of the traceback.

Hatch Usage: https://github.com/pypa/hatch/blob/6de802b882578e342bd5d397b8050b4b18574bc2/src/hatch/cli/init.py#L226

If you're interested I'd be happy to take a crack at this.

juftin avatar Dec 27 '23 04:12 juftin

I would be interested but just FYI it would be a bit tricky because the code that you linked wraps the actual CLI argument parsing. This is also why I have not yet come up with a good way to extend CLI commands via plugins.

ofek avatar Dec 27 '23 04:12 ofek

I would be interested but just FYI it would be a bit tricky because the code that you linked wraps the actual CLI argument parsing. This is also why I have not yet come up with a good way to extend CLI commands via plugins.

🤔 what if instead of the try/except -> print_exception we made use of the traceback handler from rich? https://github.com/pypa/hatch/compare/master...juftin:hatch:feat/rich_tracebacks

Side note, how do I get the formatting to work just how you prefer it? Running ruff format . gets close but there are still some diffs

juftin avatar Dec 27 '23 04:12 juftin

That should work precisely if you are using the same version as the default https://github.com/pypa/hatch/blob/master/src/hatch/env/internal/static_analysis.py

ofek avatar Dec 27 '23 05:12 ofek

How does the change affect responsiveness? https://github.com/pypa/hatch/blob/6de802b882578e342bd5d397b8050b4b18574bc2/.github/workflows/test.yml#L169

ofek avatar Dec 27 '23 05:12 ofek

It takes the response time from ~ 98ms to ~107ms (looking at the latest run on pypa/hatch/master https://github.com/juftin/hatch/actions/runs/7335489661/job/19973471718?pr=1

juftin avatar Dec 27 '23 05:12 juftin

Not super happy about that but it's the only way so that's fine

ofek avatar Dec 27 '23 05:12 ofek

Oh something else to watch out for would be the publishing stuff e.g. credentials might be saved as a variable

ofek avatar Dec 27 '23 05:12 ofek

Not super happy about that but it's the only way so that's fine

I'd be just as happy to keep everything the same and enable show_locals with an environment variable like HATCH_DEBUG=1 too. Or even something like if "--verbose" in sys.argv...

juftin avatar Dec 27 '23 06:12 juftin

An environment variable sounds nice if that's okay with you!

ofek avatar Dec 29 '23 18:12 ofek

An environment variable sounds nice if that's okay with you!

Yep, absolutely. Here's what I was thinking https://github.com/pypa/hatch/compare/master...juftin:hatch:feat/rich_tracebacks

juftin avatar Dec 31 '23 05:12 juftin