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

1

13

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.

Abilene answered 5/2, 2014 at 16:53 Comment(3)
The technique shown in the linked article should do fine afaik: import some .proj file in which you have <Compile Include="**\*.cs" Exclude="obj" />. What exactly did you try? Are you sure you put the import at the correct place?Alonaalone
@Alonaalone I just updated the question with what I triedAbilene
As per your 'edit 2': yes, I forgot to mention that part: the files will not be shown in VS and I'm not aware of a way to fix that. Which of course doesn't make it any useful having a VS project. But thinking of it I'm afraid that is how projects are dealt with: in the first case VS 'fixes' the wildcards for you else it probably can't show the files in solution explorer. In the second case it doesn't change anything, but no files are shown. Not sure if there is a solution. If not, best bet is maybe a small tool to generate the project file from a template and insert all cs files ...Alonaalone
A
16

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

Asta answered 10/2, 2014 at 14:39 Comment(1)
Heck yeah! You are the best! I wish I'd had a chance to get back and implement this sooner!Abilene

© 2022 - 2024 — McMap. All rights reserved.