Browse Source

Warn if cli-config is not available at compile time (#2135)

* warn if a package relies on the CLI config, but it is not built with the CLI

* fix default features

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
Evan Almloff 1 year ago
parent
commit
460a685fa3

+ 1 - 1
Cargo.toml

@@ -72,7 +72,7 @@ dioxus-check = { path = "packages/check", version = "0.5.0-alpha.2" }
 dioxus-rsx = { path = "packages/rsx", version = "0.5.0-alpha.2" }
 rsx-rosetta = { path = "packages/rsx-rosetta", version = "0.5.0-alpha.2" }
 dioxus-signals = { path = "packages/signals", version = "0.5.0-alpha.2" }
-dioxus-cli-config = { path = "packages/cli-config", version = "0.5.0-alpha.2" }
+dioxus-cli-config = { path = "packages/cli-config", version = "0.5.0-alpha.2", default-features = false}
 generational-box = { path = "packages/generational-box", version = "0.5.0-alpha.2" }
 dioxus-hot-reload = { path = "packages/hot-reload", version = "0.5.0-alpha.2" }
 dioxus-fullstack = { path = "packages/fullstack", version = "0.5.0-alpha.2" }

+ 2 - 1
packages/cli-config/Cargo.toml

@@ -22,5 +22,6 @@ tauri-bundler = { version = "=1.4.0", features = ["native-tls-vendored"], option
 tauri-utils = { version = "=1.5.*", optional = true }
 
 [features]
-default = []
+default = ["read-config"]
 cli = ["tauri-bundler", "tauri-utils", "clap", "toml", "cargo_toml"]
+read-config = []

+ 11 - 0
packages/cli-config/build.rs

@@ -0,0 +1,11 @@
+// warn if the "read-config" feature is enabled, but the DIOXUS_CONFIG environment variable is not set
+// This means that some library is trying to access the crate's configuration, but the dioxus CLI was not used to build the application.
+
+fn main() {
+    println!("cargo:rerun-if-env-changed=DIOXUS_CONFIG");
+    let dioxus_config = std::env::var("DIOXUS_CONFIG");
+    let built_with_dioxus = dioxus_config.is_ok();
+    if cfg!(feature = "read-config") && !built_with_dioxus {
+        println!("cargo:warning=A library is trying to access the crate's configuration, but the dioxus CLI was not used to build the application. Information about the Dioxus CLI is available at https://dioxuslabs.com/learn/0.5/CLI/installation");
+    }
+}

+ 2 - 0
packages/cli-config/src/lib.rs

@@ -42,6 +42,7 @@ impl std::fmt::Display for DioxusCLINotUsed {
 
 impl std::error::Error for DioxusCLINotUsed {}
 
+#[cfg(feature = "read-config")]
 /// The current crate's configuration.
 pub static CURRENT_CONFIG: once_cell::sync::Lazy<
     Result<crate::config::CrateConfig, DioxusCLINotUsed>,
@@ -54,5 +55,6 @@ pub static CURRENT_CONFIG: once_cell::sync::Lazy<
     })
 });
 
+#[cfg(feature = "read-config")]
 /// The current crate's configuration.
 pub const CURRENT_CONFIG_JSON: Option<&str> = std::option_env!("DIOXUS_CONFIG");

+ 4 - 1
packages/cli/Cargo.toml

@@ -15,7 +15,10 @@ thiserror = { workspace = true }
 wasm-bindgen-cli-support = "0.2"
 wasm-bindgen-shared = "0.2"
 colored = "2.0.0"
-dioxus-cli-config = { workspace = true, features = ["cli"] }
+dioxus-cli-config = { workspace = true, features = ["cli"], default-features = false }
+
+# features
+log = "0.4.14"
 fern = { version = "0.6.0", features = ["colored"] }
 serde = { version = "1.0.136", features = ["derive"] }
 serde_json = "1.0.79"

+ 1 - 1
packages/desktop/Cargo.toml

@@ -18,7 +18,7 @@ dioxus-html = { workspace = true, features = [
     "eval",
 ] }
 dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] }
-dioxus-cli-config = { workspace = true }
+dioxus-cli-config = { workspace = true, features = ["read-config"] }
 generational-box = { workspace = true }
 
 serde = "1.0.136"

+ 1 - 1
packages/fullstack/Cargo.toml

@@ -57,7 +57,7 @@ tower = { workspace = true, features = ["util"], optional = true }
 tower-layer = { version = "0.3.2", optional = true }
 web-sys = { version = "0.3.61", optional = true, features = ["Window", "Document", "Element", "HtmlDocument", "Storage", "console"] }
 
-dioxus-cli-config = { workspace = true, optional = true }
+dioxus-cli-config = { workspace = true, features = ["read-config"], optional = true }
 
 [target.'cfg(target_arch = "wasm32")'.dependencies]
 tokio = { workspace = true, features = ["rt", "sync"], optional = true }

+ 1 - 1
packages/liveview/Cargo.toml

@@ -27,7 +27,7 @@ rustc-hash = { workspace = true }
 dioxus-core = { workspace = true, features = ["serialize"] }
 dioxus-interpreter-js = { workspace = true, features = ["binary-protocol"] }
 dioxus-hot-reload = { workspace = true, optional = true }
-dioxus-cli-config = { workspace = true }
+dioxus-cli-config = { workspace = true, features = ["read-config"] }
 generational-box = { workspace = true }
 
 # axum

+ 1 - 1
packages/router/Cargo.toml

@@ -28,7 +28,7 @@ dioxus-liveview = { workspace = true, optional = true }
 dioxus-ssr = { workspace = true, optional = true }
 dioxus-fullstack = { workspace = true, optional = true }
 tokio = { workspace = true, features = ["full"], optional = true }
-dioxus-cli-config = { workspace = true }
+dioxus-cli-config = { workspace = true, features = ["read-config"] }
 
 [features]
 default = []