tauri-apps / tauri

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

[feat] Prevent error when distDir doesn't exist #3142

Closed probablykasper closed 2 years ago

probablykasper commented 2 years ago

Describe the problem

If you have a build folder that doesn't exist, you expect this error from your linter as well as when running cargo check in a GitHub action:

The `distDir` configuration is set to `"../build"` but this path doesn't exist

Describe the solution you'd like

If distDir doesn't exist, treat it like as an empty directory, but only if an environment variable like TAURI_STRICT_PATHS is not true. When tauri build runs, it should enable TAURI_STRICT_PATHS.

The disadvantage is that running cargo build directly will not panic if distDir doesn't exist.

amrbashir commented 2 years ago

what is the advantage of this? seems like creating an empty directory is better and easier rather than implementing this solution.

probablykasper commented 2 years ago

Pretty much every user will run into this error when creating a Tauri project, and when creating an action that runs cargo check, then they'll have to figure out why it's happening and learn the workaround. It's a confusing/annoying error because your code works perfectly, but you're still getting a linting/checking error.

I'd love to not see the error anymore, and avoid the extra stuff required to deal with it in actions even though it's not much.

amrbashir commented 2 years ago

The way I see this, is that cargo check should fail if the distDir doesn't exist and new tauri projects or new tauri users should be aware of that, because if distDir doesn't exist then your workflow is faulty and probably missed a step to build your front-end but if you don't want to build your front-end then just create an empty dir. It is far less work than you expect and better than implementing an env var.

lucasfernog commented 2 years ago

I agree with Amr here, the distDir is part of the application so it would be weird to handle things this way. We can't get back to using env vars, it's too annoying for Rust developers.

probablykasper commented 2 years ago

@amrbashir @lucasfernog To be clear tauri build would still panic if distDir doesn't exist. Is it actually common enough to run cargo build directly for the final build?

lucasfernog commented 2 years ago

Yeah pure Rust users (wasm etc) just go with cargo run instead of tauri dev.

AlexTMjugador commented 1 year ago

I agree with Amr here, the distDir is part of the application so it would be weird to handle things this way.

It's also weird to run an apparently innocuous command such as cargo clippy on CI and have it fail because a directory that is not supposed to be checked in to the tree is indeed not checked in. And the available workarounds are not better:

I'm not familiar enough with the Tauri build lifecycle to know what would be the best course of action here, but I think that the current behavior is far from ideal, because it makes perfectly valid and fine workflows clunkier for no good reason.

Isn't it possible to delay that existence check until an attempt to actually embed the assets is made? That sounds like a better idea to me, although I don't know how feasible it is.

dscham commented 1 year ago

@AlexTMjugador I'll add: It's weird when new users get an error after following the Getting Started page (SvelteKit). Please re-open.

FabianLars commented 1 year ago

It shouldn't be as common anymore since we removed the default features from the templates here: https://github.com/tauri-apps/tauri/pull/6074 (not yet released).

Effectively this means that tauri is in dev mode by default, therefore it looks at the devPath instead of distDir.

dscham commented 1 year ago

@FabianLars I see, so, terribly simplified i assume, the problem is that IntelliJ runs Cargo checks without tauri, ofc, and Cargo alone doesn't know what tauri will do?

FabianLars commented 1 year ago

I would not call this terribly simplified. It's pretty much exactly what's happening. To expand a bit on what's happening, tauri's cli will invoke cargo like so: tauri dev -> cargo build --no-default-features (devPath) tauri build -> cargo build --features custom-protocol (distDir)

intellij, rust-analyzer, cargo check in cli all by default do just cargo build/check without saying anything about the features. And because before 6074, custom-protocol was a default feature, and therefore always enabled unless explicitly disabled, the IDEs threw errors because they basically did the equivalent of tauri build without building your frontend first (commonly set as beforeBuildCommand in tauri.conf which also only get's executed by tauri's cli, not cargo itself)

Rizary commented 11 months ago

I'm still getting the error. My config is:

    "devPath": "http://localhost:1420/",
    "distDir": "../dist",

and it looks the distDir which is a panic error. Do I need to tell every user to create ../dist everytime they try running my app? I only run tauri dev

can I also run cargo run inside the src-tauri?

FabianLars commented 11 months ago

@Rizary Remove this line (and this line only!): https://github.com/ARK-Builders/ARK-Shelf-Desktop/blob/8cde8defefe804384fcfd6eaa714d4faa4067a35/src-tauri/Cargo.toml#L29 - this should prevent the error.

can I also run cargo run inside the src-tauri?

Yes. But then you have to run the beforeDevCommand yourself)

bw-itis-archival commented 11 months ago

@FabianLars Does removing that line prevent using custom protocols? I'm new to Tauri so I'm still working out the details, but it does seem odd that when running in dev mode, we get an error if the production build dir isn't present?

FabianLars commented 11 months ago

No it doesn't. The name is quite misleading nowadays but it simply tells Tauri whether it should serve devPath or distDir - distDir uses a custom protocol (since before generic custom protocols were really a thing), and devPath doesn't (it still does if you disable the dev server for added confusion) hence the name.

Rizary commented 11 months ago

@Rizary Remove this line (and this line only!): https://github.com/ARK-Builders/ARK-Shelf-Desktop/blob/8cde8defefe804384fcfd6eaa714d4faa4067a35/src-tauri/Cargo.toml#L29 - this should prevent the error.

can I also run cargo run inside the src-tauri?

Yes. But then you have to run the beforeDevCommand yourself)

I still get the:

proc macro panicked message: The distDir configuration is set to "../dist" but this path doesn't exist

But I think it's due to my editor I guess? So basically nowadays we don't need that custom-protocol right? I think I don't have any custom protocol in the project.

FabianLars commented 11 months ago

You don't need the default feature for it, no. You must keep the feature itself though if you ever want to build your app via the tauri build command. And yes, if your ide is configured to enable all features then you'll still see this issue.