tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
85.53k stars 2.58k forks source link

[bug] Tauri unilaterally overrides MACOSX_DEPLOYMENT_TARGET, causes sys crates to spuriously rebuild #11577

Open kornelski opened 3 weeks ago

kornelski commented 3 weeks ago

Describe the bug

On macOS, there's a special env var MACOSX_DEPLOYMENT_TARGET that implicitly configures the system's C compiler, linker, and opts in to or out of breaking changes in macOS. Objects built for different deployment targets may be incompatible. The cc crate knows this and emits cargo:rerun-if-env-changed=MACOSX_DEPLOYMENT_TARGET, which invalidates cache of sys crates whenever cargo build sees a different value of this variable.

Unfortunately, something in Tauri is trying to override MACOSX_DEPLOYMENT_TARGET to 10.13.

This causes two bugs:

  1. Even if I set MACOSX_DEPLOYMENT_TARGET env var to a different value, the project is built for 10.13 anyway.

  2. The override causes cargo to invalidate cache of sys crates, and rebuild objc-sys and all of wry, tauri, and its plugins, every time cargo tauri dev sees any file change!

    Dirty objc-sys v0.3.5: the env variable MACOSX_DEPLOYMENT_TARGET changed

    I'm not sure where Cargo is getting another value from. I suspect that rust-analyzer is building the project in a way that doesn't support Tauri's way of overriding MACOSX_DEPLOYMENT_TARGET, or perhaps there's just a race condition in Cargo that compares an unset value from before the build start with a value set during the build.

    The end result is that cargo tauri dev is painfully slow.

Reproduction

To reproduce, modify src/main.rs to add:

compile_error!(env!("MACOSX_DEPLOYMENT_TARGET"));

and run:

unset MACOSX_DEPLOYMENT_TARGET
cargo tauri dev

The expected result:

error: environment variable `MACOSX_DEPLOYMENT_TARGET` not defined at compile time

The actual result:

error: 10.13

Second test:

export MACOSX_DEPLOYMENT_TARGET=11.0
cargo tauri dev

The expected result:

error: 11.0

The actual result:

error: 10.13

Expected behavior

No response

Full tauri info output

[✔] Environment
    - OS: Mac OS 15.1.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.82.0 (f6e511eec 2024-10-15)
    ✔ cargo: 1.82.0 (8f40fc59f 2024-08-21)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (environment override by RUSTUP_TOOLCHAIN)
    - node: 23.1.0
    - yarn: 1.22.22
    - npm: 10.9.0

[-] Packages
    - tauri 🦀: 2.0.6
    - tauri-build 🦀: 2.0.2
    - wry 🦀: 0.46.3
    - tao 🦀: 0.30.5
    - tauri-cli 🦀: 2.0.4

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.2

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../src

Stack trace

No response

Additional context

No response

FabianLars commented 3 weeks ago

I suspect that rust-analyzer is building the project in a way that doesn't support Tauri's way of overriding MACOSX_DEPLOYMENT_TARGET

That sounds very likely because tauri overwrites it in the tauri cli which rust-analyzer (or cargo in general) doesn't know about.

you're right that tauri ignores existing env vars but you can change the target version it uses via https://tauri.app/reference/config/#minimumsystemversion-1

amrbashir commented 3 weeks ago

Setting that field to null should also disable the default 10.13 and would also disable tauri-cli setting it to 10.13 then you'd be free to use MACOSX_DEPLOYMENT_TARGET to control the target.

However I wouldn't recommended that, since it will remove LSMinimumSystemVersion in your Info.plist so it is better to just change the field in tauri.conf.json to match your MACOSX_DEPLOYMENT_TARGET env variable.

I will mark this issue for v3 since we can't break the default behavior.

laduke commented 2 weeks ago

sorry I couldn't figure it out from reading, is there quick work around so i can start npm run tauri dev and it be fast again? I don't have a specific MACOSX_DEPLOYMENT_TARGET requirement yet.

edit: Disabling rust-analyzer in the editor helps. It looks like the two rust-analyzer processes are using slightly different envs? first the editor does cargo check on all the deps while tauri dev is "Blocking waiting for file lock" then tauri dev compiles everything again.

edit:

I set the same version in:

and it seems to be better for now

vscode settings.json

{
    "rust-analyzer.cargo.extraEnv": {
        "MACOSX_DEPLOYMENT_TARGET": "14.6"
    }
}

tauri.conf.json

 "macOS": {

      "minimumSystemVersion": "14.6"
    }