Skip to content

Additional logging around packages.config paths during project inspection #25

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ public Container GetContainer()


bool packagesConfigExists = !String.IsNullOrWhiteSpace(Options.PackagesConfigPath) && File.Exists(Options.PackagesConfigPath);
// File.exists is a safe method that will not throw exceptions and simply return false
if (!packagesConfigExists)
{
Console.WriteLine($"Unable to find packages config file: {Options.PackagesConfigPath}. Checking if file permissions were denied...");
CheckFilePermissions(Options.PackagesConfigPath);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition is to check the theory that file permissions could be an issue.

bool projectJsonExists = !String.IsNullOrWhiteSpace(Options.ProjectJsonPath) && File.Exists(Options.ProjectJsonPath);
bool projectJsonLockExists = !String.IsNullOrWhiteSpace(Options.ProjectJsonLockPath) && File.Exists(Options.ProjectJsonLockPath);
bool projectAssetsJsonExists = !String.IsNullOrWhiteSpace(Options.ProjectAssetsJsonPath) && File.Exists(Options.ProjectAssetsJsonPath);
Expand Down Expand Up @@ -265,6 +272,24 @@ public Container GetContainer()
}


public static void CheckFilePermissions(String path)
{
try
{
// Attempt to read file, will throw UnauthorizedAccessException if denied
using (File.OpenRead(path)) { }
Console.WriteLine("File permissions confirmed.");
}
catch (UnauthorizedAccessException ex)
{
Console.WriteLine("UnauthorizedAccessException when trying to open file. \n" + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Some other exception when trying to open file: " + ex.Message);
}
}

public List<String> FindOutputPaths()
{
//TODO: Move to a new class (OutputPathResolver?)
Expand Down Expand Up @@ -292,7 +317,8 @@ public List<String> FindOutputPaths()
}
catch (Exception e)
{
Console.WriteLine("Skipping configuration output paths.");
Console.WriteLine("Skipping configuration output paths because a problem occurred (likely during project evaluation at target path).");
Console.WriteLine("Exception was: " + e.Message);
return new List<string>() { };
}
}
Expand Down Expand Up @@ -364,6 +390,7 @@ public bool IsExcluded()

private string CreateProjectPackageConfigPath(string projectDirectory)
{
Console.WriteLine($"Creating packages.config path from project directory: {projectDirectory}");
return PathUtil.Combine(projectDirectory, "packages.config");
Copy link
Collaborator Author

@shantyk shantyk Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This addition is to check/highlight the theory that the file path origin could be the issue.

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,16 @@ public Container GetContainer()
.Where(group => group.Count() > 1)
.Select(group => group.Key);

Console.WriteLine("Processing project files found in solution file.");
Copy link
Collaborator Author

@shantyk shantyk Feb 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the problem has to do with the project paths given in their solution file, added further logging in this area to check that (included in exe attached to ticket on Feb 20)

foreach (ProjectFile project in projectFiles)
{
try
{
string projectRelativePath = project.Path;
Console.WriteLine("Project file relative path: '{0}'", projectRelativePath);
Console.WriteLine("Contains null character? " + projectRelativePath.Contains('\0'));
Console.WriteLine("Contains space character? " + projectRelativePath.Contains(' '));

string projectPath = null;
Boolean directoryPackagesExists = false;
if (projectRelativePath.Contains("Directory.Packages.props"))
Expand All @@ -137,7 +142,13 @@ public Container GetContainer()
}
parentPath =
parentPath.Substring(0, OperatingSystem.IsWindows() ? parentPath.LastIndexOf("\\") : parentPath.LastIndexOf("/"));
Console.WriteLine("Parent path is: '{0}'", parentPath);
directoryPackagesExists = File.Exists(checkFile);
if (!directoryPackagesExists)
{
Console.WriteLine($"Unable to find project file: {checkFile}. Checking if file permissions were denied...");
ProjectInspector.CheckFilePermissions(checkFile);
}
fileNotFound = !directoryPackagesExists;
projectPath = checkFile;
}
Expand All @@ -157,10 +168,21 @@ public Container GetContainer()
try
{
projectFileExists = File.Exists(projectPath);
if (!projectFileExists)
{
Console.WriteLine($"Unable to find project file: {projectPath}. Checking if file permissions were denied...");
if (projectPath != null)
{ ProjectInspector.CheckFilePermissions(projectPath);}
}
if (!projectFileExists && !directoryPackagesExists)
{
projectPath = PathUtil.Combine(projectPath, "Directory.Packages.props");
directoryPackagesExists = File.Exists(projectPath);
if (!directoryPackagesExists)
{
Console.WriteLine($"Unable to find project file: {projectPath}. Checking if file permissions were denied...");
ProjectInspector.CheckFilePermissions(projectPath);
}
}

}
Expand Down
1 change: 1 addition & 0 deletions detect-nuget-inspector/detect-nuget-inspector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public static void Main(string[] args)

try
{
Console.WriteLine("Running 4588 Debug version of NuGet Inspector standalone.");
Console.WriteLine("Registering MSBuild defaults.");
MSBuildLocator.RegisterDefaults();
}
Expand Down