csproj ComputeFilesToPublish section cause "Error occurred during dotnet publish." error on Angular projects #174

Closed
opened 2026-01-29 16:32:54 +00:00 by claunia · 3 comments
Owner

Originally created by @Blind-Striker on GitHub (Jun 7, 2018).

Hi,

First of all, thank you for developing such a project. I trying to use Electron.NET with Visual Studio Angular 5 template. To facilitate publishing process they have put some post publish scripts to csproj file.

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
    <!-- Set this to true if you enable server-side prerendering -->
    <BuildServerSideRenderer>false</BuildServerSideRenderer>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="ElectronNET.API" Version="0.0.9" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
    <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.0.0" />
  </ItemGroup>
  
  <ItemGroup>
    <DotNetCliToolReference Include="ElectronNET.CLI" Version="0.0.9" />
  </ItemGroup>
  
  <ItemGroup>
    <!-- Don't publish the SPA source files, but do show them in the project files list -->
    <Content Remove="$(SpaRoot)**" />
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" />
  </ItemGroup>
  
  <ItemGroup>
    <!-- Files not to publish (note that the 'dist' subfolders are re-added below) -->
    <Content Remove="ClientApp\**" />
  </ItemGroup>
  
  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
  </Target>
  
  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <!-- As part of publishing, ensure the JS resources are freshly built in production mode -->
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
    <!-- Include the newly-built files in the publish output -->
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" />
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>%(DistFiles.Identity)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
  
  <ItemGroup>
    <Content Update="electron.manifest.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

As you can see they run some npm commands on PublishRunWebpack. But if i run dotnet electronize start command, , I get the "Error occurred during dotnet publish." error and if i remove this section dotnet electronize start runs without an error.

I have inspect the source code of ElectronNET.CLI and found which command cause this error.

StartElectronCommand.cs#L54

Its executing dotnet publish -r win-x64 --output "D:\My\infinity-item\src\InfinityItem\InfinityItem\obj\Host\bin" command in my environment.
And if I run this command manually, it works run without an error.
Then i reliazed this command also runs succesfuly with dotnet electronize start . Because i can see compiled output on D:\My\infinity-item\src\InfinityItem\InfinityItem\obj\Host\bin path.

So, the problem may be related to exit codes, even if npm commands are successful, when ProcessHelper.CmdExecute is called.

I temporarily solve my problem with powershell script below

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$clientAppName = 'ClientApp'
$spaRoot = $directorypath + "\$clientAppName"

echo $clientAppName
echo $spaRoot

cd $spaRoot
npm install
npm run build -- --prod

cd..

Copy-Item -Path $spaRoot\dist -Recurse -Destination "$directorypath\obj\Host\bin\$clientAppName\dist" -Container

dotnet electronize start

Build command

$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$clientAppName = 'ClientApp'
$spaRoot = $directorypath + "\$clientAppName"

echo $clientAppName
echo $spaRoot

dotnet electronize build /target win

