Skip to content

Fix built-in resource and settings discovery when dsc is invoked using a symlink #625

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

Merged
merged 2 commits into from
Jan 16, 2025
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
3 changes: 2 additions & 1 deletion dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing::{debug, info, trace, warn, warn_span};
use tracing_indicatif::span_ext::IndicatifSpanExt;

use crate::util::get_setting;
use crate::util::get_exe_path;

pub struct CommandDiscovery {
// use BTreeMap so that the results are sorted by the typename, the Vec is sorted by version
Expand Down Expand Up @@ -135,7 +136,7 @@ impl CommandDiscovery {

// if exe home is not already in PATH env var then add it to env var and list of searched paths
if !using_custom_path {
if let Some(exe_home) = env::current_exe()?.parent() {
if let Some(exe_home) = get_exe_path()?.parent() {
let exe_home_pb = exe_home.to_path_buf();
if paths.contains(&exe_home_pb) {
trace!("Exe home is already in path: {}", exe_home.to_string_lossy());
Expand Down
21 changes: 20 additions & 1 deletion dsc_lib/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use crate::dscerror::DscError;
use serde_json::Value;
use std::fs;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
Expand Down Expand Up @@ -79,7 +80,7 @@ pub fn get_setting(value_name: &str) -> Result<DscSettingValue, DscError> {
let mut result: DscSettingValue = DscSettingValue::default();
let mut settings_file_path : PathBuf;

if let Some(exe_home) = env::current_exe()?.parent() {
if let Some(exe_home) = get_exe_path()?.parent() {
// First, get setting from the default settings file
settings_file_path = exe_home.join(DEFAULT_SETTINGS_FILE_NAME);
if let Ok(v) = load_value_from_json(&settings_file_path, DEFAULT_SETTINGS_SCHEMA_VERSION) {
Expand Down Expand Up @@ -141,6 +142,24 @@ fn load_value_from_json(path: &PathBuf, value_name: &str) -> Result<serde_json::
Err(DscError::NotSupported(value_name.to_string()))
}

/// Gets path to the current dsc process.
/// If dsc is started using a symlink, this functon returns target of the symlink.
///
/// # Errors
///
/// Will return `Err` if path to the current exe can't be retrived.
pub fn get_exe_path() -> Result<PathBuf, DscError> {
if let Ok(exe) = env::current_exe() {
if let Ok(target_path) = fs::read_link(exe.clone()) {
return Ok(target_path);
};

return Ok(exe);
}

Err(DscError::NotSupported("Can't get the path to dsc executable".to_string()))
Copy link
Preview

Copilot AI Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message could be more informative. Consider including the specific error encountered.

Suggested change
Err(DscError::NotSupported("Can't get the path to dsc executable".to_string()))
Err(DscError::NotSupported(format!("Can't get the path to dsc executable: {}", env::current_exe().unwrap_err())))

Copilot uses AI. Check for mistakes.

}

#[cfg(target_os = "windows")]
fn get_settings_policy_file_path() -> String
{
Expand Down
Loading