Excel-DNA / ExcelDna

Excel-DNA - Free and easy .NET for Excel. This repository contains the core Excel-DNA library.
https://excel-dna.net
zlib License
1.26k stars 270 forks source link

Debugging fails when <ExcelDnaUnpack>true<ExcelDnaUnpack> is set #695

Closed govert closed 4 days ago

govert commented 1 month ago

With the project option to make an output 'publish' directory with a completely-unpacked add-in, the add-in we get in the output folder does not work anymore. The -AddIn64.xll here does not contain the packed ExcelDna.* assemblies, but they are not present in this directory either. When starting to debug, we get image

One option is to copy back the ExcelDna.*.dll files from the publish directory

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
      <!-- Copy all ExcelDna.*.dll files back to the output directory -->
    <Exec Command="xcopy &quot;$(OutputPath)publish\ExcelDna.*.dll&quot; &quot;$(OutputPath)&quot; /Y /I /S" />
  </Target>

But then opening the add-in without the debugger crashes Excel, and with the debugger we get this unhandled exception in the loading image

The culprit in this case (which makes the output directory different to the publish directory) is the presence of an <MyProject>-AddIn64.deps.json file. This file does not list the extra ExcelDna.Loader.dll, and this causes the load to fail for some reason (it would be nice to know why). This seems to have changed in Excel-DNA version 1.7.0 (and is reported to still have worked in 1.6.0, I haven't checked).

By deleting the <MyProject>-AddIn64.deps.json file we get an output directory that can be debugged. This can be done in another project task:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">

    <!-- Copy all ExcelDna.*.dll files back to the output directory -->
    <Exec Command="xcopy &quot;$(OutputPath)publish\ExcelDna.*.dll&quot; &quot;$(OutputPath)&quot; /Y /I /S" />

    <!-- Delete <ProjectName>-AddIn64.deps.json from the output directory -->
    <Message Text="Deleting $(OutputPath)$(MSBuildProjectName)-AddIn64.deps.json" Importance="high" />
    <Exec Command="del /Q &quot;$(OutputPath)$(MSBuildProjectName)-AddIn64.deps.json&quot;" />

    <!-- Delete <ProjectName>-AddIn.deps.json from the output directory -->
    <Message Text="Deleting $(OutputPath)$(MSBuildProjectName)-AddIn.deps.json" Importance="high" />
    <Exec Command="del /Q &quot;$(OutputPath)$(MSBuildProjectName)-AddIn.deps.json&quot;" />

  </Target>

It is possible to edit the .deps.json file to add the ExcelDna.Loader.dll but that doesn't seem like a practical approach:

      "ExcelDna.Integration/1.8.0-rc": {
        "runtime": {
          "lib/net6.0-windows7.0/ExcelDna.Integration.dll": {
            "assemblyVersion": "1.1.0.0",
            "fileVersion": "1.8.0.4"
          },
          "ExcelDna.Loader.dll": {}
        }