Copy-Item -Path $spaRoot\dist -Recurse -Destination "$directorypath\bin\desktop\ElectronNET.Host-win32-x64\resources\app\bin\$clientAppName\dist" -Container
Originally created by @Blind-Striker on GitHub (Jun 7, 2018). Hi, First of all, thank you for developing such a project. I trying to use Electron.NET with Visual Studio Angular 5 template. To facilitate publishing process they have put some post publish scripts to csproj file. ```xml <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked> <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion> <IsPackable>false</IsPackable> <SpaRoot>ClientApp\</SpaRoot> <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes> <!-- Set this to true if you enable server-side prerendering --> <BuildServerSideRenderer>false</BuildServerSideRenderer> </PropertyGroup> <ItemGroup> <PackageReference Include="ElectronNET.API" Version="0.0.9" /> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" /> <PackageReference Include="Microsoft.AspNetCore.SpaServices.Extensions" Version="2.0.0" /> </ItemGroup> <ItemGroup> <DotNetCliToolReference Include="ElectronNET.CLI" Version="0.0.9" /> </ItemGroup> <ItemGroup> <!-- Don't publish the SPA source files, but do show them in the project files list --> <Content Remove="$(SpaRoot)**" /> <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**" /> </ItemGroup> <ItemGroup> <!-- Files not to publish (note that the 'dist' subfolders are re-added below) --> <Content Remove="ClientApp\**" /> </ItemGroup> <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') "> <!-- Ensure Node.js is installed --> <Exec Command="node --version" ContinueOnError="true"> <Output TaskParameter="ExitCode" PropertyName="ErrorCode" /> </Exec> <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE." /> <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" /> </Target> <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish"> <!-- As part of publishing, ensure the JS resources are freshly built in production mode --> <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " /> <!-- Include the newly-built files in the publish output --> <ItemGroup> <DistFiles Include="$(SpaRoot)dist\**; $(SpaRoot)dist-server\**" /> <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" /> <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)"> <RelativePath>%(DistFiles.Identity)</RelativePath> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> </ResolvedFileToPublish> </ItemGroup> </Target> <ItemGroup> <Content Update="electron.manifest.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Project> ``` As you can see they run some npm commands on `PublishRunWebpack`. But if i run `dotnet electronize start` command, , I get the "Error occurred during dotnet publish." error and if i remove this section `dotnet electronize start` runs without an error. I have inspect the source code of ElectronNET.CLI and found which command cause this error. [StartElectronCommand.cs#L54](https://github.com/ElectronNET/Electron.NET/blob/master/ElectronNET.CLI/Commands/StartElectronCommand.cs#L54) Its executing `dotnet publish -r win-x64 --output "D:\My\infinity-item\src\InfinityItem\InfinityItem\obj\Host\bin"` command in my environment. And if I run this command manually, it works run without an error. Then i reliazed this command also runs succesfuly with `dotnet electronize start` . Because i can see compiled output on `D:\My\infinity-item\src\InfinityItem\InfinityItem\obj\Host\bin` path. So, the problem may be related to exit codes, even if npm commands are successful, when ProcessHelper.CmdExecute is called. I temporarily solve my problem with powershell script below ```powershell $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $clientAppName = 'ClientApp' $spaRoot = $directorypath + "\$clientAppName" echo $clientAppName echo $spaRoot cd $spaRoot npm install npm run build -- --prod cd.. Copy-Item -Path $spaRoot\dist -Recurse -Destination "$directorypath\obj\Host\bin\$clientAppName\dist" -Container dotnet electronize start ``` Build command ```powershell $invocation = (Get-Variable MyInvocation).Value $directorypath = Split-Path $invocation.MyCommand.Path $clientAppName = 'ClientApp' $spaRoot = $directorypath + "\$clientAppName" echo $clientAppName echo $spaRoot dotnet electronize build /target win Copy-Item -Path $spaRoot\dist -Recurse -Destination "$directorypath\bin\desktop\ElectronNET.Host-win32-x64\resources\app\bin\$clientAppName\dist" -Container ```
Author
Owner

@Blind-Striker commented on GitHub (Jun 9, 2018):

I made an cake addin for Electron.Net to use in my personel project. Maybe it can be useful for someone else too.

https://github.com/Blind-Striker/Cake.Electron.Net

@Blind-Striker commented on GitHub (Jun 9, 2018): I made an cake addin for Electron.Net to use in my personel project. Maybe it can be useful for someone else too. https://github.com/Blind-Striker/Cake.Electron.Net
Author
Owner

@GregorBiswanger commented on GitHub (Jul 22, 2018):

Thank you @Blind-Striker

@GregorBiswanger commented on GitHub (Jul 22, 2018): Thank you @Blind-Striker
Author
Owner

@leidsongermano commented on GitHub (Aug 14, 2018):

I could solved this problem supressing the ng build output. I made these changes on .csproj:
From:
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
To:
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod --no-progress" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod --no-progress" Condition=" '$(BuildServerSideRenderer)' == 'true' " />

@leidsongermano commented on GitHub (Aug 14, 2018): I could solved this problem supressing the ng build output. I made these changes on .csproj: From: `<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />` To: `<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod --no-progress" /> <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod --no-progress" Condition=" '$(BuildServerSideRenderer)' == 'true' " /> `
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#174