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

Add DSC home directory to PATH to find included resources #499

Merged
merged 4 commits into from
Jul 25, 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
19 changes: 19 additions & 0 deletions dsc/tests/dsc_osinfo.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,23 @@ Describe 'Tests for osinfo examples' {
$LASTEXITCODE | Should -Be 0
$out.results[0].result.inDesiredState | Should -Be $IsMacOS
}

It 'Verify dsc home directory is added to PATH to find included resources' -Tag z1{
$oldPath = $env:PATH
$oldLocation = Get-Location
try {
$exe_path = (Get-Command dsc).Path | Split-Path
$exe_path | Split-Path | Set-Location
# Remove exe_path from PATH if it is there
$new_path = ($oldPath.Split([System.IO.Path]::PathSeparator) | Where-Object { $_ -ne $exe_path }) -join [System.IO.Path]::PathSeparator
$env:PATH = $new_path

$null = & "$exe_path/dsc" config test -p "$PSScriptRoot/../examples/osinfo_parameters.dsc.yaml"
$LASTEXITCODE | Should -Be 0
}
finally {
$env:PATH = $oldPath
$oldLocation | Set-Location
}
}
}
27 changes: 18 additions & 9 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CommandDiscovery {
trace!("DSC_RESOURCE_PATH not set, trying PATH");
match env::var_os("PATH") {
Some(value) => {
trace!("Using PATH: {:?}", value.to_string_lossy());
trace!("Original PATH: {:?}", value.to_string_lossy());
value
},
None => {
Expand All @@ -58,18 +58,27 @@ impl CommandDiscovery {
};

let mut paths = env::split_paths(&path_env).collect::<Vec<_>>();
// remove duplicate entries
let mut uniques = HashSet::new();
paths.retain(|e|uniques.insert((*e).clone()));
anmenaga marked this conversation as resolved.
Show resolved Hide resolved

// add exe home to start of path
// 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() {
debug!("Adding exe home to path: {}", exe_home.to_string_lossy());
paths.insert(0, exe_home.to_path_buf());
}
}
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());
} else {
trace!("Adding exe home to path: {}", exe_home.to_string_lossy());
paths.push(exe_home_pb);

// remove duplicate entries to improve perf of resource search
let mut uniques = HashSet::new();
paths.retain(|e|uniques.insert((*e).clone()));
if let Ok(new_path) = env::join_paths(paths.clone()) {
debug!("Using PATH: {:?}", new_path.to_string_lossy());
env::set_var("PATH", &new_path);
}
}
}
};

Ok(paths)
}
Expand Down
Loading