I have a bunch of .cs
files that represent test cases for certain manipulations using the Roslyn API. Since they are supposed to have valid compile time syntax, I would like to have Intellisense, Resharper and other pre-compile time checks available on those files when writing in them, but not to actually compile them when I build the solution (since the test will only be looking at the syntax).
Now, I could probably create another project just for these input test files (and make sure it never compiles), but I would rather keep them in the test project (where I believe they belong and for simplicity's sake). I don't really care if the files don't end up actually being included IN the solution, as long as I can get syntactic checking in the end (although this seems unlikely).
At first, I thought I could simply exclude a directory from being compiled in the .csproj
, but I found no such property that would allow that. I've heard that <CompileDependsOn>
could potentially help me here, but I don't understand exactly how.
My direct question is this: is it possible to have syntactic checking (Intellisense, Resharper, etc) for files that will not end up being compiled when building a project/solution, without having them in a separate, non-building project ?
I can think of at least a couple potential solutions.
One solution might be to use a preprocessor definition. This solution may be unsatisfactory if the directives interfere with your tests, but if you wanted to try it, your .cs file might look like:
#if TESTFILE
namespace ClassesNotToCompile
{
public class IndividualClassNotToCompile
{
...
}
}
#endif
You would then need to define the symbol TESTFILE
in a build configuration for your project. Then, just don't use that build configuration. You might have to select that configuration in the IDE in order to get syntax highlighting, though.
Another would be to edit the .csproj
file, much as you suggested. Assuming .NET 3.5 or later, there is an item attribute named Remove
that can be used when an ItemGroup
is inside a Target
. You can use this attribute in the BeforeBuild
target to remove files from the Compile
item:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
<ItemGroup>
<Compile Include="CompileMe.cs" />
<Compile Include="NonCompiling\DoNotCompileMe.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
...
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild">
<ItemGroup>
<Compile Remove="NonCompiling\*" />
<Content Include="NonCompiling\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Target>
...
</Project>
It is important for your BeforeBuild
target definition to appear after the Import
of Microsoft.CSharp.targets
. Otherwise, your custom BeforeBuild
target will not override the default, empty BeforeBuild
target from the import.