diff --git a/src/ptvsd/_vendored/pydevd/pydevd.py b/src/ptvsd/_vendored/pydevd/pydevd.py index 49560ddb3..6ad22c26f 100644 --- a/src/ptvsd/_vendored/pydevd/pydevd.py +++ b/src/ptvsd/_vendored/pydevd/pydevd.py @@ -1203,6 +1203,13 @@ def run(self, file, globals=None, locals=None, is_module=False, set_trace=True): module_name = None entry_point_fn = '' if is_module: + # When launching with `python -m `, python automatically adds + # an empty path to the PYTHONPATH which resolves files in the current + # directory, so, depending how pydevd itself is launched, we may need + # to manually add such an entry to properly resolve modules in the + # current directory (see: https://github.com/Microsoft/ptvsd/issues/1010). + if '' not in sys.path: + sys.path.insert(0, '') file, _, entry_point_fn = file.partition(':') module_name = file filename = get_fullname(file) diff --git a/src/ptvsd/_vendored/pydevd/tests_python/test_run.py b/src/ptvsd/_vendored/pydevd/tests_python/test_run.py index 3f48b55f9..1f646b5ee 100644 --- a/src/ptvsd/_vendored/pydevd/tests_python/test_run.py +++ b/src/ptvsd/_vendored/pydevd/tests_python/test_run.py @@ -2,10 +2,11 @@ str('_pytest.pytester'), ] -def _run_and_check(testdir, path): + +def _run_and_check(testdir, path, check_for='Worked'): result = testdir.runpython(path) result.stdout.fnmatch_lines([ - 'Worked' + check_for ]) def test_run(testdir): @@ -63,3 +64,41 @@ def test_run(testdir): py_db.run(%(foo_module)r, is_module=True, set_trace=False) ''' % locals())) + +def test_run_on_local_module_without_adding_to_pythonpath(testdir): + import sys + import os + + pydevd_dir = os.path.dirname(os.path.dirname(__file__)) + assert os.path.exists(os.path.join(pydevd_dir, 'pydevd.py')) + + foo_module = 'local_foo' + with open(os.path.join(os.getcwd(), 'local_foo.py'), 'w') as stream: + stream.write('print("WorkedLocalFoo")') + + _run_and_check(testdir, testdir.makepyfile(''' +import sys +import os +sys.path.append(%(pydevd_dir)r) +sys.argv.append('--as-module') +cwd = os.path.abspath(os.getcwd()) +while cwd in sys.path: + sys.path.remove(cwd) +import pydevd +py_db = pydevd.PyDB() +py_db.ready_to_run = True +py_db.run(%(foo_module)r, is_module=True) +''' % locals()), check_for='WorkedLocalFoo') + + _run_and_check(testdir, testdir.makepyfile(''' +import sys +import os +sys.argv.append('--as-module') +sys.path.append(%(pydevd_dir)r) +cwd = os.path.abspath(os.getcwd()) +while cwd in sys.path: + sys.path.remove(cwd) +import pydevd +py_db = pydevd.PyDB() +py_db.run(%(foo_module)r, is_module=True, set_trace=False) +''' % locals()), check_for='WorkedLocalFoo')