Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Issues using coreclr on Linux #613

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

Closed
Cronan opened this issue Feb 1, 2018 · 9 comments
Closed

Issues using coreclr on Linux #613

Cronan opened this issue Feb 1, 2018 · 9 comments

Comments

@Cronan
Copy link
Contributor

Cronan commented Feb 1, 2018

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>
@djoyce82
Copy link

djoyce82 commented Feb 2, 2018

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

@Cronan
Copy link
Contributor Author

Cronan commented Feb 2, 2018

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

@dmitriyse
Copy link
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.

@dmitriyse
Copy link
Contributor

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

@dmitriyse
Copy link
Contributor

This helps for managed C# code debugging.

@Cronan
Copy link
Contributor Author

Cronan commented Feb 2, 2018

@dmitriyse That's great, thanks 👍

@djoyce82
Copy link

djoyce82 commented Feb 5, 2018

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/

@Cronan
Copy link
Contributor Author

Cronan commented Feb 5, 2018

@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?

@filmor
Copy link
Member

filmor commented Nov 8, 2019

Closed in favour of #984.

@filmor filmor closed this as completed Nov 8, 2019
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

4 participants