@@ -90,24 +90,28 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
9090 // replace mismatched reference paths if requested by the user
9191 if ( referencesToEdit . Remove ( libraryName ) ) {
9292 if ( installedPackages . TryGetValue ( libraryName , out var version ) ) {
93- var fullLibraryPath = Program . CalculateFullPathToLibrary ( packagesFolder , libraryName , version ) ;
94- if ( reference != fullLibraryPath ) {
95- Console . WriteLine ( $ "Changing reference from { reference } to { fullLibraryPath } ") ;
96- reference = fullLibraryPath ;
97- content [ i ] = referenceHeader + fullLibraryPath ;
98- changed = true ;
93+ var fullLibraryPath = Program . FindFullLibraryPath ( packagesFolder , libraryName , version ) ;
94+ if ( fullLibraryPath != null ) {
95+ if ( reference != fullLibraryPath ) {
96+ Console . WriteLine ( $ "Changing reference from { reference } to { fullLibraryPath } ") ;
97+ reference = fullLibraryPath ;
98+ content [ i ] = referenceHeader + fullLibraryPath ;
99+ changed = true ;
100+ } else {
101+ if ( config . LogSkipped )
102+ Console . WriteLine ( $ "Skipping reference replacement for { fullLibraryPath } which already matched") ;
103+ }
99104 } else {
100- if ( config . LogSkipped )
101- Console . WriteLine ( $ "Skipping reference replacement for { fullLibraryPath } which already matched") ;
105+ Console . Error . WriteLine ( $ "Unable to find library { libraryName } in packages folder") ;
102106 }
103107 } else {
104108 Console . Error . WriteLine ( $ "Unable to find existing reference { libraryName } in project file") ;
105109 }
106110 }
107111
108112 var refPath = Path . GetFullPath ( Path . Combine ( contentFile . DirectoryName , reference ) ) ;
109- Program . SafeAssemblyLoad ( refPath ) ;
110- Console . WriteLine ( $ "Using reference { refPath } ") ;
113+ if ( Program . SafeAssemblyLoad ( refPath ) )
114+ Console . WriteLine ( $ "Using reference { refPath } ") ;
111115 }
112116
113117 if ( referencesToEdit . Count > 0 ) {
@@ -126,14 +130,14 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
126130 // add references that aren't in the content file yet
127131 foreach ( var reference in referencesToEdit ) {
128132 if ( installedPackages . TryGetValue ( reference , out var version ) ) {
129- try {
130- var path = Program . CalculateFullPathToLibrary ( packagesFolder , reference , version ) ;
133+ var path = Program . FindFullLibraryPath ( packagesFolder , reference , version ) ;
134+ if ( path != null ) {
131135 content . Insert ( lastReferenceLine ++ , referenceHeader + path ) ;
132136 changed = true ;
133- Program . SafeAssemblyLoad ( path ) ;
134- Console . WriteLine ( $ "Adding reference { path } ") ;
135- } catch ( Exception e ) {
136- Console . Error . WriteLine ( $ "Error adding reference { reference } : { e } ") ;
137+ if ( Program . SafeAssemblyLoad ( path ) )
138+ Console . WriteLine ( $ "Adding reference { path } ") ;
139+ } else {
140+ Console . Error . WriteLine ( $ "Unable to find library { reference } in packages folder ") ;
137141 }
138142 } else {
139143 Console . Error . WriteLine ( $ "Unable to find configured reference { reference } in project file") ;
@@ -227,11 +231,13 @@ private static int ProcessContentFile(FileInfo contentFile, string packagesFolde
227231 return 0 ;
228232 }
229233
230- private static void SafeAssemblyLoad ( string refPath ) {
234+ private static bool SafeAssemblyLoad ( string refPath ) {
231235 try {
232236 Assembly . LoadFrom ( refPath ) ;
237+ return true ;
233238 } catch ( Exception e ) {
234239 Console . Error . WriteLine ( $ "Error loading reference { refPath } : { e } ") ;
240+ return false ;
235241 }
236242 }
237243
@@ -247,8 +253,21 @@ private static Dictionary<string, string> ExtractPackagesFromProject(string cspr
247253 return ret ;
248254 }
249255
250- private static string CalculateFullPathToLibrary ( string packageFolder , string libraryName , string referencesVersion ) {
251- return Path . Combine ( packageFolder , libraryName . ToLowerInvariant ( ) , referencesVersion , "tools" , $ "{ libraryName } .dll") . Replace ( '\\ ' , '/' ) ;
256+ private static string FindFullLibraryPath ( string packageFolder , string libraryName , string version ) {
257+ // we try several paths, finally arriving at the least specific one (which is just any file with the dll name)
258+ foreach ( var dir in new [ ] { "tools" , "lib/net*" , "*" } ) {
259+ foreach ( var name in new [ ] { libraryName . ToLowerInvariant ( ) , libraryName } ) {
260+ var basePath = Path . Combine ( packageFolder , name , version ) ;
261+ if ( ! Directory . Exists ( basePath ) )
262+ continue ;
263+ foreach ( var subdir in Directory . GetDirectories ( basePath , dir , dir == "*" ? SearchOption . AllDirectories : SearchOption . TopDirectoryOnly ) ) {
264+ var path = Path . Combine ( subdir , $ "{ libraryName } .dll") ;
265+ if ( File . Exists ( path ) )
266+ return path . Replace ( '\\ ' , '/' ) ;
267+ }
268+ }
269+ }
270+ return null ;
252271 }
253272
254273 private static ( List < ImporterInfo > , List < string > ) GetContentData ( ) {
0 commit comments