Skip to content
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

Filesystem usage analyzer #3725

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

RRode
Copy link

@RRode RRode commented Nov 2, 2024

Resolves #3643

Implements analyzer that searches for file system usage and suggests IFileSystem usage. Currently checks for usages of File, FileInfo, Directory and DirectoryInfo. No warning is produced, if these are used within IFileSystem implementation.

Analyzer was added to Sentry project and finds usage of file system outside of IFileSystem implementations, which then breaks the build. Usages found in PortablePdbReader.cs, CFunctions.cs and FileDiagnosticLogger.cs.

@bitsandfoxes Is this roughly what you had in mind?

@bruno-garcia
Copy link
Member

This is really awesome and promising. Thank you!

Is there a way to ignore some places where we it would be safe to call from, for example the CFunction one perhaps will be

@RRode
Copy link
Author

RRode commented Nov 4, 2024

Is there a reason why you could not use #pragma warning disable / enable? IMO, with additional comment, this would make it explicit and well documented why the warning should be ignored. But, if you want, I could add some exceptions to the rule. Just let me know what you need.
Also is it OK that I placed the new projects under src and test? For me the analyzer project feels a bit odd there, since it's not a deliverable. I thought about putting both projects under modules, but that seemed a bit off too, since those are git submodules that get pulled in.
Other thing is, would it make sense to put the analyzer into Directory.Build.props in src, so it is added in all src files? I assume that you want to enforce the warning in those projects as well.

@bruno-garcia
Copy link
Member

bruno-garcia commented Nov 4, 2024

Is there a reason why you could not use #pragma warning disable / enable? IMO, with additional comment, this would make it explicit and well documented why the warning should be ignored. But, if you want, I could add some exceptions to the rule. Just let me know what you need.

That makes sense, #pragma sounds like the right solution

Also is it OK that I placed the new projects under src and test? For me the analyzer project feels a bit odd there, since it's not a deliverable. I thought about putting both projects under modules, but that seemed a bit off too, since those are git submodules that get pulled in.

I also think it's kind of odd, but it's not a big deal. Alternatively it could be a new top level directory: analyzers or build-src I dunno. I think as you did it's fine. But worth adding a README.md on the project directory to clarify this analyizer is only for consumption from Sentry itself, for development of the SDK.

Other thing is, would it make sense to put the analyzer into Directory.Build.props in src, so it is added in all src files? I assume that you want to enforce the warning in those projects as well.

We only really need this on Sentry since this is a requirement coming from Sentry.Unity. So, technically we'd want that analyizer there too.

We'd include that here too: https://github.com/getsentry/sentry-unity/tree/main/src/Sentry.Unity
Using relative paths to the sentry-dotnet submodule

<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="SN0001Description" xml:space="preserve">
<value>Route file system access through IFileSystem wrapper.</value><comment>An optional longer localizable description of the diagnostic.</comment>
Copy link
Member

Choose a reason for hiding this comment

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

If it's optional, can we got without them? If not, I suggest taking more details from the ticket that introduced this requirement (tldr: Switch file system requirements)

Suggested change
<value>Route file system access through IFileSystem wrapper.</value><comment>An optional longer localizable description of the diagnostic.</comment>
<value>Route file system access through IFileSystem wrapper.</value>

Copy link
Author

Choose a reason for hiding this comment

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

I completely dropped the resx and hardcoded the warning, since I assume that no warning translations will be needed.

@@ -49,6 +49,10 @@
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Sentry.Analyzers\Sentry.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
Copy link
Member

Choose a reason for hiding this comment

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

Does this need PrivateAssets>all ?

Copy link
Author

Choose a reason for hiding this comment

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

If I understand the docs correctly, it could have even been fine, but I added it anyway. I also added private assets in analyzer references.

Rok Rode added 3 commits November 6, 2024 23:32
Analyzer will only be used internally within SDK, so warning translation is not needed
@RRode
Copy link
Author

RRode commented Nov 6, 2024

That makes sense, #pragma sounds like the right solution

Can you provide some comments or should I just disable the warning in currently offending files?

I also think it's kind of odd, but it's not a big deal. Alternatively it could be a new top level directory: analyzers or build-src I dunno. I think as you did it's fine. But worth adding a README.md on the project directory to clarify this analyizer is only for consumption from Sentry itself, for development of the SDK.

Then I will keep it simple for now and leave it as it is. I added the README, but was not able to remove the one added via Directory.Build.props to the project, so I hope that is OK.

We only really need this on Sentry since this is a requirement coming from Sentry.Unity. So, technically we'd want that analyizer there too.

We'd include that here too: https://github.com/getsentry/sentry-unity/tree/main/src/Sentry.Unity
Using relative paths to the sentry-dotnet submodule

I guess it makes sense to first merge it here and then add it in the other repo, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add analyzer for FileSystem
2 participants