0

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 ?

Phil Gref
  • 987
  • 8
  • 19
  • I tried just changing the "Build Action" in the properties for the .cs file from "Compile" to something else and I still get syntax coloring, ReSharper actions, etc. – Lasse V. Karlsen Mar 28 '15 at 18:31
  • Yes, you still get the coloring based on simple syntax interpretation (class names as cyan, operators as blue etc.), but you don't get the actual type checking or intellisense autocomplete that you would expect in a compiled file. For example, omitting a semicolon at then end of a statement or forgetting to properly close an if statement with a bracket will show up as an error, but using a non-existent or inaccessible method in the context of the class will not. Try closing and reopening the file after switching to content if you don't see what I mean. – Phil Gref Mar 28 '15 at 21:01

2 Answers2

1

It would be simmilar to this one https://stackoverflow.com/questions/29242600/compile-your-application-with-outer-documents, except that instead of changing the build action to "Embedded resource", change it to "None". Not 100% sure of this since when I tried it - it did exclude the file from the compile, but all of the text changed colors, but the intellisense stilled seemed to be there. All the resharper options still seem to be there as well.

Community
  • 1
  • 1
gerrycox
  • 36
  • 3
1

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.

Andrew
  • 14,325
  • 4
  • 43
  • 64
  • Second solution worked like a charm, thanks ! I only had to modify it a little and include a `` instruction that copied the test files to the output directory, since removing them using your solution had the effect of not moving them anymore when compiling, even if set to `CopyAlways`. – Phil Gref Mar 28 '15 at 16:19
  • 1
    @PhilGref I forgot that you wanted the output in the build. That's also easily done without a `` element. I will update my answer. – Andrew Mar 28 '15 at 18:24