microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

"Run Test with Coverage" should catch `NoSource` exceptions #24308

Closed bersbersbers closed 1 week ago

bersbersbers commented 1 month ago

The root cause of my observations in https://github.com/microsoft/vscode-python/issues/24307 (which still deserves its own solution IMHO) is https://github.com/nedbat/coveragepy/issues/1392. Unfortunately, configuring omit alone does not help because this code snipped does not consider it:

https://github.com/microsoft/vscode-python/blob/e8dd8c057b18e7a47a389c3144e9bce948862a84/python_files/vscode_pytest/__init__.py#L432-L448

cov._check_include_omit_etc being private, the extension cannot really know what to check. So I propose replacing

https://github.com/microsoft/vscode-python/blob/e8dd8c057b18e7a47a389c3144e9bce948862a84/python_files/vscode_pytest/__init__.py#L440

by

try:
    analysis = cov.analysis2(file)
except NoSource:
    continue
bersbersbers commented 1 month ago

There may be better solutions still. E.g., Coverage() takes an omit parameter that may help:

https://github.com/microsoft/vscode-python/blob/e8dd8c057b18e7a47a389c3144e9bce948862a84/python_files/vscode_pytest/__init__.py#L435

Compare also https://github.com/microsoft/vscode-python/issues/24309

eleanorjboyd commented 3 weeks ago

How are you listing omitted files? Omitting seems to be something I need to explore in general as I have gotten a few comments related to this. How does your issue compare to https://github.com/microsoft/vscode-python/issues/24366? It seems your ask is less about the UI including or not including and more about if the run works due to files you need to skip?

bersbersbers commented 3 weeks ago

At its core, this issue is not about omitting files. Being able to omit files (either during run [which is currently possible via the coverage configuration] or report [which is not currently possible], compare https://github.com/microsoft/vscode-python/issues/24309#issuecomment-2449358989) would be one workaround for this issue, but it's a different problem altogether.

In this issue, I care about error handling during the reporting phase, in case a file that existed during run is missing during report. This happens when a Python library unpacks code at runtime, only to delete these files when done. See https://github.com/nedbat/coveragepy/issues/1392#issuecomment-1149310741 for one example. These files will then be missing during the reporting phase, leading to errors in the VS Code extension.

This can happen for other reasons, too - e.g., when processing coverage results after uninstalling a Python library. So I would argue that the extension should catch NoSource extensions under all circumstances.

bersbersbers commented 3 weeks ago

It seems your ask is less about the UI including or not including and more about if the run works due to files you need to skip?

Yes, correct.

eleanorjboyd commented 3 weeks ago

Thank you for the clarification! I will look into this now I understand the whole premise