Skip to content

Commit 7d1f434

Browse files
committed
update
1 parent aecbe48 commit 7d1f434

File tree

20 files changed

+109
-15
lines changed

20 files changed

+109
-15
lines changed

gazelle/python/resolve.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []reso
7171
}
7272
pythonProjectRoot := cfg.PythonProjectRoot()
7373
provide := importSpecFromSrc(pythonProjectRoot, f.Pkg, src)
74+
// log.Printf("import to index: %v", provide.Imp)
7475
provides = append(provides, provide)
7576
}
7677
if len(provides) == 0 {
@@ -156,46 +157,56 @@ func (py *Resolver) Resolve(
156157
mod := it.Value().(module)
157158
moduleName := mod.Name
158159
// Transform relative imports `.` or `..foo.bar` into the package path from root.
159-
if strings.HasPrefix(moduleName, ".") {
160-
// If not package generation mode, skip relative imports
160+
if strings.HasPrefix(mod.From, ".") {
161161
if !isPackageGeneration {
162162
continue MODULES_LOOP
163163
}
164+
165+
// Count number of leading dots in mod.From (e.g., ".." = 2, "...foo.bar" = 3)
164166
relativeDepth := 0
165-
for i := 0; i < len(moduleName); i++ {
166-
if moduleName[i] == '.' {
167+
for i := 0; i < len(mod.From); i++ {
168+
if mod.From[i] == '.' {
167169
relativeDepth++
168170
} else {
169171
break
170172
}
171173
}
172174

173-
// Extract suffix after leading dots
174-
relativeSuffix := moduleName[relativeDepth:]
175-
var relativeSuffixParts []string
176-
if relativeSuffix != "" {
177-
relativeSuffixParts = strings.Split(relativeSuffix, ".")
175+
// Extract final symbol (e.g., "some_function") from mod.Name
176+
imported := mod.Name
177+
if idx := strings.LastIndex(mod.Name, "."); idx >= 0 {
178+
imported = mod.Name[idx+1:]
179+
}
180+
181+
// Optional subpath in 'from' clause, e.g. "from ...my_library.foo import x"
182+
fromPath := strings.TrimLeft(mod.From, ".")
183+
var fromParts []string
184+
if fromPath != "" {
185+
fromParts = strings.Split(fromPath, ".")
178186
}
179187

180-
// Split current package label into parts
188+
// Current Bazel package as path segments
181189
pkgParts := strings.Split(from.Pkg, "/")
182190

183-
if relativeDepth- 1 > len(pkgParts) {
184-
// Trying to go above the root
185-
log.Printf("ERROR: Invalid relative import %q in %q: exceeds package root.", moduleName, mod.Filepath)
191+
if relativeDepth-1 > len(pkgParts) {
192+
log.Printf("ERROR: Invalid relative import %q in %q: exceeds package root.", mod.Name, mod.Filepath)
186193
continue MODULES_LOOP
187194
}
188195

189-
// Go up `relativeDepth - 1` levels
196+
// Go up relativeDepth - 1 levels
190197
baseParts := pkgParts
191198
if relativeDepth > 1 {
192199
baseParts = pkgParts[:len(pkgParts)-(relativeDepth-1)]
193200
}
201+
// Build absolute module path
202+
absParts := append([]string{}, baseParts...) // base path
203+
absParts = append(absParts, fromParts...) // subpath from 'from'
204+
absParts = append(absParts, imported) // actual imported symbol
194205

195-
absParts := append(baseParts, relativeSuffixParts...)
196206
moduleName = strings.Join(absParts, ".")
197207
}
198208

209+
199210
moduleParts := strings.Split(moduleName, ".")
200211
possibleModules := []string{moduleName}
201212
for len(moduleParts) > 1 {

gazelle/python/testdata/relative_imports_package_mode/package1/BUILD.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@rules_python//python:defs.bzl", "py_library")
33
py_library(
44
name = "package1",
55
srcs = [
6+
"__init__.py",
67
"module1.py",
78
"module2.py",
89
],
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def some_function():
2+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "my_library",
5+
srcs = ["__init__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "my_library",
5+
srcs = ["__init__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def some_function():
2+
return "some_function"

gazelle/python/testdata/relative_imports_package_mode/package1/my_library/foo/BUILD.in

Whitespace-only changes.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "foo",
5+
srcs = ["__init__.py"],
6+
visibility = ["//:__subpackages__"],
7+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def some_function():
2+
return "some_function"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_python//python:defs.bzl", "py_library")
2+
3+
py_library(
4+
name = "subpackage1",
5+
srcs = [
6+
"__init__.py",
7+
"some_module.py",
8+
],
9+
visibility = ["//:__subpackages__"],
10+
)

0 commit comments

Comments
 (0)