rapid-exabyte-76685
03/29/2022, 9:22 AM[WARN] The target src/xyz.py imports `Abc`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['src/abc/__init__.py', 'test/abc/__init__.py'].
test/abc/__init__.py is blank. The source for Abc is in src/abc/__init__.py . src/xyz.py contains from abc import Abc
My repo has src and test roots that largely mirror each other in their hierarchy. Is this a bad pattern? Is there a way to default the dependency inference to consider the src root over the test root - unless there is an explicit override?
Also is there a way to retest the whole codebase for this class of error as it appears to appear for one invocation of ./pants dependencies src/xyz.py but then disappears (and I think I have other similar issues that I would like to resolve)happy-kitchen-89482
03/29/2022, 1:40 PMtest be a source root. Source roots are for resolving imports to source files, and it sounds like you never want to import from any files under test? How are your source roots configured right now?rapid-exabyte-76685
03/29/2022, 1:48 PM[source]
root_patterns = [
'/src/python',
'/test/python',
]
from https://www.pantsbuild.org/docs/source-roots#configuring-source-roots-using-patterns
or in my case
root_patterns = ["src", "test"]rapid-exabyte-76685
03/29/2022, 1:49 PMsrc would never import from test but files under test would import from srcrapid-exabyte-76685
03/29/2022, 1:52 PMtest/app/test_app.py can from app.something import TheThing from src/app/something.py containing TheThingrapid-exabyte-76685
03/29/2022, 1:54 PMtest ) via dependencies on python_test to python_test_utils targetshappy-kitchen-89482
03/29/2022, 1:54 PMroot_patterns = ["src", "/"] so that the test code's source root is the repo root, and if you did want to import from it you'd import <http://test.app|test.app>happy-kitchen-89482
03/29/2022, 1:54 PMhappy-kitchen-89482
03/29/2022, 1:55 PMtest?rapid-exabyte-76685
03/29/2022, 1:57 PMrapid-exabyte-76685
03/29/2022, 1:57 PMpython_test_utils have been definedhappy-kitchen-89482
03/29/2022, 1:57 PMhappy-kitchen-89482
03/29/2022, 1:57 PMhappy-kitchen-89482
03/29/2022, 1:58 PMsrc/hundreds-father-404
03/29/2022, 1:59 PMrapid-exabyte-76685
03/29/2022, 2:04 PMconftest.py files, but also have other files like util.py or application_test_harness.py . So to get to the point where the tests are passing I've defined quite a few python_test_utils targets in BUILD files across the test side of the source. And the python_tests targets generally need to be explicitly told to depend on these python_test_utils ... but that might be clearer once I understand the infer side of thingshundreds-father-404
03/29/2022, 2:05 PMhundreds-father-404
03/29/2022, 2:07 PM/ rather than test as a source root, then you'd update imports to be <http://test.my|test.my>_project.foo rather than my_project.foorapid-exabyte-76685
03/29/2022, 2:07 PMBackend:Is that correct, more a general python thing than awslamba?pants.backend.awslambda.python
happy-kitchen-89482
03/29/2022, 2:17 PMBackend: pants.backend.awslambda.pythonOooh, you've found a bug in the code that generates this documentation...
happy-kitchen-89482
03/29/2022, 2:17 PMhappy-kitchen-89482
03/29/2022, 2:18 PMhappy-kitchen-89482
03/29/2022, 2:21 PMimport abc.test_util.TestUtilThing to fail because presumably your PYTHONPATH has src/ before test/, so Python will look for abc in src first, find it there and then fail to find test_util under it. (If your PYTHONPATH has test/ before src/ then your tests should fail to import anything from src/).happy-kitchen-89482
03/29/2022, 2:21 PMhappy-kitchen-89482
03/29/2022, 2:23 PMrapid-exabyte-76685
03/29/2022, 11:06 PM/app
cli.py # this being the module that would be run from the command line with an argument to indicate the mode and
# which class to invoke in /app/modes/mode1, /app/modes/mode2, etc.
/modes
/mode1 ...code for mode1, imports from shared1, shared2 as appropriate
/mode2
... # more modes to come.
/shared1
/shared2
/test
/app
/modes
/mode1 - tests for /app/modes/mode1
...etc...
To spike 1. my implementation of Pants AND 2. refactoring to a monorepo (only worked on 1 so far) I have copied everything from /app under /src - while leaving /app in place so that the 'old way' still works (although CI for this branch is bit of a mess at the moment TBH 😅)
/app
...contents remains untouched...
/src
/app #copied from ./app
cli.py
/modes
/mode1
/mode2
/shared1
/shared2
/test
/app
/modes
/mode1
...contents remains untouched, apart from addition of BUILD files...
And the old /app is being ignored, so the relevant parts of my pants.toml...
[GLOBAL]
pants_ignore.add = ["app/**"]
[source]
root_patterns = ["src", "test"]rapid-exabyte-76685
03/29/2022, 11:09 PM/src
/app
/modes
/mode1
...
/test
/app
/modes
/mode1
...hundreds-father-404
03/29/2022, 11:11 PMsrc/app and copying the files—which risks things falling out of sync—could you set source roots to / and /test? That will cause your production imports to be app.modes.mode1.foo
looks like you'll still have test imports as app.modes.mode1 too though because the test/ will get stripped out, so same ambiguity you asked about in the beginningrapid-exabyte-76685
03/29/2022, 11:15 PM/ ? Not sure I am getting any benefit from multiple source roots?rapid-exabyte-76685
03/29/2022, 11:16 PMhundreds-father-404
03/29/2022, 11:19 PM/ and /test is that it reflects how your repo actually is, if I'm understanding you correctly. It sounds lik before Pants, you import the file app/modes/mode1/foo.py as app.modes.mode1.foo. You also import test/app/modes/mode1/test_helpers.py as app.modes.mode1.test_helpers
If you get rid of the source root /test and always use /, then your test code will need to be updated to import test/app/modes/mode1/test_helpers.py as test.app.modes.mode1.teset_helpers. That's probably a good change for your repo to make - it removes Pants's warning about ambiguous imports, and it also makes things simpler. But you will need to update imports, which you might not want to do yethundreds-father-404
03/29/2022, 11:19 PMrapid-exabyte-76685
03/29/2022, 11:30 PMmain branch state....
Imports within the production code under /app are all app.modes.mode1.foo to import /app/modes/mode1/foo.py
But imports within the test code under /test are ...
to import /app/modes/mode1/foo.py we import app.modes.mode1.foo (so same as prod code)
to import /test/app/modes/mode1/test_helpers.py we import test.app.modes.mode1.test_helpers
there is also a util /test/util.pyrapid-exabyte-76685
03/29/2022, 11:32 PMhundreds-father-404
03/29/2022, 11:32 PM/ as your one and only source root 🙂happy-kitchen-89482
03/29/2022, 11:47 PM/happy-kitchen-89482
03/29/2022, 11:48 PMso I think we are already in the 'good change' state in a pre-Pants world, I've just confused things by introducing separate source roots when trying to implement Pants?I think this may be right. Just using
/ and keeping your sources the way they were should work finehappy-kitchen-89482
03/29/2022, 11:48 PM