Skip to content

Commit e97846a

Browse files
authored
feat(core): validate devPath and distDir values (#1848)
* feat(core): validate `devPath` and `distDir` values * fix tests
1 parent e08065d commit e97846a

4 files changed

Lines changed: 61 additions & 26 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
"tauri-utils": patch
4+
"tauri-codegen": patch
5+
---
6+
7+
Validate `tauri.conf.json > build > devPath` and `tauri.conf.json > build > distDir` values.

core/tauri-codegen/src/context.rs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::embedded_assets::{AssetOptions, EmbeddedAssets, EmbeddedAssetsError};
66
use proc_macro2::TokenStream;
77
use quote::quote;
88
use std::path::PathBuf;
9-
use tauri_utils::config::Config;
9+
use tauri_utils::config::{Config, WindowUrl};
1010

1111
/// Necessary data needed by [`context_codegen`] to generate code for a Tauri application context.
1212
pub struct ContextData {
@@ -24,15 +24,31 @@ pub fn context_codegen(data: ContextData) -> Result<TokenStream, EmbeddedAssetsE
2424
config_parent,
2525
root,
2626
} = data;
27-
let assets_path = if dev {
28-
// if dev_path is a dev server, we don't have any assets to embed
29-
if config.build.dev_path.starts_with("http") {
30-
None
31-
} else {
32-
Some(config_parent.join(&config.build.dev_path))
33-
}
27+
let app_url = if dev {
28+
&config.build.dev_path
3429
} else {
35-
Some(config_parent.join(&config.build.dist_dir))
30+
&config.build.dist_dir
31+
};
32+
let assets_path = match app_url {
33+
WindowUrl::External(_) => None,
34+
WindowUrl::App(path) => {
35+
if path.components().count() == 0 {
36+
panic!(
37+
"The `{}` configuration cannot be empty",
38+
if dev { "devPath" } else { "distDir" }
39+
)
40+
}
41+
let assets_path = config_parent.join(path);
42+
if !assets_path.exists() {
43+
panic!(
44+
"The `{}` configuration is set to `{:?}` but this path doesn't exist",
45+
if dev { "devPath" } else { "distDir" },
46+
path
47+
)
48+
}
49+
Some(assets_path)
50+
}
51+
_ => unimplemented!(),
3652
};
3753

3854
// generate the assets inside the dist dir into a perfect hash function

core/tauri-utils/src/config.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
//! The Tauri configuration used at runtime.
6+
//! It is pulled from a `tauri.conf.json` file and the [`Config`] struct is generated at compile time.
7+
//!
8+
//! # Stability
9+
//! This is a core functionality that is not considered part of the stable API.
10+
//! If you use it, note that it may include breaking changes in the future.
11+
512
use std::{collections::HashMap, path::PathBuf};
613

714
use serde::Deserialize;
@@ -384,21 +391,21 @@ impl Default for TauriConfig {
384391
pub struct BuildConfig {
385392
/// the devPath config.
386393
#[serde(default = "default_dev_path")]
387-
pub dev_path: String,
394+
pub dev_path: WindowUrl,
388395
/// the dist config.
389396
#[serde(default = "default_dist_path")]
390-
pub dist_dir: String,
397+
pub dist_dir: WindowUrl,
391398
/// Whether we should inject the Tauri API on `window.__TAURI__` or not.
392399
#[serde(default)]
393400
pub with_global_tauri: bool,
394401
}
395402

396-
fn default_dev_path() -> String {
397-
"http://localhost:8080".to_string()
403+
fn default_dev_path() -> WindowUrl {
404+
WindowUrl::External(Url::parse("http://localhost:8080").unwrap())
398405
}
399406

400-
fn default_dist_path() -> String {
401-
"../dist".to_string()
407+
fn default_dist_path() -> WindowUrl {
408+
WindowUrl::App("../dist".into())
402409
}
403410

404411
impl Default for BuildConfig {
@@ -762,8 +769,8 @@ mod build {
762769

763770
impl ToTokens for BuildConfig {
764771
fn to_tokens(&self, tokens: &mut TokenStream) {
765-
let dev_path = str_lit(&self.dev_path);
766-
let dist_dir = str_lit(&self.dist_dir);
772+
let dev_path = &self.dev_path;
773+
let dist_dir = &self.dist_dir;
767774
let with_global_tauri = self.with_global_tauri;
768775

769776
literal_struct!(tokens, BuildConfig, dev_path, dist_dir, with_global_tauri);
@@ -915,8 +922,8 @@ mod test {
915922

916923
// create a build config
917924
let build = BuildConfig {
918-
dev_path: String::from("http://localhost:8080"),
919-
dist_dir: String::from("../dist"),
925+
dev_path: WindowUrl::External(Url::parse("http://localhost:8080").unwrap()),
926+
dist_dir: WindowUrl::App("../dist".into()),
920927
with_global_tauri: false,
921928
};
922929

@@ -925,7 +932,10 @@ mod test {
925932
assert_eq!(b_config, build);
926933
assert_eq!(d_bundle, tauri.bundle);
927934
assert_eq!(d_updater, tauri.updater);
928-
assert_eq!(d_path, String::from("http://localhost:8080"));
935+
assert_eq!(
936+
d_path,
937+
WindowUrl::External(Url::parse("http://localhost:8080").unwrap())
938+
);
929939
assert_eq!(d_title, tauri.windows[0].title);
930940
assert_eq!(d_windows, tauri.windows);
931941
}

core/tauri/src/manager.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ impl<P: Params> WindowManager<P> {
239239
// setup content for dev-server
240240
#[cfg(dev)]
241241
fn get_url(&self) -> String {
242-
if self.inner.config.build.dev_path.starts_with("http") {
243-
self.inner.config.build.dev_path.clone()
244-
} else {
245-
"tauri://localhost".into()
242+
match &self.inner.config.build.dev_path {
243+
WindowUrl::External(url) => url.to_string(),
244+
_ => "tauri://localhost".into(),
246245
}
247246
}
248247

249248
#[cfg(custom_protocol)]
250249
fn get_url(&self) -> String {
251-
"tauri://localhost".into()
250+
match &self.inner.config.build.dist_dir {
251+
WindowUrl::External(url) => url.to_string(),
252+
_ => "tauri://localhost".into(),
253+
}
252254
}
253255

254256
fn prepare_pending_window(
@@ -505,7 +507,7 @@ mod test {
505507
assert_eq!(manager.get_url(), "tauri://localhost");
506508

507509
#[cfg(dev)]
508-
assert_eq!(manager.get_url(), manager.config().build.dev_path);
510+
assert_eq!(manager.get_url(), "http://localhost:4000/");
509511
}
510512
}
511513

0 commit comments

Comments
 (0)