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

[WIP] Updating MSBuild for the future #667

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<add key="nugetbuild" value="https://www.myget.org/F/nugetbuild/api/v3/index.json" />
<clear/>
<add key="buildtools" value="https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json" />
<add key="cli-deps" value="https://dotnet.myget.org/F/cli-deps/api/v3/index.json" />
<add key="NuGet" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
17 changes: 17 additions & 0 deletions src/OmniSharp.Host/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ public static void Main(string[] args)
}
}

#if NET46
if (PlatformHelper.IsMono)
{
// Mono uses ThreadPool threads for its async/await implementation.
// Ensure we have an acceptable lower limit on the threadpool size to avoid deadlocks and ThreadPool starvation.
const int MIN_WORKER_THREADS = 8;

int currentWorkerThreads, currentCompletionPortThreads;
System.Threading.ThreadPool.GetMinThreads(out currentWorkerThreads, out currentCompletionPortThreads);

if (currentWorkerThreads < MIN_WORKER_THREADS)
{
System.Threading.ThreadPool.SetMinThreads(MIN_WORKER_THREADS, currentCompletionPortThreads);
}
}
#endif

Environment = new OmnisharpEnvironment(applicationRoot, serverPort, hostPID, logLevel, transportType, otherArgs.ToArray());

var config = new ConfigurationBuilder()
Expand Down
1 change: 0 additions & 1 deletion src/OmniSharp.Host/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
},
"dependencies": {
"OmniSharp.Abstractions": "1.0.0",
"OmniSharp.Nuget": "1.0.0",
"OmniSharp.Stdio": "1.0.0",
"OmniSharp.Plugins": "1.0.0",
"OmniSharp.Roslyn": "1.0.0",
Expand Down
46 changes: 42 additions & 4 deletions src/OmniSharp.MSBuild/MSBuildProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ private ProjectFileInfo AddProject(string filePath)
return null;
}

_logger.LogInformation($"Add project: {fileInfo.ProjectFilePath}");

_projects.Add(fileInfo);

var compilationOptions = CreateCompilationOptions(fileInfo);
Expand Down Expand Up @@ -211,6 +213,24 @@ private static CSharpCompilationOptions CreateCompilationOptions(ProjectFileInfo
result = result.WithAllowUnsafe(true);
}

var specificDiagnosticOptions = new Dictionary<string, ReportDiagnostic>(projectFileInfo.SuppressedDiagnosticIds.Count);

// Always suppress CS1701 (Assuming assembly reference 'x' used by 'y' matches identity 'z'. you may need to supply runtime policy)
specificDiagnosticOptions.Add("CS1701", ReportDiagnostic.Suppress);

if (projectFileInfo.SuppressedDiagnosticIds.Any())
{
foreach (var id in projectFileInfo.SuppressedDiagnosticIds)
{
if (!specificDiagnosticOptions.ContainsKey(id))
{
specificDiagnosticOptions.Add(id, ReportDiagnostic.Suppress);
}
}
}

result = result.WithSpecificDiagnosticOptions(specificDiagnosticOptions);

if (projectFileInfo.SignAssembly && !string.IsNullOrEmpty(projectFileInfo.AssemblyOriginatorKeyFile))
{
var keyFile = Path.Combine(projectFileInfo.ProjectDirectory, projectFileInfo.AssemblyOriginatorKeyFile);
Expand Down Expand Up @@ -330,7 +350,13 @@ private void UpdateSourceFiles(Project project, IList<string> sourceFiles)
continue;
}

// If not, add a new document.
// If the source file doesn't exist on disk, don't try to add it.
if (!File.Exists(sourceFile))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is needed? Many editors atom/vscode don't require you to save a file to start writing in the file.

It would be great if we could offer a nice editing experience even if the file doesn't yet exist. Though that then has it's own set of problems... that maybe we shouldn't dive into just yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is avoiding a crash below. Unless atom/vscode show you a view of your project, I'm not sure it matters whether we do something fancy in the workspace. This is the scenario where VS would show you a little warning icon in the solution explorer.

{
continue;
}

