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

Correctly set compilation platform of the project #2135

Merged
merged 5 commits into from
Apr 16, 2021
Merged
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
13 changes: 9 additions & 4 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.ProjectData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private class ProjectData

public string Configuration { get; }
public string Platform { get; }
public string PlatformTarget { get; }
public FrameworkName TargetFramework { get; }
public ImmutableArray<string> TargetFrameworks { get; }

Expand Down Expand Up @@ -88,7 +89,7 @@ private ProjectData(
Guid guid, string name,
string assemblyName, string targetPath, string outputPath, string intermediateOutputPath,
string projectAssetsFile,
string configuration, string platform,
string configuration, string platform, string platformTarget,
FrameworkName targetFramework,
ImmutableArray<string> targetFrameworks,
OutputKind outputKind,
Expand Down Expand Up @@ -121,6 +122,7 @@ private ProjectData(

Configuration = configuration;
Platform = platform;
PlatformTarget = platformTarget;
TargetFramework = targetFramework;
TargetFrameworks = targetFrameworks.EmptyIfDefault();

Expand Down Expand Up @@ -149,7 +151,7 @@ private ProjectData(
Guid guid, string name,
string assemblyName, string targetPath, string outputPath, string intermediateOutputPath,
string projectAssetsFile,
string configuration, string platform,
string configuration, string platform, string platformTarget,
FrameworkName targetFramework,
ImmutableArray<string> targetFrameworks,
OutputKind outputKind,
Expand Down Expand Up @@ -180,7 +182,7 @@ private ProjectData(
ImmutableDictionary<string, string> projectReferenceAliases,
ImmutableArray<IMSBuildGlob> fileInclusionGlobs)
: this(guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
configuration, platform, platformTarget, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset)
{
SourceFiles = sourceFiles.EmptyIfDefault();
Expand All @@ -206,6 +208,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)
var projectAssetsFile = project.GetPropertyValue(PropertyNames.ProjectAssetsFile);
var configuration = project.GetPropertyValue(PropertyNames.Configuration);
var platform = project.GetPropertyValue(PropertyNames.Platform);
var platformTarget = project.GetPropertyValue(PropertyNames.PlatformTarget);
var defaultNamespace = project.GetPropertyValue(PropertyNames.RootNamespace);

var targetFramework = new FrameworkName(project.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));
Expand Down Expand Up @@ -236,7 +239,7 @@ public static ProjectData Create(MSB.Evaluation.Project project)

return new ProjectData(
guid, name, assemblyName, targetPath, outputPath, intermediateOutputPath, projectAssetsFile,
configuration, platform, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
configuration, platform, platformTarget, targetFramework, targetFrameworks, outputKind, languageVersion, nullableContextOptions, allowUnsafeCode, checkForOverflowUnderflow,
documentationFile, preprocessorSymbolNames, suppressedDiagnosticIds, warningsAsErrors, warningsNotAsErrors, signAssembly, assemblyOriginatorKeyFile, treatWarningsAsErrors, defaultNamespace, runAnalyzers, runAnalyzersDuringLiveAnalysis, ruleset: null);
}

Expand All @@ -253,6 +256,7 @@ public static ProjectData Create(string projectFilePath, MSB.Execution.ProjectIn
var projectAssetsFile = projectInstance.GetPropertyValue(PropertyNames.ProjectAssetsFile);
var configuration = projectInstance.GetPropertyValue(PropertyNames.Configuration);
var platform = projectInstance.GetPropertyValue(PropertyNames.Platform);
var platformTarget = projectInstance.GetPropertyValue(PropertyNames.PlatformTarget);
var defaultNamespace = projectInstance.GetPropertyValue(PropertyNames.RootNamespace);

var targetFramework = new FrameworkName(projectInstance.GetPropertyValue(PropertyNames.TargetFrameworkMoniker));
Expand Down Expand Up @@ -346,6 +350,7 @@ public static ProjectData Create(string projectFilePath, MSB.Execution.ProjectIn
projectAssetsFile,
configuration,
platform,
platformTarget,
targetFramework,
targetFrameworks,
outputKind,
Expand Down
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal partial class ProjectFileInfo

public string Configuration => _data.Configuration;
public string Platform => _data.Platform;
public string PlatformTarget => _data.PlatformTarget;
public FrameworkName TargetFramework => _data.TargetFramework;
public ImmutableArray<string> TargetFrameworks => _data.TargetFrameworks;

Expand Down
18 changes: 18 additions & 0 deletions src/OmniSharp.MSBuild/ProjectFile/ProjectFileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public static CSharpCompilationOptions CreateCompilationOptions(this ProjectFile
.WithSpecificDiagnosticOptions(projectFileInfo.GetDiagnosticOptions())
.WithOverflowChecks(projectFileInfo.CheckForOverflowUnderflow);

var platformTarget = ParsePlatform(projectFileInfo.PlatformTarget);
if (platformTarget != compilationOptions.Platform)
{
compilationOptions = compilationOptions.WithPlatform(platformTarget);
}

if (projectFileInfo.AllowUnsafeCode != compilationOptions.AllowUnsafe)
{
compilationOptions = compilationOptions.WithAllowUnsafe(projectFileInfo.AllowUnsafeCode);
Expand Down Expand Up @@ -111,5 +117,17 @@ public static ImmutableArray<AnalyzerFileReference> ResolveAnalyzerReferencesFor

return projectFileInfo.Analyzers.Select(analyzerCandicatePath => new AnalyzerFileReference(analyzerCandicatePath, analyzerAssemblyLoader)).ToImmutableArray();
}

private static Platform ParsePlatform(string value) => (value?.ToLowerInvariant()) switch
{
"x86" => Platform.X86,
"x64" => Platform.X64,
"itanium" => Platform.Itanium,
"anycpu" => Platform.AnyCpu,
"anycpu32bitpreferred" => Platform.AnyCpu32BitPreferred,
"arm" => Platform.Arm,
"arm64" => Platform.Arm64,
_ => Platform.AnyCpu,
};
}
}
1 change: 1 addition & 0 deletions src/OmniSharp.MSBuild/ProjectFile/PropertyNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal static class PropertyNames
public const string Nullable = nameof(Nullable);
public const string OutputPath = nameof(OutputPath);
public const string Platform = nameof(Platform);
public const string PlatformTarget = nameof(PlatformTarget);
public const string ProjectAssetsFile = nameof(ProjectAssetsFile);
public const string ProvideCommandLineArgs = nameof(ProvideCommandLineArgs);
public const string ProjectGuid = nameof(ProjectGuid);
Expand Down
1 change: 1 addition & 0 deletions test-assets/test-projects/HelloWorld/HelloWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<LangVersion>7.1</LangVersion>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>

</Project>
39 changes: 18 additions & 21 deletions tests/OmniSharp.MSBuild.Tests/ProjectFileInfoTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ namespace OmniSharp.MSBuild.Tests
public class ProjectFileInfoTests : AbstractTestFixture
{
private readonly TestAssets _testAssets;
private readonly SharedOmniSharpHostFixture _sharedOmniSharpHostFixture;

public ProjectFileInfoTests(ITestOutputHelper output)
: base(output)
public ProjectFileInfoTests(ITestOutputHelper output, SharedOmniSharpHostFixture sharedOmniSharpHostFixture)
: base(output, sharedOmniSharpHostFixture)
{
this._testAssets = TestAssets.Instance;
_testAssets = TestAssets.Instance;
_sharedOmniSharpHostFixture = sharedOmniSharpHostFixture;
}

private ProjectFileInfo CreateProjectFileInfo(OmniSharpTestHost host, ITestProject testProject, string projectFilePath)
Expand All @@ -45,12 +47,11 @@ private ProjectFileInfo CreateProjectFileInfo(OmniSharpTestHost host, ITestProje
[Fact]
public async Task HelloWorld_has_correct_property_values()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("HelloWorld"))
{
var projectFilePath = Path.Combine(testProject.Directory, "HelloWorld.csproj");

var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);

Assert.NotNull(projectFileInfo);
Assert.Equal(projectFilePath, projectFileInfo.FilePath);
Expand All @@ -63,22 +64,23 @@ public async Task HelloWorld_has_correct_property_values()
Assert.True(projectFileInfo.TreatWarningsAsErrors);
Assert.Equal("Debug", projectFileInfo.Configuration);
Assert.Equal("AnyCPU", projectFileInfo.Platform);
Assert.Equal("x64", projectFileInfo.PlatformTarget);

var compilationOptions = projectFileInfo.CreateCompilationOptions();
Assert.Equal(ReportDiagnostic.Error, compilationOptions.GeneralDiagnosticOption);
Assert.Equal(Platform.X64, compilationOptions.Platform);
Assert.True(compilationOptions.CheckOverflow);
}
}

[Fact]
public async Task HelloWorldSlim_has_correct_property_values()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("HelloWorldSlim"))
{
var projectFilePath = Path.Combine(testProject.Directory, "HelloWorldSlim.csproj");

var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);

