Repository: python/mypy

Issue:
Crash on master branch when using `--custom-typeshed-dir` with a relative path
**Crash Report**

If you try to check typeshed with stubtest using the mypy master branch, it crashes. This is a result of the changes made in #13155.

**Traceback**

```
(venv) C:\Users\alexw\coding\typeshed>python -m mypy.stubtest --check-typeshed --custom-typeshed-dir .
Traceback (most recent call last):
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1650, in <module>
    sys.exit(main())
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1646, in main
    return test_stubs(parse_options(sys.argv[1:]))
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1508, in test_stubs
    modules = build_stubs(modules, options, find_submodules=not args.check_typeshed)
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1381, in build_stubs
    res = mypy.build.build(sources=sources, options=options)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 186, in build
    result = _build(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 250, in _build
    manager = BuildManager(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 677, in __init__
    if os.path.commonpath((path, custom_typeshed_dir)) == custom_typeshed_dir:
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 772, in commonpath
    raise ValueError("Can't mix absolute and relative paths") from None
ValueError: Can't mix absolute and relative paths
```

**To Reproduce**

- `cd` into a local clone of mypy, checkout the `master` branch
- Activate a virtual environment with an editable install of mypy.
- `cd` into a local clone of typeshed.
- run `python -m mypy.stubtest --check-typeshed --custom-typeshed-dir .`

**Your Environment**

- Mypy version used: commit cbabbf7f1450eb1098d947386bcac92e90ff5909
- Python version used: 3.10
- Operating system and version: Windows

Cc. @hauntsaninja, @podshumok, @ilevkivskyi 
Crash on master branch when using `--custom-typeshed-dir` with a relative path
**Crash Report**

If you try to check typeshed with stubtest using the mypy master branch, it crashes. This is a result of the changes made in #13155.

**Traceback**

```
(venv) C:\Users\alexw\coding\typeshed>python -m mypy.stubtest --check-typeshed --custom-typeshed-dir .
Traceback (most recent call last):
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1650, in <module>
    sys.exit(main())
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1646, in main
    return test_stubs(parse_options(sys.argv[1:]))
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1508, in test_stubs
    modules = build_stubs(modules, options, find_submodules=not args.check_typeshed)
  File "c:\users\alexw\coding\mypy\mypy\stubtest.py", line 1381, in build_stubs
    res = mypy.build.build(sources=sources, options=options)
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 186, in build
    result = _build(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 250, in _build
    manager = BuildManager(
  File "c:\users\alexw\coding\mypy\mypy\build.py", line 677, in __init__
    if os.path.commonpath((path, custom_typeshed_dir)) == custom_typeshed_dir:
  File "C:\Users\alexw\AppData\Local\Programs\Python\Python310\lib\ntpath.py", line 772, in commonpath
    raise ValueError("Can't mix absolute and relative paths") from None
ValueError: Can't mix absolute and relative paths
```

**To Reproduce**

- `cd` into a local clone of mypy, checkout the `master` branch
- Activate a virtual environment with an editable install of mypy.
- `cd` into a local clone of typeshed.
- run `python -m mypy.stubtest --check-typeshed --custom-typeshed-dir .`

**Your Environment**

- Mypy version used: commit cbabbf7f1450eb1098d947386bcac92e90ff5909
- Python version used: 3.10
- Operating system and version: Windows

Cc. @hauntsaninja, @podshumok, @ilevkivskyi 


Discussion:

Resolving patch:
diff --git a/mypy/build.py b/mypy/build.py
--- a/mypy/build.py
+++ b/mypy/build.py
@@ -674,6 +674,7 @@ def __init__(
             if options.custom_typeshed_dir is not None:
                 # Check if module lives under custom_typeshed_dir subtree
                 custom_typeshed_dir = os.path.abspath(options.custom_typeshed_dir)
+                path = os.path.abspath(path)
                 if os.path.commonpath((path, custom_typeshed_dir)) == custom_typeshed_dir:
                     continue
 


Test patch:
diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test
--- a/test-data/unit/cmdline.test
+++ b/test-data/unit/cmdline.test
@@ -1465,3 +1465,16 @@ sys.path = path
 mypy: "typing.py" shadows library module "typing"
 note: A user-defined top-level module with name "typing" is not supported
 == Return code: 2
+
+[case testCustomTypeshedDirWithRelativePathDoesNotCrash]
+# cmd: mypy --custom-typeshed-dir dir dir/typing.pyi
+[file dir/stdlib/abc.pyi]
+[file dir/stdlib/builtins.pyi]
+[file dir/stdlib/sys.pyi]
+[file dir/stdlib/types.pyi]
+[file dir/stdlib/typing.pyi]
+[file dir/stdlib/typing_extensions.pyi]
+[file dir/stdlib/VERSIONS]
+[out]
+Failed to find builtin module mypy_extensions, perhaps typeshed is broken?
+== Return code: 2