More fullstack fixes (#4737)

* allow `--server` only builds!

* rollback example

* capture `/:route` routes

* fix: properly implement file uploads in forms

* implement desktop form uploads

* Performant serialization of form bytes

* finish up file stuff

* cleanups

* add request headers to streaming context

* remove some unwraps
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2025-10-07 23:07:43 -07:00
1 parent fc01a002fd
commit 92792aded9
47 files changed
+1241 -703

No files matched your search

+11 -6
View File
@@ -154,19 +154,20 @@ impl AppBuilder {
}
/// Create a new `AppBuilder` and immediately start a build process.
pub fn started(request: &BuildRequest, mode: BuildMode) -> Result<Self> {
pub fn started(request: &BuildRequest, mode: BuildMode, build_id: BuildId) -> Result<Self> {
let mut builder = Self::new(request)?;
builder.start(mode);
builder.start(mode, build_id);
Ok(builder)
}
pub(crate) fn start(&mut self, mode: BuildMode) {
pub(crate) fn start(&mut self, mode: BuildMode, build_id: BuildId) {
self.build_task = tokio::spawn({
let request = self.build.clone();
let tx = self.tx.clone();
async move {
let ctx = BuildContext {
mode,
build_id,
tx: tx.clone(),
};
request.verify_tooling(&ctx).await?;
@@ -290,10 +291,12 @@ impl AppBuilder {
update
}
pub(crate) fn patch_rebuild(&mut self, changed_files: Vec<PathBuf>) {
pub(crate) fn patch_rebuild(&mut self, changed_files: Vec<PathBuf>, build_id: BuildId) {
// We need the rustc args from the original build to pass to the new build
let Some(artifacts) = self.artifacts.as_ref().cloned() else {
tracing::warn!("Ignoring patch rebuild since there is no existing build.");
tracing::warn!(
"Ignoring patch rebuild for {build_id:?} since there is no existing build."
);
return;
};
@@ -327,6 +330,7 @@ impl AppBuilder {
self.build_task = tokio::spawn({
let request = self.build.clone();
let ctx = BuildContext {
build_id,
tx: self.tx.clone(),
mode: BuildMode::Thin {
changed_files,
@@ -340,7 +344,7 @@ impl AppBuilder {
}
/// Restart this builder with new build arguments.
pub(crate) fn start_rebuild(&mut self, mode: BuildMode) {
pub(crate) fn start_rebuild(&mut self, mode: BuildMode, build_id: BuildId) {
// Abort all the ongoing builds, cleaning up any loose artifacts and waiting to cleanly exit
// And then start a new build, resetting our progress/stage to the beginning and replacing the old tokio task
self.abort_all(BuildStage::Restarting);
@@ -351,6 +355,7 @@ impl AppBuilder {
let ctx = BuildContext {
tx: self.tx.clone(),
mode,
build_id,
};
async move { request.build(&ctx).await }
});
+9 -2
View File
@@ -16,6 +16,7 @@ use std::{path::PathBuf, process::ExitStatus};
pub struct BuildContext {
pub tx: ProgressTx,
pub mode: BuildMode,
pub build_id: BuildId,
}
pub type ProgressTx = UnboundedSender<BuilderUpdate>;
@@ -24,8 +25,8 @@ pub type ProgressRx = UnboundedReceiver<BuilderUpdate>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize)]
pub struct BuildId(pub(crate) usize);
impl BuildId {
pub const CLIENT: Self = Self(0);
pub const SERVER: Self = Self(1);
pub const PRIMARY: Self = Self(0);
pub const SECONDARY: Self = Self(1);
}
#[allow(clippy::large_enum_variant)]
@@ -78,6 +79,12 @@ pub enum BuilderUpdate {
}
impl BuildContext {
/// Returns true if this is a client build - basically, is this the primary build?
/// We try not to duplicate work between client and server builds, like asset copying.
pub(crate) fn is_primary_build(&self) -> bool {
self.build_id == BuildId::PRIMARY
}
pub(crate) fn status_wasm_bindgen_start(&self) {
_ = self.tx.unbounded_send(BuilderUpdate::Progress {
stage: BuildStage::RunningBindgen,
+1 -1
View File
@@ -45,7 +45,7 @@ pub(crate) async fn pre_render_static_routes(
devserver_ip,
Some(fullstack_address),
false,
BuildId::SERVER,
BuildId::SECONDARY,
);
// Run the server executable
let _child = Command::new(&server_exe)
+10 -11
View File
@@ -320,9 +320,9 @@
//! - xbuild: <https://github.com/rust-mobile/xbuild/blob/master/xbuild/src/command/build.rs>
use crate::{
AndroidTools, BuildContext, BundleFormat, DioxusConfig, Error, LinkAction, LinkerFlavor,
Platform, Renderer, Result, RustcArgs, TargetArgs, TraceSrc, WasmBindgen, WasmOptConfig,
Workspace, DX_RUSTC_WRAPPER_ENV_VAR,
AndroidTools, BuildContext, BuildId, BundleFormat, DioxusConfig, Error, LinkAction,
LinkerFlavor, Platform, Renderer, Result, RustcArgs, TargetArgs, TraceSrc, WasmBindgen,
WasmOptConfig, Workspace, DX_RUSTC_WRAPPER_ENV_VAR,
};
use anyhow::{bail, Context};
use cargo_metadata::diagnostic::Diagnostic;
@@ -445,7 +445,6 @@ pub enum BuildMode {
#[derive(Clone, Debug)]
pub struct BuildArtifacts {
pub(crate) root_dir: PathBuf,
pub(crate) bundle: BundleFormat,
pub(crate) exe: PathBuf,
pub(crate) direct_rustc: RustcArgs,
pub(crate) time_start: SystemTime,
@@ -454,6 +453,7 @@ pub struct BuildArtifacts {
pub(crate) mode: BuildMode,
pub(crate) patch_cache: Option<Arc<HotpatchModuleCache>>,
pub(crate) depinfo: RustcDepInfo,
pub(crate) build_id: BuildId,
}
impl BuildRequest {
@@ -989,10 +989,10 @@ impl BuildRequest {
_ = std::fs::File::create_new(self.windows_command_file());
if !matches!(ctx.mode, BuildMode::Thin { .. }) {
self.prepare_build_dir()?;
self.prepare_build_dir(ctx)?;
}
if self.bundle == BundleFormat::Server {
if !ctx.is_primary_build() {
return Ok(());
}
@@ -1250,7 +1250,6 @@ impl BuildRequest {
let assets = self.collect_assets(&exe, ctx).await?;
let time_end = SystemTime::now();
let mode = ctx.mode.clone();
let bundle = self.bundle;
let depinfo = RustcDepInfo::from_file(&exe.with_extension("d")).unwrap_or_default();
tracing::debug!(
@@ -1261,7 +1260,6 @@ impl BuildRequest {
Ok(BuildArtifacts {
time_end,
bundle,
exe,
direct_rustc,
time_start,
@@ -1270,6 +1268,7 @@ impl BuildRequest {
depinfo,
root_dir: self.root_dir(),
patch_cache: None,
build_id: ctx.build_id,
})
}
@@ -1460,7 +1459,7 @@ impl BuildRequest {
/// Should be the same on all platforms - just copy over the assets from the manifest into the output directory
async fn write_assets(&self, ctx: &BuildContext, assets: &AssetManifest) -> Result<()> {
// Server doesn't need assets - web will provide them
if self.bundle == BundleFormat::Server {
if !ctx.is_primary_build() {
return Ok(());
}
@@ -4320,14 +4319,14 @@ __wbg_init({{module_or_path: "/{}/{wasm_path}"}}).then((wasm) => {{
/// - extra scaffolding
///
/// It's not guaranteed that they're different from any other folder
pub(crate) fn prepare_build_dir(&self) -> Result<()> {
pub(crate) fn prepare_build_dir(&self, ctx: &BuildContext) -> Result<()> {
use std::fs::{create_dir_all, remove_dir_all};
use std::sync::OnceLock;
static INITIALIZED: OnceLock<Result<()>> = OnceLock::new();
let success = INITIALIZED.get_or_init(|| {
if self.bundle != BundleFormat::Server {
if ctx.is_primary_build() {
_ = remove_dir_all(self.exe_dir());
}
+8 -4
View File
@@ -1,8 +1,8 @@
use dioxus_dx_wire_format::StructuredBuildArtifacts;
use crate::{
cli::*, Anonymized, AppBuilder, BuildArtifacts, BuildMode, BuildRequest, BundleFormat,
TargetArgs, Workspace,
cli::*, Anonymized, AppBuilder, BuildArtifacts, BuildId, BuildMode, BuildRequest, BundleFormat,
Platform, TargetArgs, Workspace,
};
/// Build the Rust Dioxus app and all of its assets.
@@ -128,6 +128,10 @@ impl CommandWithPlatformOverrides<BuildArgs> {
BuildRequest::new(&server_args.build_arguments, workspace.clone()).await?,
);
}
None if client_args.platform == Platform::Server => {
// If the user requests a server build with `--server`, then we don't need to build a separate server binary.
// There's no client to use, so even though fullstack is true, we only build the server.
}
None => {
let mut args = self.shared.build_arguments.clone();
args.platform = crate::Platform::Server;
@@ -171,7 +175,7 @@ impl CommandWithPlatformOverrides<BuildArgs> {
request: &BuildRequest,
mode: BuildMode,
) -> Result<BuildArtifacts> {
AppBuilder::started(request, mode)?
AppBuilder::started(request, mode, BuildId::PRIMARY)?
.finish_build()
.await
.inspect(|_| {
@@ -189,7 +193,7 @@ impl CommandWithPlatformOverrides<BuildArgs> {
};
// If the server is present, we need to build it as well
let mut server_build = AppBuilder::started(server, mode)?;
let mut server_build = AppBuilder::started(server, mode, BuildId::SECONDARY)?;
let server_artifacts = server_build.finish_build().await?;
// Run SSG and cache static routes
+6 -5
View File
@@ -1,4 +1,4 @@
use crate::{AppBuilder, BuildArgs, BuildMode, BuildRequest, BundleFormat};
use crate::{AppBuilder, BuildArgs, BuildId, BuildMode, BuildRequest, BundleFormat};
use anyhow::{bail, Context};
use path_absolutize::Absolutize;
use std::collections::HashMap;
@@ -40,16 +40,17 @@ impl Bundle {
let BuildTargets { client, server } = self.args.into_targets().await?;
let mut server_artifacts = None;
let client_artifacts = AppBuilder::started(&client, BuildMode::Base { run: false })?
.finish_build()
.await?;
let client_artifacts =
AppBuilder::started(&client, BuildMode::Base { run: false }, BuildId::PRIMARY)?
.finish_build()
.await?;
tracing::info!(path = ?client.root_dir(), "Client build completed successfully! 🚀");
if let Some(server) = server.as_ref() {
// If the server is present, we need to build it as well
server_artifacts = Some(
AppBuilder::started(server, BuildMode::Base { run: false })?
AppBuilder::started(server, BuildMode::Base { run: false }, BuildId::SECONDARY)?
.finish_build()
.await?,
);
+13 -3
View File
@@ -1,5 +1,5 @@
use crate::{
platform_override::CommandWithPlatformOverrides, AppBuilder, BuildArgs, BuildMode,
platform_override::CommandWithPlatformOverrides, AppBuilder, BuildArgs, BuildId, BuildMode,
HotpatchModuleCache, Result, StructuredOutput,
};
use anyhow::Context;
@@ -34,8 +34,16 @@ impl HotpatchTip {
pub async fn run(self) -> Result<StructuredOutput> {
let targets = self.build_args.into_targets().await?;
let patch_server = self.patch_server.unwrap_or(false);
let build_id = if patch_server {
BuildId::SECONDARY
} else {
BuildId::PRIMARY
};
// Select which target to patch
let request = if self.patch_server.unwrap_or_default() {
let request = if patch_server {
targets.server.as_ref().context("No server to patch!")?
} else {
&targets.client
@@ -68,7 +76,9 @@ impl HotpatchTip {
cache: cache.clone(),
};
let artifacts = AppBuilder::started(request, mode)?.finish_build().await?;
let artifacts = AppBuilder::started(request, mode, build_id)?
.finish_build()
.await?;
let patch_exe = request.patch_exe(artifacts.time_start);
Ok(StructuredOutput::Hotpatch {
+2 -2
View File
@@ -109,12 +109,12 @@ pub(crate) async fn serve_all(args: ServeArgs, tracer: &TraceController) -> Resu
pid,
} => {
devserver
.send_hotreload(builder.applied_hot_reload_changes(BuildId::CLIENT))
.send_hotreload(builder.applied_hot_reload_changes(BuildId::PRIMARY))
.await;
if builder.server.is_some() {
devserver
.send_hotreload(builder.applied_hot_reload_changes(BuildId::SERVER))
.send_hotreload(builder.applied_hot_reload_changes(BuildId::SECONDARY))
.await;
}
+2 -2
View File
@@ -268,12 +268,12 @@ impl Output {
}
KeyCode::Char('D') => {
return Ok(Some(ServeUpdate::OpenDebugger {
id: BuildId::SERVER,
id: BuildId::SECONDARY,
}));
}
KeyCode::Char('d') => {
return Ok(Some(ServeUpdate::OpenDebugger {
id: BuildId::CLIENT,
id: BuildId::PRIMARY,
}));
}
KeyCode::Char('c') => {
+38 -30
View File
@@ -234,9 +234,9 @@ impl AppServer {
false => BuildMode::Base { run: true },
};
self.client.start(build_mode.clone());
self.client.start(build_mode.clone(), BuildId::PRIMARY);
if let Some(server) = self.server.as_mut() {
server.start(build_mode);
server.start(build_mode, BuildId::SECONDARY);
}
}
@@ -273,14 +273,14 @@ impl AppServer {
// Wait for the client to finish
client_update = client_wait => {
ServeUpdate::BuilderUpdate {
id: BuildId::CLIENT,
id: BuildId::PRIMARY,
update: client_update,
}
}
Some(server_update) = server_wait => {
ServeUpdate::BuilderUpdate {
id: BuildId::SERVER,
id: BuildId::SECONDARY,
update: server_update,
}
}
@@ -491,17 +491,18 @@ impl AppServer {
// A full rebuild is required when the user modifies static initializers which we haven't wired up yet.
if needs_full_rebuild && self.automatic_rebuilds {
if self.use_hotpatch_engine {
self.client.patch_rebuild(files.to_vec());
self.client.patch_rebuild(files.to_vec(), BuildId::PRIMARY);
if let Some(server) = self.server.as_mut() {
server.patch_rebuild(files.to_vec());
server.patch_rebuild(files.to_vec(), BuildId::SECONDARY);
}
self.clear_hot_reload_changes();
self.clear_cached_rsx();
server.send_patch_start().await;
} else {
self.client.start_rebuild(BuildMode::Base { run: true });
self.client
.start_rebuild(BuildMode::Base { run: true }, BuildId::PRIMARY);
if let Some(server) = self.server.as_mut() {
server.start_rebuild(BuildMode::Base { run: true });
server.start_rebuild(BuildMode::Base { run: true }, BuildId::SECONDARY);
}
self.clear_hot_reload_changes();
self.clear_cached_rsx();
@@ -560,13 +561,14 @@ impl AppServer {
devserver: &mut WebServer,
) -> Result<()> {
// Make sure to save artifacts regardless of if we're opening the app or not
match artifacts.bundle {
BundleFormat::Server => {
match artifacts.build_id {
BuildId::PRIMARY => self.client.artifacts = Some(artifacts.clone()),
BuildId::SECONDARY => {
if let Some(server) = self.server.as_mut() {
server.artifacts = Some(artifacts.clone());
}
}
_ => self.client.artifacts = Some(artifacts.clone()),
_ => {}
}
let should_open = self.client.stage == BuildStage::Success
@@ -634,7 +636,7 @@ impl AppServer {
fullstack_address,
false,
false,
BuildId::SERVER,
BuildId::SECONDARY,
&self.server_args,
)
.await?;
@@ -649,7 +651,7 @@ impl AppServer {
fullstack_address,
open_browser,
self.always_on_top,
BuildId::CLIENT,
BuildId::PRIMARY,
&self.client_args,
)
.await?;
@@ -696,9 +698,10 @@ impl AppServer {
false => BuildMode::Base { run: true },
};
self.client.start_rebuild(build_mode.clone());
self.client
.start_rebuild(build_mode.clone(), BuildId::PRIMARY);
if let Some(s) = self.server.as_mut() {
s.start_rebuild(build_mode)
s.start_rebuild(build_mode, BuildId::SECONDARY);
}
self.clear_hot_reload_changes();
@@ -719,8 +722,8 @@ impl AppServer {
.unwrap_or_default();
let jump_table = match id {
BuildId::CLIENT => self.client.hotpatch(bundle, cache).await,
BuildId::SERVER => {
BuildId::PRIMARY => self.client.hotpatch(bundle, cache).await,
BuildId::SECONDARY => {
self.server
.as_mut()
.context("Server not found")?
@@ -730,7 +733,7 @@ impl AppServer {
_ => bail!("Invalid build id"),
}?;
if id == BuildId::CLIENT {
if id == BuildId::PRIMARY {
self.applied_client_hot_reload_message.jump_table = self.client.patches.last().cloned();
}
@@ -759,11 +762,16 @@ impl AppServer {
.context("Missing server jump table")?;
devserver
.send_patch(server_jump_table, elapsed, BuildId::SERVER, server.pid)
.send_patch(server_jump_table, elapsed, BuildId::SECONDARY, server.pid)
.await;
devserver
.send_patch(client_jump_table, elapsed, BuildId::CLIENT, self.client.pid)
.send_patch(
client_jump_table,
elapsed,
BuildId::PRIMARY,
self.client.pid,
)
.await;
}
@@ -772,8 +780,8 @@ impl AppServer {
pub(crate) fn get_build(&self, id: BuildId) -> Option<&AppBuilder> {
match id {
BuildId::CLIENT => Some(&self.client),
BuildId::SERVER => self.server.as_ref(),
BuildId::PRIMARY => Some(&self.client),
BuildId::SECONDARY => self.server.as_ref(),
_ => None,
}
}
@@ -791,18 +799,18 @@ impl AppServer {
pub(crate) fn applied_hot_reload_changes(&mut self, build: BuildId) -> HotReloadMsg {
let mut msg = self.applied_client_hot_reload_message.clone();
if build == BuildId::CLIENT {
if build == BuildId::PRIMARY {
msg.jump_table = self.client.patches.last().cloned();
msg.for_build_id = Some(BuildId::CLIENT.0 as _);
msg.for_build_id = Some(BuildId::PRIMARY.0 as _);
if let Some(lib) = msg.jump_table.as_mut() {
lib.lib = PathBuf::from("/").join(lib.lib.clone());
}
}
if build == BuildId::SERVER {
if build == BuildId::SECONDARY {
if let Some(server) = self.server.as_mut() {
msg.jump_table = server.patches.last().cloned();
msg.for_build_id = Some(BuildId::SERVER.0 as _);
msg.for_build_id = Some(BuildId::SECONDARY.0 as _);
}
}
@@ -828,7 +836,7 @@ impl AppServer {
pid: Option<u32>,
) {
match build_id {
BuildId::CLIENT => {
BuildId::PRIMARY => {
// multiple tabs on web can cause this to be called incorrectly, and it doesn't
// make any sense anyways
if self.client.build.bundle != BundleFormat::Web {
@@ -840,7 +848,7 @@ impl AppServer {
}
}
}
BuildId::SERVER => {
BuildId::SECONDARY => {
if let Some(server) = self.server.as_mut() {
server.aslr_reference = aslr_reference;
}
@@ -1146,10 +1154,10 @@ impl AppServer {
}
match build {
BuildId::CLIENT => {
BuildId::PRIMARY => {
_ = self.client.open_debugger(dev).await;
}
BuildId::SERVER => {
BuildId::SECONDARY => {
if let Some(server) = self.server.as_mut() {
_ = server.open_debugger(dev).await;
}
+1 -1
View File
@@ -149,7 +149,7 @@ impl WebServer {
if let Some(new_socket) = new_hot_reload_socket {
let aslr_reference = new_socket.aslr_reference;
let pid = new_socket.pid;
let id = new_socket.build_id.unwrap_or(BuildId::CLIENT);
let id = new_socket.build_id.unwrap_or(BuildId::PRIMARY);
drop(new_message);
self.hot_reload_sockets.push(new_socket);