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

Emit warning if executable in resource manifest can't be found #688

Merged
merged 4 commits into from
Mar 14, 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
7 changes: 6 additions & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,12 @@ if (!$SkipBuild) {
}
}

Copy-Item "*.dsc.resource.json" $target -Force -ErrorAction Ignore
if ($IsWindows) {
Copy-Item "*.dsc.resource.json" $target -Force -ErrorAction Ignore
}
else { # don't copy WindowsPowerShell resource manifest
Copy-Item "*.dsc.resource.json" $target -Exclude 'windowspowershell.dsc.resource.json' -Force -ErrorAction Ignore
}

# be sure that the files that should be executable are executable
if ($IsLinux -or $IsMacOS) {
Expand Down
25 changes: 25 additions & 0 deletions dsc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions dsc/tests/dsc_discovery.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,41 @@ Describe 'tests for resource discovery' {
$out = dsc resource schema -r abc/def
$LASTEXITCODE | Should -Be 7
}

It 'Verify warning message when executable not found for: <operation>' -TestCases @(
@{ operation = 'get' }
@{ operation = 'set' }
@{ operation = 'test' }
@{ operation = 'delete' }
@{ operation = 'export' }
@{ operation = 'resolve' }
@{ operation = 'whatIf' }
) {
param($operation)

$manifest = @"
{
"`$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
"type": "Test/ExecutableNotFound",
"version": "0.1.0",
"$operation": {
"executable": "doesNotExist"
}
}
"@
$oldPath = $env:DSC_RESOURCE_PATH
try {
$env:DSC_RESOURCE_PATH = $testdrive
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $manifest
$out = dsc resource list 'Test/ExecutableNotFound' 2> "$testdrive/error.txt" | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.Count | Should -Be 1
$out.Type | Should -BeExactly 'Test/ExecutableNotFound'
$out.Kind | Should -BeExactly 'resource'
Get-Content -Path "$testdrive/error.txt" | Should -Match "WARN.*?Executable 'doesNotExist' not found"
}
finally {
$env:DSC_RESOURCE_PATH = $oldPath
}
}
}
54 changes: 54 additions & 0 deletions dsc_lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dsc_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tree-sitter = "0.25"
tree-sitter-rust = "0.23"
tree-sitter-dscexpression = { path = "../tree-sitter-dscexpression" }
uuid = { version = "1.15", features = ["v4"] }
which = "7.0"

[dev-dependencies]
serde_yaml = "0.9"
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 @@ -82,6 +82,7 @@ progressSearching = "Searching for resources"
foundResourceManifest = "Found resource manifest: %{path}"
adapterFound = "Resource adapter '%{adapter}' found"
resourceFound = "Resource '%{resource}' found"
executableNotFound = "Executable '%{executable}' not found for operation '%{operation}' for resource '%{resource}'"

[dscresources.commandResource]
invokeGet = "Invoking get for '%{resource}'"
Expand Down
39 changes: 27 additions & 12 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::discovery::discovery_trait::ResourceDiscovery;
use crate::discovery::convert_wildcard_to_regex;
use crate::dscresources::dscresource::{Capability, DscResource, ImplementedAs};
use crate::dscresources::resource_manifest::{import_manifest, validate_semver, Kind, ResourceManifest};
use crate::dscresources::resource_manifest::{import_manifest, validate_semver, Kind, ResourceManifest, SchemaKind};
use crate::dscresources::command_resource::invoke_command;
use crate::dscerror::DscError;
use crate::progress::{ProgressBar, ProgressFormat};
Expand All @@ -22,6 +22,7 @@ use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use tracing::{debug, info, trace, warn};
use which::which;

use crate::util::get_setting;
use crate::util::get_exe_path;
Expand Down Expand Up @@ -531,33 +532,41 @@ fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
Kind::Resource
};

// all command based resources are required to support `get`
let mut capabilities = if manifest.get.is_some() {
vec![Capability::Get]
} else {
vec![]
};
let mut capabilities: Vec<Capability> = vec![];
if let Some(get) = &manifest.get {
verify_executable(&manifest.resource_type, "get", &get.executable);
capabilities.push(Capability::Get);
}
if let Some(set) = &manifest.set {
verify_executable(&manifest.resource_type, "set", &set.executable);
capabilities.push(Capability::Set);
if set.handles_exist == Some(true) {
capabilities.push(Capability::SetHandlesExist);
}
}
if manifest.what_if.is_some() {
if let Some(what_if) = &manifest.what_if {
verify_executable(&manifest.resource_type, "what_if", &what_if.executable);
capabilities.push(Capability::WhatIf);
}
if manifest.test.is_some() {
if let Some(test) = &manifest.test {
verify_executable(&manifest.resource_type, "test", &test.executable);
capabilities.push(Capability::Test);
}
if manifest.delete.is_some() {
if let Some(delete) = &manifest.delete {
verify_executable(&manifest.resource_type, "delete", &delete.executable);
capabilities.push(Capability::Delete);
}
if manifest.export.is_some() {
if let Some(export) = &manifest.export {
verify_executable(&manifest.resource_type, "export", &export.executable);
capabilities.push(Capability::Export);
}
if manifest.resolve.is_some() {
if let Some(resolve) = &manifest.resolve {
verify_executable(&manifest.resource_type, "resolve", &resolve.executable);
capabilities.push(Capability::Resolve);
}
if let Some(SchemaKind::Command(command)) = &manifest.schema {
verify_executable(&manifest.resource_type, "schema", &command.executable);
}

let resource = DscResource {
type_name: manifest.resource_type.clone(),
Expand All @@ -575,6 +584,12 @@ fn load_manifest(path: &Path) -> Result<DscResource, DscError> {
Ok(resource)
}

fn verify_executable(resource: &str, operation: &str, executable: &str) {
if which(executable).is_err() {
warn!("{}", t!("discovery.commandDiscovery.executableNotFound", resource = resource, operation = operation, executable = executable));
}
}

fn sort_adapters_based_on_lookup_table(unsorted_adapters: &BTreeMap<String, Vec<DscResource>>, needed_resource_types: &Vec<String>) -> LinkedHashMap<String, Vec<DscResource>>
{
let mut result = LinkedHashMap::<String, Vec<DscResource>>::new();
Expand Down
Loading