diff --git a/ptvsd.code-workspace b/ptvsd.code-workspace index 84289e7df..1b616d80d 100644 --- a/ptvsd.code-workspace +++ b/ptvsd.code-workspace @@ -8,5 +8,11 @@ "python.linting.enabled": true, "python.linting.pylintEnabled": false, "python.envFile": "${workspaceFolder}/.env", - } + "files.exclude": { + "**/*.pyc": true, + "**/__pycache__": true, + ".tox": true, + "src/ptvsd.egg-info": true, + } + }, } diff --git a/src/ptvsd/__main__.py b/src/ptvsd/__main__.py index ef5984cc3..6069fa171 100644 --- a/src/ptvsd/__main__.py +++ b/src/ptvsd/__main__.py @@ -213,6 +213,10 @@ def setup_connection(): elif opts.target_kind == 'file': sys.argv[0] = opts.target elif opts.target_kind == 'module': + # Add current directory to path, like Python itself does for -m. This must + # be in place before trying to use find_spec below to resolve submodules. + sys.path.insert(0, '') + # We want to do the same thing that run_module() would do here, without # actually invoking it. On Python 3, it's exposed as a public API, but # on Python 2, we have to invoke a private function in runpy for this. @@ -280,9 +284,6 @@ def run_module(): ptvsd.log.info('Running module {0}', target) - # Add current directory to path, like Python itself does for -m. - sys.path.insert(0, '') - # Docs say that runpy.run_module is equivalent to -m, but it's not actually # the case for packages - -m sets __name__ to '__main__', but run_module sets # it to `pkg.__main__`. This breaks everything that uses the standard pattern diff --git a/tests/func/test_run.py b/tests/func/test_run.py index de1923d05..f3c25e5e7 100644 --- a/tests/func/test_run.py +++ b/tests/func/test_run.py @@ -11,6 +11,7 @@ import ptvsd from tests.helpers import print +from tests.helpers.pathutils import get_test_root from tests.helpers.pattern import ANY, Regex from tests.helpers.session import DebugSession from tests.helpers.timeline import Event @@ -49,6 +50,20 @@ def code_to_debug(): session.wait_for_exit() +def test_run_submodule(): + cwd = get_test_root('testpkgs') + with DebugSession() as session: + session.initialize( + target=('module', 'pkg1.sub'), + start_method='launch', + ignore_unobserved=[Event('continued')], + cwd=cwd, + ) + session.start_debugging() + session.wait_for_next(Event('output', ANY.dict_with({'category': 'stdout', 'output': 'three'}))) + session.wait_for_exit() + + @pytest.mark.parametrize('run_as', ['file', 'module', 'code']) def test_nodebug(pyfile, run_as): @pyfile diff --git a/tests/func/testfiles/testpkgs/pkg1/sub/__init__.py b/tests/func/testfiles/testpkgs/pkg1/sub/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/func/testfiles/testpkgs/pkg1/sub/__main__.py b/tests/func/testfiles/testpkgs/pkg1/sub/__main__.py new file mode 100644 index 000000000..35133dfb6 --- /dev/null +++ b/tests/func/testfiles/testpkgs/pkg1/sub/__main__.py @@ -0,0 +1,3 @@ +print('one') +print('two') +print('three')