-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
Convert relative paths to absolute paths for analyzer references #232
Convert relative paths to absolute paths for analyzer references #232
Conversation
I would wonder, can you create a unit tests that fails without your changes, and succeeds with? |
To create unit tests is kind of chicken and egg problem because in order for me to do that, I would need to include a copy of the old method which does not have the file path conversion code in it which wouldn't make sense. I can provide a logical justification though which is from the Microsoft Roslyn code that is being called. Here is a link to the Microsoft code for the AnalyzerFileReference that is being created in that method. A copy of the constructor follows for reference:
You can see from both the XML doc comments and the first line of the constructor that a full path is expected so defensive programming would dictate that the caller's of this method should verify they are passing full paths. With my patch that is now what is occurring. |
I think you misunderstood me. I asked for a test that would fail if you (temporary) disable your change, and would succeed again, after you re-enable it. Just to ensure that nobody in the future can break it again (unintentionally). (Note that I'm not the owner of the lib, so it are just my 2cts anyway) |
Here is a minimum VS2022 C# project which demonstrates calling the |
Thanks for looking into this (and sorry for the late reply)! I guess my question/concern here is what base path is being used to convert a relative path into an absolute path? In the new calls to |
I am not sure of the correct answer to your inquiry but I can show the relative references that were received by the extension and what full path references it resolved them to. In this example the relative references were resolved to the Here are the relative references as passed into the
Here is how
|
I'm trying to move forward with some outstanding Buildalyzer stuff this week, so I finally gave this a closer look. I understand the problem, and we probably don't want relative paths here. That said, a relative path is always relative to something and I still don't love that this change is based on the current path, whatever that is. This is from the docs:
The examples of what's it's returning and what you're expecting were very helpful. From that I can see that the relative paths are rooted at the project file, and I think that's a reasonable path to use as the base (I honestly don't know what else we possibly could use). I'll merge this PR, but then I'll go ahead and patch the code to use the path of the project file as the base path to make the absolute path calculation deterministic. |
Thanks for working on this! |
This PR fixes an issue where the
AnalyzerResultExtensions.GetAnalyzerReferences
method allows the return of relative paths instead of absolute paths as expected by Roslyn and causing an exception to be thrown.Depending on the execution environment: from within Visual Studio, from the command line by running the executable, or via
dotnet run
some references may be passed in as relative paths. If these references are then passed to the Roslyn subsystem then you will see an exception similar to below (call stack has been abbreviated and code related to PR is highlighted):This PR fixes that issue by using the
Path.GetFullPath
.NET method to convert all paths in the extension method to absolute paths before returning them from the method.