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

fix environment loading, add windows support, add show env command #3326

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
23 changes: 16 additions & 7 deletions lapce-app/src/app.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[cfg(target_os = "windows")]
use std::os::windows::process::CommandExt;
use std::{
io::{BufReader, Read, Write},
io::{BufReader, IsTerminal, Read, Write},
ops::Range,
path::PathBuf,
process::Stdio,
Expand Down Expand Up @@ -3573,9 +3573,9 @@ pub fn launch() {
)));
}

// if PWD is not set, then we are not being launched via a terminal
#[cfg(any(target_os = "macos", target_os = "linux"))]
if std::env::var("PWD").is_err() {
let stdin = std::io::stdin();
if !stdin.is_terminal() {
trace!(TraceLevel::INFO, "Loading custom environment from shell");
load_shell_env();
}

Expand Down Expand Up @@ -3770,12 +3770,12 @@ pub fn launch() {
}

/// Uses a login shell to load the correct shell environment for the current user.
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn load_shell_env() {
use std::process::Command;

use tracing::warn;

#[cfg(not(windows))]
let shell = match std::env::var("SHELL") {
Ok(s) => s,
Err(error) => {
Expand All @@ -3788,9 +3788,16 @@ fn load_shell_env() {
}
};

#[cfg(windows)]
let shell = "powershell";

let mut command = Command::new(shell);

command.args(["--login"]).args(["-c", "printenv"]);
#[cfg(not(windows))]
command.args(["--login", "-c", "printenv"]);

#[cfg(windows)]
command.args(["{ ls env: | foreach { '{0}={1}' -f $_.Name, $_.Value } }"]);

let env = match command.output() {
Ok(output) => String::from_utf8(output.stdout).unwrap_or_default(),
Expand All @@ -3808,7 +3815,9 @@ fn load_shell_env() {
.filter_map(|line| line.split_once('='))
.for_each(|(key, value)| {
if let Ok(v) = std::env::var(key) {
warn!("Overwriting '{key}', previous value: '{v}', new value '{value}'");
if v != value {
warn!("Overwriting '{key}', previous value: '{v}', new value '{value}'");
}
};
std::env::set_var(key, value);
})
Expand Down
3 changes: 2 additions & 1 deletion lapce-app/src/app/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub(super) fn logging() -> (Handle<Targets>, Option<WorkerGuard>) {
let log_file_filter_targets = filter::Targets::new()
.with_target("lapce_app", LevelFilter::DEBUG)
.with_target("lapce_proxy", LevelFilter::DEBUG)
.with_target("lapce_core", LevelFilter::DEBUG);
.with_target("lapce_core", LevelFilter::DEBUG)
.with_default(LevelFilter::from_level(TraceLevel::INFO));
let (log_file_filter, reload_handle) =
reload::Subscriber::new(log_file_filter_targets);

Expand Down
4 changes: 4 additions & 0 deletions lapce-app/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ pub enum LapceWorkbenchCommand {
#[strum(message = "Open Internal UI Inspector")]
OpenUIInspector,

#[strum(serialize = "show_env")]
#[strum(message = "Show Environment")]
ShowEnvironment,

#[strum(serialize = "change_color_theme")]
#[strum(message = "Change Color Theme")]
ChangeColorTheme,
Expand Down
15 changes: 15 additions & 0 deletions lapce-app/src/main_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,21 @@ impl MainSplitData {
}
}
}

pub fn show_env(&self) {
let child = self.new_file();
if let EditorTabChild::Editor(id) = child {
if let Some(editor) = self.editors.editor_untracked(id) {
let doc = editor.doc();
doc.reload(
Rope::from(
std::env::vars().map(|(k, v)| format!("{k}={v}")).join("\n"),
),
true,
);
}
}
}
}

fn workspace_edits(edit: &WorkspaceEdit) -> Option<HashMap<Url, Vec<TextEdit>>> {
Expand Down
3 changes: 3 additions & 0 deletions lapce-app/src/window_tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,9 @@ impl WindowTabData {
OpenUIInspector => {
self.common.view_id.get_untracked().inspect();
}
ShowEnvironment => {
self.main_split.show_env();
}

// ==== Source Control ====
SourceControlInit => {
Expand Down