Resolving "The file header copyright text should match the copyright text from the settings"

  • ALM used Azure DevOps
  • CI system used Azure DevOps
  • Scanner command used
 task: SonarCloudPrepare@1
   inputs:
     SonarCloud: 'Sonarcloud Connection'
     organization: 'us'
     scannerMode: 'MSBuild'
     projectKey: 'Us_OurSerivce'
     projectName: 'OurService'
     extraProperties: |
         sonar.cs.opencover.reportsPaths=$(Common.TestResultsDirectory)\*.opencover.xml
         sonar.exclusions=**.js,**.css,**.scss,**.less,**.html,**.xml
         sonar.coverage.exclusions=**\OurProject.OurService.Web.UnitTests\*.cs,**\Program.cs,**\Startup.cs,**/wwwroot/**
         sonar.verbose=true
     condition: succeeded()

I am getting “The file header copyright text should match the copyright text from the settings” Code Smells (54 of them) but I beleive that I have configured it correctly.

I have a stylecop.json with a documentationRules node

"documentationRules": {
      "companyName": "Magic Bullet Ltd",
      "copyrightText": "Copyright (c) {companyName}. All rights reserved."
    }

Is that correct or are the “settings” elsewhere?

  • C#

Hello,

Yes, you should set it up in SonarCloud, to your organization rule configuration - https://sonarcloud.io/organizations/<YOUR_ORGANIZATION>/rules. Search for Track lack of copyright and license headers or the rule ID S1451 and filter by the language.

Scroll down below the rule description and click change:

You can then set the header and change the severity of the rule.

Is there a Token for filename? In fact is there a list of supported tokens?

e.g.

// <copyright file="TheClass.cs" company="<Company-Name>">

You can use regular expressions in your headerFormat. See the isRegularExpression dropdown.

@Andrei_Epure, after much regex fiddling I have an expression that matches the header we want. Using RegEx101.com

This

\/\/ -{71,}\n\/\/ <copyright file=".*\.cs" company=".*">\n\/\/ Copyright \(c\) 20(19|20|21|22) All Rights Reserved.\n\/\/ <\/copyright>\n\/\/ -{71,}

Matches this required header

// -----------------------------------------------------------------------
// <copyright file="MyFile.cs" company="Our Company">
//       Copyright (c) 2021 All Rights Reserved.
// </copyright>
// -----------------------------------------------------------------------

However when I use SonarLint to insert\validate that header it leaves out the top and bottom lines of “-”

// -----------------------------------------------------------------------

Do you know why that might be?

I checked the sonarlint.xml and it has the expected XML

<Rule>
      <Key>S1451</Key>
      <Parameters>
        <Parameter>
          <Key>headerFormat</Key>
          <Value>\/\/ -{71,}\n\/\/ &lt;copyright file=".*\.cs" company=".*"&gt;\n\/\/       Copyright \(c\) 20(19|20|21|22) All Rights Reserved.\n\/\/ &lt;\/copyright&gt;\n\/\/ -{71,}</Value>
        </Parameter>
        <Parameter>
          <Key>isRegularExpression</Key>
          <Value>true</Value>
        </Parameter>
      </Parameters>
    </Rule>

Can anyone offer some guidance on this issue with the missing top and bottom lines of the header?

If this isn’t the right place to get support on this issue can you point me in the right direction

Hello @pat_munkiisoft

I apologize for the delay. Our implementation CheckFileLicenseBase uses the .NET Regular Expression library System.Text.RegularExpressions.Regex - did you test your regular expression in a .NET Console playground app before configuring in SonarCloud? Did it work?

This website does not have the .NET regular expression engine available. How did you test your regex with .NET?

Please debug your regular expression in .NET. If that works in a .NET reproducer and does not work in SonarCloud, we will investigate (and we would appreciate providing a .NET reproducer test that will help us investigate).

You can also check our test cases.

Thanks @Andrei_Epure

I have updated the regEx and ran it thought a simple .NET Console application. I assume this is what you mean by a “.NET Reproducer test”

	string strRegex = @"\/\/ -{71,}(\n|\r|\r\n)\/\/ <copyright file="".*\.cs"" company="".*"">(\n|\r|\r\n)\/\/\s{5}Copyright \(c\) Us Ltd. All rights reserved.(\n|\r|\r\n)\/\/ <\/copyright>(\n|\r|\r\n)\/\/ -{71,}";
			Regex myRegex = new Regex(strRegex, RegexOptions.None);
			string strTargetString =
@"// -----------------------------------------------------------------------
// <copyright file=""BrowseObservationsController.cs"" company=""Us Ltd"">
//     Copyright (c) Us Ltd. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------";

			foreach (Match myMatch in myRegex.Matches(strTargetString))
			{
				if (myMatch.Success)
				{
					Console.WriteLine($"Index {myMatch.Index} Length {myMatch.Length}");
					Console.WriteLine(myMatch.ToString());
				}
			}

			Console.ReadLine();

This matches.

  1. If I updated the rules file in VS2019 and run Code Analysis I still get Errors for the header in both VS2019 and https://sonarcloud.io/ report
  2. I have dropped the Year from the copyright as despite us wanting to put the year in the copyright line I cannot be sure how to format the RegEx so that appears. I know how to test for it but I cannot see where RegEx rules for checking occurence meet the rules needed by VS2019|Sonarsource to insert the year into the Header when I choose “Add file header” in VS.

Hi @pat_munkiisoft

I tried with \/\/ -{71,}(\n|\r|\r\n)\/\/ <copyright file=".*\.cs" company=".*">(\n|\r|\r\n)\/\/\s{5}Copyright \(c\) Us Ltd. All rights reserved.(\n|\r|\r\n)\/\/ <\/copyright>(\n|\r|\r\n)\/\/ -{71,} and it worked with your test case.

Indeed, getting the right regex right can be difficult.

Also, in SonarCloud you need the “clean” regex, and when trying to use it in C# you need to escape characters so it’s difficult to test your regex.

But this is a regular expression problem rather than a SonarCloud problem. From our point of view, the rule works as expected, as long as the correct regular expression is provided.

Have a nice weekend,
Andrei

Thanks Andrei. If I take that cleaned RegEx and configure the rule in sonarcloud.io, then “Update” the SonarQube rules in VS2019 I do now get a clean analysis in the IDE.

However when I push the updated changes to a branch AND check that branch it is still reporting the “file header copyright text should match the copyright text from the settings” as a CODE SMELL

Interesting… Does it work well on the main branch and on PRs targeting the main branch?

No it doesn’t work well. I just ran our AzDo Pipeline on a new branch that just had the updates to the file headers and the same number of Code Smells as the previous branch. Same thing when that new branch is PR’ed.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Aplogies if this is not the correct protocol for chasing up this issue but the original “get helkp” request is now locked, I am happy to return to it if it can be unlocked and I will delete this one

My iussues are well documented in this post and you can jump to then end to see my current position

Essentially the Regex that Andrei helped me with works well in the IDE but the files containing the new file header still get flagged in sonarcloud as issues

Sorry for the delay, vacations.

It’s a bit strange, given that I cannot repro on :sonarcloud: .

In the IDE in connected mode, connected with the sonarcloud profile?

Sorry for the delay in coming back. Reason didn’t see Notification, sorry

Heer are scome screenshots of the Resuklt of Code Analysis and the SonarQube screen in Team Explorer that will hopefully confirm I am in connected mode

I assume you can see my project in SonarCloud for yourself and see all the failures.

I just pushed a minor textual change to the test branch and after the pipeline ran it was 100% clean with no “New Code Smells”. I will look to push it to the Master branch ASAP

I have pushed to “MASTER” branch and it did not make amny difference to the COde Small value and score on that branch.

However I noitice that version was set to specific version BUT we never set projectVersion in the pipeline.