<#19618 Relative imports not mapping to correct so...
# github-notifications
c
#19618 Relative imports not mapping to correct sources when multiple files have identical contents Issue created by njgrisafi Describe the bug Some background can be found on slack https://pantsbuild.slack.com/archives/C046T6T9U/p1692230510929189 When a renaming/moving files with relative imports, pants dependency inference fails on the new file with
UnownedDependencyError
. The error only occurs on relative imports and what's interesting is pants says the source for the relative import is the old file that was moved. Pants version 2.17.0rc1 OS Both MacOS and Linux Additional info This issue is a bit difficult to reproduce. There's some specific order of operations that causes pants to get into this state. I pushed this a branch to my example repo https://github.com/njgrisafi/pants-example/tree/example-dep-inf-issue to hopefully help with narrowing down reproducing this. Simple reproduction 1. Clone repo
git clone git@github.com:njgrisafi/pants-example.git
2. Checkout the first branch
git checkout example-dep-inf-issue
3. Run dependency inference
./pants peek :: >> /dev/null
4. Checkout the second branch
git checkout example-dep-inf-issue-2
5. Run dependency inference
./pants peek :: >> /dev/null
You should see the following error
Copy code
UnownedDependencyError: Pants cannot infer owners for the following imports in the target app/module_2/break_pants/__init__.py:

  * module_1.break_pants._example.x (line: 1)
To fix the issue you can nuke all the pants artifacts
Copy code
rm -rf ~/.cache/pants
rm -rf .pids
rm -rf .pants.d
Rerunning dependency inference will now succeed as expected. Detailed reproduction Detailed steps to repro this behavior (first checkout repo / branch
example-dep-inf-issue
): 1. Run dependency inference
./pants peek :: >> /dev/null
2. Add new module
app/module_2/break_pants
• Create file
app/module_2/break_pants/__init__.py
(with content
from ._example import x
) • Move file
app/module_1/break_pants/_example.py
->
app/module_2/break_pants/_example.py
3. Update the old code •
app/module_1/break_pants/__init__.py
(with content
from module_2.break_pants import *
) 4. Run dependency inference
./pants peek :: >> /dev/null
You should see an error like
Copy code
UnownedDependencyError: Pants cannot infer owners for the following imports in the target app/module_2/break_pants/__init__.py:

  * module_1.break_pants._example.x (line: 1)
The expected behavior is pants would map the relative import to
module_2.break_pants._example.x
. pantsbuild/pants