fix: move coverage pkg to end of sys.path to avoid collisions (#1045)

* fix: move coverage to end of sys.path when running pip-compile

Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>

* fix: generic fix for getting the coverage package right

Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>

---------

Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel
index 811f285..f2e1c9b 100644
--- a/python/private/BUILD.bazel
+++ b/python/private/BUILD.bazel
@@ -36,6 +36,7 @@
 # on Skylib.)
 exports_files(
     [
+        "coverage.patch",
         "py_package.bzl",
         "py_wheel.bzl",
         "reexports.bzl",
diff --git a/python/private/coverage.patch b/python/private/coverage.patch
new file mode 100644
index 0000000..4cab60c
--- /dev/null
+++ b/python/private/coverage.patch
@@ -0,0 +1,15 @@
+# Because of how coverage is run, the current directory is the first in
+# sys.path. This is a problem for the tests, because they may import a module of
+# the same name as a module in the current directory.
+diff --git a/coverage/cmdline.py b/coverage/cmdline.py
+index dbf66e0a..780505ac 100644
+--- a/coverage/cmdline.py
++++ b/coverage/cmdline.py
+@@ -937,6 +937,7 @@ def main(argv=None):
+     This is installed as the script entry point.
+ 
+     """
++    sys.path.append(sys.path.pop(0))
+     if argv is None:
+         argv = sys.argv[1:]
+     try:
diff --git a/python/private/coverage_deps.bzl b/python/private/coverage_deps.bzl
index d6092e6..377dfb7 100644
--- a/python/private/coverage_deps.bzl
+++ b/python/private/coverage_deps.bzl
@@ -97,6 +97,8 @@
 }
 #END: managed by update_coverage_deps.py script
 
+_coverage_patch = Label("//python/private:coverage.patch")
+
 def coverage_dep(name, python_version, platform, visibility, install = True):
     """Register a singe coverage dependency based on the python version and platform.
 
@@ -149,6 +151,8 @@
     """.format(
             visibility = visibility,
         ),
+        patch_args = ["-p1"],
+        patches = [_coverage_patch],
         sha256 = sha256,
         type = "zip",
         urls = [url],