Skip to content

Commit 4ca98bf

Browse files
author
Andrew
committed
Fix builtin resource/settings discovery when dsc is invoked using a symlink
1 parent 1d8398a commit 4ca98bf

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-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: 15 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,19 @@ fn load_value_from_json(path: &PathBuf, value_name: &str) -> Result<serde_json::
141142
Err(DscError::NotSupported(value_name.to_string()))
142143
}
143144

145+
pub fn get_exe_path() -> Result<PathBuf, DscError> {
146+
if let Ok(exe) = env::current_exe() {
147+
if let Ok(target_path) = fs::read_link(exe.clone()) {
148+
return Ok(target_path);
149+
}
150+
else {
151+
return Ok(exe);
152+
}
153+
}
154+
155+
Err(DscError::NotSupported("Can't get the path to dsc executable".to_string()))
156+
}
157+
144158
#[cfg(target_os = "windows")]
145159
fn get_settings_policy_file_path() -> String
146160
{

0 commit comments

Comments
 (0)