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

process: sync commits from Aspect-internal silo #712

Merged
merged 21 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5eed124
chore: bump Typescript versions (#5598)
mattem May 30, 2024
3b157d2
refactor: switch lint handler to be a single call with all results in…
gregmagolan May 31, 2024
880c275
feat(cli): add pro-configure command with starzelle plugins (#5612)
jbedard Jun 1, 2024
446046a
refactor: starzelle native library setup (#5624)
jbedard Jun 3, 2024
40e246c
docs(gazelle): correct js_tsconfig directive docs (#5627)
jbedard Jun 3, 2024
46e867e
fix(CLI): Ensure reportFile is not nil when processing (#5636)
JesseTatasciore Jun 4, 2024
d3436a8
fix: lock version of Aspect CLI by default when unstamped (#5648)
gregmagolan Jun 6, 2024
32fb094
refactor: align sync cli configure code with gazelle (#5649)
jbedard Jun 6, 2024
e55e451
build: upgrade gazelle v0.37 (#5640)
jbedard Jun 6, 2024
2f4b042
fix(gazelle): do not output stats on error (#5661)
jbedard Jun 6, 2024
e4affa6
refactor(cli): update comments referencing gazelle src (#5660)
jbedard Jun 6, 2024
45d27b5
refactor(cli): remove processing of .bazelignore which gazelle now ha…
jbedard Jun 7, 2024
71fdae3
refactor(cli): enable gazelle profiling via env vars (#5658)
jbedard Jun 7, 2024
ce6883f
fix: allow for clearing BAZELISK_BASE_URL in the Aspect CLI via BAZEL…
gregmagolan Jun 7, 2024
647242d
refactor(starzelle): traverse starlark Dict using iterators (#5670)
jbedard Jun 7, 2024
0fededf
feat(cli): support #aspect: gazelle directives (#5678)
jbedard Jun 7, 2024
5230b95
chore: upgrade go-git to v5 (#5685)
jbedard Jun 8, 2024
499f66a
feat(gazelle): respect #gazelle:build_file_name (#5684)
jbedard Jun 10, 2024
8dd2bef
feat(cli/configure): add BUILD generation for Python (#5698)
alexeagle Jun 10, 2024
12e1a9f
chore: go mod tidy
alexeagle Jun 10, 2024
948fd68
chore: patch gazelle to allow 'aspect' directives
alexeagle Jun 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ protoc_toolchains(
version = "v21.7",
)

http_archive(
name = "rules_python",
integrity = "sha256-bERKXOYmJB6fdw/0TFPBLWen8f+81eG2p0EAneLkBAo=",
strip_prefix = "rules_python-49cdf7d3fe000076d6432a34238e5d25f5b598d0",
# NB: version matches go.mod where we fetch the rules_python/gazelle Go package.
url = "https://github.com/bazelbuild/rules_python/archive/49cdf7d3fe000076d6432a34238e5d25f5b598d0.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

load("@rules_python//gazelle:deps.bzl", "python_stdlib_list_deps")

python_stdlib_list_deps()

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("//:go.bzl", _go_repositories = "deps")

Expand Down
9 changes: 7 additions & 2 deletions cmd/aspect/configure/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func NewDefaultCmd() *cobra.Command {
}

func NewCmd(streams ioutils.Streams) *cobra.Command {
v := configure.New(streams)

return NewCmdWithConfigure(streams, configure.New(streams))
}
func NewCmdWithConfigure(streams ioutils.Streams, v *configure.Configure) *cobra.Command {
cmd := &cobra.Command{
Use: "configure",
Short: "Auto-configure Bazel by updating BUILD files",
Expand All @@ -46,13 +47,17 @@ Run 'aspect help directives' for more documentation on directives.

So far these languages are supported:
- Go and Protocol Buffers, thanks to code from [gazelle]
- Python, thanks to code from [rules_python]
- JavaScript (including TypeScript)
- Kotlin (experimental, see https://github.com/aspect-build/aspect-cli/issues/474)
- Starlark, thanks to code from [bazel-skylib]

configure is based on [gazelle]. We are very grateful to the authors of that software.
The advantage of configure in Aspect CLI is that you don't need to compile the tooling before running it.

[gazelle]: https://github.com/bazelbuild/bazel-gazelle
[rules_python]: https://github.com/bazelbuild/rules_python/tree/main/gazelle
[bazel-skylib]: https://github.com/bazelbuild/bazel-skylib/tree/main/gazelle

To change the behavior of configure, you add "directives" to your BUILD files, which are comments
in a special syntax.
Expand Down
4 changes: 2 additions & 2 deletions cmd/aspect/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
)

func NewDefaultCmd(pluginSystem system.PluginSystem) *cobra.Command {
return NewCmd(ioutils.DefaultStreams, pluginSystem, bazel.WorkspaceFromWd, []lint.LintHandler{})
return NewCmd(ioutils.DefaultStreams, pluginSystem, bazel.WorkspaceFromWd, []lint.LintResultsHandler{})
}

func NewCmd(streams ioutils.Streams, pluginSystem system.PluginSystem, bzl bazel.Bazel, lintHandlers []lint.LintHandler) *cobra.Command {
func NewCmd(streams ioutils.Streams, pluginSystem system.PluginSystem, bzl bazel.Bazel, lintHandlers []lint.LintResultsHandler) *cobra.Command {
cmd := &cobra.Command{
Use: "lint <target patterns>",
Args: cobra.MinimumNArgs(1),
Expand Down
9 changes: 6 additions & 3 deletions cmd/aspect/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ func NewDefaultCmd(pluginSystem system.PluginSystem) *cobra.Command {

func CheckAspectLockVersionFlag(args []string) bool {
for _, arg := range args {
if arg == "--"+flags.AspectLockVersion {
if arg == "--"+flags.AspectLockVersion+"=false" {
return false
}
if arg == "--"+flags.AspectLockVersion+"=true" || arg == "--"+flags.AspectLockVersion {
return true
}
}
return false
return flags.AspectLockVersionDefault()
}

func CheckAspectDisablePluginsFlag(args []string) bool {
Expand Down Expand Up @@ -156,13 +159,13 @@ func NewCmd(
cmd.AddCommand(test.NewDefaultCmd(pluginSystem))
cmd.AddCommand(version.NewDefaultCmd())
cmd.AddCommand(outputs.NewDefaultCmd())
cmd.AddCommand(configure.NewDefaultCmd())
cmd.SetHelpCommand(help.NewCmd())

if !buildinfo.Current().IsAspectPro {
// Aspect CLI only commands
cmd.AddCommand(lint.NewDefaultCmd(pluginSystem))
cmd.AddCommand(license.NewDefaultCmd())
cmd.AddCommand(configure.NewDefaultCmd())
}

// ### "Additional help topic commands" which are not runnable
Expand Down
4 changes: 4 additions & 0 deletions docs/aspect_configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ Run 'aspect help directives' for more documentation on directives.

So far these languages are supported:
- Go and Protocol Buffers, thanks to code from [gazelle]
- Python, thanks to code from [rules_python]
- JavaScript (including TypeScript)
- Kotlin (experimental, see https://github.com/aspect-build/aspect-cli/issues/474)
- Starlark, thanks to code from [bazel-skylib]

configure is based on [gazelle]. We are very grateful to the authors of that software.
The advantage of configure in Aspect CLI is that you don't need to compile the tooling before running it.

[gazelle]: https://github.com/bazelbuild/bazel-gazelle
[rules_python]: https://github.com/bazelbuild/rules_python/tree/main/gazelle
[bazel-skylib]: https://github.com/bazelbuild/bazel-skylib/tree/main/gazelle

To change the behavior of configure, you add "directives" to your BUILD files, which are comments
in a special syntax.
Expand Down
4 changes: 2 additions & 2 deletions docs/help/topics/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ enable other projects to declare dependencies on the package.
| Equivalent to `js_files` but for the test `ts_project` rule. |
| `# gazelle:js_npm_package_target_name _name_` | `{dirname}` |
| The format used to generate the name of the `npm_package` rule. |
| `# gazelle:js_tsconfig _filename_` | `tsconfig.json` |
| Path to a `tsconfig.json` file used to help generate TypeScript rules.<br />This value is inherited by sub-directories and applied relative to each BUILD.<br />The `ts_project(tsconfig)` attribute is *NOT* set and must be done manually if necessary |
| `# gazelle:js_tsconfig enabled\|disabled` | `enabled` |
| Enable generation of `ts_config` rules.<br />This value is inherited by sub-directories and applied relative to each BUILD.<br />The `ts_project(tsconfig)` attribute is *NOT* set and must be done manually if necessary |
| `# gazelle:js_custom_files _name_ _glob_` | | Generate additional custom `ts_project` targets |
| `# gazelle:js_custom_test_files _name_ _glob_` | | Generate additional custom `ts_project` testonly targets |
<!-- prettier-ignore-end -->
Expand Down
1 change: 1 addition & 0 deletions gazelle/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
deps = [
"//gazelle/common/git",
"//pkg/logger",
"@bazel_gazelle//config:go_default_library",
"@bazel_gazelle//label:go_default_library",
"@bazel_gazelle//language:go_default_library",
"@bazel_gazelle//rule:go_default_library",
Expand Down
20 changes: 7 additions & 13 deletions gazelle/common/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ package gazelle
import (
"os"
"path"

"github.com/bazelbuild/bazel-gazelle/config"
)

var (
// BUILD file names.
buildFileNames = []string{"BUILD", "BUILD.bazel"}

// A set of already-seen Bazel packages so we avoid doing
// disk IO over and over to determine if a directory is a Bazel package.
isPackageCache = make(map[string]bool)
)

// IsBazelPackage determines if the directory is a Bazel package by probing for
// the existence of a known BUILD file name.
func IsBazelPackage(dir string) bool {
func IsBazelPackage(config *config.Config, dir string) bool {
return HasBUILDFile(config.ValidBuildFileNames, dir)
}

func HasBUILDFile(buildFileNames []string, dir string) bool {
if isPkg, cached := isPackageCache[dir]; cached {
return isPkg
}
Expand All @@ -31,12 +34,3 @@ func IsBazelPackage(dir string) bool {
isPackageCache[dir] = false
return false
}

func isBuildFile(filename string) bool {
for _, buildFilename := range buildFileNames {
if filename == buildFilename {
return true
}
}
return false
}
4 changes: 3 additions & 1 deletion gazelle/common/excludes.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Required for using go:linkname below for using the private isExcluded.
// https://github.com/bazelbuild/bazel-gazelle/blob/v0.28.0/walk/config.go#L54-L73
// walkConfig: https://github.com/bazelbuild/bazel-gazelle/blob/v0.37.0/walk/config.go#L41-L46
type walkConfig struct {
excludes []string
// Below are fields that are not used by the isExcluded function but match the walkConfig
Expand All @@ -19,6 +19,8 @@ type walkConfig struct {
_ sync.Once // loadOnce sync.Once
}

// isExcluded: https://github.com/bazelbuild/bazel-gazelle/blob/v0.37.0/walk/config.go#L54-L59
//
//go:linkname isExcluded github.com/bazelbuild/bazel-gazelle/walk.(*walkConfig).isExcluded
func isExcluded(wc *walkConfig, rel, base string) bool

Expand Down
2 changes: 1 addition & 1 deletion gazelle/common/git/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
deps = [
"//pkg/logger",
"@bazel_gazelle//config:go_default_library",
"@com_github_go_git_go_git//plumbing/format/gitignore",
"@com_github_go_git_go_git_v5//plumbing/format/gitignore",
],
)

Expand Down
16 changes: 5 additions & 11 deletions gazelle/common/git/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ import (

BazelLog "aspect.build/cli/pkg/logger"
"github.com/bazelbuild/bazel-gazelle/config"
gitignore "github.com/go-git/go-git/plumbing/format/gitignore"
gitignore "github.com/go-git/go-git/v5/plumbing/format/gitignore"
)

// Ignore files following .gitignore syntax for files gazelle will ignore.
var bazelIgnoreFiles = []string{".bazelignore", ".gitignore"}

// Wrap the ignore files along with the relative path they were loaded from
// to enable quick-exit checks.
type ignoreEntry struct {
Expand All @@ -34,15 +31,12 @@ func NewGitIgnore() *GitIgnore {

func (i *GitIgnore) CollectIgnoreFiles(c *config.Config, rel string) {
// Collect gitignore style ignore files in this directory.
for _, ignoreFileName := range bazelIgnoreFiles {
ignoreRelPath := path.Join(rel, ignoreFileName)
ignoreFilePath := path.Join(c.RepoRoot, ignoreRelPath)
ignoreFilePath := path.Join(c.RepoRoot, rel, ".gitignore")

if ignoreReader, ignoreErr := os.Open(ignoreFilePath); ignoreErr == nil {
BazelLog.Tracef("Add ignore file %s", ignoreRelPath)
if ignoreReader, ignoreErr := os.Open(ignoreFilePath); ignoreErr == nil {
BazelLog.Tracef("Add ignore file %s/.gitignore", rel)

i.addIgnore(rel, ignoreReader)
}
i.addIgnore(rel, ignoreReader)
}
}

Expand Down
1 change: 1 addition & 0 deletions gazelle/common/starlark/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//gazelle/common",
"//gazelle/common/starlark/stdlib",
"//pkg/logger",
"@bazel_gazelle//config:go_default_library",
"@com_github_bazelbuild_buildtools//build:go_default_library",
"@com_github_yargevad_filepathx//:filepathx",
"@net_starlark_go//lib/json",
Expand Down
2 changes: 1 addition & 1 deletion gazelle/common/starlark/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var thread = &starlark.Thread{
},
}

func Eval(starpath string, libs map[string]starlark.Value, locals map[string]interface{}) (starlark.StringDict, error) {
func Eval(starpath string, libs starlark.StringDict, locals map[string]interface{}) (starlark.StringDict, error) {
// Predeclared libs in addition to the go.starlark.net/starlark standard library:
// * https://github.com/google/starlark-go/blob/f86470692795f8abcf9f837a3c53cf031c5a3d7e/starlark/library.go#L36-L73
// * https://github.com/google/starlark-go/blob/f86470692795f8abcf9f837a3c53cf031c5a3d7e/cmd/starlark/starlark.go#L96-L100
Expand Down
3 changes: 2 additions & 1 deletion gazelle/common/starlark/glob.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

common "aspect.build/cli/gazelle/common"
BazelLog "aspect.build/cli/pkg/logger"
"github.com/bazelbuild/bazel-gazelle/config"
bzl "github.com/bazelbuild/buildtools/build"
)

Expand Down Expand Up @@ -231,7 +232,7 @@ func (pt *BazelPackageTree) AddPath(parts []string) {
if !isFile {
dir := path.Join(parts[:i+1]...)
dir = path.Join(*pt.pkg, dir)
isBazelPkg = common.IsBazelPackage(dir)
isBazelPkg = common.HasBUILDFile(config.DefaultValidBuildFileNames, dir)
}
branch = &BazelPackageTree{
pkg: pt.pkg,
Expand Down
32 changes: 25 additions & 7 deletions gazelle/common/starlark/utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,15 @@ func ReadStringTuple(l starlark.Tuple) []string {
}

func ForEachMapEntry(v starlark.Value, f func(k string, v starlark.Value)) {
for _, item := range v.(*starlark.Dict).Items() {
f(ReadString(item[0]), item[1])
d := v.(*starlark.Dict)

iter := d.Iterate()
defer iter.Done()

var k starlark.Value
for iter.Next(&k) {
v, _, _ := d.Get(k)
f(ReadString(k), v)
}
}

Expand Down Expand Up @@ -155,9 +162,14 @@ func ReadMap[K any](v starlark.Value, f func(k string, v starlark.Value) K) map[
d := v.(*starlark.Dict)
m := make(map[string]K)

for _, item := range d.Items() {
k := ReadString(item[0])
m[k] = f(k, item[1])
iter := d.Iterate()
defer iter.Done()

var kv starlark.Value
for iter.Next(&kv) {
k := ReadString(kv)
v, _, _ := d.Get(kv)
m[k] = f(k, v)
}

return m
Expand All @@ -167,8 +179,14 @@ func ReadMap2[K any](v starlark.Value, f func(v starlark.Value) K) map[string]K
d := v.(*starlark.Dict)
m := make(map[string]K)

for _, item := range d.Items() {
m[ReadString(item[0])] = f(item[1])
iter := d.Iterate()
defer iter.Done()

var kv starlark.Value
for iter.Next(&kv) {
k := ReadString(kv)
v, _, _ := d.Get(kv)
m[k] = f(v)
}

return m
Expand Down
6 changes: 3 additions & 3 deletions gazelle/common/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func GazelleWalkDir(args language.GenerateArgs, ignore *git.GitIgnore, excludes
// Source files in the primary directory
for _, f := range args.RegularFiles {
// Skip BUILD files
if isBuildFile(f) {
if args.Config.IsValidBuildFileName(f) {
continue
}

Expand Down Expand Up @@ -52,7 +52,7 @@ func GazelleWalkDir(args language.GenerateArgs, ignore *git.GitIgnore, excludes
}

// Skip BUILD files
if isBuildFile(filePath) {
if args.Config.IsValidBuildFileName(path.Base(filePath)) {
return nil
}

Expand All @@ -76,7 +76,7 @@ func GazelleWalkDir(args language.GenerateArgs, ignore *git.GitIgnore, excludes

// If visiting a directory recurse if it is not a bazel package.
if info.IsDir() {
if IsBazelPackage(filePath) {
if IsBazelPackage(args.Config, filePath) {
return filepath.SkipDir
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion gazelle/js/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (ts *typeScriptLang) GenerateRules(args language.GenerateArgs) language.Gen

// If this directory has not been declared as a bazel package only continue
// if generating new BUILD files is enabled.
if cfg.GenerationMode() == GenerationModeNone && !gazelle.IsBazelPackage(args.Dir) {
if cfg.GenerationMode() == GenerationModeNone && !gazelle.IsBazelPackage(args.Config, args.Dir) {
BazelLog.Tracef("GenerateRules '%s' BUILD creation disabled", args.Rel)
return language.GenerateResult{}
}
Expand Down
1 change: 1 addition & 0 deletions gazelle/js/tests/ignore_config_files/.test-bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subtarget/s1.ts

This file was deleted.

2 changes: 1 addition & 1 deletion gazelle/js/tests/parse_errors/BUILD.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# gazelle:js_ignore_imports mystery-module
# gazelle:js_ignore_imports unknown-library
# aspect:js_ignore_imports unknown-library
2 changes: 1 addition & 1 deletion gazelle/js/tests/parse_errors/BUILD.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@aspect_rules_ts//ts:defs.bzl", "ts_project")

# gazelle:js_ignore_imports mystery-module
# gazelle:js_ignore_imports unknown-library
# aspect:js_ignore_imports unknown-library

ts_project(
name = "parse_errors",
Expand Down
2 changes: 1 addition & 1 deletion gazelle/js/tests/pnpm_workspace/lib/no-direct/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"private": true,
"dependencies": {
"@lib/b": "workspace:*",
"typescript": "4.9.5"
"typescript": "5.4.5"
}
}
2 changes: 1 addition & 1 deletion gazelle/js/tests/pnpm_workspace/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"dependencies": {
"typescript": "4.9.5",
"typescript": "5.4.5",
"@aspect-test/a": "5.0.2"
},
"devDependencies": {
Expand Down
Loading
Loading