Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Packages are missing after a successful dotnet publish

The publish command works fine but after it is run, I have to restore the project as there are missing packages...

So if I run the following command :

dotnet publish --runtime win-x64

Then the publish works but I have missing packages in my project right after.

BUT if I run publish without a runtime :

dotnet publish

Then the publish works fine and I don't have any missing package.

Is it a normal behaviour? What can I do to fix this? This is annoying.

Here is my csproj file :

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <LangVersion>7.1</LangVersion>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="AWSSDK.Extensions.NETCore.Setup">
        <Version>3.3.6</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.App">
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.StaticFiles">
        <Version>2.1.1</Version>
    </PackageReference>
    <PackageReference Include="NSwag.AspNetCore">
        <Version>12.0.13</Version>
    </PackageReference>
    <PackageReference Include="NSwag.MSBuild">
        <Version>12.0.13</Version>
    </PackageReference>
</ItemGroup>
<ItemGroup>
    <ProjectReference Include="..\MyProject.Analytics\MyProject.Analytics.csproj" />
    <ProjectReference Include="..\MyProject.ApiClient\MyProject.ApiClient.csproj" />
    <ProjectReference Include="..\MyProject.CommonApi\MyProject.CommonApi.csproj" />
    <ProjectReference Include="..\MyProject.Common\MyProject.Common.csproj" />
    <ProjectReference Include="..\MyProject.DbAccess\MyProject.DbAccess.csproj" />
    <ProjectReference Include="..\MyProject.Logging\MyProject.Logging.csproj" />
    <ProjectReference Include="..\MyProject.Messaging\MyProject.Messaging.csproj" />
    <ProjectReference Include="..\MyProject.Model\MyProject.Model.csproj" />
    <ProjectReference Include="..\MyProject.Settings\MyProject.Settings.csproj" />
</ItemGroup>
<ItemGroup>
    <Content Update="appsettings.Api.Development.json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.Api.json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.Api.Production.json">
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Include=".ebextensions\never-sleep-again.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
    <Content Update="appsettings.Api.PreProduction.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
</ItemGroup>
<ItemGroup>
  <Folder Include="MyProjectlogs\Development" />
</ItemGroup>
<ItemGroup>
    <ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<Target Name="NSwag" AfterTargets="Build">
    <Copy SourceFiles="@(Reference)" DestinationFolder="$(OutDir)References" />
    <Exec Command="$(NSwagExe_Core21) run nswag.json /variables" />
    <RemoveDir Directories="$(OutDir)References" />
</Target>

Edit : From the restore nugets logs, I get the following for each dependency of the project I publish

@   Project 'MyProject.Api' is affected (InstallCount = 0)

So it actually thinks something is different but doesn't seem to install anything.

like image 408
dyesdyes Avatar asked Jun 26 '19 14:06

dyesdyes


People also ask

How do I restore a .NET package?

Restore using the dotnet CLIUse the dotnet restore command, which restores packages listed in the project file (see PackageReference). With . NET Core 2.0 and later, restore is done automatically with dotnet build and dotnet run . As of NuGet 4.0, this runs the same code as nuget restore .

What does dotnet publish command do?

Description. dotnet publish compiles the application, reads through its dependencies specified in the project file, and publishes the resulting set of files to a directory. The output includes the following assets: Intermediate Language (IL) code in an assembly with a dll extension.

What is the difference between dotnet build and publish?

Build compiles the source code into a (hopefully) runnable application. Publish takes the results of the build, along with any needed third-party libraries and puts it somewhere for other people to run it.


1 Answers

Digging around, I found this post.

The interesting part is :

restore and build can be run implicitly as part of another command, like publish. When run implicitly as part of another command, they are provided with additional context so that the right artifacts are produced. When you publish with a runtime (for example, dotnet publish -r linux-x64), the implicit restore restores packages for the linux-x64 runtime. If you call restore explicitly, it does not restore runtime packages by default, because it doesn't have that context.

Basically I had a mismatch between the runtime .netcore 2.1.1 vs the restore one 2.1.X (X != 1).

The solution (explained in the link above) is to add this to the project file :

<PropertyGroup>
    ...
    <RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
    <TargetLatestRuntimePatch>true</TargetLatestRuntimePatch>
</PropertyGroup>
like image 118
dyesdyes Avatar answered Oct 15 '22 23:10

dyesdyes