Patrick vD
Patrick vD

Reputation: 789

Disabling a specific compiler warning in VS Code

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.

Upvotes: 35

Views: 27625

Answers (3)

Douglas Hahn
Douglas Hahn

Reputation: 61

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.

Upvotes: 1

Bri Bri
Bri Bri

Reputation: 2204

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().

Upvotes: 1

skmichaelson
skmichaelson

Reputation: 510

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>.

Upvotes: 38

Related Questions