From 624a7ef533abcf170f162b68445a2584c9687450 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Thu, 23 Oct 2025 20:28:17 +0200 Subject: [PATCH 1/3] 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 9aa3f07f..29d5d84b 100644 --- a/tower-http/Cargo.toml +++ b/tower-http/Cargo.toml @@ -89,7 +89,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", "dep:url", "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 c413338f390cb4f66a3615d8b5dcbbf93f3d41a1 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Thu, 23 Oct 2025 20:41:28 +0200 Subject: [PATCH 2/3] 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`. From 0528e7579130e52d6e7ff0f8689877a0e4aa5ff4 Mon Sep 17 00:00:00 2001 From: ginnyTheCat Date: Mon, 23 Feb 2026 02:37:04 +0100 Subject: [PATCH 3/3] Use _err directly instead of let _ --- tower-http/src/services/fs/serve_dir/mod.rs | 6 ++---- tower-http/src/services/fs/serve_dir/open_file.rs | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/tower-http/src/services/fs/serve_dir/mod.rs b/tower-http/src/services/fs/serve_dir/mod.rs index ea1fb583..c9c4dcbc 100644 --- a/tower-http/src/services/fs/serve_dir/mod.rs +++ b/tower-http/src/services/fs/serve_dir/mod.rs @@ -411,11 +411,9 @@ where let future = self .try_call(req) .map(|result: Result<_, _>| -> Result<_, Infallible> { - let response = result.unwrap_or_else(|err| { + let response = result.unwrap_or_else(|_err| { #[cfg(feature = "tracing")] - tracing::error!(error = %err, "Failed to read file"); - #[cfg(not(feature = "tracing"))] - let _ = err; + tracing::error!(error = %_err, "Failed to read file"); 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 b8233b61..0605b017 100644 --- a/tower-http/src/services/fs/serve_dir/open_file.rs +++ b/tower-http/src/services/fs/serve_dir/open_file.rs @@ -348,11 +348,9 @@ fn append_slash_on_path(uri: Uri) -> Result { uri_builder.path_and_query("/") }; - uri_builder.build().map_err(|err| { + uri_builder.build().map_err(|_err| { #[cfg(feature = "tracing")] - tracing::error!(?err, "redirect uri failed to build"); - #[cfg(not(feature = "tracing"))] - let _ = err; + tracing::error!(?_err, "redirect uri failed to build"); OpenFileOutput::InvalidRedirectUri })