MSBuildSdks icon indicating copy to clipboard operation
MSBuildSdks copied to clipboard

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

Open tautropfli opened this issue 5 years ago • 5 comments

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

tautropfli avatar Mar 12 '20 13:03 tautropfli

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

ViktorHofer avatar Feb 17 '22 20:02 ViktorHofer

@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>

jzabroski avatar Mar 30 '22 19:03 jzabroski

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

@jzabroski Thanks 🙂

tautropfli avatar Mar 31 '22 07:03 tautropfli

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

ViktorHofer avatar Apr 03 '22 14:04 ViktorHofer

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.

kkirkfield avatar Dec 07 '22 23:12 kkirkfield