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

Incorrect OutputKind in AdhocWorkspace #7

Closed
josephwoodward opened this issue Oct 13, 2017 · 5 comments
Closed

Incorrect OutputKind in AdhocWorkspace #7

josephwoodward opened this issue Oct 13, 2017 · 5 comments

Comments

@josephwoodward
Copy link
Collaborator

josephwoodward commented Oct 13, 2017

I've noticed when loading a project into the AdHocWorkspace the OutputKind enum is always set to ConsoleApplication (see below as an example).

Having looked through the Buildalyzer source code can't see where the project type is inferred, leading me to assume the reason it's set to ConsoleApplication is because it's the first property in the OutputKind enum so defaults to that.

var analyzer = new Analyzer();
var projectAnalyzer = analyzer.GetProject(@"../demo/ExampleDllLibrary.csproj");
var workspace = projectAnalyzer.GetWorkspace();
var project = workspace.CurrentSolution.Projects.FirstOrDefault(x => x.AssemblyName == "ExampleDllLibrary");
OutputKind kind = project.CompilationOptions.OutputKind;
Console.WriteLine(kind); // ConsoleApplication

Not being too savvy with the MSBuild API I'm wondering if you can point me in the right direction of where I'd be able to get this information so I can create a PR to fix it?

@josephwoodward josephwoodward changed the title OutputKind Incorrect OutputKind in AdhocWorkspace Oct 13, 2017
@daveaglick
Copy link
Collaborator

Good catch. Looks like the MSBuild property OutputType is what you want. It has possible values of "Library", "Exe", "Module", or "Winexe". Those look like they map pretty well to the OutputKind enum in Roslyn.

You can get the MSBuild property from the MSBuild ProjectInstance instance (ProjectAnalyzer.CompiledProject or the return value of Compile()) like this:

string outputType = analyzer.CompiledProject?.GetPropertyValue("OutputType");

Be sure to check for null on the ProjectAnalyzer.CompiledProject property since it won't be set if compilation failed.

@josephwoodward
Copy link
Collaborator Author

josephwoodward commented Oct 13, 2017

Didn't see GetPropertyValue, that's far more elegant than my solution:

projectInstance.Properties.FirstOrDefault(x => x.Name == "OutputType");

😁

So you have to compile the project before you can figure out its output type? Can it not be inferred from the .csproj file?

@daveaglick
Copy link
Collaborator

Hmm... Might be available on the Project instance after Load().

@josephwoodward
Copy link
Collaborator Author

Yup, there it is! Much better, thanks. Currently putting together a PR now.

@josephwoodward
Copy link
Collaborator Author

There we go, #8 adds CompilationOptions and is ready for review.

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

2 participants