diff --git a/.changes/no-server-fix.md b/.changes/no-server-fix.md new file mode 100644 index 000000000..e555fcb61 --- /dev/null +++ b/.changes/no-server-fix.md @@ -0,0 +1,7 @@ +--- +"tauri": minor +"tauri-api": minor +--- + +Fixes no-server mode not running on another machine due to fs::read_to_string usage instead of the include_str macro. +Build no longer fails when compiling without environment variables, now the app will show an error. diff --git a/tauri-api/Cargo.toml b/tauri-api/Cargo.toml index 6e23f1de1..2e17d3e39 100644 --- a/tauri-api/Cargo.toml +++ b/tauri-api/Cargo.toml @@ -30,7 +30,6 @@ tauri-dialog = { git = "https://github.com/tauri-apps/tauri-dialog-rs", version attohttpc = {version = "0.14.0", features=["json", "form" ]} http = "0.2" tauri-utils = {version = "0.5", path = "../tauri-utils"} -envmnt = "0.8.2" clap = { git = "https://github.com/clap-rs/clap", rev = "1a276f8", version = "3.0.0-beta.1", optional = true } notify-rust = { version = "4.0.0", optional = true } once_cell = "1.4.0" diff --git a/tauri-api/build.rs b/tauri-api/build.rs new file mode 100644 index 000000000..7a362a0bd --- /dev/null +++ b/tauri-api/build.rs @@ -0,0 +1,38 @@ +use std::{ + env, + error::Error, + fs::{read_to_string, File}, + io::{BufWriter, Write}, + path::Path, +}; + +pub fn main() -> Result<(), Box> { + let out_dir = env::var("OUT_DIR")?; + + let dest_config_path = Path::new(&out_dir).join("tauri.conf.json"); + let mut config_file = BufWriter::new(File::create(&dest_config_path)?); + + match env::var_os("TAURI_CONFIG") { + Some(tauri_config) => { + let tauri_config_string = tauri_config.into_string().unwrap(); + write!(config_file, "{}", tauri_config_string)?; + } + None => match env::var_os("TAURI_DIR") { + Some(tauri_dir) => { + let tauri_dir_string = tauri_dir.into_string().unwrap(); + + println!("cargo:rerun-if-changed={}", tauri_dir_string); + + let original_config_path = Path::new(&tauri_dir_string).join("tauri.conf.json"); + let original_config = read_to_string(original_config_path)?; + + write!(config_file, "{}", original_config)?; + } + None => { + write!(config_file, "{{}}")?; + println!("Build error: Couldn't find ENV: TAURI_CONFIG or TAURI_DIR"); + } + }, + } + Ok(()) +} diff --git a/tauri-api/src/cli/macros.rs b/tauri-api/src/cli/macros.rs index e43ea8b90..0118d3270 100644 --- a/tauri-api/src/cli/macros.rs +++ b/tauri-api/src/cli/macros.rs @@ -6,7 +6,7 @@ macro_rules! bind_string_arg { clap_arg = clap_arg.$clap_field(value); } clap_arg - }} + }}; } macro_rules! bind_value_arg { @@ -17,7 +17,7 @@ macro_rules! bind_value_arg { clap_arg = clap_arg.$field(value); } clap_arg - }} + }}; } macro_rules! bind_string_slice_arg { @@ -29,7 +29,7 @@ macro_rules! bind_string_slice_arg { clap_arg = clap_arg.$field(&v); } clap_arg - }} + }}; } macro_rules! bind_if_arg { @@ -41,5 +41,5 @@ macro_rules! bind_if_arg { clap_arg = clap_arg.$field(&v[0], &v[1]); } clap_arg - }} -} \ No newline at end of file + }}; +} diff --git a/tauri-api/src/config.rs b/tauri-api/src/config.rs index cdfbcebeb..27ea91fc8 100644 --- a/tauri-api/src/config.rs +++ b/tauri-api/src/config.rs @@ -2,7 +2,6 @@ use serde::Deserialize; use once_cell::sync::OnceCell; use std::collections::HashMap; -use std::{fs, path}; static CONFIG: OnceCell = OnceCell::new(); @@ -231,11 +230,8 @@ pub fn get() -> crate::Result<&'static Config> { let config: Config = match option_env!("TAURI_CONFIG") { Some(config) => serde_json::from_str(config).expect("failed to parse TAURI_CONFIG env"), None => { - let env_var = envmnt::get_or("TAURI_DIR", "../dist"); - let path = path::Path::new(&env_var); - let contents = fs::read_to_string(path.join("tauri.conf.json"))?; - - serde_json::from_str(&contents).expect("failed to read tauri.conf.json") + let config = include_str!(concat!(env!("OUT_DIR"), "/tauri.conf.json")); + serde_json::from_str(&config).expect("failed to read tauri.conf.json") } }; diff --git a/tauri-utils/src/lib.rs b/tauri-utils/src/lib.rs index 8da24a9f6..882918da3 100644 --- a/tauri-utils/src/lib.rs +++ b/tauri-utils/src/lib.rs @@ -6,18 +6,18 @@ use thiserror::Error; #[derive(Error, Debug)] pub enum Error { - #[error("Unable to determine target-architecture")] - Architecture, - #[error("Unable to determine target-os")] - OS, - #[error("Unable to determine target-environment")] - Environment, - #[error("Unknown target_os")] - Unknown, - #[error("Could not get parent process")] - ParentProcess, - #[error("Could not get parent PID")] - ParentPID, - #[error("Could not get child process")] - ChildProcess, -} \ No newline at end of file + #[error("Unable to determine target-architecture")] + Architecture, + #[error("Unable to determine target-os")] + OS, + #[error("Unable to determine target-environment")] + Environment, + #[error("Unknown target_os")] + Unknown, + #[error("Could not get parent process")] + ParentProcess, + #[error("Could not get parent PID")] + ParentPID, + #[error("Could not get child process")] + ChildProcess, +} diff --git a/tauri-utils/src/process.rs b/tauri-utils/src/process.rs index c05c20a54..43d9528fd 100644 --- a/tauri-utils/src/process.rs +++ b/tauri-utils/src/process.rs @@ -7,13 +7,9 @@ pub use sysinfo::{Process, ProcessExt, Signal, System, SystemExt}; pub fn get_parent_process(system: &mut sysinfo::System) -> Result<&Process, Error> { let pid = sysinfo::get_current_pid().unwrap(); system.refresh_process(pid); - let current_process = system - .get_process(pid) - .ok_or(Error::ParentProcess)?; + let current_process = system.get_process(pid).ok_or(Error::ParentProcess)?; let parent_pid = current_process.parent().ok_or(Error::ParentPID)?; - let parent_process = system - .get_process(parent_pid) - .ok_or(Error::ParentProcess)?; + let parent_process = system.get_process(parent_pid).ok_or(Error::ParentProcess)?; println!("{}", pid); Ok(parent_process) diff --git a/tauri/build.rs b/tauri/build.rs index 9e0f89fbe..882ad2abe 100644 --- a/tauri/build.rs +++ b/tauri/build.rs @@ -1,7 +1,22 @@ #[cfg(any(feature = "embedded-server", feature = "no-server"))] -pub fn main() { +use std::{ + env, + error::Error, + fs::{read_to_string, File}, + io::{BufWriter, Write}, + path::Path, +}; + +#[cfg(any(feature = "embedded-server", feature = "no-server"))] +pub fn main() -> Result<(), Box> { shared(); - match std::env::var_os("TAURI_DIST_DIR") { + + let out_dir = env::var("OUT_DIR")?; + + let dest_index_html_path = Path::new(&out_dir).join("index.tauri.html"); + let mut index_html_file = BufWriter::new(File::create(&dest_index_html_path)?); + + match env::var_os("TAURI_DIST_DIR") { Some(dist_path) => { let dist_path_string = dist_path.into_string().unwrap(); @@ -19,14 +34,31 @@ pub fn main() { // include assets tauri_includedir_codegen::start("ASSETS") .dir( - dist_path_string, + dist_path_string.clone(), tauri_includedir_codegen::Compression::None, ) .build("data.rs", inlined_assets) - .expect("failed to build data.rs") + .expect("failed to build data.rs"); + + let original_index_html_path = Path::new(&dist_path_string).join("index.tauri.html"); + let original_index_html = read_to_string(original_index_html_path)?; + + write!(index_html_file, "{}", original_index_html)?; + } + None => { + // dummy assets + tauri_includedir_codegen::start("ASSETS") + .dir("".to_string(), tauri_includedir_codegen::Compression::None) + .build("data.rs", vec![]) + .expect("failed to build data.rs"); + write!( + index_html_file, + "Build error: Couldn't find ENV: TAURI_DIST_DIR" + )?; + println!("Build error: Couldn't find ENV: TAURI_DIST_DIR"); } - None => println!("Build error: Couldn't find ENV: TAURI_DIST_DIR"), } + Ok(()) } #[cfg(not(any(feature = "embedded-server", feature = "no-server")))] diff --git a/tauri/src/app/runner.rs b/tauri/src/app/runner.rs index d68588298..0248d5b9a 100644 --- a/tauri/src/app/runner.rs +++ b/tauri/src/app/runner.rs @@ -95,16 +95,8 @@ fn setup_content() -> crate::Result> { // setup content for no-server #[cfg(feature = "no-server")] fn setup_content() -> crate::Result> { - let dist_dir = match option_env!("TAURI_DIST_DIR") { - Some(d) => d.to_string(), - None => env::current_dir()? - .into_os_string() - .into_string() - .expect("Unable to convert to normal String"), - }; - let index_path = Path::new(&dist_dir).join("index.tauri.html"); - - Ok(Content::Html(read_to_string(index_path)?)) + let html = include_str!(concat!(env!("OUT_DIR"), "/index.tauri.html")); + Ok(Content::Html(html.to_string())) } // get the port for the embedded server diff --git a/tauri/src/endpoints.rs b/tauri/src/endpoints.rs index 7a39b4878..c3e22330d 100644 --- a/tauri/src/endpoints.rs +++ b/tauri/src/endpoints.rs @@ -344,7 +344,8 @@ fn load_asset( loop { read_asset = crate::assets::ASSETS.get(&format!( "{}/{}", - env!("TAURI_DIST_DIR"), + option_env!("TAURI_DIST_DIR") + .expect("tauri apps should be built with the TAURI_DIST_DIR environment variable"), path.to_string_lossy() )); if read_asset.is_err() {