Skip to content

Commit 6f39f53

Browse files
committed
Fix DSC_RESOURCE_PATH env var to also limit exe search
1 parent 04dc007 commit 6f39f53

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,32 @@ Describe 'tests for resource discovery' {
220220
$env:DSC_RESOURCE_PATH = $oldPath
221221
}
222222
}
223+
224+
It 'DSC_RESOURCE_PATH should be used for executable lookup' {
225+
$dscTest = Get-Command dscecho -ErrorAction Stop
226+
$target = if ($IsWindows) {
227+
'echoIt.exe'
228+
} else {
229+
'echoIt'
230+
}
231+
Copy-Item -Path "$($dscTest.Source)" -Destination "$testdrive\$target"
232+
$manifest = Get-Content -Raw -Path "$(Split-Path -Path $dscTest.Source -Parent)\echo.dsc.resource.json" | ConvertFrom-Json
233+
$manifest.type = 'Test/MyEcho'
234+
$manifest.get.executable = $target
235+
$manifest.set = $null
236+
$manifest.test = $null
237+
$manifest.schema.command.executable = $target
238+
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value ($manifest | ConvertTo-Json -Depth 10)
239+
240+
$oldPath = $env:DSC_RESOURCE_PATH
241+
try {
242+
$env:DSC_RESOURCE_PATH = $testdrive
243+
$out = dsc resource get -r 'Test/MyEcho' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
244+
$LASTEXITCODE | Should -Be 0
245+
$out.actualState.output | Should -BeExactly 'Custom'
246+
}
247+
finally {
248+
$env:DSC_RESOURCE_PATH = $oldPath
249+
}
250+
}
223251
}

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,16 @@ impl CommandDiscovery {
149149
let mut uniques: HashSet<PathBuf> = HashSet::new();
150150
paths.retain(|e|uniques.insert((*e).clone()));
151151

152-
// if exe home is not already in PATH env var then add it to env var and list of searched paths
153-
if !using_custom_path {
152+
if using_custom_path {
153+
// when using custom path, intent is to isolate the search of manifests and executables to the custom path
154+
// so we replace the PATH with the custom path
155+
if let Ok(new_path) = env::join_paths(paths.clone()) {
156+
env::set_var("PATH", new_path);
157+
} else {
158+
return Err(DscError::Operation(t!("discovery.commandDiscovery.failedSetEnvPath").to_string()));
159+
}
160+
} else {
161+
// if exe home is not already in PATH env var then add it to env var and list of searched paths
154162
if let Some(exe_home) = get_exe_path()?.parent() {
155163
let exe_home_pb = exe_home.to_path_buf();
156164
if paths.contains(&exe_home_pb) {

0 commit comments

Comments
 (0)