Revert "Resolving sibling modules with absolute imports" (#1035)

Revert "Resolving sibling modules with absolute imports (#1029)"

This reverts commit 9fc7cfa82ac834f0dcc5ba321a46b93e9b728f87.

Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
diff --git a/gazelle/python/generate.go b/gazelle/python/generate.go
index 74e5f66..4ebb40f 100644
--- a/gazelle/python/generate.go
+++ b/gazelle/python/generate.go
@@ -224,6 +224,7 @@
 		}
 
 		pyLibrary = newTargetBuilder(pyLibraryKind, pyLibraryTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
+			setUUID(label.New("", args.Rel, pyLibraryTargetName).String()).
 			addVisibility(visibility).
 			addSrcs(pyLibraryFilenames).
 			addModuleDependencies(deps).
@@ -266,6 +267,10 @@
 			addModuleDependencies(deps).
 			generateImportsAttribute()
 
+		if pyLibrary != nil {
+			pyBinaryTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
+		}
+
 		pyBinary := pyBinaryTarget.build()
 
 		result.Gen = append(result.Gen, pyBinary)
@@ -296,6 +301,7 @@
 		}
 
 		conftestTarget := newTargetBuilder(pyLibraryKind, conftestTargetname, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
+			setUUID(label.New("", args.Rel, conftestTargetname).String()).
 			addSrc(conftestFilename).
 			addModuleDependencies(deps).
 			addVisibility(visibility).
@@ -309,8 +315,8 @@
 	}
 
 	var pyTestTargets []*targetBuilder
-	newPyTestTargetBuilder := func(srcs *treeset.Set, pyTestTargetName string) *targetBuilder {
-		deps, err := parser.parse(srcs)
+	newPyTestTargetBuilder := func(pyTestFilenames *treeset.Set, pyTestTargetName string) *targetBuilder {
+		deps, err := parser.parse(pyTestFilenames)
 		if err != nil {
 			log.Fatalf("ERROR: %v\n", err)
 		}
@@ -331,7 +337,7 @@
 			}
 		}
 		return newTargetBuilder(pyTestKind, pyTestTargetName, pythonProjectRoot, args.Rel, pyLibraryFilenames.Union(pyTestFilenames)).
-			addSrcs(srcs).
+			addSrcs(pyTestFilenames).
 			addModuleDependencies(deps).
 			generateImportsAttribute()
 	}
@@ -365,9 +371,14 @@
 	}
 
 	for _, pyTestTarget := range pyTestTargets {
-		if conftest != nil {
-			pyTestTarget.addModuleDependency(module{Name: strings.TrimSuffix(conftestFilename, ".py")})
+		if pyLibrary != nil {
+			pyTestTarget.addModuleDependency(module{Name: pyLibrary.PrivateAttr(uuidKey).(string)})
 		}
+
+		if conftest != nil {
+			pyTestTarget.addModuleDependency(module{Name: conftest.PrivateAttr(uuidKey).(string)})
+		}
+
 		pyTest := pyTestTarget.build()
 
 		result.Gen = append(result.Gen, pyTest)
diff --git a/gazelle/python/resolve.go b/gazelle/python/resolve.go
index 46014e5..607776a 100644
--- a/gazelle/python/resolve.go
+++ b/gazelle/python/resolve.go
@@ -39,6 +39,10 @@
 	// resolvedDepsKey is the attribute key used to pass dependencies that don't
 	// need to be resolved by the dependency resolver in the Resolver step.
 	resolvedDepsKey = "_gazelle_python_resolved_deps"
+	// uuidKey is the attribute key used to uniquely identify a py_library
+	// target that should be imported by a py_test or py_binary in the same
+	// Bazel package.
+	uuidKey = "_gazelle_python_library_uuid"
 )
 
 // Resolver satisfies the resolve.Resolver interface. It resolves dependencies
@@ -67,6 +71,13 @@
 			provides = append(provides, provide)
 		}
 	}
+	if r.PrivateAttr(uuidKey) != nil {
+		provide := resolve.ImportSpec{
+			Lang: languageName,
+			Imp:  r.PrivateAttr(uuidKey).(string),
+		}
+		provides = append(provides, provide)
+	}
 	if len(provides) == 0 {
 		return nil
 	}
diff --git a/gazelle/python/target.go b/gazelle/python/target.go
index 149c158..69711ce 100644
--- a/gazelle/python/target.go
+++ b/gazelle/python/target.go
@@ -29,6 +29,7 @@
 	name              string
 	pythonProjectRoot string
 	bzlPackage        string
+	uuid              string
 	srcs              *treeset.Set
 	siblingSrcs       *treeset.Set
 	deps              *treeset.Set
@@ -54,6 +55,15 @@
 	}
 }
 
