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

Set $(TreatWarningsAsErrors) = true for Release builds. #9689

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jan 15, 2025

Modern best practices for software development recommend treating warnings as errors. Although the majority of warnings are benign, warnings for any potential actual issues will be lost in the sea of benign warnings and will not be caught.

However, having to fix warnings during the development process can be annoying as code is often in a temporary state. As a balance, we only error on warnings for Release builds. This way local development isn't hindered, but CI will protect us from committing new warnings.

There are several projects that have warnings today. For now, we will simply grandfather those projects in, allowing us to move forward with protecting the rest of the projects from new warnings. With time, we should endeavor to fix these projects' warnings and remove them from this list. (Our build currently has ~170 warnings spread across these projects.)

@jpobst jpobst force-pushed the dev/jpobst/treatwarningsaserrors branch 4 times, most recently from 5cd10cc to 9575cdd Compare January 16, 2025 18:10
@jpobst jpobst force-pushed the dev/jpobst/treatwarningsaserrors branch from 9575cdd to 5a1b14d Compare January 16, 2025 19:00
@dotnet dotnet deleted a comment from azure-pipelines bot Jan 16, 2025
@jpobst
Copy link
Contributor Author

jpobst commented Jan 16, 2025

Tagging the whole team on this one, as this is something we should decide as a team.

@jpobst jpobst marked this pull request as ready for review January 16, 2025 23:55
@jpobst jpobst requested a review from jonpryor as a code owner January 16, 2025 23:55
@grendello
Copy link
Contributor

I agree that we should have a "zero warnings" policy. Treating warnings as errors for release builds only is a good idea.

@moljac
Copy link
Contributor

moljac commented Jan 17, 2025

I like to listen and understand other side of the argument and usually they state that when warnings are treated as errors, then semantically we don't need warnings. Everything is an error.

From quality standpoint this is excellent, but from feasibility (in some cases) it could cause problems. Not sure if I would be able in my human lifetime to clean up all warnings in Android Libraries.

But agreed it is easier to move abstraction "WarningAsError" to build (Release) than to design diagnostic messages without Warning category/class.

Approved

<_AllowProjectWarnings Condition=" '$(MSBuildProjectFile)' == 'Xamarin.Android.JcwGen-Tests.csproj' ">true</_AllowProjectWarnings>
<_AllowProjectWarnings Condition=" '$(MSBuildProjectFile)' == 'Xamarin.Android.NUnitLite.NET.csproj' ">true</_AllowProjectWarnings>
<_AllowProjectWarnings Condition=" '$(MSBuildProjectFile)' == 'Xamarin.ProjectTools.csproj' ">true</_AllowProjectWarnings>
<TreatWarningsAsErrors Condition=" '$(Configuration)' == 'Release' AND $(_AllowProjectWarnings) != 'true' ">true</TreatWarningsAsErrors>
Copy link
Member

@pjcollins pjcollins Jan 17, 2025

Choose a reason for hiding this comment

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

When I build locally, I'm not adding -p:Configuration=Release and I'd want to be able to catch these issues before pushing to CI and waiting for results. I wonder if we should enable this for both Debug and Release? I suppose it may be a bit annoying to work through these early in process, but I think it would be preferable to know if you're adding a breaking change before you draft a commit/PR rather than later.

Copy link
Contributor

Choose a reason for hiding this comment

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

I often use warnings to "debug" stuff, it would break workloads like that. For Debug, perhaps we should add a private property to enable warnings-as-errors?

Copy link
Contributor

Choose a reason for hiding this comment

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

I use Release builds locally also since allot of our unit tests only work in a Release build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The thought was that when you are writing early code you're just first trying to get something working, and having to fix NRT warnings or such takes you out of the flow. So only enforcing warnings on Release was what we went with in JI:

https://github.com/dotnet/java-interop/blob/d5dfa0aac009ac4a8525f35a21dd469b8ccbc155/Directory.Build.props#L6

However, not seeing errors until CI can also be annoying. Especially since android CI takes much longer than JI CI.

There are other options we could consider:

  • Keying it off of $(RunningOnCI) instead of Release.
  • Additionally adding a local opt-in property that could be enabled. This could be set as an environment variable on ones local machine if someone prefers to always have warnings as errors enabled.

Copy link
Member

Choose a reason for hiding this comment

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

Happy to be overruled here, it sounds like keeping this as currently implemented is preferable by most

Copy link
Member

Choose a reason for hiding this comment

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

I'm in favor of doing something that would help us slowly solve the warnings over time.

@jpobst maybe you can use $(RunningOnCI)=true for these checks for now?

I often use warnings to "debug" stuff, it would break workloads like that. For Debug, perhaps we should add a private property to enable warnings-as-errors?

@grendello you can set $(TreatWarningsAsErrors) for this. We still have Configuration.Override.props if you want to set it that way, or an env var should also work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@grendello you can set $(TreatWarningsAsErrors) for this. We still have Configuration.Override.props if you want to set it that way, or an env var should also work.

I don't think this will work today, as it would also enable it for all the projects with existing warnings. We would need a custom one for now to hit the grandfathering logic in this PR. But ideally we would eventually be able to just use the global one.

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.

6 participants