PLS doesn't flag error in a relative import if it is found in another root directory #509
Description
The analyzer incorrectly accepts relative imports in cases where the path is relative to a root but not to the importing file.
Consider the following case:
Directory structure:
src
|->module
|--->init.py
test.py
PYTHONPATH=src
test.py contains the following code:
from .module import Foo
This import should be flagged as invalid because the path ".module" should be relative to the test.py file. However, the code in PathResolverSnapshot.GetImportsFromRelativePath
considers all of the root search directories when it performs its search. It therefore finds an import match when it shouldn't.
I think the problem is in PathResolverSnapshot.RootContains
. It doesn't do anything to validate that the specified lastEdge starts in the same location as the root. If the relative path happens to match items within that root, the method returns true. If the relative path is zero length, all roots return true.
A proposed fix is to add the following piece of code to PathResolverSnapshot.RootContains
before the while loop:
if (root != sourceEdge.Next.Start) {
rootEdge = default;
return false;
}