Squid: S1451 "Track lack of copyright and license headers" doesn't work as expected

2,324 views
Skip to first unread message

vival...@gmail.com

unread,
Aug 21, 2017, 8:39:18 AM8/21/17
to SonarQube
Hi All,

The Squid rule: S1451 "Track lack of copyright and license headers" for C# language is not works as expected for the code (Sample1, Sample2). Both samples failed to compliant with rule. And also I tried for IsRegularExpression(True & false) & also tried for different header formats in the rules description.

How to make the code to compliant with rule S1451?

Thanks in advance..

Regards
Vival

vival...@gmail.com

unread,
Aug 21, 2017, 9:09:07 AM8/21/17
to SonarQube, vival...@gmail.com
Hi All, 

I am using  MsBuild v14, SonarQube v5.6.6 & Sonar C# v6.3.

Regards
Vival

Amaury Leve

unread,
Aug 21, 2017, 11:55:27 AM8/21/17
to vival...@gmail.com, SonarQube
Hi vival30,

What is the string you set up in the configuration of the rule?
The default value of the rule is:
Snippet
/*
 * SonarQube, open source software quality management tool.
 * Copyright (C) 2008-2013 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * SonarQube is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * SonarQube is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

(Note the extra empty line at the end)

Cheers,
Amaury

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/f09c074c-b7bd-45b1-b9d4-1721ee9d211d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Amaury Levé | SonarSource

Software Developer - .Net Team

http://sonarsource.com


Are you using SonarLint in your IDE? 

vival...@gmail.com

unread,
Aug 22, 2017, 3:07:01 AM8/22/17
to SonarQube, vival...@gmail.com
Hi Amaury,

I used the default header one as you mentioned in the snippet & the below snippet (snippet1) with "isRegularExpression" flag as false & true.
And also tried for the single space in the header (so it may expect an space in the first line of the cs file instead of any header) but all of them failed to compliant with the rule S1451.


Snippet1
// Copyright (c) SonarSource. All Rights Reserved. Licensed under the LGPL License.  See License.txt in the project root for license information.
(Note the extra empty line at the end)

Regards
Vival

vival...@gmail.com

unread,
Aug 22, 2017, 3:19:54 AM8/22/17
to SonarQube, vival...@gmail.com
Hi Amaury,

Thanks for your support. And Is there any regex format for a standard header or like the below snippet. 

Snippet
// <copyright file="Sample.cs" company="My Company Name">
// Copyright (c) 2012 All Rights Reserved
// </copyright>
// <author>Name of the Authour</author>
// <date>08/22/2017 12:39:58 AM </date>
// <summary>Class representing a Sample entity</summary>

(Note the extra empty line at the end)

Regards 
Vival.

Amaury Leve

unread,
Aug 22, 2017, 9:23:38 AM8/22/17
to vival...@gmail.com, SonarQube
Hi Vival,

I have created this small GitHub repository as an example of usage of the rule: https://github.com/Evangelink/header-samples

HeaderSampleApplication1 contains a class with no header (triggering the rule) and one with a compliant header (default value of the rule).
HeaderSampleApplication2 contains a class with no header (triggering the rule) and one with a compliant header (value defined in SonarLint.xml to reproduce the behavior of the scanner for MSBuild and regex is set to false).
HeaderSampleApplication3 contains a class with no header (triggering the rule) and two with compliant (different) headers (value defined in SonarLint.xml to reproduce the behavior of the scanner for MSBuild and regex is set to true). Note that in this case we don't offer any code fix as the header format is a regex and cannot be applied directly.

Hope it does answer your questions.  

Cheers,
Amaury


For more options, visit https://groups.google.com/d/optout.

vival...@gmail.com

unread,
Sep 1, 2017, 10:29:25 AM9/1/17
to SonarQube, vival...@gmail.com
Hi Amaury,

Thanks for your in-numerous support and Very sorry for the delay in the response. 

As per your sample it works fine with sonar lint of the VS2015. but if i use same sample for the sonarqube version 5.6.6 (with few customization from my side) & Sonar c# v6.3 its still fails to compliant (for all the three conditions am getting 10 violations {one violation per file}). so i will check it with plain sonarqube v5.6.6 & sonar c# v6.3 & then update you later.

Thanks & Regards
vival    

vival...@gmail.com

unread,
Sep 1, 2017, 10:40:52 AM9/1/17
to SonarQube, vival...@gmail.com
Hi Amaury,

Please check the same sample for the sonar analysis using the sonarqube scanner for msbuild (as per the steps in the link) to validate this rule.

Note: am using this 3 step method & getting error in the msbuild step itself.

Thanks & regards
Vishnu.

Amaury Leve

unread,
Sep 6, 2017, 8:11:53 AM9/6/17
to vival...@gmail.com, SonarQube
Hi Vishnu,

I have just tested with my reproducer running the following commands:
SonarQube.Scanner.MSBuild.exe begin /k:test /v:1
 msbuild /t:rebuild .\HeaderSampleApplication.sln

And I can't see any S1451 being triggered. I am using SQ 5.6.6 and SonarC# 6.3 as you mentioned. Could you give me more information (the log would be a good start) so I can help you with this matter?

Cheers,
Amaury


For more options, visit https://groups.google.com/d/optout.

vival...@gmail.com

unread,
Sep 7, 2017, 10:23:01 AM9/7/17
to SonarQube
Hi Amaury,

Here i have the sonar analysis logs [log1(using sonar c# v6.3) & log2(using sonar c# v6.4)].

Note:I used SQ v5.6.6, SonarQube.Scanner.MSBuild v3.0 but for me, its still fails to compliant. 

Thanks & Regards
vival 

Amaury Leve

unread,
Sep 8, 2017, 10:33:55 AM9/8/17
to vival...@gmail.com, SonarQube
Hi,

Ok so I have been mixing things here. So let me start again the explanation from scratch.

1/ SonarLint for Visual Studio doesn't handle parameterized rules but the workaround is the one I was mentioning (i.e. to manually create the SonarLint.xml file with the parameterized rules and the expected parameters). Reading back this thread this is not what you wanted so you can safely ignore this step (I was mentioning it earlier so I thought you would be interested to understand why).

2/ The scanner for MSBuild will create automatically this SonarLint.xml with the configuration from the server and automatically pass it to roslyn. So it won't use the one you would have defined in step 1 if you did. The value of the parameter will be the one you defined in the rule configuration on SonarQube. In your case this is the part you want to change to put the right regex. I did tried with the one I was mentioning but, indeed, it doesn't work as expected. I am trying to solve this now and will get back to you with the right format.

Cheers,
Amaury


For more options, visit https://groups.google.com/d/optout.

vival...@gmail.com

unread,
Sep 12, 2017, 12:15:53 AM9/12/17
to SonarQube
Hi Amaury,

Thanks for Understanding the issue which i faced, and please update me once the (Rule S1451) issue is fixed...

Thanks & Regards
Vival

Amaury Leve

unread,
Sep 13, 2017, 5:58:01 AM9/13/17
to vival...@gmail.com, SonarQube
Hi,

I finally managed to get things to work properly and it turned out to be rather simple. I was actually trying to solve issues (escaping) I shouldn't have.

So basically you want to write a classic regex in the SonarQube parameter textbox. Which means you shouldn't try to add escaping characters to match a regex within a string. For example here is a valid regex that would work for the project 3 in the header-sample I gave to you:
// <copyright file=".*\.cs" company=".*">\r\n// Copyright \(c\) 2012 All Rights Reserved\r\n// </copyright>\r\n// <author>.*</author>\r\n// <date>.*</date>\r\n// <summary>.*</summary>\r\n

Note that you can use either \n or \r\n as a end of line token.

Cheers,
Amaury


For more options, visit https://groups.google.com/d/optout.

vival...@gmail.com

unread,
Sep 18, 2017, 10:56:43 AM9/18/17
to SonarQube
Hi Amaury,

Thanks a lot. And for me also in the plain Sonarqube the below regex (as shared by you) is works fine. In my (setup) changed code am facing some other issue so i'll update you later regarding this. 

Regex:
// <copyright file=".*\.cs" company=".*">\n//Copyright \(c\) 2012 All Rights Reserved\n// </copyright>\n// <author>.*</author>\n// <date>.*</date>\n// <summary>.*</summary>\n  


Thanks & Regards
Vival

vival...@gmail.com

unread,
Sep 21, 2017, 10:16:26 AM9/21/17
to SonarQube
Hi Amaury,

Thanks a lot. The suggestion you have provide works for me. 

Hope this resolves the issue.

Thanks & Regards
Vival 
Reply all
Reply to author
Forward
0 new messages