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

feat(gazelle): Update to latest version of go-tree-sitter #2413

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

maffoo
Copy link

@maffoo maffoo commented Nov 15, 2024

The latest version of go-tree-sitter includes grammer changes for python 3.12. To use it, we need to patch the build files generated by the go_repository to support building with uplevel files references. This should fix #2396.

@maffoo maffoo changed the title feat: Update to latest version of go-tree-sitter feat(gazelle): Update to latest version of go-tree-sitter Nov 15, 2024
Copy link
Contributor

@dougthor42 dougthor42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... still seems to not work on our pyle repo. I'll have more time to investigate early next week.

Please add a test case for python 3.12 syntax to gazelle/python/testdata. Specifically, we want to make sure that imports, gazelle annotations, and if __name__ that come after py3.12-only syntax are still parsed. For example:

# my_baz.py
import foo
# gazelle:include_dep //:bar    

def baz[T]() -> T:  # 3.12-only syntax
    pass

# these still get parsed
import foobar
# gazelle:include_dep //:hello

if __name__ == "__main__":
    pass

Should generate (assuming per-file target):

py_binary(
    name = "my_baz",
    srcs = ["my_baz.py"],
    deps = [
        "//:bar",
        "//:foo",
        "//:foobar",
        "//:hello",
    ],
)

If you only get 2 deps or a py_library, then the parsing still failed 😢

CHANGELOG.md Outdated
@@ -52,7 +52,7 @@ Unreleased changes template.

