1

Visual Studio creates two files along with the .exe for my project that are required to run the exe: a deps.json and a runtimeconfig.json. A second project in my solution has the first project as a project reference, but those two files aren't being copied to my second project's output directory. How can I tell Visual Studio that it should copy these files into the output directory of the second project, because the referenced project depends on them?

Output directory of my first project:

Foo.exe
Foo.deps.json
Foo.runtimeconfig.json

Output directory of my second project:

Bar.exe
Foo.exe
Should contain deps and runtimeconfig files, but does not
Suuper W
  • 31
  • 1
  • 10
  • 2
    you can build your own post-build script to copy the files. – MakePeaceGreatAgain May 27 '22 at 12:35
  • https://stackoverflow.com/questions/834270/visual-studio-post-build-event-copy-to-relative-directory-location – Matthew Watson May 27 '22 at 12:47
  • 2
    Do you set the files property to _Copy always_? If it is set, then you can see the files under the Bar.exe output folder (/bin/Debug/netcoreapp3.1). – Péter Szilvási May 27 '22 at 12:52
  • @MakePeaceGreatAgain This is not an acceptable solution. Where would I copy them to? I would need to copy them to an intermediate location in order for the second project to know where to find them (or hard-code the output location of the first project). Additionally, every project that references the first would also need to then copy those files to its own output. I don't want this. Adding the project reference should be all that is necessary on the part of the second project, or any other projects that later will reference the first (or second!) project. – Suuper W May 28 '22 at 00:27

1 Answers1

2

The solution I found is to manually edit my .csproj file to add the following target:

  <Target Name="AddRuntimeDependenciesToContent"
          Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp'"
          BeforeTargets="GetCopyToOutputDirectoryItems"
          DependsOnTargets="GenerateBuildDependencyFile;
                            GenerateBuildRuntimeConfigurationFiles">
    <ItemGroup>
      <ContentWithTargetPath Include="$(ProjectDepsFilePath)"
                             Condition="'$(GenerateDependencyFile)' == 'true'"
                             CopyToOutputDirectory="PreserveNewest"
                             TargetPath="$(ProjectDepsFileName)" />
      <ContentWithTargetPath Include="$(ProjectRuntimeConfigFilePath)"
                             Condition="'$(GenerateRuntimeConfigurationFiles)' == 'true'"
                             CopyToOutputDirectory="PreserveNewest"
                             TargetPath="$(ProjectRuntimeConfigFileName)" />
    </ItemGroup>
  </Target>

This solution came from https://github.com/dotnet/sdk/issues/1675#issuecomment-658779827.

There were other somewhat similar solutions posted in that thread, but this is the only one that worked for me. The others would either not consistently copy the files to my second project, or cause the first project to fail to build due to attempting to access a file that didn't yet exist. The key difference with this one is the inclusion of the correct "BeforeTargets" property (and possibly also "DependsOnTargets"), controlling at which point in the build process the files are included.

Suuper W
  • 31
  • 1
  • 10