Skip to content

Commit 1e31d85

Browse files
authored
Merge pull request #625 from anmenaga/issue_618
Fix built-in resource and settings discovery when dsc is invoked using a symlink
2 parents 1d8398a + 956aefe commit 1e31d85

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use tracing::{debug, info, trace, warn, warn_span};
2424
use tracing_indicatif::span_ext::IndicatifSpanExt;
2525

2626
use crate::util::get_setting;
27+
use crate::util::get_exe_path;
2728

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

136137
// if exe home is not already in PATH env var then add it to env var and list of searched paths
137138
if !using_custom_path {
138-
if let Some(exe_home) = env::current_exe()?.parent() {
139+
if let Some(exe_home) = get_exe_path()?.parent() {
139140
let exe_home_pb = exe_home.to_path_buf();
140141
if paths.contains(&exe_home_pb) {
141142
trace!("Exe home is already in path: {}", exe_home.to_string_lossy());

dsc_lib/src/util.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use crate::dscerror::DscError;
55
use serde_json::Value;
6+
use std::fs;
67
use std::fs::File;
78
use std::io::BufReader;
89
use std::path::PathBuf;
@@ -79,7 +80,7 @@ pub fn get_setting(value_name: &str) -> Result<DscSettingValue, DscError> {
7980
let mut result: DscSettingValue = DscSettingValue::default();
8081
let mut settings_file_path : PathBuf;
8182

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

145+
/// Gets path to the current dsc process.
146+
/// If dsc is started using a symlink, this functon returns target of the symlink.
147+
///
148+
/// # Errors
149+
///
150+
/// Will return `Err` if path to the current exe can't be retrived.
151+
pub fn get_exe_path() -> Result<PathBuf, DscError> {
152+
if let Ok(exe) = env::current_exe() {
153+
if let Ok(target_path) = fs::read_link(exe.clone()) {
154+
return Ok(target_path);
155+
};
156+
157+
return Ok(exe);
158+
}
159+
160+
Err(DscError::NotSupported("Can't get the path to dsc executable".to_string()))
161+
}
162+
144163
#[cfg(target_os = "windows")]
145164
fn get_settings_policy_file_path() -> String
146165
{

0 commit comments

Comments
 (0)