+// setUUID sets the given UUID for the target. It's used to index the generated
+// target based on this value in addition to the other ways the targets can be
+// imported. py_{binary,test} targets in the same Bazel package can add a
+// virtual dependency to this UUID that gets resolved in the Resolver interface.
+func (t *targetBuilder) setUUID(uuid string) *targetBuilder {
+	t.uuid = uuid
+	return t
+}
+
 // addSrc adds a single src to the target.
 func (t *targetBuilder) addSrc(src string) *targetBuilder {
 	t.srcs.Add(src)
@@ -71,16 +81,9 @@
 
 // addModuleDependency adds a single module dep to the target.
 func (t *targetBuilder) addModuleDependency(dep module) *targetBuilder {
-	fileName := dep.Name + ".py"
-	if dep.From != "" {
-		fileName = dep.From + ".py"
+	if dep.Name+".py" == filepath.Base(dep.Filepath) || !t.siblingSrcs.Contains(dep.Name+".py") {
+		t.deps.Add(dep)
 	}
-	if t.siblingSrcs.Contains(fileName) && fileName != filepath.Base(dep.Filepath) {
-		// importing another module from the same package, converting to absolute imports to make
-		// dependency resolution easier
-		dep.Name = importSpecFromSrc(t.pythonProjectRoot, t.bzlPackage, fileName).Imp
-	}
-	t.deps.Add(dep)
 	return t
 }
 
@@ -135,6 +138,9 @@
 // build returns the assembled *rule.Rule for the target.
 func (t *targetBuilder) build() *rule.Rule {
 	r := rule.NewRule(t.kind, t.name)
+	if t.uuid != "" {
+		r.SetPrivateAttr(uuidKey, t.uuid)
+	}
 	if !t.srcs.Empty() {
 		r.SetAttr("srcs", t.srcs.Values())
 	}
diff --git a/gazelle/python/testdata/generated_test_entrypoint/BUILD.out b/gazelle/python/testdata/generated_test_entrypoint/BUILD.out
index e8e304c..48df068 100644
--- a/gazelle/python/testdata/generated_test_entrypoint/BUILD.out
+++ b/gazelle/python/testdata/generated_test_entrypoint/BUILD.out
@@ -17,5 +17,8 @@
     name = "generated_test_entrypoint_test",
     srcs = [":__test__"],
     main = ":__test__.py",
-    deps = [":__test__"],
+    deps = [
+        ":__test__",
+        ":generated_test_entrypoint",
+    ],
 )
diff --git a/gazelle/python/testdata/naming_convention/__main__.py b/gazelle/python/testdata/naming_convention/__main__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/__main__.py
+++ b/gazelle/python/testdata/naming_convention/__main__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/__test__.py b/gazelle/python/testdata/naming_convention/__test__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/__test__.py
+++ b/gazelle/python/testdata/naming_convention/__test__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/dont_rename/__main__.py b/gazelle/python/testdata/naming_convention/dont_rename/__main__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/dont_rename/__main__.py
+++ b/gazelle/python/testdata/naming_convention/dont_rename/__main__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/dont_rename/__test__.py b/gazelle/python/testdata/naming_convention/dont_rename/__test__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/dont_rename/__test__.py
+++ b/gazelle/python/testdata/naming_convention/dont_rename/__test__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/resolve_conflict/__main__.py b/gazelle/python/testdata/naming_convention/resolve_conflict/__main__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/resolve_conflict/__main__.py
+++ b/gazelle/python/testdata/naming_convention/resolve_conflict/__main__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/naming_convention/resolve_conflict/__test__.py b/gazelle/python/testdata/naming_convention/resolve_conflict/__test__.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/naming_convention/resolve_conflict/__test__.py
+++ b/gazelle/python/testdata/naming_convention/resolve_conflict/__test__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/python_target_with_test_in_name/real_test.py b/gazelle/python/testdata/python_target_with_test_in_name/real_test.py
index e390866..2f03211 100644
--- a/gazelle/python/testdata/python_target_with_test_in_name/real_test.py
+++ b/gazelle/python/testdata/python_target_with_test_in_name/real_test.py
@@ -13,6 +13,5 @@
 # limitations under the License.
 
 import boto3
-import __init__
 
 _ = boto3
diff --git a/gazelle/python/testdata/python_target_with_test_in_name/test_reality.py b/gazelle/python/testdata/python_target_with_test_in_name/test_reality.py
index a3afc79..7307559 100644
--- a/gazelle/python/testdata/python_target_with_test_in_name/test_reality.py
+++ b/gazelle/python/testdata/python_target_with_test_in_name/test_reality.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import __init__
\ No newline at end of file
diff --git a/gazelle/python/testdata/sibling_imports/README.md b/gazelle/python/testdata/sibling_imports/README.md
deleted file mode 100644
index e59be07..0000000
--- a/gazelle/python/testdata/sibling_imports/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# Sibling imports
-
-This test case asserts that imports from sibling modules are resolved correctly. It covers 3 different types of imports in `pkg/unit_test.py`
\ No newline at end of file
diff --git a/gazelle/python/testdata/sibling_imports/WORKSPACE b/gazelle/python/testdata/sibling_imports/WORKSPACE
deleted file mode 100644
index faff6af..0000000
--- a/gazelle/python/testdata/sibling_imports/WORKSPACE
+++ /dev/null
@@ -1 +0,0 @@
-# This is a Bazel workspace for the Gazelle test data.
diff --git a/gazelle/python/testdata/sibling_imports/pkg/BUILD.in b/gazelle/python/testdata/sibling_imports/pkg/BUILD.in
deleted file mode 100644
index e69de29..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/BUILD.in
+++ /dev/null
diff --git a/gazelle/python/testdata/sibling_imports/pkg/BUILD.out b/gazelle/python/testdata/sibling_imports/pkg/BUILD.out
deleted file mode 100644
index edb40a8..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/BUILD.out
+++ /dev/null
@@ -1,29 +0,0 @@
-load("@rules_python//python:defs.bzl", "py_library", "py_test")
-
-py_library(
-    name = "pkg",
-    srcs = [
-        "__init__.py",
-        "a.py",
-        "b.py",
-    ],
-    imports = [".."],
-    visibility = ["//:__subpackages__"],
-)
-
-py_test(
-    name = "test_util",
-    srcs = ["test_util.py"],
-    imports = [".."],
-)
-
-py_test(
-    name = "unit_test",
-    srcs = ["unit_test.py"],
-    imports = [".."],
-    deps = [
-        ":pkg",
-        ":test_util",
-    ],
-)
-
diff --git a/gazelle/python/testdata/sibling_imports/pkg/__init__.py b/gazelle/python/testdata/sibling_imports/pkg/__init__.py
deleted file mode 100644
index e69de29..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/__init__.py
+++ /dev/null
diff --git a/gazelle/python/testdata/sibling_imports/pkg/a.py b/gazelle/python/testdata/sibling_imports/pkg/a.py
deleted file mode 100644
index e69de29..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/a.py
+++ /dev/null
diff --git a/gazelle/python/testdata/sibling_imports/pkg/b.py b/gazelle/python/testdata/sibling_imports/pkg/b.py
deleted file mode 100644
index 7095bdc..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/b.py
+++ /dev/null
@@ -1,2 +0,0 @@
-def run():
-    pass
\ No newline at end of file
diff --git a/gazelle/python/testdata/sibling_imports/pkg/test_util.py b/gazelle/python/testdata/sibling_imports/pkg/test_util.py
deleted file mode 100644
index e69de29..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/test_util.py
+++ /dev/null
diff --git a/gazelle/python/testdata/sibling_imports/pkg/unit_test.py b/gazelle/python/testdata/sibling_imports/pkg/unit_test.py
deleted file mode 100644
index a3218e2..0000000
--- a/gazelle/python/testdata/sibling_imports/pkg/unit_test.py
+++ /dev/null
@@ -1,3 +0,0 @@
-import a
-from b import run
-import test_util
\ No newline at end of file
diff --git a/gazelle/python/testdata/sibling_imports/test.yaml b/gazelle/python/testdata/sibling_imports/test.yaml
deleted file mode 100644
index ed97d53..0000000
--- a/gazelle/python/testdata/sibling_imports/test.yaml
+++ /dev/null
@@ -1 +0,0 @@
----
diff --git a/gazelle/python/testdata/simple_binary_with_library/__main__.py b/gazelle/python/testdata/simple_binary_with_library/__main__.py
index bc7ddf0..7307559 100644
--- a/gazelle/python/testdata/simple_binary_with_library/__main__.py
+++ b/gazelle/python/testdata/simple_binary_with_library/__main__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import foo
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_main/__main__.py b/gazelle/python/testdata/subdir_sources/foo/has_main/__main__.py
index bd0fe61..7307559 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_main/__main__.py
+++ b/gazelle/python/testdata/subdir_sources/foo/has_main/__main__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import foo.has_main.python.my_module
\ No newline at end of file
diff --git a/gazelle/python/testdata/subdir_sources/foo/has_test/__test__.py b/gazelle/python/testdata/subdir_sources/foo/has_test/__test__.py
index 3c9ed1a..7307559 100644
--- a/gazelle/python/testdata/subdir_sources/foo/has_test/__test__.py
+++ b/gazelle/python/testdata/subdir_sources/foo/has_test/__test__.py
@@ -13,4 +13,3 @@
 # limitations under the License.
 
 # For test purposes only.
-import foo.has_test.python.my_module
\ No newline at end of file
diff --git a/gazelle/python/testdata/with_third_party_requirements/BUILD.out b/gazelle/python/testdata/with_third_party_requirements/BUILD.out
index 2a97d8b..2da7f2b 100644
--- a/gazelle/python/testdata/with_third_party_requirements/BUILD.out
+++ b/gazelle/python/testdata/with_third_party_requirements/BUILD.out
@@ -20,5 +20,5 @@
     srcs = ["__main__.py"],
     main = "__main__.py",
     visibility = ["//:__subpackages__"],
-    deps = ["@gazelle_python_test_baz//:pkg"],
+    deps = [":with_third_party_requirements"],
 )