⚡ Sangsakala Proxy 🏠 Home
Skip to content

Issues using coreclr on Linux #613

Closed

Description

@Cronan

Environment

Using the code in PR #612, I get the following errors trying some basic tests:

>>> import clr
>>> from System import Environment
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name Environment
>>> import clr
>>> from System import Array
>>> a = Array[int](100)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Cannot convert 100 to System.Int32[]
>>> import clr
>>> clr.AddReference("System.Drawing")
<System.Reflection.RuntimeAssembly object at 0x7f0c58881990>

Activity

  1. djoyce82 commented on Feb 2, 2018

    @djoyce82

    Other API calls do work though:

    Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58)
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import clr
    >>> from System import Array
    >>> myarray = Array[int]([1,2])
    >>> list(myarray)
    [1, 2]
    >>> from System import NullReferenceException
    >>> try:
    ...     raise NullReferenceException("aiieee!")
    ... except NullReferenceException as e:
    ...     print(e.Message)
    ...     print(e.Source)
    ...
    aiieee!
    None
    
  2. Cronan commented on Feb 2, 2018

    @Cronan
    ContributorAuthor

    That works for me too - curious - I'm trying to get debuggers working in Linux to get a handle on what's going on ...

  3. dmitriyse commented on Feb 2, 2018

    @dmitriyse
    Contributor

    I am using docker, VS 2017 +docker tools + custom C# script for DTE and debugging works perfectly.

    This an example of my script script for C# Interactive window.

    #r "EnvDTE100"
    using EnvDTE;
    using EnvDTE80;
    using System;
    using System.IO;
    using System.Diagnostics;
    using System.Linq;
    
    var dte = (DTE)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.15.0");
    
    var startupProject = Path.Combine(
        Path.GetDirectoryName(dte.Solution.FullName),
        (string)(((object[])dte.Solution.SolutionBuild.StartupProjects).First()));
    
    Project FindProjectByNameInFolder(Project solutionFolder, string name)
    {
        for (int i = 1; i <= solutionFolder.ProjectItems.Count; i++)
        {
            var subProject = solutionFolder.ProjectItems.Item(i).SubProject;
            if (subProject == null)
            {
                continue;
            }
    
            if (subProject.FullName == name)
            {
                return subProject;
            }
    
            if (subProject.Kind == ProjectKinds.vsProjectKindSolutionFolder)
            {
                var p = FindProjectByNameInFolder(subProject, name);
                if (p != null)
                {
                    return p;
                }
            }
        }
    
        return null;
    }
    
    Project FindProjectByName(string name)
    {
        foreach (var obj in dte.Solution.Projects)
        {
            var project = obj as Project;
            if (project == null)
            {
                continue;
            }
    
            if (project.FullName == name)
            {
                return project;
            }
    
            if (project.Kind == ProjectKinds.vsProjectKindSolutionFolder)
            {
                var p = FindProjectByNameInFolder(project, name);
                if (p != null)
                {
                    return p;
                }
            }
        }
    
        return null;
    }
    
    int SimpleExecute(string fileName, string arguments, out string stdOut, out string stdErr)
    {
        // Start the child process.
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        // Redirect the output stream of the child process.
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.FileName = fileName;
        p.StartInfo.Arguments = arguments;
        p.Start();
        // Do not wait for the child process to exit before
        // reading to the end of its redirected stream.
        // p.WaitForExit();
        // Read the output stream first and then wait.
        stdOut = p.StandardOutput.ReadToEnd();
        stdErr = p.StandardError.ReadToEnd();
        p.WaitForExit();
        return p.ExitCode;
    }
    
    var project = FindProjectByName(startupProject);
    
    var projectDir = Path.GetDirectoryName(project.FullName);
    var solutionDir = Path.GetDirectoryName(dte.Solution.FullName);
    var projectRelativeDir = projectDir.Replace(solutionDir + "\\", string.Empty).Replace("\\", "/");
    
    var outputPath = project.ConfigurationManager.ActiveConfiguration.Properties.Item("OutputPath").Value.ToString().Replace("\\", "/").TrimEnd('/');
    var outputFileName = project.Properties.Item("OutputFileName").Value.ToString();
    if (string.IsNullOrWhiteSpace(outputFileName))
    {
        outputFileName = project.Name + ".dll";
    }
    
    ////Console.WriteLine(outputFileName);
    ////Console.WriteLine(outputPath);
    ////Console.WriteLine(projectRelativeDir);
    var psArgs = $"ps --no-trunc --filter ancestor=my-name:{Environment.GetEnvironmentVariable("USERNAME")}-dev -n 1 --format \"{{{{.ID}}}}\"";
    ////Console.WriteLine(psArgs);
    
    var exitCode = SimpleExecute("docker", psArgs, out var stdOut, out var stdErr);
    Console.WriteLine(stdOut);
    if (exitCode != 0)
    {
        Console.WriteLine(stdOut);
        Console.WriteLine(stdErr);
        return;
    }
    var containerId = stdOut.Substring(0, 12);
    
    
    var launchOptionsContent =
        $@"
    {{
      ""version"": ""0.2.0"",
      ""adapter"": ""C:\\Program Files\\Docker\\Docker\\Resources\\bin\\docker.exe"",
      ""adapterArgs"": ""exec -i {containerId} /remote_debugger/vsdbg --interpreter=vscode"",
      ""languageMappings"": {{
            ""C#"": {{
                ""languageId"": ""3F5162F8-07C6-11D3-9053-00C04FA302A1"",
          ""extensions"": [ ""*"" ]
        }}
      }},
      ""exceptionCategoryMappings"": {{
        ""CLR"": ""449EC4CC-30D2-4032-9256-EE18EB41B62B"",
        ""MDA"": ""6ECE07A9-0EDE-45C4-8296-818D8FC401D4""
      }},
      ""configurations"": [
        {{
          ""name"": "".NET Core Launch"",
          ""type"": ""coreclr"",
          ""request"": ""launch"",
          ""preLaunchTask"": ""build"",
          ""cwd"": ""/project/{projectRelativeDir}"",
          ""program"": ""/usr/bin/dotnet"",
          ""args"": [
             ""--additionalprobingpath"",
             ""/nuget/packages"",
             ""{outputPath}/{outputFileName}"",
             ""-t=monitorAndAnalyze""]
        }}
      ]
    }}";
    
    var tempFileName = Path.GetTempFileName();
    File.WriteAllText(tempFileName, launchOptionsContent);
    
    Console.WriteLine("Starging debug with options:");
    Console.WriteLine(launchOptionsContent);
    dte.ExecuteCommand("DebugAdapterHost.Launch", $"/LaunchJson:{tempFileName}");
    //File.Delete(tempFileName);

    You can try this approach.

  4. dmitriyse commented on Feb 2, 2018

    @dmitriyse
    Contributor

    Probably (when I will have enough time) I will inject this toolset into he pythonnet project.

  5. dmitriyse commented on Feb 2, 2018

    @dmitriyse
    Contributor

    This helps for managed C# code debugging.

  6. Cronan commented on Feb 2, 2018

    @Cronan
    ContributorAuthor

    @dmitriyse That's great, thanks 👍

  7. djoyce82 commented on Feb 5, 2018

    @djoyce82

    FYI I've also had success debugging remotely by just attaching from Visual Studio:

    https://blogs.msdn.microsoft.com/devops/2017/01/26/debugging-net-core-on-unix-over-ssh/

  8. Cronan commented on Feb 5, 2018

    @Cronan
    ContributorAuthor

    @djoyce82 @dmitriyse I got debugging using VSCode working in Linux - now trying to establish what is going wrong with the basic functionality. What is the code that usually prevents clr.AddReference() in Python from displaying the System.Reflection.RuntimeAssembly?

  9. filmor commented on Nov 8, 2019

    @filmor
    Member

    Closed in favour of #984.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions