Duplicate project reference entries in a .csproj file - visual-studio-2010

I was recently checking in a change to a c# project and during the checkin, I had to do a manual merge on the .csproj file. While doing the merge, I noticed that there were duplicate entries for all the project references in the project.
Specifically, the ProjectReference tag was duplicated for each other project referenced in the project being merged. Like so:
<ProjectReference Include="..\..\..\DataSourceInterfaces\DataSourceInterfaces.csproj">
<Project>{335ED423-8DC5-4D56-86D6-9A1B0F7DB0B9}</Project>
<Name>DataSourceInterfaces</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\DataSourceInterfaces\DataSourceInterfaces.csproj">
<Project>{335ED423-8DC5-4D56-86D6-9A1B0F7DB0B9}</Project>
<Name>DataSourceInterfaces</Name>
</ProjectReference>
If I go into the Visual Studio UI and delete the reference to the DataSourceInterfaces project, only one of the tags is removed, and the next time I load the project, the reference shows up in the reference list as if it hadn't been removed.
This is easily fixable by manually removing the duplicate entries and doesn't seem to cause any problems, but I'm wondering what could have caused the problem in the first place.

I'm wondering what could have caused the problem in the first place.
It sounds like your merge tool failed during the manual merge, and added duplicate project references. When you merge manually, you need to take care that this type of thing doesn't happen - all it takes is a slight difference in things like indentation or whitespace changes to occasionally mess up some merging tools (this depends a lot on the tool in question, though).

Related

Is there a way to manually trigger Visual Studio to update .csproj files?

I sometimes lose my pending changes to the .csproj file. I'm left with a changeset of new files, which still carry the appended [add] tag in the Team Explorer, but pending updates to the .csproj file are wiped out.
I can manually edit the .csproj to add them, but this is tedious and error-prone. Is there a way to re-run whatever process normally updates the .csproj file automatically?
Normally, changes to .csproj files should be very rare, when using the new SDK style projects.
Since by default, all .cs files are included, you do not normally need any update to the project file if files are added or removed. So I suggest you remove all <Compile Add/Include/Update /> lines that refer to code as well as the line that says <EnableDefaultCompileItems>false</EnableDefaultCompileItems> if you have that.
For most projects that works just fine. It will reduce the frequency of changes to the project file significantly (and make it a lot easier to read).

Can Visual Studio use an .editorconfig not in the directory hierarchy?

We have a very large number of solutions spread across a wide number of repositories which do not always share a directory hierarchy in a way that makes it easy for us to update an .editorconfig such that it applies to all projects/solutions in the organization. We currently apply all of our code analysis configuration via an internal NuGet package and I was hoping we could include our organization-wide .editorconfig settings in this way as well?
I tried a quick experiment adding the following to a project to see if linked files would be honored (since we could simply add this to a props file we already have in the NuGet package), but it does not appear to be honored currently.
<ItemGroup>
<None Include="C:\SomeAlternatePath\ECTest\.editorconfig" Link=".editorconfig" />
</ItemGroup>
Is there some other MSBuild property or mechanism we could use to better facilitate this without literally writing a duplicate file to every solution/project/repo?
Is there some other MSBuild property or mechanism we could use to
better facilitate this without literally writing a duplicate file to
every solution/project/repo?
I'm afraid the answer is negative. Cause the .editorconfig file have nothing to do with msbuild or xx.csproj. Only file hierarchy can affect the behavior how the config file works. More details please check this document.
Some tests:
When I right-click project=>add .editorconfig to add this file in current project, there's one line added to the xx.csproj: <None Include=".editorconfig"/>.
If we set the indent_size = 32, it works for current project. Now we can right-click that file=>Exclude from Project to remove that file from current project system. (This action will remove the <None Include=".editorconfig"/> in xx.csproj, but the file is still in the same folder where xx.csproj exists)
Now reload the project, the settings(indent_size=32) still works. So it's obvious if we place this file in project directory, then it will take effect, no matter if we have definitions about it in project file(xx.csproj).
Suggestions:
According to your description, all your projects use the same .editorconfig file. Since this file's working scope is affected by file hierarchy, you can reduce some meaningless work by:
1.Place that file in Solution folder, it will work for all projects under that solution folder
2.Place that file in repos(C:\Users\xxx\source\repos) folder, it will work for all solutions and projects under this folder.
3.So if most of your solutions are under path C:\somepath, place that file here, all projects under that path will benefit from that. And about precedence in file hierarchy please see this one.
Hope all above makes some help :)

Move a project without destroying assembly reference

We setup our assembly references as project references. I am reorganizing source control, moving projects here and there. When I open my solution the project references are broken. I have to remove the references and re-add them. Doing this removes the project reference from all assemblies.
Is there a way to correct the project reference in the solution file without removing and re-adding projects?
I could go into MSBUILD and do it, but there's no unload option for my solution...so that seems like a pain. Any native way to fix this common issue? Since the files are in the same solution, I expect the project references to resolve themselves automatically.
This way I don't have to correct all my references manually.
If you need to do this just only once - Resharper could help you to do this a bit faster.
Whenever it finds broken reference - it will offer option "reference assembly X and use class Z". Probably not the best way cause it still involves manual file plumbing, but this could SLIGHTLY reduce mouse clicks and file tree navigation. Though, you still need to remove wrong reference manually.

