Skip to content

fix: allow new, subsequent rust-project.json-based workspaces to get proc macro expansion #14427

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

Conversation

davidbarsky
Copy link
Contributor

As detailed in #14417 (comment), rust-project.json workspaces added after the initial rust-project.json-based workspace was already indexed by rust-analyzer would not receive procedural macro expansion despite config.expand_proc_macros returning true. To fix this issue:

  1. I changed reload.rs to check which workspaces are newly added.
  2. Spawned new procedural macro expansion servers based on the new workspaces.
    1. This is to prevent spawning duplicate procedural macro expansion servers for already existing workspaces. While the overall memory usage of duplicate procedural macro servers is minimal, this is more about the principle of not leaking processes 😅.
  3. Launched procedural macro expansion if any workspaces are rust-project.json-based or same_workspaces is true. same_workspaces being true (and reachable) indicates that that build scripts have finished building (in Cargo-based projects), while the build scripts in rust-project.json-based projects have already been built by the build system that produced the rust-project.json.

I couldn't really think of structuring this code in a better way without engaging with #7444.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 28, 2023
@davidbarsky
Copy link
Contributor Author

Spawned new procedural macro expansion servers based on the new workspaces.

Correction: this is incorrect; I've reverted my changes for this.

// workspaces to remain in the low single digits. the `cloned_workspace` is needed for borrowck
// reasons.
let cloned_workspaces = workspaces.clone();
let different_workspaces = cloned_workspaces
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 can undo these changes and go back to the previous approach, since the reason I even did this proved to be wrong 😅

Copy link
Member

Choose a reason for hiding this comment

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

We don't even need the checks for whether the new workspaces are rust-project.json ones or not, we need respawn the proc-macro servers in general when !same_workspaces. (Think of when someone adds a new top level cargo project)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, good point. Didn't consider that; will add!

// workspaces to remain in the low single digits. the `cloned_workspace` is needed for borrowck
// reasons.
let cloned_workspaces = workspaces.clone();
let different_workspaces = cloned_workspaces
Copy link
Member

Choose a reason for hiding this comment

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

We don't even need the checks for whether the new workspaces are rust-project.json ones or not, we need respawn the proc-macro servers in general when !same_workspaces. (Think of when someone adds a new top level cargo project)

//
// The else branch is used to provide better diagnostics to users while procedural macros
// are still being built.
if same_workspaces || different_workspaces.iter().any(|ws| ws.is_json()) {
Copy link
Member

Choose a reason for hiding this comment

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

This also still doesn't fix the case where build scripts are disabled and rust-project.json are used without loading up workspaces after the start. I think I overcomplicated this part here, we should just replace this entire block of lines (477-490) with

if self.config.expand_proc_macros() {
    self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
}

And set up the proc-macros errors when building up the crate graph, that is change

pub type ProcMacroPaths = FxHashMap<CrateId, Option<(Option<String>, AbsPathBuf)>>;

to

 pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>; 

and fix up where we set those up.

Copy link
Member

Choose a reason for hiding this comment

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

Currently we signal with Option::None that proc-macros haven't been build but this overcomplicates things by pushing the error generation to a later place, where we don't really have the information anymore.

@davidbarsky
Copy link
Contributor Author

I think I addressed your comments. Thanks for your help (and I'm sorry that most of my changes are to this part of the codebase!

@Veykril
Copy link
Member

Veykril commented Mar 30, 2023

and I'm sorry that most of my changes are to this part of the codebase!

That is completely fine! By now I know this part of the codebase pretty well :)

Changes look good to me 👍
@bors r+

@bors
Copy link
Contributor

bors commented Mar 30, 2023

📌 Commit 25c59b8 has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Mar 30, 2023

⌛ Testing commit 25c59b8 with merge b46a5f6...

bors added a commit that referenced this pull request Mar 30, 2023
…paces-to-have-proc-macros, r=Veykril

fix: allow new, subsequent `rust-project.json`-based workspaces to get proc macro expansion

As detailed in #14417 (comment), `rust-project.json` workspaces added after the initial `rust-project.json`-based workspace was already indexed by rust-analyzer would not receive procedural macro expansion despite `config.expand_proc_macros` returning true. To fix this issue:
1. I changed `reload.rs` to check which workspaces are newly added.
2. Spawned new procedural macro expansion servers based on the _new_ workspaces.
    1. This is to prevent spawning duplicate procedural macro expansion servers for already existing workspaces. While the overall memory usage of duplicate procedural macro servers is minimal, this is more about the _principle_ of not leaking processes 😅.
3. Launched procedural macro expansion if any workspaces are `rust-project.json`-based _or_ `same_workspaces` is true. `same_workspaces` being true (and reachable) indicates that that build scripts have finished building (in Cargo-based projects), while the build scripts in `rust-project.json`-based projects have _already been built_ by the build system that produced the `rust-project.json`.

I couldn't really think of structuring this code in a better way without engaging with #7444.
@bors
Copy link
Contributor

bors commented Mar 30, 2023

💔 Test failed - checks-actions

@Veykril
Copy link
Member

Veykril commented Mar 30, 2023

@bors retry

@bors
Copy link
Contributor

bors commented Mar 30, 2023

⌛ Testing commit 25c59b8 with merge b915eb3...

@bors
Copy link
Contributor

bors commented Mar 30, 2023

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing b915eb3 to master...

1 similar comment
@bors
Copy link
Contributor

bors commented Mar 30, 2023

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing b915eb3 to master...

@bors
Copy link
Contributor

bors commented Mar 30, 2023

👀 Test was successful, but fast-forwarding failed: 422 Changes must be made through a pull request.

@bors bors merged commit b915eb3 into rust-lang:master Mar 30, 2023
@davidbarsky davidbarsky deleted the davidbarsky/allow-subsequent-workspaces-to-have-proc-macros branch March 30, 2023 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants