Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruff server: Support the usage of tildes and environment variables in logFile #11945

Merged
merged 5 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/ruff_server/docs/setup/HELIX.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,5 @@ environment = { "RUFF_TRACE" = "messages" }

[language-server.ruff.config.settings]
logLevel = "debug"
logFile = "/Users/developer/.cache/helix/ruff.log"
logFile = "~/.cache/helix/ruff.log"
```
2 changes: 1 addition & 1 deletion crates/ruff_server/docs/setup/NEOVIM.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ require('lspconfig').ruff.setup {
init_options = {
settings = {
logLevel = "debug",
logFile = "your/log/file/path/log.txt"
logFile = "~/.local/state/nvim/ruff.log"
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_server/src/session/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub(crate) struct ClientSettings {
#[serde(rename_all = "camelCase")]
pub(crate) struct TracingSettings {
pub(crate) log_level: Option<crate::trace::LogLevel>,
/// Path to the log file - tildes and environment variables are supported.
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
pub(crate) log_file: Option<PathBuf>,
}

Expand Down
33 changes: 25 additions & 8 deletions crates/ruff_server/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
//! A `logFile` path can also be specified in the settings, and output will be directed there instead.
use lsp_types::TraceValue;
use serde::Deserialize;
use std::sync::{Arc, Mutex, OnceLock};
use std::{
path::PathBuf,
str::FromStr,
sync::{Arc, Mutex, OnceLock},
};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{
fmt::{time::Uptime, writer::BoxMakeWriter},
Expand Down Expand Up @@ -48,13 +52,26 @@ pub(crate) fn init_tracing(
.set(sender)
.expect("logging sender should only be initialized once");

let log_file = log_file.and_then(|path| {
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.ok()
});
let log_file = log_file
.map(|path| {
// this expands `logFile` so that tildes and environment variables
// are replaced with their values, if possible.
if let Some(expanded) = shellexpand::full(&path.to_string_lossy())
.ok()
.and_then(|path| PathBuf::from_str(&path).ok())
{
expanded
} else {
path.to_path_buf()
}
})
.and_then(|path| {
std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(path)
.ok()
snowsignal marked this conversation as resolved.
Show resolved Hide resolved
});

let subscriber = tracing_subscriber::Registry::default().with(
tracing_subscriber::fmt::layer()
Expand Down
Loading