Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent Visual Studio 2012 from expanding an MSBuild 4 wildcard?

I recently migrated our build platform from an ancient one build on top of rake (don't ask, seriously) to one using msbuild. Because many of our team members don't use Visual Studio (again, don't ask), they are used to being able to drop in a .cs file into the project folder, and just having it magically appear as part of the build.

As such, the .csproj file for this project includes the following line:

<ItemGroup>
    <Compile Include="**\*.cs" Exclude="obj" />
</ItemGroup>

This works great when compiling via msbuild directly. However, when I open the project in Visual Studio, it decides to "helpfully" expand the wildcard into a full list of files, seemingly as soon as I open it:

<ItemGroup>
    <Compile Include="Controller.cs" />
    <Compile Include="MyClass.cs" />
    <Compile Include="MyClass2.cs" />
    <Compile Include="etc/etc/Something.cs" />
    <!-- and so forth -->
</ItemGroup>

While technically this still works, it also causes grief because it removes the wildcard ability.

I tried implementing a technique shown on this page, but I couldn't get it to work with simple compilation.

Has anyone had this problem before? Is there any way to work around this?

EDIT: To clarify what I mean by "couldn't get it to work", this is what I did:

In Main.csproj, I have this:

<!-- References in here -->
</ItemGroup>

<Import Project="Imports.proj" />

<ItemGroup>
<!-- ProjectReferences down here -->

Then, I created Imports.proj, with this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ItemGroup>
        <Compile Include="**\*.cs" Exclude="obj" />
    </ItemGroup>
</Project>

When I open Main.csproj in Visual Studio, it doesn't show any files. It could be that the <Import> line is wrong?

Edit 2: Interestingly, it still builds through Visual Studio, it just doesn't show the files as part of the project.

like image 300
Mike Caron Avatar asked Feb 05 '14 16:02

Mike Caron


1 Answers

I had the exact same problem, so much so that I asked the exact same question (Unwanted changes in .csproj file on build) 2 days after you did without noticing your own:

While I can't explain why VS decides to do this every time it builds a project, I do have a solution for you to try, it's identical to how I got around my problem:

<PropertyGroup>
    <IncludePattern>**\*.cs</IncludePattern>
    <ExcludePattern>obj</ExcludePattern>
</PropertyGroup>

<ItemGroup>
   <Compile Include="$(IncludePattern)" Exclude="$(ExcludePattern)" />
</ItemGroup>

VS leaves properties as they are without expanding the wildcard into a full list of files

like image 57
ShaneC Avatar answered Nov 09 '22 23:11

ShaneC