// If all is OK, add a new document.
using (var stream = File.OpenRead(sourceFile))
{
var sourceText = SourceText.From(stream, encoding: Encoding.UTF8);
Expand Down Expand Up @@ -379,7 +405,10 @@ private void UpdateParseOptions(Project project, LanguageVersion languageVersion

private void UpdateProjectReferences(Project project, IList<string> projectReferences)
{
_logger.LogInformation($"Update project: {project.Name}");

var existingProjectReferences = new HashSet<ProjectReference>(project.ProjectReferences);
var addedProjectReferences = new HashSet<ProjectReference>();

foreach (var projectReference in projectReferences)
{
Expand All @@ -394,7 +423,11 @@ private void UpdateProjectReferences(Project project, IList<string> projectRefer
continue;
}

_workspace.AddProjectReference(project.Id, reference);
if (!addedProjectReferences.Contains(reference))
{
_workspace.AddProjectReference(project.Id, reference);
addedProjectReferences.Add(reference);
}
}
else
{
Expand All @@ -411,6 +444,7 @@ private void UpdateProjectReferences(Project project, IList<string> projectRefer
private void UpdateReferences(Project project, IList<string> references)
{
var existingReferences = new HashSet<MetadataReference>(project.MetadataReferences);
var addedReferences = new HashSet<MetadataReference>();

foreach (var referencePath in references)
{
Expand All @@ -427,8 +461,12 @@ private void UpdateReferences(Project project, IList<string> references)
continue;
}

_logger.LogDebug($"Adding reference '{referencePath}' to '{project.Name}'.");
_workspace.AddMetadataReference(project.Id, metadataReference);
if (!addedReferences.Contains(metadataReference))
{
_logger.LogDebug($"Adding reference '{referencePath}' to '{project.Name}'.");
_workspace.AddMetadataReference(project.Id, metadataReference);
addedReferences.Add(metadataReference);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ private static class PropertyNames
public const string LangVersion = nameof(LangVersion);
public const string OutputType = nameof(OutputType);
public const string MSBuildExtensionsPath = nameof(MSBuildExtensionsPath);
public const string NoWarn = nameof(NoWarn);
public const string ProjectGuid = nameof(ProjectGuid);
public const string ProjectName = nameof(ProjectName);
public const string _ResolveReferenceDependencies = nameof(_ResolveReferenceDependencies);
public const string SignAssembly = nameof(SignAssembly);
public const string SolutionDir = nameof(SolutionDir);
public const string TargetFrameworkMoniker = nameof(TargetFrameworkMoniker);
public const string TargetFrameworkRootPath = nameof(TargetFrameworkRootPath);
public const string TargetPath = nameof(TargetPath);
public const string VisualStudioVersion = nameof(VisualStudioVersion);
}
Expand Down
28 changes: 17 additions & 11 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public partial class ProjectFileInfo
public string AssemblyOriginatorKeyFile { get; }
public bool GenerateXmlDocumentation { get; }
public IList<string> PreprocessorSymbolNames { get; }
public IList<string> SuppressedDiagnosticIds { get; }

public IList<string> SourceFiles { get; }
public IList<string> References { get; }
Expand All @@ -55,6 +56,7 @@ private ProjectFileInfo(
string assemblyOriginatorKeyFile,
bool generateXmlDocumentation,
IList<string> defineConstants,
IList<string> suppressedDiagnosticIds,
IList<string> sourceFiles,
IList<string> references,
IList<string> projectReferences,
Expand All @@ -73,6 +75,7 @@ private ProjectFileInfo(
this.AssemblyOriginatorKeyFile = assemblyOriginatorKeyFile;
this.GenerateXmlDocumentation = generateXmlDocumentation;
this.PreprocessorSymbolNames = defineConstants;
this.SuppressedDiagnosticIds = suppressedDiagnosticIds;
this.SourceFiles = sourceFiles;
this.References = references;
this.ProjectReferences = projectReferences;
Expand Down Expand Up @@ -101,13 +104,6 @@ public static ProjectFileInfo Create(
return null;
}

#if NET46
if (PlatformHelper.IsMono)
{
return CreateForMono(projectFilePath, solutionDirectory, options, logger, diagnostics);
}
#endif

var globalProperties = new Dictionary<string, string>
{
{ PropertyNames.DesignTimeBuild, "true" },
Expand All @@ -120,6 +116,15 @@ public static ProjectFileInfo Create(
{
globalProperties.Add(PropertyNames.MSBuildExtensionsPath, options.MSBuildExtensionsPath);
}
else
{
globalProperties.Add(PropertyNames.MSBuildExtensionsPath, AppContext.BaseDirectory);
}

if (PlatformHelper.IsMono)
{
globalProperties.Add(PropertyNames.TargetFrameworkRootPath, "/Library/Frameworks/Mono.framework/Libraries/mono/xbuild-frameworks");
}

if (!string.IsNullOrWhiteSpace(options.VisualStudioVersion))
{
Expand All @@ -128,12 +133,12 @@ public static ProjectFileInfo Create(

var collection = new ProjectCollection(globalProperties);

logger.LogInformation($"Using toolset {options.ToolsVersion ?? collection.DefaultToolsVersion} for '{projectFilePath}'");

var project = string.IsNullOrEmpty(options.ToolsVersion)
? collection.LoadProject(projectFilePath)
: collection.LoadProject(projectFilePath, options.ToolsVersion);

logger.LogInformation($"Using tools version: {project.ToolsVersion}");

var projectInstance = project.CreateProjectInstance();
var buildResult = projectInstance.Build(TargetNames.ResolveReferences,
new[] { new MSBuildLogForwarder(logger, diagnostics) });
Expand All @@ -155,6 +160,7 @@ public static ProjectFileInfo Create(
var assemblyOriginatorKeyFile = projectInstance.GetPropertyValue(PropertyNames.AssemblyOriginatorKeyFile);
var documentationFile = projectInstance.GetPropertyValue(PropertyNames.DocumentationFile);
var defineConstants = PropertyConverter.ToDefineConstants(projectInstance.GetPropertyValue(PropertyNames.DefineConstants));
var noWarn = PropertyConverter.ToSuppressDiagnostics(projectInstance.GetPropertyValue(PropertyNames.NoWarn));

var sourceFiles = projectInstance
.GetItems(ItemNames.Compile)
Expand All @@ -180,8 +186,8 @@ public static ProjectFileInfo Create(
return new ProjectFileInfo(
projectFilePath, assemblyName, name, targetFramework, specifiedLanguageVersion,
projectGuid, targetPath, allowUnsafe, outputKind, signAssembly, assemblyOriginatorKeyFile,
!string.IsNullOrWhiteSpace(documentationFile), defineConstants, sourceFiles, references,
projectReferences, analyzers);
!string.IsNullOrWhiteSpace(documentationFile), defineConstants, noWarn,
sourceFiles, references, projectReferences, analyzers);
}

private static bool ReferenceSourceTargetIsProjectReference(ProjectItemInstance projectItem)
Expand Down
138 changes: 0 additions & 138 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo_Mono.cs

This file was deleted.

Loading