Skip to content

Fix DSC_RESOURCE_PATH env var to also limit exe search #825

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

Merged
merged 4 commits into from
Jun 4, 2025
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
31 changes: 31 additions & 0 deletions dsc/tests/dsc_discovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,35 @@ Describe 'tests for resource discovery' {
$env:DSC_RESOURCE_PATH = $oldPath
}
}

It 'DSC_RESOURCE_PATH should be used for executable lookup' {
$dscTest = Get-Command dscecho -ErrorAction Stop
$target = if ($IsWindows) {
'echoIt.exe'
} else {
'echoIt'
}
Copy-Item -Path "$($dscTest.Source)" -Destination "$testdrive\$target"
$manifest = Get-Content -Raw -Path "$(Split-Path -Path $dscTest.Source -Parent)\echo.dsc.resource.json" | ConvertFrom-Json
$manifest.type = 'Test/MyEcho'
$manifest.get.executable = $target
$manifest.set = $null
$manifest.test = $null
$manifest.schema.command.executable = $target
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value ($manifest | ConvertTo-Json -Depth 10)

$oldPath = $env:DSC_RESOURCE_PATH
try {
$env:DSC_RESOURCE_PATH = $testdrive
$out = dsc resource get -r 'Test/MyEcho' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.actualState.output | Should -BeExactly 'Custom'
dsc resource get -r 'Microsoft.DSC.Debug/Echo' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 7
Get-Content -Raw -Path "$testdrive/error.txt" | Should -Match "ERROR.*?Resource not found"
}
finally {
$env:DSC_RESOURCE_PATH = $oldPath
}
}
}
7 changes: 3 additions & 4 deletions dsc/tests/dsc_extension_discover.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
Describe 'Discover extension tests' {
BeforeAll {
$oldPath = $env:PATH
$separator = [System.IO.Path]::PathSeparator
$toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover"
$env:PATH = "$toolPath$separator$oldPath"
$env:PATH = "$toolPath" + [System.IO.Path]::PathSeparator + $oldPath
}

AfterAll {
Expand Down Expand Up @@ -75,10 +74,10 @@ Describe 'Discover extension tests' {
Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json
Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null
Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null
$env:DSC_RESOURCE_PATH = $TestDrive
$env:DSC_RESOURCE_PATH = "$TestDrive" + [System.IO.Path]::PathSeparator + (Split-Path (Get-Command pwsh).Source -Parent)
try {
$out = dsc extension list | ConvertFrom-Json
$out.Count | Should -Be 1
$out.Count | Should -Be 1 -Because ($out | Out-String)
$out.type | Should -Be 'Test/DiscoverRelative'
$out = dsc resource list 2> $TestDrive/error.log
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)
Expand Down
2 changes: 1 addition & 1 deletion dsc/tests/dsc_resource_input.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Describe 'tests for resource input' {
}
'@
$oldPath = $env:DSC_RESOURCE_PATH
$env:DSC_RESOURCE_PATH = $TestDrive
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
Set-Content $TestDrive/EnvVarInput.dsc.resource.json -Value $manifest
}

Expand Down
4 changes: 2 additions & 2 deletions dsc/tests/dsc_set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ changedProperties:

$oldPath = $env:DSC_RESOURCE_PATH
try {
$env:DSC_RESOURCE_PATH = $TestDrive
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
$out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - --output-format $format | Out-String
$LASTEXITCODE | Should -Be 0
$out.Trim() | Should -BeExactly $expected
Expand All @@ -160,7 +160,7 @@ changedProperties:
It 'set can be used on a resource that does not implement test' {
$oldPath = $env:DSC_RESOURCE_PATH
try {
$env:DSC_RESOURCE_PATH = $TestDrive
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
$out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.BeforeState.test | Should -Be $true
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ couldNotReadSetting = "Could not read 'resourcePath' setting"
appendingEnvPath = "Appending PATH to resourcePath"
originalPath = "Original PATH: %{path}"
failedGetEnvPath = "Failed to get PATH environment variable"
failedJoinEnvPath = "Failed to join PATH environment variable"
exeHomeAlreadyInPath = "Exe home is already in path: %{path}"
addExeHomeToPath = "Adding exe home to path: %{path}"
usingResourcePath = "Using Resource Path: %{path}"
Expand Down
12 changes: 10 additions & 2 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,16 @@ impl CommandDiscovery {
let mut uniques: HashSet<PathBuf> = HashSet::new();
paths.retain(|e|uniques.insert((*e).clone()));

// 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 using_custom_path {
// when using custom path, intent is to isolate the search of manifests and executables to the custom path
// so we replace the PATH with the custom path
if let Ok(new_path) = env::join_paths(paths.clone()) {
env::set_var("PATH", new_path);
} else {
return Err(DscError::Operation(t!("discovery.commandDiscovery.failedJoinEnvPath").to_string()));
}
} else {
// if exe home is not already in PATH env var then add it to env var and list of searched paths
if let Some(exe_home) = get_exe_path()?.parent() {
let exe_home_pb = exe_home.to_path_buf();
if paths.contains(&exe_home_pb) {
Expand Down
10 changes: 1 addition & 9 deletions runcommandonset/tests/runcommandonset.get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@
# Licensed under the MIT License.

Describe 'tests for runcommandonset get' {
BeforeAll {
$oldPath = $env:DSC_RESOURCE_PATH
$env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".."
}

AfterAll {
$env:DSC_RESOURCE_PATH = $oldPath
}

It 'Input passed for executable, arguments, and exit code' {
$json = @"
{
Expand All @@ -21,6 +12,7 @@ Describe 'tests for runcommandonset get' {
"@

$result = $json | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$result.actualState.arguments | Should -BeExactly @('bar', 'baz')
$result.actualState.executable | Should -BeExactly 'foo'
$result.actualState.exitCode | Should -BeExactly 5
Expand Down
9 changes: 0 additions & 9 deletions runcommandonset/tests/runcommandonset.set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,12 @@
# Licensed under the MIT License.

Describe 'tests for runcommandonset set' {
BeforeAll {
$oldPath = $env:DSC_RESOURCE_PATH
$env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".."
}

AfterEach {
if (Test-Path $TestDrive/output.txt) {
Remove-Item -Path $TestDrive/output.txt
}
}

AfterAll {
$env:DSC_RESOURCE_PATH = $oldPath
}

It 'Input for executable and arguments can be sent to the resource' {
$input_json = @"
{
Expand Down
Loading