Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#load error Metadata references are not supported #523

Open
qinqoushui opened this issue Mar 23, 2024 · 1 comment
Open

#load error Metadata references are not supported #523

qinqoushui opened this issue Mar 23, 2024 · 1 comment

Comments

@qinqoushui
Copy link

Describe the bug
use any csx error Metadata references are not supported
To Reproduce
csx

#r "nuget: RazorEngineCore, 2023.11.2"

//https://github.com/adoconnection/RazorEngineCore

using RazorEngineCore;

IRazorEngine razor  = new RazorEngine();

when a use in another csx and run ,error

#r "nuget: RazorEngineCore, 2023.11.2" 
#r "nuget:Newtonsoft.Json/13.0.3"
#load "./comm/RazorCore.csx"

using Microsoft.AspNetCore.Razor.Language.Extensions;
using Newtonsoft.Json.Linq;
using RazorEngineCore;

 
Console.WriteLine("aa");

Expected behavior

Version

  • OS:
  • Build (or commit ID if built from source):
@qinqoushui
Copy link
Author

a temporary situation,edit ExecutionHost.cs

 private   SyntaxTree ParseCode(string code, CSharpParseOptions parseOptions)
 {
     foreach (var f in CodeParsers)
     {
         code = f.Invoke(code);
     }
...
}
//#load csx出现源数据引用问题,临时 直接将文件合并到代码中,并替换掉内容
CodeParsers.Add(s =>
{
    //#load "path"
    Regex reg = new Regex(@$"//#load\s*\""(?<f>.*)\""\s*", RegexOptions.Multiline);
    while (reg.IsMatch(s))
    {
        var m = reg.Match(s);
        string f = m.Groups["f"].Value.Trim().Replace("/", "\\").Trim();
        string fileName = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(_parameters.ScriptFileName)!, f));
        string output = "";
        if (File.Exists(fileName))
        {
            output = File.ReadAllText(fileName);
        }
        s = string.Format(null, "{0}{1}{2}", s.Substring(0, m.Index), output, s.Substring(m.Index + m.Length));
    }
    //调整using和#r到最前面
    reg = new Regex(@"^(?:#r\s+""[^""]*""|using\s+\w+(?:\.\w+)*;)", RegexOptions.Multiline);
    MatchCollection ms = reg.Matches(s);
    if (ms != null)
    {
        string usr = string.Join("\r\n", ms.Where(r => r.Value!.StartsWith("#r")).Select(r => r.Value).Distinct().ToArray());
        string us = string.Join("\r\n", ms.Where(r => !r.Value!.StartsWith("#r")).Select(r => r.Value).Distinct().ToArray());
        s = string.Join("\r\n", usr, us, reg.Replace(s, ""));
    }

    return s;

});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant