dotnet / project-system

The .NET Project System for Visual Studio
MIT License
967 stars 386 forks source link

Use default imports for Microsoft.Common.props and Microsoft.CSharp.targets #628

Closed jongalloway closed 7 years ago

jongalloway commented 7 years ago

Every example csproj file includes two imports - one for Microsoft.common.props at the beginning and one for Microsoft.CSharp.targets at the end:

<Project>
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="**\*.cs" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NETCore.App" Version="1.0.0" />
    <PackageReference Include="Microsoft.NET.SDK" Version="1.0.0" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

Couldn't these two imports be implied, with an option to override them if needed?

I understand this may have been discussed internally, but would like to have it publicly documented and tracked since it will likely be a common question.

terrajobst commented 7 years ago

@dotnet/msbuild thoughts?

colhountech commented 7 years ago

+1

vlesierse commented 7 years ago

I really like this one. The less XML I have to write the better.

gulbanana commented 7 years ago

perhaps Microsoft.common.props could be considered a standard implementation-defined prelude..

ErikSchierboom commented 7 years ago

Would love to see these removed!

jchannon commented 7 years ago

Had exactly the same thoughts after reading this https://blogs.msdn.microsoft.com/dotnet/2016/10/19/net-core-tooling-in-visual-studio-15/

filipw commented 7 years ago

How about a csproj without <PropertyGroup>, <ItemGroup>, the 2 default imports and an implicit <Compile Include="**\*.cs" />

<Project>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.0</TargetFramework>

    <PackageReference Include="Microsoft.NETCore.App" Version="1.0.0" />
    <PackageReference Include="Microsoft.NET.SDK" Version="1.0.0" />
</Project>
baronfel commented 7 years ago

Do keep in mind how alternate project systems (VB/F#/etc) might want to define a common set of import targets that aren't the C# ones.

ErikSchierboom commented 7 years ago

@baronfel Sure, but VB and F# don't use .csproj files, so it should be possible to figure this out based on the extension of the project file.

terrajobst commented 7 years ago

@filipw

Let's keep this issue focused on imports. Feel free to file a separate issues for PropertGroup/ItemGroup removal.

MaximRouiller commented 7 years ago

@terrajobst @filipw

Created the issue: #632

terrajobst commented 7 years ago

@MaximRouiller Thanks!

rainersigwald commented 7 years ago

I don't see a way to make this work without breaking many existing MSBuild users.

While trying to find a way to do something like this, we saw several problems:

Since this would be a nice boilerplate-reduction, but in the end saves only a couple of lines per project, we opted to spend our development time elsewhere (allowing item metadata in properties saves 2 lines/NuGet reference, better supporting wildcards in the IDE saves a ton of lines, and so on).

MaximRouiller commented 7 years ago

@rainersigwald

This was moved to https://github.com/Microsoft/msbuild/issues/1236

At least, we now have a tracking of why it wouldn't work.

If anyone asks, we can say:

See Issue #???

srivatsn commented 7 years ago

@MaximRouiller, I believe @rainersigwald's response is about the original issue here of not having to import the targets\props.

MaximRouiller commented 7 years ago

@srivatsn Ah! My misunderstanding. Time to hit the coffee shop I guess. ☕️

clayton-taylor commented 7 years ago

Perhaps this could be implemented in a backwards-compatible way by adding an attribute to the Project element that allows one to opt-in to the desired behavior.

dsplaisted commented 7 years ago

Perhaps this could be implemented in a backwards-compatible way by adding an attribute to the Project element that allows one to opt-in to the desired behavior.

Another way to do this would be to only import the default props and targets files if there is no xmlns attribute on the Project element. That attribute will no longer be necessary so we could use it to light up new, but not backwards compatible, functionality.

If we do this based on the xmlns, I'd suggest allowing a UseDefaultImports="false" property on the Project element to disable the default imports in case you want to do something before the props or after the targets or you just don't want to use the default imports at all.

rainersigwald commented 7 years ago

This is possible for new .NET Core projects with https://github.com/Microsoft/msbuild/pull/1403, released with the updated VS2017 RC.