Analyze Solution For Code Clones Missing In Visual Studio 2017

I was watching a video on Microsoft’s Channel 9 website regarding design patterns and during the video the presenter showed a feature of Visual Studio that analysed the currently loaded solution for “code clones” – sections of code that appear to be clones or duplicates in different methods.

This seemed like a very useful feature and something that I thought I could benefit from on a number of codebases that I’m currently working with.  So off I went to explore this handy feature for myself.

First thing to note this that, although this feature has been part of Visual Studio since Visual Studio 2012, it’s only available in the Enterprise SKU of the product (or the Premium or Ultimate versions for VS 2012/2013).

That was ok, as I’m using Visual Studio 2017 Enterprise, however, upon expanding the Analyze menu option, I was greeted with a distinct absence of the Analyze Solution For Code Clones option:


Hmm..  I was curious why the option was missing so went off doing my best googling to try to find out why.   And it turns out that there’s very little information out there on the internet regarding this feature, and even less information regarding troubleshooting why the option might not be appearing on the Analyze tools menu.

After some amount of aimlessly clicking around on the spurious search results, it suddenly dawned on me.   Perhaps it’s an “optional extra” component that you need to install?   Turns out I was right.

When I’d originally installed Visual Studio 2017, I’d selected one of the pre-configured “Workloads” to install.  In my case it was the “ASP.NET And Web Development” workload as that’s the sort of development work I do.  Turns out that, whilst this workload installs almost everything you might want or need for ASP.NET And Web Development, there’s a few crucial things missing, one of which is the “Architecture And Analysis Tools” component – which in turn includes the “Code Clone” tool:


In order to get the Code Clone tool installed, you can either select just that tool from the “Individual components” tab of the Visual Studio installer, or you can select the “Architecture and analysis tools” optional component (as seen on the right hand side in the above picture) which will select the Code Clone, Code Map and Live Dependency Validation tools.

Once this is done, restarting Visual Studio will give you the missing “Analyze Solution for Code Clones” menu option:


Easy when you know how, eh?

Beware NuGet’s Filename Encoding!

The other day, I was troubleshooting some issues that had occurred on a deployment of some code to a test server.  Large parts of the application were simply not working after deployment, however, the (apparently) same set of code and files worked just fine on my local development machine.

After much digging, the problem was finally discovered as being how NuGet handles and packages files with non-standard characters in the filename.

It seems that NuGet will ASCII-encode certain characters within filename, such as spaces, @ symbols etc.  This is usually not a problem as NuGet itself will correctly decode the filenames again when extracting (or installing) the package, so for example, a file named:


within your solution will be encoded inside the .nupkg file as:


And once installed / extracted again using NuGet will get it’s original filename back.  However, there’s a big caveat around this.  We’re told that NuGet’s nupkg files are “just zip files” and that simply renaming the file to have a .zip extension rather than a .nupkg extension allows the file to be opened using 7-Zip or any other zip archive tool.  This is all fine, except that if you extract the contents of a nupkg file using an archiving utility like 7-zip, any encoded filenames will retain their encoding and will not be renamed back to the original, correct, filename!

It turns out that my deployment included some manual steps which included the manual extraction of a nupkg file using 7-Zip.  It also turns out that my nupkg package contained files with @ symbols and spaces in some of the filenames.  These files were critical to the functioning of the application, and when manually extracting them from the package, the filenames were left in an encoded format meaning the application could not load the files as it was looking for the files with their correct (non-encoded) filenames.

JavaScript / jQuery IntelliSense in Visual Studio 2012

I blogged a while ago about a rather ugly and hacky way in which you could get the goodness of jQuery (and general JavaScript) IntelliSense in the Razor editor in Visual Studio 2010’s IDE.


This basically involved placing code similar to the following into every MVC view where you wanted IntelliSense to show up:


@* Stupid hack to get jQuery intellisense to work in the VS2010 IDE! *@
@if (false)
   <script src="../../Scripts/jquery-1.6.2-vsdoc.js" type="text/javascript"></script>


Well, since the release of Visual Studio 11 Beta, and the recent release of Visual Studio 2012 RC (Visual Studio 2012 is now the formal name of Visual Studio 11) we now no longer have to perform the above hack and clutter up our MVC views in order to enjoy the benefits of IntelliSense.


In Visual Studio 2012 (hereafter referred to as VS2012) this has been achieved by allowing an additional file to be placed within the solution/project which will contain a list of “references” to other JavaScript files that all MVC views will reference and honour.


The first step to configuring this is to open up VS2012’s Options dialog by selecting TOOLS > OPTIONS from the main menu bar:




Once there, you’ll want to navigate to the Text Editor > JavaScript > IntelliSense > References options:




The first thing to change here, is to select Implicit (Web) from the Reference Group drop-down list.  Doing this shows the list of references and included files within the Implicit (Web) group, as shown below the drop-down. Implicit (Web) includes a number of .js files that are included with VS2012 itself (and are located within your VS2012 install folder), but it also includes the following, project-specific entry:




Of course, this is all configurable, so you can easily add your own file in here in your own specific location or change the pre-defined _references.js, but since ASP.NET MVC is based around convention over configuration, let’s leave the default as it is!  Click OK and close the options dialog.


Now, what’s happened so far is that as part of the pre-defined Implicit (Web) reference group, VS2012 will look for a file called _references.js within a web project’s ~/Scripts/ folder (the ~/ implying the root of our web application) and use the references that are defined within that file as other files that should be referenced from within each of our MVC views automatically.


So, the next step is to add this file to one of our MVC projects in the relevant location, namely the ~/Scripts/ folder.  Right-click on the Scripts folder and select Add > New Item:




Once the Add New Item dialog is displayed, we can add a new JavaScript File, making sure that we name the file exactly as the default pre-defined Implicit (Web) reference group expects the file to be named:





The format of the _references.js file follows the JScript Comments Reference format that has been in Visual Studio since VS2008.  It’s shown below:


/// <reference path=”path to file to include” />


You can add as many or as few “references” within the _references.js file that you need.  Bear in mind, though, that the more files you add in here, the more it may negatively impact the performance of the editor/IDE as it’ll have far more files that it has to load and parse in order to determine what IntelliSense should be displayed.   A sample _references.js file is shown below:




The format/syntax of the references within this file can take a number of forms.  You can directly reference other JavaScript files without needing a path if they’re in the same folder as the _references.js file (as the example above shows):


/// <reference path="jquery-1.6.3.js" />


You can use relative paths which are relative from the folder where the _references.js file is located:


/// <reference path="SubfolderTest/jquery-1.6.3.js" />


And you can also use paths that are relative to your web project’s “root” folder by using the special ASP.NET ~ (tilde) syntax:


/// <reference path="~/Scripts/SubfolderTest/jquery-1.6.3.js" />


Once this is configured and saved, you will now have lovely IntelliSense within your MVC Views without needing additional hacky script references from within the view itself.  See the screen shot below:




Yep, that’s the entirety of the view that you can see there (no @if(false) nonsense!), and that’s lovely jQuery IntelliSense being displayed as soon as you start typing $( !


Update (10/Feb/2013): Changed the screenshots to use a nicer elliptical highlighting and tweaked some references ever-so-slightly (i.e. change jQuery from 1.6.2 to 1.6.3)