Skip to content

Commit a9032d2

Browse files
adzenithaignas
andauthored
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 <[email protected]>
1 parent 9facc3e commit a9032d2

File tree

6 files changed

+9
-1
lines changed

6 files changed

+9
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ A brief description of the categories of changes:
5555
or `requirements_in` attributes are unspecified, matching the upstream
5656
`pip-compile` behaviour more closely.
5757

58+
* (gazelle) Use relative paths if possible for dependencies added through
59+
the use of the `resolve` directive.
60+
5861
Breaking changes:
5962

6063
* (pip) `pip_install` repository rule in this release has been disabled and

gazelle/python/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func (py *Resolver) Resolve(
172172
if override.Repo == from.Repo {
173173
override.Repo = ""
174174
}
175-
dep := override.String()
175+
dep := override.Rel(from.Repo, from.Pkg).String()
176176
deps.Add(dep)
177177
if explainDependency == dep {
178178
log.Printf("Explaining dependency (%s): "+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:resolve py resolved_package //package2:resolved_package

gazelle/python/testdata/relative_imports/BUILD.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
load("@rules_python//python:defs.bzl", "py_binary", "py_library")
22

3+
# gazelle:resolve py resolved_package //package2:resolved_package
4+
35
py_library(
46
name = "relative_imports",
57
srcs = [

gazelle/python/testdata/relative_imports/package2/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ py_library(
99
"subpackage1/module5.py",
1010
],
1111
visibility = ["//:__subpackages__"],
12+
deps = [":resolved_package"],
1213
)

gazelle/python/testdata/relative_imports/package2/module3.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from . import Class1
1616
from .subpackage1.module5 import function5
1717

18+
import resolved_package
1819

1920
def function3():
2021
c1 = Class1()

0 commit comments

Comments
 (0)