kalulas / TableCraft

Console application and AvaloniaUI examples for TableCraft.Core: https://github.com/kalulas/TableCraft.Core
MIT License
4 stars 0 forks source link

Generate code with Mono.TextTemplating failed on self-contained version #10

Closed kalulas closed 1 year ago

kalulas commented 1 year ago

log-20230414.txt

2023-04-14 14:30:08.581 +08:00 [ERR] Unhandled exception
Mono.TextTemplating.TemplatingEngineException: Not a valid .NET Core host
   at Mono.TextTemplating.TemplatingEngine.GetOrCreateCompiler() in /_/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 67
   at Mono.TextTemplating.TemplatingEngine.CompileCode(IEnumerable`1 references, TemplateSettings settings, CodeCompileUnit ccu, CancellationToken token) in /_/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 303
   at Mono.TextTemplating.TemplatingEngine.CompileTemplateInternal(ParsedTemplate pt, String content, ITextTemplatingEngineHost host, TemplateSettings settings, CancellationToken token) in /_/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 277
   at Mono.TextTemplating.TemplatingEngine.ProcessTemplateAsync(ParsedTemplate pt, String content, TemplateSettings settings, ITextTemplatingEngineHost host, CancellationToken token) in /_/Mono.TextTemplating/Mono.TextTemplating/TemplatingEngine.cs:line 88
   at Mono.TextTemplating.TemplateGenerator.ProcessTemplateAsync(ParsedTemplate pt, String inputFileName, String inputContent, String outputFileName, TemplateSettings settings, CancellationToken token) in /_/Mono.TextTemplating/Mono.TextTemplating/TemplateGenerator.cs:line 269
   at TableCraft.Core.ConfigManager.GenerateCodeForUsage(String usage, ConfigInfo configInfo, String outputDirectory) in D:\Desktop\Workshop\Others\TableCraft\TableCraft.Core\ConfigManager.cs:line 128
   at TableCraft.Editor.ViewModels.MainWindowViewModel.GenerateCodeWithCurrentUsage() in D:\Desktop\Workshop\Others\TableCraft\TableCraft.Editor\ViewModels\MainWindowViewModel.cs:line 326

looking into Mono.TextTemplating.CodeCompilation.RuntimeInfo.GetDotNetCoreSdk(),

static bool DotnetRootIsValid (string root) => !string.IsNullOrEmpty (root) && (File.Exists (Path.Combine (root, "dotnet")) || File.Exists (Path.Combine (root, "dotnet.exe")));

// this should get us something like /usr/local/share/dotnet/shared/Microsoft.NETCore.App/5.0.0
var runtimeDir = Path.GetDirectoryName (typeof (object).Assembly.Location);
var dotnetRoot = Path.GetDirectoryName (Path.GetDirectoryName (Path.GetDirectoryName (runtimeDir)));
if (!DotnetRootIsValid (dotnetRoot)) {
    // this may happen on single file deployments
    return FromError (RuntimeKind.NetCore, "Not a valid .NET Core host");
}

It's clear that runtimeDir is the same as the executable file path on the self-contained version, resulting in dotnetRoot being invalid. This is the debugging output from a self-contained version of TableCraft:

  typeof (object).Assembly.Location
  "D:\\Applications\\TableCraft.Editor.win10-x64\\System.Private.CoreLib.dll"

  var runtimeDir = Path.GetDirectoryName (typeof (object).Assembly.Location)
  "D:\\Applications\\TableCraft.Editor.win10-x64"

  Path.GetDirectoryName (Path.GetDirectoryName (Path.GetDirectoryName (runtimeDir)));
  null
kalulas commented 1 year ago

the self-contained version will not be released in the future