From db69bf88cdc98964dd1bb75b8a4a6817c8b596fa Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 16 Jul 2022 18:48:43 -0700 Subject: [PATCH] Handle cwd correctly in pyinfo This fixes a recent regression introduced by the change to use sys.path Fixes #12956 --- mypy/pyinfo.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/mypy/pyinfo.py b/mypy/pyinfo.py index c129063a01a47..b4131da41cc9a 100644 --- a/mypy/pyinfo.py +++ b/mypy/pyinfo.py @@ -10,9 +10,6 @@ import sys import sysconfig -if __name__ == '__main__': - sys.path = sys.path[1:] # we don't want to pick up mypy.types - MYPY = False if MYPY: from typing import List @@ -29,10 +26,16 @@ def getsearchdirs(): ) stdlib = sysconfig.get_path("stdlib") stdlib_ext = os.path.join(stdlib, "lib-dynload") - cwd = os.path.abspath(os.getcwd()) - excludes = set([cwd, stdlib_zip, stdlib, stdlib_ext]) - - abs_sys_path = (os.path.abspath(p) for p in sys.path) + excludes = set([stdlib_zip, stdlib, stdlib_ext]) + + # Drop the first entry of sys.path + # - If pyinfo.py is executed as a script, this is the directory containing pyinfo.py + # - Otherwise, if mypy launched via console script, this is the directory of the script + # - Otherwise, if mypy launched via python -m mypy, this is the current directory + # In all cases, this is safe to drop + # Note that mypy adds the cwd to SearchPaths.python_path, so we still find things on the + # cwd consistently (pyinfo.py sets SearchPaths.package_path) + abs_sys_path = (os.path.abspath(p) for p in sys.path[1:]) return [p for p in abs_sys_path if p not in excludes]