Electron.net: csproj ComputeFilesToPublish section cause "Error occurred during dotnet publish." error on Angular projects

Created on 7 Jun 2018  路  3Comments  路  Source: ElectronNET/Electron.NET

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

Most helpful comment

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

All 3 comments

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

Thank you @Blind-Striker

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' " />

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ernestotw85 picture ernestotw85  路  3Comments

BrianAllred picture BrianAllred  路  5Comments

Marcelh1983 picture Marcelh1983  路  5Comments

GregorBiswanger picture GregorBiswanger  路  4Comments

saad749 picture saad749  路  6Comments