0

In Rider, files in a project directory are automatically shown in its Explorer window unless you ask to Exclude them. But they aren't automatically added to the csproj file. When I try to Add Existing File, if it's already in the project directory, nothing seems to happen. Without manually editing the csproj file, is there any way to add a file to the csproj file (without switching to Visual Studio)? Thanks.

uncaged
  • 597
  • 1
  • 5
  • 17
  • "they aren't automatically added to the csproj file" is by design, as the files are included via default include rules. If you are not yet familiar with such SDK based project files, read https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#default-includes-and-excludes – Lex Li Feb 08 '22 at 15:59
  • There is eye button in Rider to show all files. When you see a file, you can Include it same way as you Exclude. – Ivan Shakhov Feb 09 '22 at 04:22

1 Answers1

0

All .cs files found in folders under the (relative) root where .csproj is, are automatically part of the project (and will build, and break your build if you dump some malformed .cs files there). It's the reverse of what it used to be - you have to exclude explicitly what you don't want. So trying to explicitly add a file that is already inside the project's tree of folders is a no-op.

Don't know if VS is still adding them either since with the whole "change of style" is in MSBuild mechanics. There's a set of build properties (just XML tags as usual) for regulating behavior although most people don't bother.

If does seem weird at first but pretty soon you get lazy like everyone else :-) The whole reason for that change was exactly so that people can stop chasing files and paths. Plus with enormous codebases that are more norm than exception these days it became virtually impossible to manage things manually, not to mention the size of .csproj for 100+, 500+, 1000+ files.

That also makes .csproj by and large unnecessary - you can quite literally build just about anything with a dummy default .csproj - just add package references. One little caveat - if you do add .cs files explicitly (by adding tags) it will break your build - they'll be considered "duplicates".

This is probably the minimal .csproj that will build "everything":

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>$(AppTargetFrameworkVersion)</TargetFramework>
    <AssemblyName>$(SolutionNamespaceRoot).Domain</AssemblyName>
    <RootNamespace>$(AssemblyName)</RootNamespace>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Package.One.From.Wherever" Version="$(ComponentsVersion)" />
  .   .   .
  </ItemGroup>
</Project>
ZXX
  • 4,684
  • 27
  • 35