Coveragepy: "Already imported a file that will be measured" reported for coverage itself

Created on 21 Dec 2019  路  14Comments  路  Source: nedbat/coveragepy

Describe the bug
Admittedly, my configuration is probably ~slightly wrong -- but since this is a new message in coverage 5.x I figured I'd point it out. The warning seems non-actionable for this particular case.

To Reproduce
How can we reproduce the problem? Please be specific.

This .coveragerc (distilled from a larger example)

[run]
source = .

coverage installed into ./venv

$ ./venv/bin/pip freeze
coverage==5.0

any old simple python file, here's t.py

print('hello world')

run coverage run -m t

$ coverage run -m t
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/bin/coverage (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/version.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/control.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/env.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/annotate.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/files.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/backward.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/misc.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/report.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/collector.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/debug.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/disposition.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/pytracer.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/tracer.cpython-36m-x86_64-linux-gnu.so (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/config.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/tomlconfig.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/context.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/data.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/sqldata.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/numbits.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/html.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/results.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/templite.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/inorout.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/python.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/parser.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/bytecode.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/phystokens.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/plugin.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/jsonreport.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/plugin_support.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/summary.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/xmlreport.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/multiproc.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/cmdline.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: /tmp/x/venv/lib/python3.6/site-packages/coverage/execfile.py (already-imported)
hello world

Expected behavior

the warnings are pretty noisy, and in my case not really actionable (this is a development env, I'll later run from tox where omit will exclude these particular files).

I realize I probably should add omit = venv*/* or something of the sort

Additional context

nothing really, just new noise :)

thanks again for coverage, I don't know what I'd do without it

bug fixed

Most helpful comment

omit = venv/* stops working once plugins come into play. Let me provide proof:

Start with the exact same same setup as described above:

cd "$(mktemp -d)"
python3 -m venv venv
source venv/bin/activate
pip install coverage
echo $'[run]\nsource = .\nomit = venv/*' > .coveragerc  # the dollar syntax is a Bashism, saves "echo -e"
echo "print('hello world')" > t.py

Verify that there are no "already-imported" warnings yet:

$ coverage run -m t
hello world

Add plugin django_coverage_plugin to the mix:

pip install django django_coverage_plugin
echo 'plugins = django_coverage_plugin' >> .coveragerc

See "already-imported" warnings from the omitted path appear:

$ coverage run -m t
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/context.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/exceptions.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/base.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/library.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/engine.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/utils.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/defaultfilters.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/smartif.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/defaulttags.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/templatetags/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/templatetags/i18n.py (already-imported)
hello world

Is that a bug in coverage or in django_coverage_plugin?
Is it considered the same thing or should I open a new bug?

All 14 comments

Perhaps this is a variant of #876, that coverage should be smarter about ignoring local venv directories.

Wait, somehow i missed that this was about coverage itself...!

yep! and bin/coverage (sys.modules['__main__']) and site.py (which I assume is due to virtualenv shenanigans and doesn't get covered by the stdlib filter?)

This is similar to #876: when using --source=., the venv is part of that source tree, so all of venv is considered measurable.

You can fix it by adding this to .coveragerc:

[run]
omit = venv/*

BTW, I don't think this is a slightly wrong configuration, I think it's really common, unless I missed something?

--omit=venv/* worked for me, thanks nedbat.

omit = venv/* stops working once plugins come into play. Let me provide proof:

Start with the exact same same setup as described above:

cd "$(mktemp -d)"
python3 -m venv venv
source venv/bin/activate
pip install coverage
echo $'[run]\nsource = .\nomit = venv/*' > .coveragerc  # the dollar syntax is a Bashism, saves "echo -e"
echo "print('hello world')" > t.py

Verify that there are no "already-imported" warnings yet:

$ coverage run -m t
hello world

Add plugin django_coverage_plugin to the mix:

pip install django django_coverage_plugin
echo 'plugins = django_coverage_plugin' >> .coveragerc

See "already-imported" warnings from the omitted path appear:

$ coverage run -m t
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/context.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/exceptions.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/base.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/library.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/engine.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/utils.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/defaultfilters.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/smartif.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/template/defaulttags.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/templatetags/__init__.py (already-imported)
Coverage.py warning: Already imported a file that will be measured: [..]/venv/lib/python3.8/site-packages/django/templatetags/i18n.py (already-imported)
hello world

Is that a bug in coverage or in django_coverage_plugin?
Is it considered the same thing or should I open a new bug?

@erdseb this issue is about coverage itself, not django

Any update on this?

+1

@piranna @tlrh314 The solution is above.

This is now fixed in 0285af966a

Would you mind testing it out?

pip install git+https://github.com/nedbat/coveragepy.git@0285af966a#egg=coverage==5.6b1

soooo close, most of the console spam is gone, just one thing remains:

$ coverage run -m t
Coverage.py warning: Already imported a file that will be measured: /tmp/q/venv/bin/coverage (already-imported)
hello hello world

Ugh, newb mistake not running the actual example from the bug report! It's fixed now in 5c2f614e.

awwww yiss -- thanks a ton!

$ coverage run -m t
hello hello world
Was this page helpful?
0 / 5 - 0 ratings