atomic-server icon indicating copy to clipboard operation
atomic-server copied to clipboard

Slow build.rs / rust analyzer

Open joepio opened this issue 1 year ago • 3 comments

Running build.rs has some steps that are sometimes unnecessarily slow. This is particularly annoying because rust-analyzer seems to be slowed down due to this.

  • We could solve this by not running all code in rust analzer flow (not sure how)
  • Or we can identify the most time-consuming part and remove that somehow

joepio avatar Jul 22 '24 10:07 joepio

Skipping the check in build.rs works, lowers refresh time from 7 seconds to about 2 seconds.

    // Check if we're likely running in a check-like context
    let opt_level = std::env::var("OPT_LEVEL").unwrap_or_else(|_| "0".to_string());
    let profile = std::env::var("PROFILE").unwrap_or_else(|_| "release".to_string());

    let is_check_like = profile == "debug" && opt_level == "0";

    if is_check_like {
        println!("cargo:rerun-if-changed=build.rs");
        // Skip the heavy logic
        println!("Skipping build.rs logic for cargo check/clippy.");
    } else {
        const BROWSER_ROOT: &str = "../browser/";

EDIT: No, this does not a good solutions, it too often maps to true (e.g. when running cargo run the first time and there are no assets). I still need a different solution.

joepio avatar Jul 22 '24 11:07 joepio

New solution:

    println!("cargo:rerun-if-changed=build.rs");

    // Env is set in .vscode/settings.json when rust-analyzer runs
    let is_rust_analyzer = !std::env::var("IS_RUST_ANALYZER")
        .unwrap_or_default()
        .is_empty();

    if is_rust_analyzer {
        p!("Skipping build.rs logic to keep cargo check/clippy fast. If you see this message in some other context: the JS build not run!");
    } else {
        const BROWSER_ROOT: &str = "../browser/";

joepio avatar Aug 23 '24 11:08 joepio

These settings could help

Image Image

We could add an option to the build command to skip building the frontend. Or if that doesn't work we could use an environment variable

Polleps avatar Jan 30 '25 15:01 Polleps

Commit 7d114dc fixes this issue but you will need to configure your rust-analyzer to set ATOMICSERVER_SKIP_JS_BUILD=true when it performs cargo tasks.

I've added this to the .vscode/settings.json so you don't need to configure it yourself (when using vscode). However if you are using a multi-root workspace in vscode this setting will not work unless you add it to your own settings.json. Add the following to your settings.json:

"rust-analyzer.cargo.extraEnv": {
    "ATOMICSERVER_SKIP_JS_BUILD": "true"
  }

In the future we should look into using a rust-analyzer.toml file to configure this as it would apply to all editors. I know it's possible but I couldn't find any documentation about it.

Polleps avatar Jul 24 '25 07:07 Polleps