Skip to content

Conversation

RRode
Copy link
Contributor

@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?

#skip-changelog

@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
Contributor 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

@@ -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
Contributor 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
Contributor 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?

Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

Looks good.

Doesn't seem to be draft anymore, ready for a final pass and merge?

Comment on lines 11 to 13

<RootNamespace>Sentry.Analyzers</RootNamespace>
<AssemblyName>Sentry.Analyzers</AssemblyName>
Copy link
Member

Choose a reason for hiding this comment

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

afaik these default to the project name so are redundant

Suggested change
<RootNamespace>Sentry.Analyzers</RootNamespace>
<AssemblyName>Sentry.Analyzers</AssemblyName>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed them

@bruno-garcia
Copy link
Member

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

The current ones can be skipped, thanks

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.

Sounds good

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

Definitely, that was just a note for reference, doesn't affect this PR. Can be a follow up if you'd like to contribute further 🙏

@RRode
Copy link
Contributor Author

RRode commented Nov 10, 2024

The current ones can be skipped, thanks

I added the pragmas in all the places, but this requires changes to this submodule. If I understand correctly this needs to be merged first to main and then reference in this PR adjusted? I am not used to this submodule workflow, so I hope I am doing it right. 😅

Definitely, that was just a note for reference, doesn't affect this PR. Can be a follow up if you'd like to contribute further 🙏

I will give it a try. 🙂

@RRode RRode marked this pull request as ready for review November 10, 2024 10:04
Copy link
Member

@bruno-garcia bruno-garcia left a comment

Choose a reason for hiding this comment

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

I love this! thank you so much

@@ -30,7 +30,10 @@ public FileDiagnosticLogger(string path, bool alsoWriteToConsole = false)
public FileDiagnosticLogger(string path, SentryLevel minimalLevel, bool alsoWriteToConsole = false)
: base(minimalLevel)
{
// Allow direct file system usage
#pragma warning disable SN0001
Copy link
Member

Choose a reason for hiding this comment

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

Ah good point, @bitsandfoxes anything we need to do to make this "Not available on Switch" ?

Edge case since someoen needs to get out of their way to use this and its' just for debugging, but this analyzier really help us make these calls now.

Not doing anything about this (just ignoring it) is fine by me too but we should add a note to the docs that this thing isn't supported on Switch anyway:

https://docs.sentry.io/platforms/unity/configuration/diagnostic-logger/#FileDiagnosticLogger

@bruno-garcia
Copy link
Member

I'll leave it for @jamescrosswell to do the merging as he's the maintainer but LGTM! 🙏

Improved comment

Co-authored-by: Bruno Garcia <[email protected]>
@jamescrosswell
Copy link
Collaborator

jamescrosswell commented Nov 12, 2024

I added the pragmas in all the places, but this requires changes to this submodule. If I understand correctly this needs to be merged first to main and then reference in this PR adjusted? I am not used to this submodule workflow, so I hope I am doing it right. 😅

Hey @RRode, up until now we've been maintaining the Sentry fork of Ben.Demystifier purely for use in the sentry-dotnet repository so the workflows for changing code in there are a bit obscure and not very contributor friendly (the code used by sentry-dotnet isn't in the main branch, for example). I'll do a bit of head scratching to see if we could make this easier.

Worst case, I can cherry pick the change from your PR and get it in there but I'd rather tidy up the submodule so it's easier to accept contributions. Give me a couple days.

@jamescrosswell
Copy link
Collaborator

jamescrosswell commented Nov 13, 2024

Worst case, I can cherry pick the change from your PR and get it in there but I'd rather tidy up the submodule so it's easier to accept contributions. Give me a couple days.

Hi @RRode, I ended up just cherry picking your changes and pushing these up.

@RRode
Copy link
Contributor Author

RRode commented Nov 13, 2024

@jamescrosswell I pushed the missing warning disable for AndroidHelpers

@jamescrosswell
Copy link
Collaborator

@jamescrosswell I pushed the missing warning disable for AndroidHelpers

Thanks @RRode - apologies in advance if the build breaks on this one. GitHub just changed their macos-14 runners (no longer support XCode 16.0) so we need to bump to macos-15 but that's missing a bunch of stuff we need. Once this is merged, we can pull the changes into this branch and the build should work again:

@RRode
Copy link
Contributor Author

RRode commented Nov 14, 2024

@jamescrosswell no problem. What about this format code check? Is there some doing from my side? If I understand this correctly, git fails to do something with my branch.

@jamescrosswell
Copy link
Collaborator

@jamescrosswell no problem. What about this format code check? Is there some doing from my side? If I understand this correctly, git fails to do something with my branch.

Hm, possibly it's getting confused because there are multiple remotes defined for the Ben.Demystifier submodule (getsentry/Ben.Demystifier and benadams/Ben.Demystifier). The benadams (upstream) seems to be dead - it hasn't been updated forever and we can't get in touch with Ben so maybe we just remove that remote.

I'll look into it once we've fixed the macos builds.

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

Attention: Patch coverage is 93.54839% with 2 lines in your changes missing coverage. Please review.

Project coverage is 76.47%. Comparing base (495e742) to head (dde783f).
Report is 377 commits behind head on main.

Files with missing lines Patch % Lines
src/Sentry.Analyzers/FileSystemAnalyzer.cs 93.54% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3725      +/-   ##
==========================================
+ Coverage   75.73%   76.47%   +0.74%     
==========================================
  Files         357      388      +31     
  Lines       13466    14196     +730     
  Branches     2671     2857     +186     
==========================================
+ Hits        10198    10857     +659     
- Misses       2593     2642      +49     
- Partials      675      697      +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

@jamescrosswell
Copy link
Collaborator

@jamescrosswell no problem. What about this format code check? Is there some doing from my side? If I understand this correctly, git fails to do something with my branch.

Hm, actually I think it might be an issue with scripts/commit-formatted-code.sh:

git checkout ${GITHUB_BRANCH}

In your case, the PR is coming from a fork so it's not finding the filesystem-usage-analyzer branch (it needs to checkout RRode:filesystem-usage-analyzer).

I'm in GMT+12 so it's kind of late on a Friday night here, but I'll look into it on the weekend or early next week.

@jamescrosswell jamescrosswell merged commit 1928a48 into getsentry:main Nov 20, 2024
21 checks passed
@jamescrosswell
Copy link
Collaborator

Finally got it over the line. Thanks for your patience and huge thank you for the PR @RRode !

@RRode RRode deleted the filesystem-usage-analyzer branch November 20, 2024 20:52
@RRode
Copy link
Contributor Author

RRode commented Nov 20, 2024

Happy to help! 🙂

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
3 participants