Visual Studio Error: Unable to load arguments for the XmlPeek task. One of XmlContent or XmlInputPath arguments must be set

I opened up VS today to find this error waiting for me when I built my project. I hadn't changed anything since yesterday, when it was working just fine.
Unable to load arguments for the XmlPeek task. One of XmlContent or XmlInputPath arguments must be set.
Of course this error has no line associated with it or anything else that may be helpful...
The project is in the .NET 4 Framework with Console Output.
Any ideas as to what is going on? I tried googling this of course but the few answered I found had to do with a content pipeline (which this project does not use).
Thanks!
I received this error in case, when I have deleted Content Project of my game. It has been solved by adding correct content reference into the Content References directory.
I had two projects (proj1 and proj2) and each of them had their own content projects (proj1_content and proj2_content). I wanted to have one common content project for both projects, so I had deleted both proj1_content and proj2_content and created new content project called common_content. After that, mentioned error appeared. Solution was to go to Content References directory at each project and Add Content Reference pointing to common_content.
Due to a hardware problem I had to put in a new SSD and re-installed VS. All my projects worked except my XNA in Winforms project. Being rather large (12,000 hand-written lines of code) really didn't want to try the "copy it all in to a new project" solution above. Thought I'd try to see what versions worked. All versions prior to me adding an installer project worked. In fact, the version where I had added, then deleted the installer was broken. So I'm not sure how (or if) the installer broke it but I found one section of the main project file that had a missing entry and it did reference XNA also (all my non-XNA projects build just fine). Note that I have substituted my project's name with a reference to YOUR projects name by using <YourMainProjectName>. If your project is joesgreatproject then use that text in place of <YourMainProjectName> (don't include the <> symbols). I think this was only because the XNA didn't have a content directory and needed a reference to one.
File: <YourMainProjectName>.csproj
<ItemGroup>
<ProjectReference Include="..\< YourMainProjectName>Content\<YourMainProjectName>Content.contentproj">
<Name><YourMainProjectName>Content</Name>
<XnaReferenceType>Content</XnaReferenceType>
</ProjectReference>
</ItemGroup>

Visual Studio Linked Files Directory Structure

I have two versions of a project. One for Silverlight and one for .NET. The SL project has the vast majority of the code base in it. I want to globally add all files from the SL project into the .NET version as linked files. I've managed to do so successfully like this in the csproj file for the .NET version:
<Compile Include="..\MyProj.Common.SL\**\*.cs" Exclude="..\MyProj.Common.SL\Properties\**">
Unfortunately, this adds all the files right to the root of my project... so I end up with a long unreadable list of linked files in the .NET project. I really really really don't want to have to maintain an entire duplicate directory structure by hand and deal with directory name changes and file name changes and whatnot.
So, is there any way to have Visual Studio preserve the directory structure when adding linked files in the wildcard manner above? Or is there at least a way of making it group all the linked files together under a directory in the .NET project like MyProj.Common.SL.Links?
The very closest I've come is to set the <Visible>false</Visible> under the <Compile> tag, which effectively removes the long unreadable list of 300+ files....but unfortunately this screws up Resharper, which no longer sees those files as valid and it goes crazy on all the projects that reference the .NET project. If I could figure out a way of making Resharper not get all messed up, that would be an acceptable solution too...
Any suggestions?
Thanks.
I think I found a way of getting this to work:
<Compile Include="..\MyProj.Common.SL\**\*.cs" Exclude="..\MyProj.Common.SL\Properties\**">
<Link>MyProj.Common.SL.LinkedFiles\MyProj.Common.SL.LinkedFiles</Link>
</Compile>
It will create a MyProj.Common.SL.LinkedFiles folder and group all the linked files under that folder.
I think I would do this:
Copy the existing project's <Compile> items, which presumably have e.g. Include="foo.cs" and Include="Folder\bar.cs"
Paste them into the new project
Search and replace <Compile Include="(.*?)" /> with <Compile Include="..\Other\$1" ><Link>$1</Link></Compile>
I don't know if I got the regular expression search and replace syntax exactly right, but the point is, you already have a good project, you should be able to cut, paste, regex-replace it to get the same set of files, only referenced from a different folder, and with the same directory structure.
You'll still have two .csproj's to maintain at this point, but this is also easily fixed. Now take this new list of compile items, and put it in a file named e.g. "Common.csproj" that just contains the property group with those compile items, and then have both projects <Import Include="..\Common.csproj" /> and not include any Compile items of their own.
Basically, a little manual labor to refactor the .csproj file for sharing once, and then I think you'll be set. I am not sure if this is the 'easiest' way to unblock you, but I think this sounds approximately like what you may want for an 'ideal' structure.

Resources