Skip to content

Commit

Permalink
Properly launch modules in current directory even if it's not added t…
Browse files Browse the repository at this point in the history
…o the PYTHONPATH. Fixes microsoft#1010 (microsoft#1069)
  • Loading branch information
fabioz authored and karthiknadig committed Dec 10, 2018
1 parent 8ea0421 commit 4950b1f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/ptvsd/_vendored/pydevd/pydevd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <module>`, 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)
Expand Down
43 changes: 41 additions & 2 deletions src/ptvsd/_vendored/pydevd/tests_python/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')

0 comments on commit 4950b1f

Please sign in to comment.