@@ -20,14 +20,13 @@ use serde::Deserialize;
2020use std:: { collections:: { BTreeMap , HashMap , HashSet } , sync:: { LazyLock , RwLock } } ;
2121use std:: env;
2222use std:: ffi:: OsStr ;
23- use std:: fs;
23+ use std:: fs:: { create_dir_all , read , read_to_string , write } ;
2424use std:: path:: { Path , PathBuf } ;
2525use std:: str:: FromStr ;
2626use tracing:: { debug, info, trace, warn} ;
27- use which:: which;
2827
2928use crate :: util:: get_setting;
30- use crate :: util:: get_exe_path;
29+ use crate :: util:: { canonicalize_which , get_exe_path} ;
3130
3231const DSC_EXTENSION_EXTENSIONS : [ & str ; 3 ] = [ ".dsc.extension.json" , ".dsc.extension.yaml" , ".dsc.extension.yml" ] ;
3332const DSC_MANIFEST_LIST_EXTENSIONS : [ & str ; 3 ] = [ ".dsc.manifests.json" , ".dsc.manifests.yaml" , ".dsc.manifests.yml" ] ;
@@ -621,7 +620,7 @@ fn insert_resource(resources: &mut BTreeMap<String, Vec<DscResource>>, resource:
621620///
622621/// * Returns a `DscError` if the manifest could not be loaded or parsed.
623622pub fn load_manifest ( path : & Path ) -> Result < Vec < ImportedManifest > , DscError > {
624- let contents = fs :: read_to_string ( path) ?;
623+ let contents = read_to_string ( path) ?;
625624 let file_name_lowercase = path. file_name ( ) . and_then ( OsStr :: to_str) . unwrap_or ( "" ) . to_lowercase ( ) ;
626625 let extension_is_json = path. extension ( ) . is_some_and ( |ext| ext. eq_ignore_ascii_case ( "json" ) ) ;
627626 if DSC_RESOURCE_EXTENSIONS . iter ( ) . any ( |ext| file_name_lowercase. ends_with ( ext) ) {
@@ -711,38 +710,38 @@ fn load_resource_manifest(path: &Path, manifest: &ResourceManifest) -> Result<Ds
711710
712711 let mut capabilities: Vec < Capability > = vec ! [ ] ;
713712 if let Some ( get) = & manifest. get {
714- verify_executable ( & manifest. resource_type , "get" , & get. executable ) ;
713+ verify_executable ( & manifest. resource_type , "get" , & get. executable , path . parent ( ) . unwrap ( ) ) ;
715714 capabilities. push ( Capability :: Get ) ;
716715 }
717716 if let Some ( set) = & manifest. set {
718- verify_executable ( & manifest. resource_type , "set" , & set. executable ) ;
717+ verify_executable ( & manifest. resource_type , "set" , & set. executable , path . parent ( ) . unwrap ( ) ) ;
719718 capabilities. push ( Capability :: Set ) ;
720719 if set. handles_exist == Some ( true ) {
721720 capabilities. push ( Capability :: SetHandlesExist ) ;
722721 }
723722 }
724723 if let Some ( what_if) = & manifest. what_if {
725- verify_executable ( & manifest. resource_type , "what_if" , & what_if. executable ) ;
724+ verify_executable ( & manifest. resource_type , "what_if" , & what_if. executable , path . parent ( ) . unwrap ( ) ) ;
726725 capabilities. push ( Capability :: WhatIf ) ;
727726 }
728727 if let Some ( test) = & manifest. test {
729- verify_executable ( & manifest. resource_type , "test" , & test. executable ) ;
728+ verify_executable ( & manifest. resource_type , "test" , & test. executable , path . parent ( ) . unwrap ( ) ) ;
730729 capabilities. push ( Capability :: Test ) ;
731730 }
732731 if let Some ( delete) = & manifest. delete {
733- verify_executable ( & manifest. resource_type , "delete" , & delete. executable ) ;
732+ verify_executable ( & manifest. resource_type , "delete" , & delete. executable , path . parent ( ) . unwrap ( ) ) ;
734733 capabilities. push ( Capability :: Delete ) ;
735734 }
736735 if let Some ( export) = & manifest. export {
737- verify_executable ( & manifest. resource_type , "export" , & export. executable ) ;
736+ verify_executable ( & manifest. resource_type , "export" , & export. executable , path . parent ( ) . unwrap ( ) ) ;
738737 capabilities. push ( Capability :: Export ) ;
739738 }
740739 if let Some ( resolve) = & manifest. resolve {
741- verify_executable ( & manifest. resource_type , "resolve" , & resolve. executable ) ;
740+ verify_executable ( & manifest. resource_type , "resolve" , & resolve. executable , path . parent ( ) . unwrap ( ) ) ;
742741 capabilities. push ( Capability :: Resolve ) ;
743742 }
744743 if let Some ( SchemaKind :: Command ( command) ) = & manifest. schema {
745- verify_executable ( & manifest. resource_type , "schema" , & command. executable ) ;
744+ verify_executable ( & manifest. resource_type , "schema" , & command. executable , path . parent ( ) . unwrap ( ) ) ;
746745 }
747746
748747 let resource = DscResource {
@@ -768,15 +767,15 @@ fn load_extension_manifest(path: &Path, manifest: &ExtensionManifest) -> Result<
768767
769768 let mut capabilities: Vec < dscextension:: Capability > = vec ! [ ] ;
770769 if let Some ( discover) = & manifest. discover {
771- verify_executable ( & manifest. r#type , "discover" , & discover. executable ) ;
770+ verify_executable ( & manifest. r#type , "discover" , & discover. executable , path . parent ( ) . unwrap ( ) ) ;
772771 capabilities. push ( dscextension:: Capability :: Discover ) ;
773772 }
774773 if let Some ( secret) = & manifest. secret {
775- verify_executable ( & manifest. r#type , "secret" , & secret. executable ) ;
774+ verify_executable ( & manifest. r#type , "secret" , & secret. executable , path . parent ( ) . unwrap ( ) ) ;
776775 capabilities. push ( dscextension:: Capability :: Secret ) ;
777776 }
778777 let import_extensions = if let Some ( import) = & manifest. import {
779- verify_executable ( & manifest. r#type , "import" , & import. executable ) ;
778+ verify_executable ( & manifest. r#type , "import" , & import. executable , path . parent ( ) . unwrap ( ) ) ;
780779 capabilities. push ( dscextension:: Capability :: Import ) ;
781780 if import. file_extensions . is_empty ( ) {
782781 warn ! ( "{}" , t!( "discovery.commandDiscovery.importExtensionsEmpty" , extension = manifest. r#type) ) ;
@@ -803,8 +802,8 @@ fn load_extension_manifest(path: &Path, manifest: &ExtensionManifest) -> Result<
803802 Ok ( extension)
804803}
805804
806- fn verify_executable ( resource : & str , operation : & str , executable : & str ) {
807- if which ( executable) . is_err ( ) {
805+ fn verify_executable ( resource : & str , operation : & str , executable : & str , directory : & Path ) {
806+ if canonicalize_which ( executable, Some ( directory . to_string_lossy ( ) . as_ref ( ) ) ) . is_err ( ) {
808807 info ! ( "{}" , t!( "discovery.commandDiscovery.executableNotFound" , resource = resource, operation = operation, executable = executable) ) ;
809808 }
810809}
@@ -839,8 +838,8 @@ fn save_adapted_resources_lookup_table(lookup_table: &HashMap<String, String>)
839838
840839 let path = std:: path:: Path :: new ( & file_path) ;
841840 if let Some ( prefix) = path. parent ( ) {
842- if fs :: create_dir_all ( prefix) . is_ok ( ) {
843- if fs :: write ( file_path. clone ( ) , lookup_table_json) . is_err ( ) {
841+ if create_dir_all ( prefix) . is_ok ( ) {
842+ if write ( file_path. clone ( ) , lookup_table_json) . is_err ( ) {
844843 info ! ( "Unable to write lookup_table file {file_path:?}" ) ;
845844 }
846845 } else {
@@ -858,7 +857,7 @@ fn load_adapted_resources_lookup_table() -> HashMap<String, String>
858857{
859858 let file_path = get_lookup_table_file_path ( ) ;
860859
861- let lookup_table: HashMap < String , String > = match fs :: read ( file_path. clone ( ) ) {
860+ let lookup_table: HashMap < String , String > = match read ( file_path. clone ( ) ) {
862861 Ok ( data) => { serde_json:: from_slice ( & data) . unwrap_or_default ( ) } ,
863862 Err ( _) => { HashMap :: new ( ) }
864863 } ;
0 commit comments