diff --git a/mypy/fscache.py b/mypy/fscache.py index 15679ad03e85..c4a1a016df67 100644 --- a/mypy/fscache.py +++ b/mypy/fscache.py @@ -230,7 +230,8 @@ def exists_case(self, path: str, prefix: str) -> bool: if path in self.exists_case_cache: return self.exists_case_cache[path] head, tail = os.path.split(path) - if not head.startswith(prefix) or not tail: + prefix = prefix.rstrip(os.sep) + if (prefix != "" and not (head.startswith(prefix + os.sep) or head == prefix)) or not tail: # Only perform the check for paths under prefix. self.exists_case_cache[path] = True return True diff --git a/mypy/test/testfscache.py b/mypy/test/testfscache.py index 44b0d32f5797..66e9d70d2501 100644 --- a/mypy/test/testfscache.py +++ b/mypy/test/testfscache.py @@ -88,6 +88,20 @@ def test_isfile_case_other_directory(self) -> None: # this path is not under the prefix, case difference is fine. assert self.isfile_case(os.path.join(other, "PKG/other_dir.py")) + def test_exists_case_1(self) -> None: + self.make_file(os.path.join("foo", "bar", "baz.py")) + # Run twice to test both cached and non-cached code paths. + for i in range(2): + assert self.exists_case(os.path.join("foo", "bar", "baz.py"), "foo") + assert self.exists_case(os.path.join("foo", "bar"), "foo") + assert self.exists_case(os.path.join("foo", "bar", "non_existent1.py"), "bar") + assert self.exists_case(os.path.join("foobar", "non_existent2.py"), "foo") + assert not self.exists_case(os.path.join("foo", "bar", "non_existent3.py"), "foo") + assert not self.exists_case(os.path.join("foo", "bar", "not_a_dir"), "foo") + assert not self.exists_case( + os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep + ) + def make_file(self, path: str, base: str | None = None) -> None: if base is None: base = self.tempdir @@ -99,3 +113,8 @@ def make_file(self, path: str, base: str | None = None) -> None: def isfile_case(self, path: str) -> bool: return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir) + + def exists_case(self, path: str, prefix: str) -> bool: + return self.fscache.exists_case( + os.path.join(self.tempdir, path), os.path.join(self.tempdir, prefix) + )