1

MSBuildWorkspace does now show documents on computers other than the one I'm using to build the application.

I have seen Roslyn workspace for .NET Core's new .csproj format, but in my case, it works fine on the development computer (Documents is populated), but it is empty for the same project on a different computer.

So I am not sure why it would work on my development computer, but not on my other computer...?

This is the code :

    public static IReadOnlyList<CodeFile> ReadSolution(string path)
    {
        List<CodeFile> codes = new List<CodeFile>();
        using (MSBuildWorkspace workspace = MSBuildWorkspace.Create())
        {
            var solution = workspace.OpenSolutionAsync(path).Result;
            foreach (var project in solution.Projects)
            {
                //project.Documents.Count() is 0
                foreach (var doc in project.Documents)
                {
                    if (doc.SourceCodeKind == SourceCodeKind.Regular)
                    {
                        StringBuilder sb = new StringBuilder();
                        using (var sw = new StringWriter(sb))
                        {
                            var source = doc.GetTextAsync().Result;
                            source.Write(sw);
                            sw.WriteLine();
                        }

                        codes.Add(new CodeFile(doc.FilePath, sb.ToString()));
                    }
                }
            }
        }
        return codes;
    }
MineR
  • 2,144
  • 12
  • 18
  • MSBuild workspace needs msbuild installed and all the required msbuild task files for csharp/vb in order to work. It will likely only work in an app executing under a developer command prompt (one where msbuild.exe also works.) You will also need to include all the binding redirects found in msbuild.exe.config in your own app.confg. – Matt Warren Oct 03 '17 at 18:58

2 Answers2

2

It turns out that the newer versions of MSBuildWorkspace no long throw exceptions on failures, instead raising an event, WorkspaceFailure. (https://github.com/dotnet/roslyn/issues/15056)

This indicated that the required build tools (v15 / VS2017) were not available. The new 2017 Build Tools installer is both too complicated for our end users and much slower to install than the 2015 build tools installer was.

Instead, I came up with this less robust method to avoid that dependency:

    public static IReadOnlyList<CodeFile> ReadSolution(string path)
    {
        List<CodeFile> codes = new List<CodeFile>();

        var dirName = System.IO.Path.GetDirectoryName(path);
        var dir = new DirectoryInfo(dirName);
        var csProjFiles = dir.EnumerateFiles("*.csproj", SearchOption.AllDirectories);
        foreach(var csProjFile in csProjFiles)
        {
            var csProjPath = csProjFile.Directory.FullName;
            using (var fs = new FileStream(csProjFile.FullName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = XmlReader.Create(fs))
                {
                    while(reader.Read())
                    {
                        if(reader.Name.Equals("Compile", StringComparison.OrdinalIgnoreCase))
                        {
                            var fn = reader["Include"];
                            var filePath = Path.Combine(csProjPath, fn);
                            var text = File.ReadAllText(filePath);
                            codes.Add(new CodeFile(fn,text));
                        }
                    }
                }
            }
        }
        return codes;
    }
MineR
  • 2,144
  • 12
  • 18
  • If you don't execute the project file via MSBuild, you won't know about any generated files or what the correct set of references are. – Matt Warren Oct 03 '17 at 18:55
0

I had same problem I was using a .Net Core Console App.

I noticed that Microsoft.CodeAnalysis.Workspaces.MSBuild had the warning:

Package Microsoft.CodeAnalysis.Workspaces.MSBuild 2.10.0 was restored using '.Net Framework,version=v4.6.1' instead of the project target framework '.NetCoreApp,Version=v2.1'. this package may not be fully compatible with your project.

I changed to a .Net Framework Console App and I could access the documents.

Paul Spencer
  • 860
  • 5
  • 14