Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving incorrect project reference GUIDs

I have some projects in a Visual Studio solution that have ended up with project references that include the wrong GUID for the referenced project. (Possibly due to the referenced project being recreated at some stage)

eg. Consider a project CoreProject.csproj with the following properties:

<ProjectGuid>{93803F9C-8C65-4949-8D44-AB7A3D0452C8}</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>CoreProject</RootNamespace>
<AssemblyName>CoreProject</AssemblyName>

Another project includes a reference to this, but at some stage the GUID has changed and is now incorrect.

<ProjectReference Include="..\CoreProject\CoreProject.csproj">
  <Project>{5FD52517-79F8-41D2-B6F2-EA2D8A886549}</Project>
  <Name>CoreProject</Name>
</ProjectReference>

The solution still loads and builds correctly in Visual Studio and msbuild, but I suspect having the wrong GUIDs may have some performance impact within VS.

The solution is quite large with many projects that have this issue, and I'd prefer not to have to re-add these references manually. Are there any tools or macros that can 'fix' up the project reference GUIDs?

like image 500
David Gardiner Avatar asked May 30 '12 14:05

David Gardiner


1 Answers

I think a basic console app should do the trick, something like this:

using System;
using System.IO;
using System.Linq;
using Microsoft.Build.Evaluation;

public class Program
{
    public static void Main(String[] args)
    {
        var projects = Directory.EnumerateFiles(@"...", "*.csproj", SearchOption.AllDirectories)
            .Select(x =>
                    {
                        try
                        {
                            return new Project(x);
                        }
                        catch
                        {
                            return null;
                        }
                    })
            .Where(x => x != null)
            .ToList();
        var guids = projects.ToDictionary(p => p.FullPath, p => p.GetPropertyValue("ProjectGuid"));

        foreach (var project in projects)
        {
            var save = false;

            foreach (var reference in project.GetItems("ProjectReference"))
            {
                var path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FullPath), reference.EvaluatedInclude));
                if (!guids.ContainsKey(path))
                {
                    Console.WriteLine("No {0}", Path.GetFileName(path));
                    continue;
                }

                var guid = guids[path];
                var meta = reference.GetMetadataValue("Project");
                if (meta != guid)
                {
                    Console.WriteLine("{0} -> {1}", meta, guid);
                    reference.SetMetadataValue("Project", guid);
                    save = true;
                }
            }

            if (save)
                project.Save(project.FullPath);
        }
    }
}
like image 76
Ilya Kozhevnikov Avatar answered Sep 28 '22 07:09

Ilya Kozhevnikov