{#v0-0-0-changed}
### Changed
* Nothing yet.
* (gazelle): Update to latest version of go-tree-sitter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include that this adds support for python 3.12 grammar (and 3.13?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: please move to a "patches" directory

@hunshcn
Copy link
Contributor

hunshcn commented Nov 16, 2024

maybe we can publish go-tree-sitter with patches to bazel registry? like https://registry.bazel.build/modules/circl

github-merge-queue bot pushed a commit that referenced this pull request Nov 18, 2024
While investigating #2396 and why #2413 doesn't appear to be working for
us, I realized that one of the things I was making heavy use of was
additional parser logging that I had added. This adds some of that
logging. I also throw in some documentation because I found it helpful.

Users can (attempt to) get additional parse failure information by
setting the `GAZELLE_VERBOSE` environment variable to `1`. Eg:

```console
$ GAZELLE_VERBOSE=1 bazel run //:gazelle
```

Here are some example logs:

```console
$ GAZELLE_VERBOSE=1 bazel run //:gazelle
INFO: Invocation ID: a4e026d8-17df-426c-b1cc-d3980690dd53
...
INFO: Running command line: bazel-bin/gazelle
INFO: Streaming build results to: https://btx.cloud.google.com/invocations/a4e026d8-17df-426c-b1cc-d3980690dd53
gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect.
gazelle: Parse error at {Row:1 Column:0}:
def search_one_more_level[T]():
gazelle: The above was parsed as: (ERROR (identifier) (call function: (list (identifier)) arguments: (argument_list)))
gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention.
$
$ bazel run //:gazelle
INFO: Invocation ID: 726c9fd6-f566-4c30-95ef-c4781ad155de
...
INFO: Running command line: bazel-bin/gazelle
INFO: Streaming build results to: https://btx.cloud.google.com/invocations/726c9fd6-f566-4c30-95ef-c4781ad155de
gazelle: WARNING: failed to parse "hello/get_deps.py". The resulting BUILD target may be incorrect.
gazelle: ERROR: failed to generate target "//hello:get_deps" of kind "py_library": a target of kind "pyle_py_binary" with the same name already exists. Use the '# gazelle:python_library_naming_convention' directive to change the naming convention.
```

---------

Co-authored-by: Richard Levasseur <[email protected]>
Co-authored-by: Richard Levasseur <[email protected]>
Include a patch to the build files generated by the go_repository to
support building with uplevel files references.
@maffoo
Copy link
Author

maffoo commented Nov 20, 2024

I think I got things working now. Had to remove go-tree-sitter from go.mod because this seems to override the custom go_respository definition and results in patches not being applied to go-tree-sitter before building. I'm not sure whether we still need go.mod for other dependencies or if they can be handled by the go_repository definitions in deps.bzl too; experts please weight in.

I also moved the patch file into a patches subdirectory and added a test case exercising the py3.12 syntax that fails on main but succeeds on this branch. I tried to create a minimal failing test case based on our internal code; interestingly, I found that the function docstring is key to getting this to fail on main, no idea why that is the case...

@dougthor42
Copy link
Contributor

Just relaying info that @maffoo and I had at work:

This still doesn't work for pyle:

$ bazel run //:gazelle src/pyle/tools
INFO: Invocation ID: e06d2fbb-5642-4159-9abf-f1402367d298
INFO: Streaming build results to: https://btx.cloud.google.com/invocations/e06d2fbb-5642-4159-9abf-f1402367d298
WARNING: For repository 'bazel_skylib', the root module requires module version [email protected], but got [email protected] in the resolved dependency graph.
ERROR: no such package '@@[unknown repo 'com_github_smacker_go_tree_sitter' requested from @@rules_python_gazelle_plugin~]//python': The repository '@@[unknown repo 'com_github_smacker_go_tree_sitter' requested from @@rules_python_gazelle_plugin~]' could not be resolved: No repository visible as '@com_github_smacker_go_tree_sitter' from repository '@@rules_python_gazelle_plugin~'
ERROR: /usr/local/google/home/dthor/.cache/bazel/_bazel_dthor/dbe74c4144b5c9a438d84a119652bef9/external/rules_python_gazelle_plugin~/python/BUILD.bazel:6:11: no such package '@@[unknown repo 'com_github_smacker_go_tree_sitter' requested from @@rules_python_gazelle_plugin~]//python': The repository '@@[unknown repo 'com_github_smacker_go_tree_sitter' requested from @@rules_python_gazelle_plugin~]' could not be resolved: No repository visible as '@com_github_smacker_go_tree_sitter' from repository '@@rules_python_gazelle_plugin~' and referenced by '@@rules_python_gazelle_plugin~//python:python'
ERROR: Analysis of target '//:gazelle' failed; build aborted: Analysis failed
INFO: Elapsed time: 1.437s, Critical Path: 0.51s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

Here's something weird. If I run bazel sync in my rules_pyle/gazelle directory it applies patches to go-tree-sitter:

DEBUG: /usr/local/google/home/maffoo/.cache/bazel/_bazel_maffoo/051c217dd68266c8e1eaa3206da93fe4/external/gazelle~/internal/go_repository.bzl:345:10: bazel_gazelle/go_repository: applying patches. name=com_github_smacker_go_tree_sitter, patches=[Label("//:patches/0001-go-tree-sitter-build.patch")]format_text_overflowformat_text_wrap

but whenever I try to run some other build command I see patches=[], no patches are applied, and the build fails.

I think this is the fundamental problem. The go_repository definitions are only having an effect in the WORKSPACE. The MODULE.bazel uses a different mechanism with the go_deps extension and this doesn't know about the patches we want to apply. There's a way to specify patches for repositories brought in with go_deps but it only works in the root module so we can't use it (it's fine if running bazel in rules_python/gazelle, but when we pull this in as a dependency to pyle the patches get ignored again. The only way I can see to fix this is to update bazel-gazelle itself or add go-tree-sitter to the bazel central registry.


maybe we can publish go-tree-sitter with patches to bazel registry?

It's looking like we might have to do that. We can apply patches like @maffoo has done, but then those patches won't be used when some other project uses rules_python_gazelle_plugin because things like go_deps.module_override and will only work on the root module.

@hunshcn How do you recommend we publish to BCR? Fork go-tree-sitter, add MODULE.bazel and rules, and go from there? Or is there another option?

@hunshcn
Copy link
Contributor

hunshcn commented Nov 21, 2024

Just like bazelbuild/bazel-central-registry#674
Maybe @fmeum can provide some information about.

@dougthor42
Copy link
Contributor

Ah thanks, that process looks pretty easy. I'll start it now.

@maffoo
Copy link
Author

maffoo commented Nov 22, 2024

I filed bazel-contrib/bazel-gazelle#1984 to propose allowing default patches on libraries included by go_deps and put up bazel-contrib/bazel-gazelle#1985 to actually implement this. We'll see what the bazel-gazelle maintainers say...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gazelle seems to not like PEP 695 – Type Parameter Syntax
3 participants