microsoft / MSBuildSdks

MSBuild project SDKs
MIT License
455 stars 81 forks source link

NoTargets does not work with multi targeting when project extension is .msbuildproj #155

Open bash opened 4 years ago

bash commented 4 years ago

Minimal, Reproducible Example

Source: https://github.com/bash/MSBuildNoTargetsIssueRepro Failing Travis build: https://travis-ci.com/github/bash/MSBuildNoTargetsIssueRepro/builds/152979950 Error Message:

/usr/share/dotnet/sdk/3.1.102/Microsoft.Common.CurrentVersion.targets(1175,5): error MSB3644: The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks [/home/travis/build/bash/MSBuildNoTargetsIssueRepro/MSBuildNoTargetsIssueRepro.msbuildproj]

Possible Workaround

I believe the issue is that Microsoft.Common.CrossTargeting.targets is never imported when the project extension doesn't correspond to a language. My workaround sets the propertyLanguageTargets to a custom file, that imports CrossTargeting.targets

ViktorHofer commented 2 years ago

Another easier workaround, use the .csproj extension instead. Not perfect but it works...

jzabroski commented 2 years ago

@bash I think you have a typo in the issue. ist -> is

@ViktorHofer I get a build error when using csproj now that I upgraded to VS2022 and uninstalled all old SDKs.

Severity Code Description Project File Line Suppression State Error MSB3644 The reference assemblies for .NETFramework,Version=v4.0 were not found. To resolve this, install the Developer Pack (SDK/Targeting Pack) for this framework version or retarget your application. You can download .NET Framework Developer Packs at https://aka.ms/msbuild/developerpacks PowerShellBuildProject C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 1220

My xml is

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.Build.NoTargets/3.3.0" InitialTargets="RunBeforeAnySsrsReport">
  <!-- ReSharper disable UnknownProperty -->
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <OutputPath>bin</OutputPath>
    <RestoreNoCache>True</RestoreNoCache>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="RunBeforeAnySsrsReport.ps1" />
  </ItemGroup>

  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />

  <Target Name="RunBeforeAnySsrsReport">
    <PropertyGroup>
      <PowerShellExe Condition=" '$(PowerShellExe)' == '' ">
        "C:\Program Files\PowerShell\7\pwsh.exe"
      </PowerShellExe>
      <ScriptLocation Condition=" '$(ScriptLocation)' == '' ">$(MSBuildProjectDirectory)\RunBeforeAnySsrsReport.ps1</ScriptLocation>
    </PropertyGroup>

    <Message Importance="High" Condition=" '$(PowerShellExe)' == '' " Text=" PowerShellExe not configured. ">
    </Message>
    <Message Importance="High" Condition=" '$(ScriptLocation)' == '' " Text=" ScriptLocation not configured. ">
    </Message>

    <PropertyGroup>
      <PwshCommand>pwsh.exe -NonInteractive -ExecutionPolicy Unrestricted -File &quot;$(ScriptLocation)&quot;</PwshCommand>
    </PropertyGroup>

    <Message Importance="High" Text=" $(PwshCommand) ">
    </Message>

    <Exec Command="$(PwshCommand)" ConsoleToMSBuild="true"  />
  </Target>
</Project>
bash commented 2 years ago

I think you have a typo in the issue. ist -> is

@jzabroski Thanks 🙂

ViktorHofer commented 2 years ago

@jzabroski do you use the latest version of this msbuildsdk? I think that error was fixed recently.

kkirkfield commented 1 year ago

Apparently setting the LanguageTargets using your workaround isn't enough. That will get it to build from the command line, but you won't be able to open the project in Visual Studio and will get the error "The project file cannot be opened by the project system because it is missing some critical imports or the referenced SDK cannot be found." If you don't have a solution file, or you added your project to a solution before renaming it to .msbuildproj then you won't get the error because it is using the ProjectTypeGuids of the .csproj file in the solution.

To get this working with the .msbuildproj extension, add this code in addition to your LanguageTargets workaround. This GUID is the same used in the .csproj project type.

<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Then you can add the project to a solution file and it will open and build correctly.