high-magician-46188
10/02/2022, 4:50 PMBUILD
files.
Hi,
I’m trying to setup Pantsbuild (2.13) for the first time in an existing Python monorepo.
At the root of the repo, I’ve added an empty BUILD_ROOT
file and a pants.toml
file with:
[GLOBAL]
backend_packages = ["pants.backend.python"]
[source]
root_patterns = ["/X_*/"] # "X" is the name of the company and "X_" is the prefix of each project
There are about 50 projects in the repo.
I’ve run pants tailor ::
and then git status | wc -l
and got 644.
Do I really need to commit 640 BUILD
files?
Is there any way around it? (maybe creating one per project?)
Thanks in advance 🙂high-magician-46188
10/02/2022, 4:51 PMhappy-kitchen-89482
10/02/2022, 6:08 PM__defaults__
(see https://www.pantsbuild.org/v2.14/docs/targets) it’s less necessary TBH.happy-kitchen-89482
10/02/2022, 6:08 PMhappy-kitchen-89482
10/02/2022, 6:10 PMsources=
for python_sources
wouldn’t suffice, and you’d need to be explicit: sources=["**/*.py", "!**/*_test.py"]
or similar.happy-kitchen-89482
10/02/2022, 6:10 PMhappy-kitchen-89482
10/02/2022, 6:11 PMhappy-kitchen-89482
10/02/2022, 6:11 PMhappy-kitchen-89482
10/02/2022, 6:12 PMhappy-kitchen-89482
10/02/2022, 6:13 PMcareful-address-89803
10/02/2022, 6:15 PMhappy-kitchen-89482
10/02/2022, 6:17 PMhappy-kitchen-89482
10/02/2022, 6:17 PMhappy-kitchen-89482
10/02/2022, 6:17 PMresources(name="configs", sources="path/to/configs/**/*.json")
happy-kitchen-89482
10/02/2022, 6:18 PMpath/to/configs
if you wanted the target to be more local to the files.happy-kitchen-89482
10/02/2022, 6:19 PMcurved-television-6568
10/02/2022, 11:36 PMyou’d create an asset or file target and add a dependency of the python_sources to that target.
python_sources(sources=["proj/**/*.py"], overrides={("proj/a/src1.py", "proj/b/src33.py"): {"dependencies":[":resource-tgt"]}})
lets you add the dependency to only a select set of source files.high-magician-46188
10/03/2022, 6:37 AMcurved-television-6568
10/03/2022, 12:21 PMcurved-television-6568
10/03/2022, 12:22 PMwitty-crayon-22786
10/03/2022, 4:58 PMhigh-magician-46188
10/03/2022, 5:03 PMBUILD
files to first understand how things go, but I’m somewhat dumbfounded with an error that repeats 100s or 1000s of times when I’m running pants lint ::
(after the initial pants tailor ::
.
The message is of the form:
12:36:15.30 [WARN] The target proj_1/test/my_dir_1/my_dir_2/test_my_file.py:tests imports `proj_1.my_dir_1.my_dir_2.my_file.MyClass`, but Pants cannot safely infer a dependency because more than one target owns this module, so it is ambiguous which to use: ['proj_2:reqs0#proj_1', 'proj_3:reqs0#proj_1', 'proj_4:reqs0#proj_1', 'proj_5:reqs0#proj_1', 'proj_6:reqs0#proj_1', 'proj_7:reqs1#proj_1', 'proj_1/proj_1/my_dir_1/my_dir_2/my_file.py'].
Please explicitly include the dependency you want in the `dependencies` field of proj_1/test/my_dir_1/my_dir_2/test_my_file.py:tests, or ignore the ones you do not want by prefixing with `!` or `!!` so that one or no targets are left.
I’m not sure why a target owns a module, or what is a target in the first place.
I’m pretty sure I have some config issues, but didn’t found them yet.witty-crayon-22786
10/03/2022, 5:07 PMwitty-crayon-22786
10/03/2022, 5:09 PMThis ambiguity is often a problem when you have 2+one option in this case would be to manually create a merged requirements.txt, and disable tailoring (i.e. “hide”) all of the other filesorrequirements.txt
files in your project, such aspyproject.toml
andproject1/requirements.txt
both specifyingproject2/requirements.txt
. You may want to set up each `poetry_requirements`/`python_requirements` target generator to use a distinct resolve so that there is no overlap. Alternatively, if the versions are the same, you may want to consolidate the requirements into a common file.django
high-magician-46188
10/03/2022, 5:12 PM*-requirements.txt
files.
I don’t understand what part of it is an issue though, since it looks like the tailor
command (surprisingly) did pick that up without an hints from me.high-magician-46188
10/03/2022, 5:15 PMtest-external-requirements.txt
for test-only dependencies (like pytest
)
2. prod-external-requirements.txt
for dependencies that are used outside of tests and are external to the mono-repo (like boto3
)
3. prod-internal-requirements.txt
for dependencies that are used outside of tests and are internal to the mono-repo (like proj_1
and proj_2
)witty-crayon-22786
10/03/2022, 5:16 PMwitty-crayon-22786
10/03/2022, 5:17 PMcommon-requirements.txt
essentially, which are used in more than one project, and then only declare those in one placewitty-crayon-22786
10/03/2022, 5:18 PMhigh-magician-46188
10/03/2022, 5:20 PMprod-internal-requirements.txt
files since they only include packages that are in the repo?witty-crayon-22786
10/03/2022, 5:28 PM…means that 6 different projects have a requirement in a requirements.txt file which is ambiguous with a 7th source which is a firstparty source fileCopy code'proj_2:reqs0#proj_1', 'proj_3:reqs0#proj_1', 'proj_4:reqs0#proj_1', 'proj_5:reqs0#proj_1', 'proj_6:reqs0#proj_1', 'proj_7:reqs1#proj_1', 'proj_1/proj_1/my_dir_1/my_dir_2/my_file.py
witty-crayon-22786
10/03/2022, 5:29 PMSo IIUC, do I need pants to ignore theso yes: you should skip tailoring for a file that declares the firstparty code as thirdparty code after that take a new look at the warnings and see what you see.files since they only include packages that are in the repo?prod-internal-requirements.txt
witty-crayon-22786
10/03/2022, 5:29 PMwitty-crayon-22786
10/03/2022, 5:30 PMhappy-kitchen-89482
10/03/2022, 5:37 PMhappy-kitchen-89482
10/03/2022, 5:38 PMhappy-kitchen-89482
10/03/2022, 5:40 PMhappy-kitchen-89482
10/03/2022, 5:40 PMhigh-magician-46188
10/03/2022, 7:06 PMprod-internal-requirements.txt
files and re-running, it takes some time.
Actually, in the current implementation, there is a global constraints.txt
to specify the conditions and each project declares its own direct dependencies in the various *-requirement.txt
files, without the constraints.
That is,
The ext-req file of proj_1
will have:
proj_2
proj_3
The internal req file will have:
boto3
requests
And the global constraints.txt will have:
boto3>1.1.1
requests<=1.2.3
happy-kitchen-89482
10/03/2022, 7:18 PMconstraints.txt
you have a global requirements.txt
with relevant version bounds as needed. Then use Pants to generate a lockfile from that, which locks down specific versions (with sha256s) of your entire transitive requirement tree. That lockfile is now the global source of truth for which versions are actually used. Then Pants automatically brings in just the subset that is needed in any situation (running a test, building a pex, building a distribution and so on).high-magician-46188
10/03/2022, 7:21 PMpip
would handle it with the -c
flag?curved-television-6568
10/03/2022, 9:18 PMhigh-magician-46188
10/05/2022, 7:18 AMpants-requirements.txt
that combines all of the *requirements*.txt
files.
2. Put all of the old *requirements*.txt
files under pants_ignore.add
.
3. Upgraded to version 2.14.rc2
.
4. Set __default__ = "constraints.txt"
(under [python.resolves_to_constraints_file]
).
I think that I’m having much less warnings.
I’m now trying to tackle the following case:
There is a company-internal package, lets call it “utils”.
To install it, we do company-utils
but to import we do from utils import whatever
.
I think that Pants either doesn’t know how to automatically handle these cases, or it is somehow related to the resolution part, which it is also having a hard time with at the moment…
Any suggestions?high-magician-46188
10/05/2022, 8:50 AMmodule_mapping = {"company-utils": ["utils"]}
under python_requirements
.
Can it be done in pants.toml
for it to be globally available?
Also, if it is indeed to solution, it could be good to add it to https://www.pantsbuild.org/v2.14/docs/troubleshooting#import-errors-and-missing-dependencies.curved-television-6568
10/05/2022, 1:49 PMcurved-television-6568
10/05/2022, 1:51 PMpython_requirements
targets, so there will not be much duplication. But in case you do, you can do this at a top-level BUILD file (or where it suits your project best, as it applies to all BUILD files in its subtree):
# BUILD
__defaults__({
python_requirements: {
"module_mapping": {"company-utils":["utils"]}
}
})
curved-television-6568
10/05/2022, 1:52 PMhigh-magician-46188
10/05/2022, 2:34 PMpants.toml
file?happy-kitchen-89482
10/05/2022, 3:10 PMhappy-kitchen-89482
10/05/2022, 3:11 PMpython_requirements
target that owns your pants-requirements.txt
, AFAICT?high-magician-46188
10/05/2022, 3:14 PMpants tailor ::
, grep python_requirements -r .
returns no result.
Should it?happy-kitchen-89482
10/05/2022, 4:33 PM*requirements*.txt
as a requirements file, yeshappy-kitchen-89482
10/05/2022, 4:33 PMwitty-crayon-22786
10/05/2022, 4:34 PMhappy-kitchen-89482
10/05/2022, 4:34 PMpython_requirements
(once we create one) is the only place you would need to add the module mapping. So there is no duplication necessary.