From 7d8e98e51bf6c11a10e7a5d5f3afd1862f91515c Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Thu, 23 Oct 2025 20:28:17 +0200 Subject: [PATCH 1/2] Don't automatically enable tracing for fs feature --- tower-http/Cargo.toml | 2 +- tower-http/src/services/fs/serve_dir/mod.rs | 3 +++ tower-http/src/services/fs/serve_dir/open_file.rs | 4 ++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/tower-http/Cargo.toml b/tower-http/Cargo.toml index 90c8116e..2ab4ee82 100644 --- a/tower-http/Cargo.toml +++ b/tower-http/Cargo.toml @@ -88,7 +88,7 @@ auth = ["base64", "validate-request"] catch-panic = ["tracing", "futures-util/std", "dep:http-body", "dep:http-body-util"] cors = [] follow-redirect = ["futures-util", "dep:http-body", "iri-string", "tower/util"] -fs = ["futures-core", "futures-util", "dep:http-body", "dep:http-body-util", "tokio/fs", "tokio-util/io", "tokio/io-util", "dep:http-range-header", "mime_guess", "mime", "percent-encoding", "httpdate", "set-status", "futures-util/alloc", "tracing"] +fs = ["futures-core", "futures-util", "dep:http-body", "dep:http-body-util", "tokio/fs", "tokio-util/io", "tokio/io-util", "dep:http-range-header", "mime_guess", "mime", "percent-encoding", "httpdate", "set-status", "futures-util/alloc"] limit = ["dep:http-body", "dep:http-body-util"] map-request-body = [] map-response-body = [] diff --git a/tower-http/src/services/fs/serve_dir/mod.rs b/tower-http/src/services/fs/serve_dir/mod.rs index 61b956d1..3bc27755 100644 --- a/tower-http/src/services/fs/serve_dir/mod.rs +++ b/tower-http/src/services/fs/serve_dir/mod.rs @@ -411,7 +411,10 @@ where .try_call(req) .map(|result: Result<_, _>| -> Result<_, Infallible> { let response = result.unwrap_or_else(|err| { + #[cfg(feature = "tracing")] tracing::error!(error = %err, "Failed to read file"); + #[cfg(not(feature = "tracing"))] + let _ = err; let body = ResponseBody::new(UnsyncBoxBody::new( Empty::new().map_err(|err| match err {}).boxed_unsync(), diff --git a/tower-http/src/services/fs/serve_dir/open_file.rs b/tower-http/src/services/fs/serve_dir/open_file.rs index 852b2ee3..b8233b61 100644 --- a/tower-http/src/services/fs/serve_dir/open_file.rs +++ b/tower-http/src/services/fs/serve_dir/open_file.rs @@ -349,7 +349,11 @@ fn append_slash_on_path(uri: Uri) -> Result { }; uri_builder.build().map_err(|err| { + #[cfg(feature = "tracing")] tracing::error!(?err, "redirect uri failed to build"); + #[cfg(not(feature = "tracing"))] + let _ = err; + OpenFileOutput::InvalidRedirectUri }) } From 695246bafe05603c70437c8fc1ca88af3aff2c64 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Thu, 23 Oct 2025 20:41:28 +0200 Subject: [PATCH 2/2] Clarify behavior in docs --- tower-http/src/services/fs/serve_dir/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tower-http/src/services/fs/serve_dir/mod.rs b/tower-http/src/services/fs/serve_dir/mod.rs index 3bc27755..ea1fb583 100644 --- a/tower-http/src/services/fs/serve_dir/mod.rs +++ b/tower-http/src/services/fs/serve_dir/mod.rs @@ -255,7 +255,8 @@ impl ServeDir { /// By default `>::call` will handle IO errors and convert them into /// responses. It does that by converting [`std::io::ErrorKind::NotFound`] and /// [`std::io::ErrorKind::PermissionDenied`] to `404 Not Found` and any other error to `500 - /// Internal Server Error`. The error will also be logged with `tracing`. + /// Internal Server Error`. The error will also be logged with `tracing` in case the `tracing` + /// crate feature is enabled. /// /// If you want to manually control how the error response is generated you can make a new /// service that wraps a `ServeDir` and calls `try_call` instead of `call`.