diff --git a/.changes/dev-path-dist-dir-validation.md b/.changes/dev-path-dist-dir-validation.md new file mode 100644 index 000000000..648ec4b65 --- /dev/null +++ b/.changes/dev-path-dist-dir-validation.md @@ -0,0 +1,7 @@ +--- +"tauri": patch +"tauri-utils": patch +"tauri-codegen": patch +--- + +Validate `tauri.conf.json > build > devPath` and `tauri.conf.json > build > distDir` values. diff --git a/core/tauri-codegen/src/context.rs b/core/tauri-codegen/src/context.rs index 2fbbfc54e..76f02a023 100644 --- a/core/tauri-codegen/src/context.rs +++ b/core/tauri-codegen/src/context.rs @@ -6,7 +6,7 @@ use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError}; use proc_macro2::TokenStream; use quote::quote; use std::path::PathBuf; -use tauri_utils::config::Config; +use tauri_utils::config::{Config, WindowUrl}; /// Necessary data needed by [`context_codegen`] to generate code for a Tauri application context. pub struct ContextData { @@ -24,15 +24,31 @@ pub fn context_codegen(data: ContextData) -> Result None, + WindowUrl::App(path) => { + if path.components().count() == 0 { + panic!( + "The `{}` configuration cannot be empty", + if dev { "devPath" } else { "distDir" } + ) + } + let assets_path = config_parent.join(path); + if !assets_path.exists() { + panic!( + "The `{}` configuration is set to `{:?}` but this path doesn't exist", + if dev { "devPath" } else { "distDir" }, + path + ) + } + Some(assets_path) + } + _ => unimplemented!(), }; // generate the assets inside the dist dir into a perfect hash function diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 0e3d9b092..4c038468a 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -2,6 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: MIT +//! The Tauri configuration used at runtime. +//! It is pulled from a `tauri.conf.json` file and the [`Config`] struct is generated at compile time. +//! +//! # Stability +//! This is a core functionality that is not considered part of the stable API. +//! If you use it, note that it may include breaking changes in the future. + use std::{collections::HashMap, path::PathBuf}; use serde::Deserialize; @@ -384,21 +391,21 @@ impl Default for TauriConfig { pub struct BuildConfig { /// the devPath config. #[serde(default = "default_dev_path")] - pub dev_path: String, + pub dev_path: WindowUrl, /// the dist config. #[serde(default = "default_dist_path")] - pub dist_dir: String, + pub dist_dir: WindowUrl, /// Whether we should inject the Tauri API on `window.__TAURI__` or not. #[serde(default)] pub with_global_tauri: bool, } -fn default_dev_path() -> String { - "http://localhost:8080".to_string() +fn default_dev_path() -> WindowUrl { + WindowUrl::External(Url::parse("http://localhost:8080").unwrap()) } -fn default_dist_path() -> String { - "../dist".to_string() +fn default_dist_path() -> WindowUrl { + WindowUrl::App("../dist".into()) } impl Default for BuildConfig { @@ -762,8 +769,8 @@ mod build { impl ToTokens for BuildConfig { fn to_tokens(&self, tokens: &mut TokenStream) { - let dev_path = str_lit(&self.dev_path); - let dist_dir = str_lit(&self.dist_dir); + let dev_path = &self.dev_path; + let dist_dir = &self.dist_dir; let with_global_tauri = self.with_global_tauri; literal_struct!(tokens, BuildConfig, dev_path, dist_dir, with_global_tauri); @@ -915,8 +922,8 @@ mod test { // create a build config let build = BuildConfig { - dev_path: String::from("http://localhost:8080"), - dist_dir: String::from("../dist"), + dev_path: WindowUrl::External(Url::parse("http://localhost:8080").unwrap()), + dist_dir: WindowUrl::App("../dist".into()), with_global_tauri: false, }; @@ -925,7 +932,10 @@ mod test { assert_eq!(b_config, build); assert_eq!(d_bundle, tauri.bundle); assert_eq!(d_updater, tauri.updater); - assert_eq!(d_path, String::from("http://localhost:8080")); + assert_eq!( + d_path, + WindowUrl::External(Url::parse("http://localhost:8080").unwrap()) + ); assert_eq!(d_title, tauri.windows[0].title); assert_eq!(d_windows, tauri.windows); } diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 0368443fe..2cd7ae42f 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -239,16 +239,18 @@ impl WindowManager

{ // setup content for dev-server #[cfg(dev)] fn get_url(&self) -> String { - if self.inner.config.build.dev_path.starts_with("http") { - self.inner.config.build.dev_path.clone() - } else { - "tauri://localhost".into() + match &self.inner.config.build.dev_path { + WindowUrl::External(url) => url.to_string(), + _ => "tauri://localhost".into(), } } #[cfg(custom_protocol)] fn get_url(&self) -> String { - "tauri://localhost".into() + match &self.inner.config.build.dist_dir { + WindowUrl::External(url) => url.to_string(), + _ => "tauri://localhost".into(), + } } fn prepare_pending_window( @@ -505,7 +507,7 @@ mod test { assert_eq!(manager.get_url(), "tauri://localhost"); #[cfg(dev)] - assert_eq!(manager.get_url(), manager.config().build.dev_path); + assert_eq!(manager.get_url(), "http://localhost:4000/"); } }