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

Remove cyclic dev dependency with the parser crate #11261

Merged
merged 4 commits into from
May 7, 2024

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented May 3, 2024

Summary

This PR removes the cyclic dev dependency some of the crates had with the parser crate.

The cyclic dependencies are:

  • ruff_python_ast has a dev dependency on ruff_python_parser and ruff_python_parser directly depends on ruff_python_ast
  • ruff_python_trivia has a dev dependency on ruff_python_parser and ruff_python_parser has an indirect dependency on ruff_python_trivia (ruff_python_parser - ruff_python_ast - ruff_python_trivia)

Specifically, this PR does the following:

  • Introduce two new crates
    • ruff_python_ast_integration_tests and move the tests from the ruff_python_ast crate which uses the parser in this crate
    • ruff_python_trivia_integration_tests and move the tests from the ruff_python_trivia crate which uses the parser in this crate

Motivation

The main motivation for this PR is to help development. Before this PR, rust-analyzer wouldn't provide any intellisense in the ruff_python_parser crate regarding the symbols in ruff_python_ast crate.

[ERROR][2024-05-03 13:47:06] .../vim/lsp/rpc.lua:770	"rpc"	"/Users/dhruv/.cargo/bin/rust-analyzer"	"stderr"	"[ERROR project_model::workspace] cyclic deps: ruff_python_parser(Idx::<CrateData>(50)) -> ruff_python_ast(Idx::<CrateData>(37)), alternative path: ruff_python_ast(Idx::<CrateData>(37)) -> ruff_python_parser(Idx::<CrateData>(50))\n"

Test Plan

Check the logs of rust-analyzer to not see any signs of cyclic dependency.

@dhruvmanila dhruvmanila added the internal An internal refactor or improvement label May 3, 2024
@dhruvmanila dhruvmanila marked this pull request as ready for review May 3, 2024 09:00
@dhruvmanila dhruvmanila requested a review from MichaReiser as a code owner May 3, 2024 09:00
Copy link
Contributor

github-actions bot commented May 3, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

Hmmm, I struggle with

Move all of the test cases that depends on the parser from ruff_python_ast to ruff_python_parser

The problem is that the tests are now no longer living next to the implementations. This makes them hard to discover or even knowing why a test failed (My intuition for a failing test in the parser crate is that the parser is incorrect).

@charliermarsh
Copy link
Member

Removing the cyclic dep is more important to me than co-locating the tests, so I’d still vote in favor of this.

@charliermarsh
Copy link
Member

charliermarsh commented May 3, 2024

The other alternative is to create a third crate that depends on both, and put the tests in there, but that will also bring some confusion.

@MichaReiser
Copy link
Member

Removing the cyclic dep is more important to me than co-locating the tests, so I’d still vote in favor of this.

Because of rust-analyzer? Because it isn't a cyclic dependency according to rust

  • ruff_python_ast
  • ruff_python_parser: depends on ruff_python_ast
  • ruff_python_ast (tests): depends on ruff_python_parser

@charliermarsh
Copy link
Member

Yeah, because of rust-analyzer. It’s causing real problems for people!

@MichaReiser
Copy link
Member

Yeah. A few alternative options where I don't have a strong preference:

  • Create a subfolder in the parser crate for the ast tests, e.g. tests/ast to make it clear what this tests are about
  • Create a sub-crate in the ruff_python_ast/integration_tests named ruff_python_ast_integration_tests and add it to the workspace members in the root Cargo.toml

I'm slightly preferring the second option. I don't know if @BurntSushi has any other recommendation on how we could restructure our project to avoid the rust-analyzer cyclic dependency.

@BurntSushi
Copy link
Member

It seems like this is the rust-analyzer issue: rust-lang/rust-analyzer#3390

I guess it's a problem because rust-analyzer just always enables cfg(test). Which makes sense. That's what you want. But when the test library introduces a circular dependency, that turns into a true circular dependency if cfg(test) is always unconditionally enabled.

I don't really know another way to disable it. I ran into this problem too, but didn't diagnose the root cause. I'd definitely vote in favor of fixing this somehow. It'd be nice if RA could figure out how to make this work though, because this is a legitimate pattern in the ecosystem.

@charliermarsh
Copy link
Member

Yeah, weak vote for making a separate crate. (Personally fine with it being at crates/ruff_python_ast_integration_tests for simplicity.)

@dhruvmanila dhruvmanila force-pushed the dhruv/resolve-ast-parser-dev-cycle-dep branch from be7a813 to ecc14aa Compare May 6, 2024 10:43
@dhruvmanila
Copy link
Member Author

I've updated the PR description to reflect the recent changes. Thank you all for the recommendations.

@dhruvmanila dhruvmanila requested a review from charliermarsh May 7, 2024 03:31
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Thanks!

You may want to add a readme to both crates shortly explaining why they exist (link to the rust analyzer issue). It could prevent that someone undoes the change in the future

@dhruvmanila dhruvmanila enabled auto-merge (squash) May 7, 2024 08:55
@dhruvmanila dhruvmanila force-pushed the dhruv/resolve-ast-parser-dev-cycle-dep branch from b40cbdd to f1dcdab Compare May 7, 2024 09:15
@dhruvmanila dhruvmanila force-pushed the dhruv/resolve-ast-parser-dev-cycle-dep branch from f1dcdab to 13e2e66 Compare May 7, 2024 09:16
@dhruvmanila dhruvmanila merged commit 28cc71f into main May 7, 2024
19 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/resolve-ast-parser-dev-cycle-dep branch May 7, 2024 09:24
@Veykril
Copy link

Veykril commented May 8, 2024

It seems like this is the rust-analyzer issue: rust-lang/rust-analyzer#3390

Stumbled upon this just now, there is an issue on the rust-analyzer repo with some more info about this than the one you linked here rust-lang/rust-analyzer#14167

dhruvmanila added a commit that referenced this pull request Jun 12, 2024
…11844)

## Summary

This PR re-uses the `ruff_python_trivia::is_python_whitespace` in the
lexer instead of defining its own. This was mainly to avoid circular
dependency which was resolved in #11261.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants