diff --git a/Disassembler/AssemblyLoader.cs b/Disassembler/AssemblyLoader.cs index 397a28d..8fc39f1 100644 --- a/Disassembler/AssemblyLoader.cs +++ b/Disassembler/AssemblyLoader.cs @@ -37,7 +37,15 @@ internal class AssemblyLoader public AssemblyLoader(string assemblyPath) { this.assemblyPath = assemblyPath; - context = new LocalFolderLoadContext(Path.GetDirectoryName(assemblyPath)); + var assemDir = Path.GetDirectoryName(assemblyPath); + if (assemDir != null) + { + context = new LocalFolderLoadContext(assemDir); + } + else + { + throw new Exception("Unexpected null getting assembly directory."); + } } public Assembly? Load() diff --git a/Disassembler/CompiledFileLocator.cs b/Disassembler/CompiledFileLocator.cs index 42b0a2a..c8f8ae4 100644 --- a/Disassembler/CompiledFileLocator.cs +++ b/Disassembler/CompiledFileLocator.cs @@ -53,9 +53,9 @@ private List ReadCompiledFile(string file) } } } - catch (Exception ex) + catch (Exception) { - + //ignore } return result; } diff --git a/DotNETDepends/Dependencies.cs b/DotNETDepends/Dependencies.cs index aae3994..f50a691 100644 --- a/DotNETDepends/Dependencies.cs +++ b/DotNETDepends/Dependencies.cs @@ -100,7 +100,7 @@ public List FindSourceSymbolReferences(SourceType symbol) { foreach (var typeRef in type.TypeReferences) { - if (typeRef.Name.Equals(symbol.Name) && typeRef.Namespace.Equals(symbol.Namespace)) + if (typeRef.Name.Equals(symbol.Name) && symbol.Namespace.Equals(typeRef.Namespace)) { result.Add(type); break; diff --git a/DotNETDepends/PublishedWebProject.cs b/DotNETDepends/PublishedWebProject.cs index a2fbed0..e7c535e 100644 --- a/DotNETDepends/PublishedWebProject.cs +++ b/DotNETDepends/PublishedWebProject.cs @@ -40,10 +40,14 @@ private static void AddFilesToList(List paths, FileInfo[] files) private List GetSourceFiles() { var result = new List(); - var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath)); - AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories)); - AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories)); - AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories)); + var dir = Path.GetDirectoryName(project.FilePath); + if (dir != null) + { + var dirInfo = new DirectoryInfo(dir); + AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories)); + AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories)); + AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories)); + } return result; } @@ -105,7 +109,7 @@ private void ReadProjectFile(IErrorReporter errorReporter) { if (project.FilePath != null && assemblyName != null) { - var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath), "bin", config }); + var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath)?? "", "bin", config }); DirectoryInfo dirInfo = new(binRoot); var dirs = dirInfo.GetDirectories(); string? assemPath = null; diff --git a/DotNETDepends/RoslynProject.cs b/DotNETDepends/RoslynProject.cs index 2b88a53..e43c112 100644 --- a/DotNETDepends/RoslynProject.cs +++ b/DotNETDepends/RoslynProject.cs @@ -41,19 +41,20 @@ internal class RoslynProject protected readonly Project project; private readonly IErrorReporter errorReporter; private readonly Dependencies dependencies; - private readonly string solutionRoot; + private readonly string? solutionRoot; public RoslynProject(Project project, Dependencies dependencies, IErrorReporter errorReporter) { this.project = project; this.dependencies = dependencies; this.errorReporter = errorReporter; solutionRoot = Path.GetDirectoryName(project.Solution.FilePath); + } public async Task Analyze() { var compilation = await project.GetCompilationAsync().ConfigureAwait(false); - if (compilation != null) + if (compilation != null && solutionRoot != null) { foreach (var tree in compilation.SyntaxTrees) diff --git a/DotNETDepends/SolutionReader.cs b/DotNETDepends/SolutionReader.cs index db0395a..b339973 100644 --- a/DotNETDepends/SolutionReader.cs +++ b/DotNETDepends/SolutionReader.cs @@ -211,21 +211,25 @@ private static bool ProjectContainsNETWebFiles(Project project) { if (project.FilePath != null) { - var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath)); - var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories); - if (cshtml.Length > 0) + var projDir = Path.GetDirectoryName(project.FilePath); + if (projDir != null) { - return true; - } - var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories); - if (vbhtml.Length > 0) - { - return true; - } - var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories); - if (razor.Length > 0) - { - return true; + var dirInfo = new DirectoryInfo(projDir); + var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories); + if (cshtml.Length > 0) + { + return true; + } + var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories); + if (vbhtml.Length > 0) + { + return true; + } + var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories); + if (razor.Length > 0) + { + return true; + } } } return false; diff --git a/DotNETDepends/SourceTypeBuilderFactory.cs b/DotNETDepends/SourceTypeBuilderFactory.cs index f722925..3e0dab3 100644 --- a/DotNETDepends/SourceTypeBuilderFactory.cs +++ b/DotNETDepends/SourceTypeBuilderFactory.cs @@ -79,7 +79,7 @@ internal abstract class SourceTypeBuilderBase : SourceTypeBuilder { return null; } - return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir), projectDir, importFileName, importFileExtension, out typeRoot); + return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir) ?? "", projectDir, importFileName, importFileExtension, out typeRoot); } /* @@ -89,28 +89,30 @@ protected string DetermineNamespaceFromPath(string path, string? rootNamespace, { var relativePath = Path.GetRelativePath(projectDir, path); relativePath = Path.GetDirectoryName(relativePath); - if (relativePath.StartsWith(Path.DirectorySeparatorChar)) + if (relativePath != null) { - relativePath = relativePath.Substring(1); - } - if (relativePath.EndsWith(Path.DirectorySeparatorChar)) - { - relativePath = relativePath[..^1]; - } - relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.'); - if (rootNamespace != null && rootNamespace.Length != 0) - { - if (relativePath.Length > 0) + if (relativePath.StartsWith(Path.DirectorySeparatorChar)) { - return rootNamespace + "." + relativePath; + relativePath = relativePath.Substring(1); } - else + if (relativePath.EndsWith(Path.DirectorySeparatorChar)) { - return rootNamespace; + relativePath = relativePath[..^1]; + } + relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.'); + if (rootNamespace != null && rootNamespace.Length != 0) + { + if (relativePath.Length > 0) + { + return rootNamespace + "." + relativePath; + } + else + { + return rootNamespace; + } } } - - return relativePath; + return relativePath ?? ""; } } @@ -124,7 +126,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string var ns = ReadNamespaceFromFile(filePath); if (ns == null) { - ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_Imports", ".razor", out string? typeRoot); + ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_Imports", ".razor", out string? typeRoot); ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir); } ns ??= ""; @@ -145,7 +147,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string var ns = ReadNamespaceFromFile(filePath); if (ns == null) { - ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot); + ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot); ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir); } ns ??= ""; @@ -160,15 +162,19 @@ private string GetTypeName(string filePath, string projectDir) { var relativeFile = Path.GetRelativePath(projectDir, filePath); var relativeDir = Path.GetDirectoryName(relativeFile); - if (relativeDir.StartsWith(Path.DirectorySeparatorChar)) - { - relativeDir = relativeDir.Substring(1); - } - if (relativeDir.EndsWith(Path.DirectorySeparatorChar)) + if (relativeDir != null) { - relativeDir = relativeDir[..^1]; + if (relativeDir.StartsWith(Path.DirectorySeparatorChar)) + { + relativeDir = relativeDir.Substring(1); + } + if (relativeDir.EndsWith(Path.DirectorySeparatorChar)) + { + relativeDir = relativeDir[..^1]; + } + return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath); } - return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath); + return ""; } } internal class SourceTypeBuilderFactory diff --git a/TestDependencyReading/TestUtils.cs b/TestDependencyReading/TestUtils.cs index d8c8452..dc24ed2 100644 --- a/TestDependencyReading/TestUtils.cs +++ b/TestDependencyReading/TestUtils.cs @@ -8,13 +8,19 @@ namespace TestDependencyReading { internal class TestUtils { - public static string? GetFixturesPath() + public static string GetFixturesPath() { - string workingDir = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)); - while(workingDir != null && !workingDir.EndsWith("TestDependencyReading")) { - workingDir= Path.GetDirectoryName(workingDir); + var assembly = System.Reflection.Assembly.GetExecutingAssembly(); + if (assembly != null && assembly.Location != null) + { + string? workingDir = Path.GetDirectoryName(assembly.Location); + while (workingDir != null && !workingDir.EndsWith("TestDependencyReading")) + { + workingDir = Path.GetDirectoryName(workingDir); + } + return Path.Combine(workingDir ?? "", "Fixtures"); } - return Path.Combine(workingDir, "Fixtures"); + throw new Exception("Unable to determine fixture directory."); } } }