I am trying to configure my Licence Header in Visual Studio 2019, but after updating in my profile, it is showing my SonarQube Header Only

I have deactivate currently rule for S1451 and added my custom license header but while fixing it it never givesme updated custome header in visual studio 2019. I need resolution fast otherwise i could lose my job

Hello @AkashU. Welcome to the community.

Your question isn’t very clear. You’ve deactivated rule S1451 so it doesn’t complain about missing file/incorrect headers. What exactly is the problem you are facing, and where does it occur - in the IDE, in CI builds, when analysis results are pushed to the server?

  • are you using Connected Mode in SonarLint?
  • what do you mean by “my profile” and “my SonarQube Header”?
  • how did you deactivate S1451 - in a ruleset on your machine, or in the SonarQube server?
2 Likes

I want to add my License Header in SonarQube can you tell me the procedure . I am unable to add License Header. I am getting warning S1451.

Do you mean you want to configure the value the rule should use in SonarQube when checking the header?

If so, do the following:

  • log in to SonarQube and select the Rules tab, then search to find the specific rule (I did this by setting the search language to C# and searching for the text “header”):

  • select the rule from the list to see the rule detail.
  • click the Activate button at the bottom of the page.

A dialogue will pop up that allows you to configure the header format.
Make sure you change the header for the correct Quality Profile.

That’s the procedure for setting the rule parameter in SonarQube.

What are you trying to do in Visual Studio? Is the problem that you have already set the custom parameter in SonarQube, but that parameter is not being used in Visual Studio so you are still seeing S1451 being raised in the IDE?

1 Like

Yes In Visual Studio It is Still Showing me SonalQube License Header so do i need to change something in visual studio 2019 Enterprise

What should be my Quality Profile

Your question and responses are still very unclear which is making it difficult to help you.

I am assuming the position is as follows:

  • you or someone else has configured the license header parameter in SonarQube.
  • however, the settings are not taken into account in Visual Studio so S1451 is still being reported in the IDE, even though the code files have the correct file headers.

If this is the case, look at the Manual Workaround described in this post which describes how to configure Visual Studio to use the rules settings. In short, you have to manually create a settings file in your solution and modify the projects to reference it.

Please ignore all my previous questions. I have followed all above steps. but still in Visual Studio 2019, when i try to resolved my S1451 warning in a page using visual studio using show potential fixes feature it by default adds SonarQube License but not my updated License Header.

Ok. So it sounds like your problem is the following:

I have configured the expected header text for S1451 in SonarQube.
SonarLint is correctly raising issues for files with incorrect headers. However, if I use the lightbuild “suggested fix” feature, the proposed header text isn’t what I have configured in SonarQube:

The fix is the one given in the Manual Workaround I referred to in my previous answer. The steps are as follows:

  1. create an XML file in your project to contain the rules settings e.g. SonarLint.xml
  2. add the file to the project and set the Build Action to AdditionalFiles:

  1. edit the XML in the file to contain the expected header:
<?xml version="1.0" encoding="UTF-8"?>
<AnalysisInput>
  <Rules>
    <Rule>
      <Key>S1451</Key>
      <Parameters>
        <Parameter>
          <Key>headerFormat</Key>
          <Value><![CDATA[/*
 * 
 * Copyright (C) 2019-2020 ACME2
 * mailto: contact AT acme DOT com
 *
 */
]]></Value>
        </Parameter>
        <Parameter>
          <!-- <Key>isRegularExpression</Key> -->
          <Value>false</Value>
        </Parameter>
      </Parameters>
    </Rule>
  </Rules>
</AnalysisInput>

You might need to reload the solution or restart VS for it to pick up the changes.

You should then see your license text in the quick fix window e.g.

This is my project structure, where should i add my xml file, It is Blazor App I am trying to add my file in VS 2019Project Structure

It doesn’t matter where you put the file, although since you only need one copy of it I would put it at solution level.

However, the file needs to be referenced from every project, so you can either

  • add the AdditionalFiles reference to every project, or
  • create a Directory.Build.props file at the solution level that will be picked up by all of the projects, and add the AdditionalFiles reference to that.

The Directory.Build.props file would look like this:

<Project>

  <ItemGroup>
    <!-- Assumes the SonarLint.xml file is in the same directory -->
    <AdditionalFiles Include="$(MSBuildThisFileDirectory)\SonarLint.xml">
      <Link>SonarLint.xml</Link>
    </AdditionalFiles>
  </ItemGroup>

</Project>
  1. Okay Can you tell the steps to do it in more detail
  2. whenever i add my .xml in my solution and right click on it to change build action i do not get the option to set its build path
  1. No. It’s described in the Microsoft documentation I referred to: Directory.Build.props
  2. If you are using Directory.Build.props with the XML content I gave above you don’t need to set the build action in the IDE; the XML already contains the correct content.