Skip to content

Commit 466b4f6

Browse files
authored
Merge pull request #499 from anmenaga/issue_494
Add DSC home directory to PATH to find included resources
2 parents 79bb481 + 48edaf0 commit 466b4f6

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

dsc/tests/dsc_osinfo.tests.ps1

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@ Describe 'Tests for osinfo examples' {
1818
$LASTEXITCODE | Should -Be 0
1919
$out.results[0].result.inDesiredState | Should -Be $IsMacOS
2020
}
21+
22+
It 'Verify dsc home directory is added to PATH to find included resources' -Tag z1{
23+
$oldPath = $env:PATH
24+
$oldLocation = Get-Location
25+
try {
26+
$exe_path = (Get-Command dsc).Path | Split-Path
27+
$exe_path | Split-Path | Set-Location
28+
# Remove exe_path from PATH if it is there
29+
$new_path = ($oldPath.Split([System.IO.Path]::PathSeparator) | Where-Object { $_ -ne $exe_path }) -join [System.IO.Path]::PathSeparator
30+
$env:PATH = $new_path
31+
32+
$null = & "$exe_path/dsc" config test -p "$PSScriptRoot/../examples/osinfo_parameters.dsc.yaml"
33+
$LASTEXITCODE | Should -Be 0
34+
}
35+
finally {
36+
$env:PATH = $oldPath
37+
$oldLocation | Set-Location
38+
}
39+
}
2140
}

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl CommandDiscovery {
4848
trace!("DSC_RESOURCE_PATH not set, trying PATH");
4949
match env::var_os("PATH") {
5050
Some(value) => {
51-
trace!("Using PATH: {:?}", value.to_string_lossy());
51+
trace!("Original PATH: {:?}", value.to_string_lossy());
5252
value
5353
},
5454
None => {
@@ -58,18 +58,27 @@ impl CommandDiscovery {
5858
};
5959

6060
let mut paths = env::split_paths(&path_env).collect::<Vec<_>>();
61+
// remove duplicate entries
62+
let mut uniques = HashSet::new();
63+
paths.retain(|e|uniques.insert((*e).clone()));
6164

62-
// add exe home to start of path
65+
// if exe home is not already in PATH env var then add it to env var and list of searched paths
6366
if !using_custom_path {
6467
if let Some(exe_home) = env::current_exe()?.parent() {
65-
debug!("Adding exe home to path: {}", exe_home.to_string_lossy());
66-
paths.insert(0, exe_home.to_path_buf());
67-
}
68-
}
68+
let exe_home_pb = exe_home.to_path_buf();
69+
if paths.contains(&exe_home_pb) {
70+
trace!("Exe home is already in path: {}", exe_home.to_string_lossy());
71+
} else {
72+
trace!("Adding exe home to path: {}", exe_home.to_string_lossy());
73+
paths.push(exe_home_pb);
6974

70-
// remove duplicate entries to improve perf of resource search
71-
let mut uniques = HashSet::new();
72-
paths.retain(|e|uniques.insert((*e).clone()));
75+
if let Ok(new_path) = env::join_paths(paths.clone()) {
76+
debug!("Using PATH: {:?}", new_path.to_string_lossy());
77+
env::set_var("PATH", &new_path);
78+
}
79+
}
80+
}
81+
};
7382

7483
Ok(paths)
7584
}

0 commit comments

Comments
 (0)