Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The "src" source root doesn't apply to tests that exist in a different directory when there is a single BUILD file for each project. #15084

Open
AlirezaRoshanzamir opened this issue Apr 10, 2022 · 4 comments
Assignees
Labels

Comments

@AlirezaRoshanzamir
Copy link

AlirezaRoshanzamir commented Apr 10, 2022

Bug Description:

Using the following famous Python project structure [1,2,3,4], the "src" directory is not appended to the "tests" Python path automatically. Actually, the reason for selecting this structure is forcing the developers to test the installed library (editable in development time, non-editable before pushing to the repository) which prevents some packaging (such as invalid patterns in MANIFEST.in) problems.

.
├── BUILD
├── pants
├── pants.toml
├── src
│   └── foolib
│       ├── __init__.py
│       └── main.py
└── tests
    └── test_main.py
[GLOBAL]
pants_version = '2.10.0'
backend_packages = [
    'pants.backend.python'
]
python_sources(name='src', sources=['src/**/*.py'])
python_tests(name='tests', sources=['tests/**/test_*.py'])

Running ./pants test :tests results:

ModuleNotFoundError: No module named 'foolib'

and the sys.path value for the running tests is:

['/tmp/process-executionPDL4n8/tests', '/home/roshanzamir/.cache/pants/named_caches/pex_root/venvs/c083917077e03ea6059ab65ba9ca23881c240ba2/e8a7fc14ab36c4adbef2284ef08fb2296f9b8bdb', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/roshanzamir/.cache/pants/named_caches/pex_root/venvs/s/b7bdc59a/venv/lib/python3.6/site-packages', '.']

which doesn't include the src directory. However, the src is determined as a source root by running ./pants roots:

.
src

On the other hand, if you use ./pants tailor which puts different BUILD files for the tests and src/foolib (not the src directory itself) directories, it works well (the src directory is included in sys.path, and the tests run without import problem) while the source roots are the same.

Pants version: 2.10.0
OS: Linux Ubuntu 18.04.4 LTS

@AlirezaRoshanzamir AlirezaRoshanzamir changed the title The source root doesn't apply to tests that exist in a different directory when there is a single BUILD file for each project. The "src" source root doesn't apply to tests that exist in a different directory when there is a single BUILD file for each project. Apr 10, 2022
@AlirezaRoshanzamir
Copy link
Author

It seems that the issue is related to #11646 and #11118 issues. I think the phrase "At some point, source root stripping applied to the targets that owned the sources, rather than to the sources themselves" from #11118 is partly the answer, but I do not fully understand what it means. Both the targets declared in the BUILD file in the repo root directory (/) and src/foolib contain the python_sources target that owned the sources, so, what does the phrase exactly mean?

@stuhood

@benjyw
Copy link
Sponsor Contributor

benjyw commented Apr 13, 2022

Ah, hm, interesting. I think the BUILD file needs to be inside the source root. So you might need to have two BUILD files instead of one (one in src/ and one in test/) but you still don't need one in every subdir (which is what tailor generates) if you don't want that.

@benjyw
Copy link
Sponsor Contributor

benjyw commented Apr 13, 2022

We should at least properly document this though.

@benjyw benjyw self-assigned this Apr 13, 2022
@AlirezaRoshanzamir
Copy link
Author

That's correct. Finally, I decided to put different BUILD files for the src/ and tests/ directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants