feat(gazelle): use relative paths for resolved imports (#1554)

Modify the Gazelle plugin so that when it adds a `dep` because of a
`resolve` directive it makes it a relative import if possible.

The first commit adds a test for the existing behavior, where inside of
`//package2` the dependency `//package2:resolved_package` is added. The
second commit updates the test and the behavior so inside of
`//package2` we add `: resolved_package` instead.

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dc3b079..a3ea068 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -55,6 +55,9 @@
   or `requirements_in` attributes are unspecified, matching the upstream
   `pip-compile` behaviour more closely.
 
+* (gazelle) Use relative paths if possible for dependencies added through
+  the use of the `resolve` directive.
+
 Breaking changes:
 
 * (pip) `pip_install` repository rule in this release has been disabled and
diff --git a/gazelle/python/resolve.go b/gazelle/python/resolve.go
index 87eed76..1ddd63d 100644
--- a/gazelle/python/resolve.go
+++ b/gazelle/python/resolve.go
@@ -172,7 +172,7 @@
 						if override.Repo == from.Repo {
 							override.Repo = ""
 						}
-						dep := override.String()
+						dep := override.Rel(from.Repo, from.Pkg).String()
 						deps.Add(dep)
 						if explainDependency == dep {
 							log.Printf("Explaining dependency (%s): "+
diff --git a/gazelle/python/testdata/relative_imports/BUILD.in b/gazelle/python/testdata/relative_imports/BUILD.in
index e69de29..c04b5e5 100644
--- a/gazelle/python/testdata/relative_imports/BUILD.in
+++ b/gazelle/python/testdata/relative_imports/BUILD.in
@@ -0,0 +1 @@
+# gazelle:resolve py resolved_package //package2:resolved_package
diff --git a/gazelle/python/testdata/relative_imports/BUILD.out b/gazelle/python/testdata/relative_imports/BUILD.out
index 2c08627..bf95244 100644
--- a/gazelle/python/testdata/relative_imports/BUILD.out
+++ b/gazelle/python/testdata/relative_imports/BUILD.out
@@ -1,5 +1,7 @@
 load("@rules_python//python:defs.bzl", "py_binary", "py_library")
 
+# gazelle:resolve py resolved_package //package2:resolved_package
+
 py_library(
     name = "relative_imports",
     srcs = [
diff --git a/gazelle/python/testdata/relative_imports/package2/BUILD.out b/gazelle/python/testdata/relative_imports/package2/BUILD.out
index cf61691..3e03e75 100644
--- a/gazelle/python/testdata/relative_imports/package2/BUILD.out
+++ b/gazelle/python/testdata/relative_imports/package2/BUILD.out
@@ -9,4 +9,5 @@
         "subpackage1/module5.py",
     ],
     visibility = ["//:__subpackages__"],
+    deps = [":resolved_package"],
 )
diff --git a/gazelle/python/testdata/relative_imports/package2/module3.py b/gazelle/python/testdata/relative_imports/package2/module3.py
index 74978a0..478dea9 100644
--- a/gazelle/python/testdata/relative_imports/package2/module3.py
+++ b/gazelle/python/testdata/relative_imports/package2/module3.py
@@ -15,6 +15,7 @@
 from . import Class1
 from .subpackage1.module5 import function5
 
+import resolved_package
 
 def function3():
     c1 = Class1()