31

I want to know how to suppress a specific compiler warning within VS Code for the entire project.
I have seen this queston: Is it possible to disable specific compiler warnings? but it is for Visual studio, not Visual Studio Code.

Here are the answers that where recommended in the question linked above:
1. Solution Explorer > View > Properties > Build > Suppress Warnings
and
2. #pragma warning disable warning-list

For #1: I can't find the Solution Explorer anywhere within VS Code.

For #2 This only works if I include it at the top of each of the scripts. I need a way to do so for the entire Project.

Updates:

I tried using <noWarn>01699,8019</noWarn> in my .csproj files, but no go.

After reviewing the last changes, I noticed that it had reverted to <noWarn>0169</noWarn>. I then realised that what I needed was <noWarn>0169;8019</noWarn>. Switching the , for a ; Solved the problem.

Well, it turns out that the above solution didn't work after all. As soon as I restarted VS Code, all the warnings came back. Maybe the error code I need isn't 8019, even though it worked as the error code within a #pragma statement. Are the codes used within a <noWarn> different than the codes used at the end of a #pragma statement?

For those saying too switch to VS Community, that's not the point. I'm using VS Code AS a text editor with Unity Editor. I'm looking for which file I need to change and what changes need to be made to apply a statement like #pragma warning disable 8019 to the entire project.

Community
  • 1
  • 1
Patrick vD
  • 684
  • 1
  • 7
  • 24
  • Are you saying #1 above does not work? If so, what are you seeing? – Kory Gill Jan 25 '16 at 22:10
  • VS Code has no Solution Explorer. Or if there is, I can't find it. I'll clarify this. – Patrick vD Jan 25 '16 at 22:11
  • From the answer in the link: In Solution Explorer, choose the project in which you want to suppress warnings. Edit your project properties (under your solution). – Kory Gill Jan 25 '16 at 22:14
  • Where can I find the solution explorer within VS Code? – Patrick vD Jan 25 '16 at 22:16
  • 1
    Now that you have clarified you are using Visual Studio Code, not Visual Studio, unfortunately, I am unable to assist you as I have no familiarity with this. Perhaps a good alternative is a Visual Studio Community Edition which is free and more powerful. – Kory Gill Jan 25 '16 at 22:36
  • I'm using Unity, and wanted to use VS Code instead of MonoDevelop – Patrick vD Jan 25 '16 at 22:42
  • @PatrickvD then use the community edition, VS Code is just a enhanched text editor – Icepickle Jan 26 '16 at 16:10

3 Answers3

32

I was able to get this to work. My solution looks something like this:

<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeFrameworkVersion>2.1.1</RuntimeFrameworkVersion>
    <NoWarn>0169;8019</NoWarn>
</PropertyGroup>

<NoWarn> is PascalCase rather than camelCase, and the element is nested inside of a <PropertyGroup>.

skmichaelson
  • 450
  • 5
  • 7
  • Worked perfectly for me in the following scenario: * Linux (Ubuntu based) * VS Code (1.79) * .NET 7.0.x * C# Dev Kit (extension) NOTE: Using the whole code like `IDE0090` also works inside the tags. – ryuzakyl Jun 28 '23 at 16:30
1

Thanks to some code posted by user rakkarage on the Unity forums I was finally able to fix this by creating the following editor script in my Unity project:

using System.IO;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;

[InitializeOnLoad]
class FixCSProjs : AssetPostprocessor
{
    private static void OnGeneratedCSProjectFiles() {
        Debug.Log("Fixing csproj files...");
        var dir = Directory.GetCurrentDirectory();
        var files = Directory.GetFiles(dir, "*.csproj");
        
        foreach (var file in files) {
            FixProject(file);
        }
    }
    
    static bool FixProject(string file) {
        var text = File.ReadAllText(file);
        var find = "<NoWarn>([0-9;]+)<\\/NoWarn>";
        var replace = "<NoWarn>$1;3003;3009</NoWarn>";
        
        if (Regex.IsMatch(text, find)) {
            text = Regex.Replace(text, find, replace);
            File.WriteAllText(file, text);
            return true;
            
        } else {
            return false;
        }
    }
}

That will add extra warning codes to the NoWarn attribute of each csproj file every time they're automatically generated by Unity. This has the important advantage that it does not involve manually editing files automatically generated by Unity, meaning it's more robust and is appropriate to include in a code repository such as git.

In my case I'm disabling warnings 3003 and 3009 (the CLS-compliance warnings) but just replace 3003;3009 in my code with whatever semicolon-separated warning codes you want and it should do the trick.

I'd also like to say that this is a heinous solution and I really wish there was a better, cleaner way of accomplishing this.

edit: This solution no longer works in the latest versions of Unity, as it no longer calls OnGeneratedCSProjectFiles().

Bri Bri
  • 2,169
  • 3
  • 19
  • 44
1

There's something missing from skmikelson's response, that's "Where do I put the XML code?" Here's a better response (at least it would have helped me.)

  1. Exit out of Visual Studio.

  2. Make a backup of your project file. If your project is named "HelloWorld", and you're using VB, your project file will be "HelloWorld.vbproj". If you are using C#, your project file will be "HelloWorld.csproj".

  3. Use Notepad to manually edit your project file. Don't use Word, or anything else except an editor like Notepad. Notepad will not reformat your file or add extra junk that will corrupt your file.

  4. Search for the string "NoWarn". This tag should be within a PropertyGroup tag.

  5. If the error is IDE0060, then edit the XML so it looks something like this:

<NoWarn>0060
</NoWarn>

Notice that I didn't include the letters "IDE", just the number "0060".

  1. If you have multiple warnings that you want to ignore, separate the codes with semicolons like this:
<NoWarn>0060;0061
</NoWarn>
  1. Save the project file, exit out of Notepad, then reload your project in Visual Studio.

  2. If that doesn't work, then check your project file--there may be multiple instances of the NoWarn tag and you may have to change all of them.

  • It's your turn to explain where to find this project file then.. – gk_2000 Nov 09 '22 at 04:28
  • @gk_2000 You find the Project file by using Windows Explorer or VS Code instead of VS Pro. Pro hides the actual project files in the solution view, providing you with a property editor instead of direct access to the content. – Ryan Grange May 10 '23 at 06:16
  • I mean what location do I navigate to using Windows Explorer? What name should I look for? – gk_2000 May 10 '23 at 16:24