dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.67k stars 1.06k forks source link

dotnet pack "--no-build" option is not honored for F# project references #20001

Open lGSMl opened 3 years ago

lGSMl commented 3 years ago

While trying to run dotnet pack --no-build on F# project with project reference - dotnet tries to build referenced project and errors on "NoBuild option in build target" check.

Reproduction:

Env: Docker image mcr.microsoft.com/dotnet/sdk:5.0

Steps:

mkdir fsharptmp
cd fsharptmp
dotnet new sln
dotnet new classlib --language "F#" --name "fsharpproj"
dotnet new classlib --language "F#" --name "fsharprefproj"
dotnet add fsharpproj reference fsharprefproj
dotnet sln fsharptmp.sln add fsharpproj
dotnet sln fsharptmp.sln add fsharprefproj
dotnet build
dotnet pack --no-build fsharpproj

Expected: Successfully created package Actual:

/usr/share/dotnet/sdk/5.0.202/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(146,5): error NETSDK1085: The 'NoBuild' property was set to true but the 'Build' target was invoked. [/fsharptmp/fsharprefproj/fsharprefproj.fsproj]```

Sample output on steps above:

docker run -it --rm mcr.microsoft.com/dotnet/sdk:5.0
root@6a74518f734d:/# mkdir fsharptmp
root@6a74518f734d:/# cd fsharptmp/
root@6a74518f734d:/fsharptmp# dotnet new sln
Getting ready...
The template "Solution File" was created successfully.
root@6a74518f734d:/fsharptmp#  dotnet new classlib --language "F#" --name "fsharpproj"
The template "Class library" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on fsharpproj/fsharpproj.fsproj...
  Determining projects to restore...
  Restored /fsharptmp/fsharpproj/fsharpproj.fsproj (in 1.38 sec).
Restore succeeded.

root@6a74518f734d:/fsharptmp#  dotnet new classlib --language "F#" --name "fsharprefproj"
The template "Class library" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on fsharprefproj/fsharprefproj.fsproj...
  Determining projects to restore...
  Restored /fsharptmp/fsharprefproj/fsharprefproj.fsproj (in 141 ms).
Restore succeeded.

root@6a74518f734d:/fsharptmp# dotnet add fsharpproj reference fsharprefproj
Reference `..\fsharprefproj\fsharprefproj.fsproj` added to the project.
root@6a74518f734d:/fsharptmp# cat fsharpproj/fsharpproj.fsproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Library.fs" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\fsharprefproj\fsharprefproj.fsproj" />
  </ItemGroup>

</Project>

root@6a74518f734d:/fsharptmp# dotnet sln fsharptmp.sln add fsharpproj
Project `fsharpproj/fsharpproj.fsproj` added to the solution.

root@6a74518f734d:/fsharptmp# dotnet sln fsharptmp.sln add fsharprefproj/
Project `fsharprefproj/fsharprefproj.fsproj` added to the solution.

root@6a74518f734d:/fsharptmp# dotnet build
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /fsharptmp/fsharpproj/fsharpproj.fsproj (in 153 ms).
  1 of 2 projects are up-to-date for restore.
  fsharprefproj -> /fsharptmp/fsharprefproj/bin/Debug/net5.0/fsharprefproj.dll
  fsharpproj -> /fsharptmp/fsharpproj/bin/Debug/net5.0/fsharpproj.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:06.47

root@6a74518f734d:/fsharptmp# dotnet pack --no-build fsharpproj
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

/usr/share/dotnet/sdk/5.0.202/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(146,5): error NETSDK1085: The 'NoBuild' property was set to true but the 'Build' target was invoked. [/fsharptmp/fsharprefproj/fsharprefproj.fsproj]

root@6a74518f734d:/fsharptmp# dotnet pack --no-build fsharprefproj
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Successfully created package '/fsharptmp/fsharprefproj/bin/Debug/fsharprefproj.1.0.0.nupkg'.

root@6a74518f734d:/fsharptmp# dotnet --version
5.0.202

root@6a74518f734d:/fsharptmp# cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Reproducible on Windows 10 as well. Not reproducible with C# projects.

bromanko commented 1 year ago

I also encountered this issue attempting to package a project with Nix. Nix support for F# packages does not work because of it.

xperiandri commented 4 weeks ago

We also experience the same issue when publishing Android target @vzarytovskii is SDK team or F# team area?

Juriyx commented 4 weeks ago

Our command is: dotnet publish --no-build /p:Configuration=Release /p:TargetFramework=net8.0-android /p:PublishDir=$(Build.ArtifactStagingDirectory)\Android

vzarytovskii commented 4 weeks ago

We also experience the same issue when publishing Android target @vzarytovskii is SDK team or F# team area?

Not entirely sure. @baronfel is SDK doing something special here?

baronfel commented 4 weeks ago

We'd need a binlog for erroring cases to tell in any case so we can do a proper investigation.

We're not doing anything special, but no build is a bit of a corner case so there may be gaps in the implementation or there may be requirements imposed by the android or F# targets that make an implicit build happen for some reason.

Juriyx commented 3 weeks ago

@baronfel, here are binlogs for build and publish tasks: Android build and publish logs.zip

If needed, the build command is the following (the problem is with the publish one that I wrote above):

  - task: PowerShell@2
    displayName: "Build Android platform"
    inputs:
      targetType: 'inline'
      script: |
        $msbuild = vswhere -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe
        $collectionOfArgs = @("$(ConsoleApp)", "/p:TargetFramework=net8.0-android", "/p:Configuration=$(BuildConfiguration)", "/bl:$(Build.ArtifactStagingDirectory)\Android.build.binlog")
        & $msbuild $collectionOfArgs
    continueOnError: true
baronfel commented 3 weeks ago

The error in question is triggered from this code:

  <!-- TODO: this target should not check GeneratePackageOnBuild.
             remove when https://github.com/NuGet/Home/issues/7801 is fixed.
    -->
  <Target Name="_CheckForBuildWithNoBuild"
          Condition="'$(NoBuild)' == 'true' and '$(GeneratePackageOnBuild)' != 'true'">
    <NETSdkError ResourceName="NoBuildRequested" />
  </Target>

@nkolev92 it looks like https://github.com/NuGet/Home/issues/7801 has been fixed for quite a while, can we remove the GeneratePackageOnBuild check from this target now safely?

nkolev92 commented 3 weeks ago

Yeah, seems like it. I haven't manually verified any potential side effects but that should be something easily doable.