Domanda

I have a solution that contains twenty c# projects. Until recently, StyleCop would run against all files, except auto-generated files, across all projects and report any issues that it found. Recently (it's not clear exactly when) it has become picky about which files it will report issues against.

Within a given project, I deliberately add the same defect to multiple source files and StyleCop will report the issue in some cases but not others.

An earlier branch of the same code, largely unaltered since October, does not display this behavior. Changing nothing but the source code I can demonstrate the problem existing in the latest code, but not in the code from October.

The skipped files do not contain any of the "I am auto-generated" markers that I would expect to cause StyleCop to skip them and I can find no commonality between either the skipped files or the analyzed files.

The solution file is unaltered between branches and the only changes to the csproj files are the addition/removal of source files.

Does anyone have any ideas what might be causing this behavior?

È stato utile?

Soluzione

So, after significant chocolate consumption and much swearing, I have the answer:

We recently inserted a copyright notice at the top of all our source files. It turns out that some of our source files had a byte order mark (U+FEFF) at the start of the file and the notice ended up being inserted ahead of this character.

StyleCop takes offense to the presence of this character and silently ignores the rest of the file.

Given that the character is, correctly, not rendered by the Visual Studio IDE, it took me three days to spot it :(

EDIT (2013.01.14) : I've created a Perl script to remove BOMs from our source files:

#!/usr/bin/perl -w
use strict; 
use warnings;
use File::Find;

my @dir = "C:/TopLevelSourceCodeDirectory";

find(\&edits, @dir);

sub edits() {

    my $file = $_;

    if( (-f $file) 
        && 
        (
            ($file =~ /.*\.cs$/)
            ||
            ($file =~ /.*\.xaml$/)
            ||
            ($file =~ /.*\.whatever$/)
        )
      ) {

        #Open the file and read in the data
        open (my $in, '<', $file) or die "Can't open $file: $!\n";
        my @lines = <$in>;
        close $in;

        #Open same file for writing
        open (my $out, '>', $file) or die "Can't open $file: $!\n";

        #Walk through lines, putting into $_, and remove BOMs
        for ( @lines ) {
            s/\xef\xbb\xbf//g;
            print $out $_;
        }

        close $out;
    }
}

One final note; I had all manor of issues writing this script because notepad seemed to add BOMs in the middle of my file (which are, of course, invisible) when I pasted certain character strings in (even though those strings didn't originally contain a BOM). Working out why your regex doesn't match when you can't tell that it has an invisible BOM in the middle of the matcher string is not pleasant.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top