Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gazelle): __init__.py in per-file targets #1582

Merged
merged 9 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 20 additions & 9 deletions gazelle/python/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,16 @@ func (py *Python) GenerateRules(args language.GenerateArgs) language.GenerateRes
result.Imports = append(result.Imports, pyLibrary.PrivateAttr(config.GazelleImportsKey))
}
if cfg.PerFileGeneration() {
hasInit, nonEmptyInit := hasLibraryEntrypointFile(args.Dir)
pyLibraryFilenames.Each(func(index int, filename interface{}) {
if filename == pyLibraryEntrypointFilename {
stat, err := os.Stat(filepath.Join(args.Dir, filename.(string)))
if err != nil {
log.Fatalf("ERROR: %v\n", err)
}
if stat.Size() == 0 {
return // ignore empty __init__.py
}
pyLibraryTargetName := strings.TrimSuffix(filepath.Base(filename.(string)), ".py")
if filename == pyLibraryEntrypointFilename && !nonEmptyInit {
return // ignore empty __init__.py.
}
srcs := treeset.NewWith(godsutils.StringComparator, filename)
pyLibraryTargetName := strings.TrimSuffix(filepath.Base(filename.(string)), ".py")
if hasInit && nonEmptyInit {
srcs.Add(pyLibraryEntrypointFilename)
}
appendPyLibrary(srcs, pyLibraryTargetName)
})
} else if !pyLibraryFilenames.Empty() {
Expand Down Expand Up @@ -463,6 +461,19 @@ func hasEntrypointFile(dir string) bool {
return false
}

// hasLibraryEntrypointFile returns if the given directory has the library
// entrypoint file, and if it is non-empty.
func hasLibraryEntrypointFile(dir string) (bool, bool) {
stat, err := os.Stat(filepath.Join(dir, pyLibraryEntrypointFilename))
if os.IsNotExist(err) {
return false, false
}
if err != nil {
log.Fatalf("ERROR: %v\n", err)
}
return true, stat.Size() != 0
}

// isEntrypointFile returns whether the given path is an entrypoint file. The
// given path can be absolute or relative.
func isEntrypointFile(path string) bool {
Expand Down
14 changes: 10 additions & 4 deletions gazelle/python/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ func (py *Resolver) Imports(c *config.Config, r *rule.Rule, f *rule.File) []reso
provides := make([]resolve.ImportSpec, 0, len(srcs)+1)
for _, src := range srcs {
ext := filepath.Ext(src)
if ext == ".py" {
pythonProjectRoot := cfg.PythonProjectRoot()
provide := importSpecFromSrc(pythonProjectRoot, f.Pkg, src)
provides = append(provides, provide)
if ext != ".py" {
continue
}
if cfg.PerFileGeneration() && len(srcs) > 1 && src == pyLibraryEntrypointFilename {
// Do not provide import spec from __init__.py when it is being included as
// part of another module.
continue
}
pythonProjectRoot := cfg.PythonProjectRoot()
provide := importSpecFromSrc(pythonProjectRoot, f.Pkg, src)
provides = append(provides, provide)
}
if len(provides) == 0 {
return nil
Expand Down
5 changes: 4 additions & 1 deletion gazelle/python/testdata/per_file_non_empty_init/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ py_library(

py_library(
name = "foo",
srcs = ["foo.py"],
srcs = [
"__init__.py",
siddharthab marked this conversation as resolved.
Show resolved Hide resolved
"foo.py",
],
visibility = ["//:__subpackages__"],
)
14 changes: 13 additions & 1 deletion gazelle/python/testdata/per_file_subdirs/bar/BUILD.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,21 @@ py_library(
visibility = ["//:__subpackages__"],
)

py_library(
name = "bar",
srcs = [
"__init__.py",
"bar.py",
],
visibility = ["//:__subpackages__"],
)

py_library(
name = "foo",
srcs = ["foo.py"],
srcs = [
"__init__.py",
"foo.py",
],
visibility = ["//:__subpackages__"],
)

Expand Down
Empty file.