Skip to content

Conversation

StefanBertels
Copy link

Taken you have have a solution with two projects:

  1. class library, multiple target frameworks (e.g. netstandard2.1;net461)
  2. console application, one target framework (e.g. netcoreapp3.1)

Currently we cannot run "dotnet publish" on solution level because of the error in Microsoft.NET.Sdk.CrossTargeting.targets.
This is true, even if the class library is marked as <IsPublishable>false</IsPublishable>.

Currently you have call publish just for console project (2). This isn't simple in more generic scenarios or in larger solutions with multiple projects having different target opt-in/opt-out settings defined only by project properties (like IsPublishable, IsPackable, IsTestProject, RuntimeIdentifier etc.)

There is no need to throw a "cannot publish error" message for projects not to be published at all. This change should fix this.

See #1059 (comment)

Taken you have have a solution with two projects:

1. class library, multiple target frameworks (e.g. netstandard2.1;net461)
2. console application, one target framework (e.g. netcoreapp3.1)

Currently we cannot run "dotnet publish" on solution level because of the error in `Microsoft.NET.Sdk.CrossTargeting.targets`.
This is true, even if the class library is marked as `<IsPublishable>false</IsPublishable>`.

Currently you have call publish just for console project (2). This isn't simple in more generic scenarios or in larger solutions with multiple projects having different target opt-in/opt-out settings defined only by project properties (like `IsPublishable`, `IsPackable`, `IsTestProject`, `RuntimeIdentifier` etc.)

There is no need to throw a "cannot publish error" message for projects not to be published at all. This change should fix this.

See dotnet#1059 (comment)
StefanBertels added a commit to StefanBertels/sdk that referenced this pull request Feb 16, 2020
@StefanBertels
Copy link
Author

Here's some unit test showing the problem. I couldn't make the unit test stable / working on my machine out of the box, therefore this is just a comment -- maybe needs some polishing.

public class GivenThatWeWantToPublishASolution : SdkTest
    {
        public GivenThatWeWantToPublishASolution(ITestOutputHelper log) : base(log)
        {
        }

        [Fact]
        public void It_publishes_only_publishable_projects()
        {
            var libProject = new TestProject {Name = "Lib", TargetFrameworks = "netstandard2.1;net461", IsSdkProject = true};
            libProject.AdditionalProperties.Add("IsPublishable", "false");
            var libAsset = _testAssetsManager.CreateTestProject(libProject);

            var consoleProject = new TestProject {Name = "App", TargetFrameworks = "netcoreapp3.1", IsExe = true, IsSdkProject = true,};
            var consoleAsset = _testAssetsManager.CreateTestProject(consoleProject);

            var solutionAsset = _testAssetsManager.CreateTestDirectory();
            File.WriteAllText(Path.Combine(solutionAsset.Path, "MySolution.sln"), $@"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26006.2
MinimumVisualStudioVersion = 10.0.40219.1
Project(""{{9A19103F-16F7-4668-BE54-9A1E7A4F7556}}"") = ""{libProject.Name}"", ""{libAsset.Path}\{libProject.Name}\{libProject.Name}.csproj"", ""{{7072A694-548F-4CAE-A58F-12D257D5F486}}""
EndProject
Project(""{{9A19103F-16F7-4668-BE54-9A1E7A4F7556}}"") = ""{consoleProject.Name}"", ""{consoleAsset.Path}\{consoleProject.Name}\{consoleProject.Name}.csproj"", ""{{B38B1FA5-B4C9-456A-8B71-8FCD62ACF400}}""
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{{7072A694-548F-4CAE-A58F-12D257D5F486}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{7072A694-548F-4CAE-A58F-12D257D5F486}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{7072A694-548F-4CAE-A58F-12D257D5F486}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{7072A694-548F-4CAE-A58F-12D257D5F486}}.Release|Any CPU.Build.0 = Release|Any CPU
		{{B38B1FA5-B4C9-456A-8B71-8FCD62ACF400}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{B38B1FA5-B4C9-456A-8B71-8FCD62ACF400}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{B38B1FA5-B4C9-456A-8B71-8FCD62ACF400}}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{{B38B1FA5-B4C9-456A-8B71-8FCD62ACF400}}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal
");

            var result = new DotnetCommand(Log)
                .WithWorkingDirectory(solutionAsset.Path)
                .Execute("publish");

            result.Should().Pass();

            var libPublishDirectories = new DirectoryInfo(Path.Combine(libAsset.Path, libProject.Name))
                .GetDirectories("*", SearchOption.AllDirectories)
                .Where(_ => _.Name.Equals("publish", StringComparison.InvariantCultureIgnoreCase));
            
            libPublishDirectories.Should().BeEmpty();

            var consolePublishDirectory = new DirectoryInfo(Path.Combine(consoleAsset.Path, consoleProject.Name, "bin", "Debug", "netcoreapp3.1", "publish"));
            
            consolePublishDirectory.Should().Exist();
        }
    }

@marcpopMSFT
Copy link
Member

@StefanBertels Can you fix the merge conflicts and look at adding a test for this scenario and then we can accept the change? Thanks.

@StefanBertels
Copy link
Author

I hope someone can take on this, it's still an issue with net5.0.

Meanwhile a workaround is to add this to the csproj (for Microsoft.NET.Sdk projects with multiple target frameworks):

    <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
    <Target Name="Publish">
        <!-- hack https://stackoverflow.com/questions/47179705/net-core-override-default-build-targets -->
        <!-- can be removed after https://github.com/dotnet/sdk/pull/10659 is fixed or single TargetFramework is used -->
    </Target>

Note: this hack will result in warnings.

Base automatically changed from master to main March 18, 2021 21:05
@marcpopMSFT
Copy link
Member

Reviewing older PRs, I realized that we already got this fixed: #15402. Thanks for raising the visibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants