0

I need to include a file depending on project configuration, tried MSBuild conditions but seems not to work

I've created the two files and edited the .csproj adding following declarations

<ItemGroup>
    <AppxManifest Include="Package.debug.appxmanifest" Condition="'$(Configuration)'=='Debug'" >
      <SubType>Designer</SubType>
    </AppxManifest>
    <AppxManifest Include="Package.release.appxmanifest" Condition="'$(Configuration)'!='Debug'" >
      <SubType>Designer</SubType>
    </AppxManifest>
  </ItemGroup>

No matter what configuration i am using i always see both files inside the project

  • Does it affect your build or pack? Any error? Cause as I know, the UI won't refresh until you reload the project in this situation, but when build and pack, the logic in your project file can be executed well. – LoLance Aug 23 '19 at 09:09
  • 1
    No errors, what i see is that both files are always included (and being a UWP app this is not permitted and build suddenly fails) tried reloading the project, restarting VS but nothing changes. Looks like condition is simply ignored. – Corrado Cavalli Aug 23 '19 at 09:18
  • Looks like that same issue was posted here and the truth is that Visual Studio doesn't filter the files depending on the condition. https://stackoverflow.com/questions/8115696/conditional-content-based-upon-configuration And we still have this behavior in VS2019... – Corrado Cavalli Aug 23 '19 at 09:42

2 Answers2

0

OK, so you have to have both files included in both configs but you can selectively check the "Exclude from Build" option on a config-basis. Right-click the file(s) in Solution Explorer and get the "Properties" page up. You should see "Exclude from Build," which you can check for the file you don't want in the selected configuration.

Then, as you change configuration, the Solution Explorer will (should) show one file with the "No Entry" icon added - but the file shown thus will be different in each config.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

No matter what configuration i am using i always see both files inside the project

1.As we know, the UI in Solution Explorer is corresponding to content in .xxproj file. So when I write content like below:

  <ItemGroup>
    <Content Include="TextFile1.txt" />
  </ItemGroup>

VS will display TextFile1.txt in Solution Explorer even though the file actually doesn't exist.

enter image description here

So when you use the script above, it shows both two files.

2.And I think what confused you a lot is why the UI didn't include file depending on configurations. Please have a look at this similar issue, I agree with Andrey that ItemGroup is also affected but this behavior.(Conditions not work when project loaded)

3.And in my opinion, It seems when VS load project file and display the UI in Solution Explorer,it can't access the Configuration value from the switch box. The switch box is disabled and grey when my project is unloaded, and if I right-click the project to reload it, since now the switch box is disbaled, the conditions about Configurations can't work well at load time.

enter image description here

Also, I tested this script, the three files are now displayed in Solution Explorer:

  <ItemGroup>
    <Content Include="TextFile1.txt" Condition="$(RootNamespace)=='App'"/>
    <Content Include="TextFile2.txt" Condition="$(RootNamespace)=='AppApp'"/>
    <Content Include="TextFile3.txt" Condition="$(RootNamespace)=='AppAppApp'"/>
  </ItemGroup>

So it's obvious the conditions are ignored during load time. And according to the Note in remarks, this is by design.

And since conditions don't work in this situation, you may have to set its property manually in property window. i.e: Set the Package.release.appxmanifest's build action as AppxManifest while Package.debug.appxmanifest's build action None during Release configuration.

Hope it helps and if I misunderstand anything, feel free to correct me:)

Update1:

Find a discussion in github, and get the good hint from davkean.

The new project system currently drives the solution tree off the "active" configuration. The old project system drives the solution tree by ignoring conditions. They both have advantages/disadvantages.

LoLance
  • 25,666
  • 1
  • 39
  • 73