Assert.NotNull(projectFileInfo);
Assert.Equal(projectFilePath, projectFileInfo.FilePath);
Expand All @@ -89,22 +91,23 @@ public async Task HelloWorldSlim_has_correct_property_values()
Assert.Equal(3, projectFileInfo.SourceFiles.Length); // Program.cs, AssemblyInfo.cs, AssemblyAttributes.cs
Assert.Equal("Debug", projectFileInfo.Configuration);
Assert.Equal("AnyCPU", projectFileInfo.Platform);
Assert.Equal("", projectFileInfo.PlatformTarget);

var compilationOptions = projectFileInfo.CreateCompilationOptions();
Assert.Equal(ReportDiagnostic.Default, compilationOptions.GeneralDiagnosticOption);
Assert.Equal(Platform.AnyCpu, compilationOptions.Platform);
Assert.False(compilationOptions.CheckOverflow);
}
}

[Fact]
public async Task NetStandardAndNetCoreApp_has_correct_property_values()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("NetStandardAndNetCoreApp"))
{
var projectFilePath = Path.Combine(testProject.Directory, "NetStandardAndNetCoreApp.csproj");

var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);

Assert.NotNull(projectFileInfo);
Assert.Equal(projectFilePath, projectFileInfo.FilePath);
Expand All @@ -122,12 +125,11 @@ public async Task NetStandardAndNetCoreApp_has_correct_property_values()
[Fact]
public async Task CSharp8AndNullableContext_has_correct_property_values()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("CSharp8AndNullableContext"))
{
var projectFilePath = Path.Combine(testProject.Directory, "CSharp8AndNullableContext.csproj");

var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);

