VS2015 project.json compile exclusion

302 views Asked by At

In Visual Studio 2013, I was able to exclude some .cs file from being compiled by using a compiler constant and modifying the project's .csproj file.

Something like this:

-- myproject.csproj

<PropertyGroup>
  ...
  <DefineConstants>MY_COMPILER_CONSTANT</DevineConstants>
  ...
</PropertyGroup>
<ItemGroup>
  ...
  <Compile Include="myproject\myfile.cs" Condition="'$(Configuration)' != 'MY_COMPILER_CONSTANT'" />
  ...
</ItemGroup>

Then my project completely excluded this myfile.cs from being compiled, allowing me to avoid some nasty errors.

But now I'm trying to migrate my project environment to Visual Studio 2015.

I could easily figure out how to add a compiler constant to a VS2015 project, but still trying to figure out how to exclude a file from being compiled.

My project.json currently looks something like this:

{
  "frameworks": {
    "net451": {
      "dependencies": { }
    }
  },
  "dependencies": {
    ...
  },
  "buildOptions": {
    "define": [ "MY_COMPILER_CONSTANT" ]
  }
}

Adding a file for compilation seems straight forward. I presume that it has to do something with the "compile" configuration variable in buildOptions. What do I need to add to make the compiler to ignore myfile.cs on build?

2

There are 2 answers

0
Lukasz Mk On BEST ANSWER

Try: buildOptions -> compile -> exclude

For example - from my project.json - how to exclude whole lib folder from compilation:

"buildOptions": {
  "compile": {
    "exclude": [ "lib" ]
  }
}

or another example from old docs:

"buildOptions": {
    "compile": ["*.cs"],
    "exclude": ["buggy/**/*.cs", "foo/**/*.*"],
    "publishExclude": ["something/**/*.*"],
    "resource": ["embed/**/*.*"]
}


for more information please find project.json reference.

Note

The .NET Core tooling is going to move from project.json to MSBuild-based projects in a future release.

The recommendation is to still use project.json files for new .NET Core projects since there will be a path to convert your project to MSBuild when the tooling is released.

For more information, see the Changes to project.json post on the .NET blog and the Using MSBuild to build .NET Core projects topic.

0
rrirower On

Right click on the .cs file in the Source Explorer to display the Properties pane. In the properties pane, change the Build action to “None”. The file should not be compiled.

enter image description here