Home > Blockchain >  How do I include .deps and .runtimeconfig files as project dependencies?
How do I include .deps and .runtimeconfig files as project dependencies?

Time:05-31

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

CodePudding user response:

Right click the file in your Solution Explorer and select Properties. Within the dialog set

Build Action to Content

Copy to output directory to Copy if newer.

CodePudding user response:

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.

  • Related