Skip to content

Fix crash when the same file is processed under multiple names #8644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2736,6 +2736,11 @@ def load_graph(sources: List[BuildSource], manager: BuildManager,
graph[st.id] = st
new.append(st)
entry_points.add(bs.module)

# Note: Running this each time could be slow in the daemon. If it's a problem, we
# can do more work to maintain this incrementally.
seen_files = {st.path: st for st in graph.values() if st.path}

# Collect dependencies. We go breadth-first.
# More nodes might get added to new as we go, but that's fine.
for st in new:
Expand Down Expand Up @@ -2781,6 +2786,17 @@ def load_graph(sources: List[BuildSource], manager: BuildManager,
if dep in st.dependencies_set:
st.suppress_dependency(dep)
else:
if newst.path in seen_files:
manager.errors.report(
-1, 0,
"Source file found twice under different module names: '{}' and '{}'".
format(seen_files[newst.path].id, newst.id),
blocker=True)
manager.errors.raise_error()

if newst.path:
seen_files[newst.path] = newst

assert newst.id not in graph, newst.id
graph[newst.id] = newst
new.append(newst)
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/cmdline.test
Original file line number Diff line number Diff line change
Expand Up @@ -1067,3 +1067,18 @@ test.py:5: note: Subscripting classes that are not generic at runtime may requir
[out]
mypy: stubgen does not support .pyd files: 'a.pyd'
== Return code: 2

[case testDuplicateModules]
# cmd: mypy src
[file mypy.ini]
\[mypy]
mypy_path = src
[file src/__init__.py]
[file src/a.py]
import foo.bar
[file src/foo/__init__.py]
[file src/foo/bar.py]
1+'x'
[out]
src/foo/bar.py: error: Source file found twice under different module names: 'src.foo.bar' and 'foo.bar'
== Return code: 2