Assert.NotNull(projectFileInfo);
Assert.Equal(projectFilePath, projectFileInfo.FilePath);
Expand All @@ -146,11 +148,10 @@ public async Task CSharp8AndNullableContext_has_correct_property_values()
[Fact]
public async Task ExternAliasWithReference()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("ExternAlias"))
{
var projectFilePath = Path.Combine(testProject.Directory, "ExternAlias.App", "ExternAlias.App.csproj");
var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);
Assert.Single(projectFileInfo.ReferenceAliases);
foreach (var kv in projectFileInfo.ReferenceAliases)
{
Expand All @@ -166,11 +167,10 @@ public async Task ExternAliasWithReference()
[Fact]
public async Task ExternAliasWithProjectReference()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("ExternAlias"))
{
var projectFilePath = Path.Combine(testProject.Directory, "ExternAlias.App2", "ExternAlias.App2.csproj");
var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);
Assert.Single(projectFileInfo.ProjectReferenceAliases);
foreach (var kv in projectFileInfo.ProjectReferenceAliases)
{
Expand All @@ -186,11 +186,10 @@ public async Task ExternAliasWithProjectReference()
[Fact]
public async Task AllowUnsafe()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("AllowUnsafe"))
{
var projectFilePath = Path.Combine(testProject.Directory, "AllowUnsafe.csproj");
var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);
Assert.True(projectFileInfo.AllowUnsafeCode);

var compilationOptions = projectFileInfo.CreateCompilationOptions();
Expand All @@ -202,11 +201,10 @@ public async Task AllowUnsafe()
[Fact]
public async Task WarningsAsErrors()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("WarningsAsErrors"))
{
var projectFilePath = Path.Combine(testProject.Directory, "WarningsAsErrors.csproj");
var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);
Assert.NotEmpty(projectFileInfo.WarningsAsErrors);
Assert.Contains("CS1998", projectFileInfo.WarningsAsErrors);
Assert.Contains("CS7080", projectFileInfo.WarningsAsErrors);
Expand All @@ -233,11 +231,10 @@ public async Task WarningsAsErrors()
[Fact]
public async Task ProjectReferenceProducingAnalyzerItems()
{
using (var host = CreateOmniSharpHost())
using (var testProject = await _testAssets.GetTestProjectAsync("ProjectWithAnalyzersFromReference"))
{
var projectFilePath = Path.Combine(testProject.Directory, "ConsumingProject", "ConsumingProject.csproj");
var projectFileInfo = CreateProjectFileInfo(host, testProject, projectFilePath);
var projectFileInfo = CreateProjectFileInfo(_sharedOmniSharpHostFixture.OmniSharpTestHost, testProject, projectFilePath);
Assert.Empty(projectFileInfo.ProjectReferences);
var analyzerFileReference = Assert.Single(projectFileInfo.Analyzers);
Assert.EndsWith("Analyzer.dll", analyzerFileReference);
Expand Down