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

Use ruff-python-parser to compute Python imports #3103

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

Conversation

hoodmane
Copy link
Contributor

@hoodmane hoodmane commented Nov 13, 2024

The goal is to use ruff to replace Dominik's ad-hoc parser. This just adds the
new code, I'll switch to using it and remove the old code in a followup.

@hoodmane
Copy link
Contributor Author

@mikea when I try to run this with:

bazel test src/rust/python-parser:import_parsing

I get:

In file included from external/crates_vendor__cxx-1.0.129/src/cxx.cc:1:
external/crates_vendor__cxx-1.0.129/src/../include/cxx.h:2:10: fatal error: 'algorithm' file not found
    2 | #include <algorithm>
      |          ^~~~~~~~~~~
1 error generated.
ERROR: /home/rchatham/.cache/bazel/_bazel_rchatham/494cfed3ffd15c51a9fc504927f340c6/external/capnp-cpp/src/kj/BUILD.bazel:5:11: Compiling src/kj/encoding.c++ failed: (Exit 1): clang failed: error executing CppCompile command (from target @@capnp-cpp//src/kj:kj) 

any ideas what I'm doing wrong?

@hoodmane
Copy link
Contributor Author

I get the same error when I try to run bazel test src/rust/cxx-integration-test:cxx-rust-integration-test hmm.

@mikea
Copy link
Collaborator

mikea commented Nov 13, 2024

CI has a different error. You need this:

$ git diff
diff --git a/src/rust/python-parser/import_parsing.c++ b/src/rust/python-parser/import_parsing.c++
index 83b1a5cd..8d89f58b 100644
--- a/src/rust/python-parser/import_parsing.c++
+++ b/src/rust/python-parser/import_parsing.c++
@@ -2,7 +2,7 @@
 // Licensed under the Apache 2.0 license found in the LICENSE file or at:
 //     https://opensource.org/licenses/Apache-2.0
 
-#include <rust/python-parser/lib.rs.h>
+#include <workerd/rust/python-parser/lib.rs.h>
 
 #include <kj/test.h>

Workerd rust includes are prefixed with "workerd/" to disambiguate with edgeworker rust folders.

@mikea
Copy link
Collaborator

mikea commented Nov 13, 2024

ERROR: /home/rchatham/.cache/bazel/_bazel_rchatham/494cfed3ffd15c51a9fc504927f340c6/external/capnp-cpp/src/kj/BUILD.bazel:5:11: Compiling src/kj/encoding.c++ failed: (Exit 1): clang failed: error executing CppCompile command (from target @@capnp-cpp//src/kj:kj) 

something is broken for you since it can't even build kj:kj target (which has nothing to do with rust). I assume your environment is broken? This happens to me with workerd everytime I touch anything on my system.

@hoodmane
Copy link
Contributor Author

Thanks @mikea! I got it to work by running it from edgeworker, and get the same path error. After fixing that it seems to works correctly.

@hoodmane hoodmane force-pushed the hoodmane/ruff-parser branch from 2a6a506 to 7ada9d9 Compare November 13, 2024 16:29
@hoodmane hoodmane marked this pull request as ready for review November 13, 2024 16:30
@hoodmane hoodmane requested review from a team as code owners November 13, 2024 16:30
@hoodmane hoodmane requested review from mikea and vickykont November 13, 2024 16:30
@hoodmane hoodmane force-pushed the hoodmane/ruff-parser branch from 7ada9d9 to 51afa67 Compare November 13, 2024 16:33

#[cxx::bridge(namespace = "edgeworker::rust::python_parser")]
mod ffi {

Copy link
Member

Choose a reason for hiding this comment

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

I don't think we have formatting enabled for Rust files. Can you run just rustfmt to see if any formatting changes are being made? If yes, we need to fix this (in a separate PR)

Copy link
Collaborator

Choose a reason for hiding this comment

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

we do and --config=lint should be checking it. Let me know if this is not the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ran this and don't see any changes. Maybe it's already set up correctly?

Copy link
Collaborator

@mikea mikea left a comment

Choose a reason for hiding this comment

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

Awesome to see rust usage!

Just a lot of nits, sorry.

@@ -21,6 +21,8 @@ PACKAGES = {
"pico-args": crate.spec(version = "0"),
"proc-macro2": crate.spec(version = "1"),
"quote": crate.spec(version = "1"),
"ruff_python_ast": crate.spec(git = "https://github.com/astral-sh/ruff.git", tag = "0.7.0"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

any idea why the don't publish on crates.io ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My guess is that they have a big monorepo and don't really care about supporting downstream users of the crates only of the CLI tools they are used to define.

@@ -21,6 +21,8 @@ PACKAGES = {
"pico-args": crate.spec(version = "0"),
"proc-macro2": crate.spec(version = "1"),
"quote": crate.spec(version = "1"),
"ruff_python_ast": crate.spec(git = "https://github.com/astral-sh/ruff.git", tag = "0.7.0"),
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will need downstream changes to add these crates too.

deps/rust/cargo.bzl Outdated Show resolved Hide resolved

#[cxx::bridge(namespace = "edgeworker::rust::python_parser")]
mod ffi {

Copy link
Collaborator

Choose a reason for hiding this comment

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

we do and --config=lint should be checking it. Let me know if this is not the case.

mod ffi {

extern "Rust" {
fn get_imports(sources: &Vec<String>) -> Vec<String>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's add some /// comments here. They will be visible from c++ code too.
Don't forget to document sorting behavior.

src/rust/python-parser/lib.rs Outdated Show resolved Hide resolved
src/rust/python-parser/lib.rs Outdated Show resolved Hide resolved
src/rust/python-parser/import_parsing.c++ Outdated Show resolved Hide resolved
src/rust/python-parser/import_parsing.c++ Outdated Show resolved Hide resolved
src/rust/python-parser/import_parsing.c++ Outdated Show resolved Hide resolved
@mikea
Copy link
Collaborator

mikea commented Nov 13, 2024

ouch. Windows as usual. The name doesn't seem to be really long, is windows limit so low??? Can we raise it?

error: unable to create file crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_docstring_doubles_over_docstring_singles_mixed_quotes_module_singleline_var_1.py.snap: Filename too long
error: unable to create file crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_docstring_doubles_over_docstring_singles_mixed_quotes_module_singleline_var_2.py.snap: Filename too long
error: unable to create file crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_docstring_singles_over_docstring_doubles_mixed_quotes_module_singleline_var_1.py.snap: Filename too long
error: unable to create file crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_docstring_singles_over_docstring_doubles_mixed_quotes_module_singleline_var_2.py.snap: Filename too long

@anonrig
Copy link
Member

anonrig commented Nov 13, 2024

@mikea we need to add a prefix for UNC paths to avoid such errors.

Using "\\? \" permits a maximum path length of approximately 32,000 characters composed of components up to 255 characters in length. To specify a UNC path, use the "\\? \UNC\" prefix.

@mikea
Copy link
Collaborator

mikea commented Nov 13, 2024

@mikea we need to add a prefix for UNC paths to avoid such errors.

Using "\\? \" permits a maximum path length of approximately 32,000 characters composed of components up to 255 characters in length. To specify a UNC path, use the "\\? \UNC\" prefix.

we can try doing it for C:\tmp that we set for bazel root

@hoodmane hoodmane force-pushed the hoodmane/ruff-parser branch 3 times, most recently from 6c100fc to 225d6dc Compare November 13, 2024 20:59
@hoodmane hoodmane force-pushed the hoodmane/ruff-parser branch from 225d6dc to ee358fb Compare December 18, 2024 11:27
The goal is to use ruff to replace Dominik's ad-hoc parser. This just adds the
new code, I'll switch to using it and remove the old code in a followup.
@hoodmane hoodmane force-pushed the hoodmane/ruff-parser branch from ee358fb to cf7021d Compare December 18, 2024 14:54
@hoodmane hoodmane requested a review from a team as a code owner December 18, 2024 20:28
@hoodmane
Copy link
Contributor Author

Well I tried --output_user_root=\\?\C:\\tmp but it doesn't seem to work? Maybe there needs to be some sort of quoting?

@danlapid
Copy link
Collaborator

danlapid commented Dec 19, 2024

Well I tried --output_user_root=\\?\C:\\tmp but it doesn't seem to work? Maybe there needs to be some sort of quoting?

I think it should be "\\\\?\\C:\\tmp"

@hoodmane
Copy link
Contributor Author

Using \\\?\\C:\\tmp seems to work but it just pushes the error back a bit. Now there are a ton of errors like:

error: unable to create file crates/ruff_linter/src/rules/flake8_quotes/snapshots/ruff_linter__rules__flake8_quotes__tests__require_docstring_doubles_over_docstring_singles_mixed_quotes_module_singleline_var_1.py.snap: Filename too long

@hoodmane
Copy link
Contributor Author

All the too long paths have __tests__ in them, maybe we can filter those out while cloning...

@@ -49,6 +49,14 @@ inline kj::ArrayPtr<const char> fromRust(const ::rust::Str& str) {
return kj::ArrayPtr<const char>(str.data(), str.size());
}

inline kj::Array<kj::String> fromRust(::rust::Vec<::rust::String> vec) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This copies 😢

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.

4 participants