Skip to content

Commit

Permalink
feat(gazelle): use relative paths for resolved imports (#1554)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
adzenith and aignas authored Nov 13, 2023
1 parent 9facc3e commit a9032d2
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ A brief description of the categories of changes:
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
Expand Down
2 changes: 1 addition & 1 deletion gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (py *Resolver) Resolve(
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): "+
Expand Down
1 change: 1 addition & 0 deletions gazelle/python/testdata/relative_imports/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:resolve py resolved_package //package2:resolved_package
2 changes: 2 additions & 0 deletions gazelle/python/testdata/relative_imports/BUILD.out
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ py_library(
"subpackage1/module5.py",
],
visibility = ["//:__subpackages__"],
deps = [":resolved_package"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from . import Class1
from .subpackage1.module5 import function5

import resolved_package

def function3():
c1 = Class1()
Expand Down

0 comments on commit a9032d2

Please sign in to comment.