GivenThatWeWantToBuildANetCoreApp.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. using System.Runtime.CompilerServices;
  4. using Microsoft.Extensions.DependencyModel;
  5. using Microsoft.NET.Build.Tasks;
  6. using Newtonsoft.Json.Linq;
  7. using NuGet.Common;
  8. using NuGet.Frameworks;
  9. using NuGet.ProjectModel;
  10. using NuGet.Versioning;
  11. namespace Microsoft.NET.Build.Tests
  12. {
  13. public class GivenThatWeWantToBuildANetCoreApp : SdkTest
  14. {
  15. public GivenThatWeWantToBuildANetCoreApp(ITestOutputHelper log) : base(log)
  16. {
  17. }
  18. private BuildCommand GetBuildCommand([CallerMemberName] string callingMethod = "")
  19. {
  20. var testAsset = _testAssetsManager
  21. .CopyTestAsset("HelloWorldWithSubDirs", callingMethod)
  22. .WithSource();
  23. return new BuildCommand(testAsset);
  24. }
  25. [Theory]
  26. // TargetFramework, RuntimeFrameworkVersion, ExpectedPackageVersion, ExpectedRuntimeFrameworkVersion
  27. [InlineData("netcoreapp1.0", null, "1.0.5", "1.0.5")]
  28. [InlineData("netcoreapp1.0", "1.0.0", "1.0.0", "1.0.0")]
  29. [InlineData("netcoreapp1.0", "1.0.3", "1.0.3", "1.0.3")]
  30. [InlineData("netcoreapp1.1", null, "1.1.2", "1.1.2")]
  31. [InlineData("netcoreapp1.1", "1.1.0", "1.1.0", "1.1.0")]
  32. [InlineData("netcoreapp1.1.1", null, "1.1.1", "1.1.1")]
  33. [InlineData("netcoreapp2.0", null, "2.0.0", "2.0.0")]
  34. [InlineData("netcoreapp2.1", null, "2.1.0", "2.1.0")]
  35. public void It_targets_the_right_shared_framework(string targetFramework, string runtimeFrameworkVersion,
  36. string expectedPackageVersion, string expectedRuntimeVersion)
  37. {
  38. string testIdentifier = "SharedFrameworkTargeting_" + string.Join("_", targetFramework, runtimeFrameworkVersion ?? "null");
  39. It_targets_the_right_framework(testIdentifier, targetFramework, runtimeFrameworkVersion,
  40. selfContained: false, isExe: true,
  41. expectedPackageVersion: expectedPackageVersion, expectedRuntimeVersion: expectedRuntimeVersion);
  42. }
  43. // Test behavior when implicit version differs for framework-dependent and self-contained apps
  44. [Theory]
  45. [InlineData("netcoreapp1.0", false, true, "1.0.5")]
  46. [InlineData("netcoreapp1.0", true, true, "1.0.16")]
  47. [InlineData("netcoreapp1.0", false, false, "1.0.5")]
  48. [InlineData("netcoreapp1.1", false, true, "1.1.2")]
  49. [InlineData("netcoreapp1.1", true, true, "1.1.13")]
  50. [InlineData("netcoreapp1.1", false, false, "1.1.2")]
  51. [InlineData("netcoreapp2.0", false, true, "2.0.0")]
  52. [InlineData("netcoreapp2.0", true, true, TestContext.LatestRuntimePatchForNetCoreApp2_0)]
  53. [InlineData("netcoreapp2.0", false, false, "2.0.0")]
  54. public void It_targets_the_right_framework_depending_on_output_type(string targetFramework, bool selfContained, bool isExe, string expectedFrameworkVersion)
  55. {
  56. if (!EnvironmentInfo.SupportsTargetFramework(targetFramework))
  57. {
  58. return;
  59. }
  60. string testIdentifier = "Framework_targeting_" + targetFramework + "_" + (isExe ? "App_" : "Lib_") + (selfContained ? "SelfContained" : "FrameworkDependent");
  61. It_targets_the_right_framework(testIdentifier, targetFramework, null, selfContained, isExe, expectedFrameworkVersion, expectedFrameworkVersion);
  62. }
  63. [Fact]
  64. public void The_RuntimeFrameworkVersion_can_float()
  65. {
  66. var testProject = new TestProject()
  67. {
  68. Name = "RuntimeFrameworkVersionFloat",
  69. TargetFrameworks = "netcoreapp2.0",
  70. RuntimeFrameworkVersion = "2.0.*",
  71. IsExe = true
  72. };
  73. var testAsset = _testAssetsManager.CreateTestProject(testProject);
  74. var buildCommand = new BuildCommand(testAsset);
  75. buildCommand
  76. .Execute()
  77. .Should()
  78. .Pass();
  79. LockFile lockFile = LockFileUtilities.GetLockFile(Path.Combine(buildCommand.ProjectRootPath, "obj", "project.assets.json"), NullLogger.Instance);
  80. var target = lockFile.GetTarget(NuGetFramework.Parse(testProject.TargetFrameworks), null);
  81. var netCoreAppLibrary = target.Libraries.Single(l => l.Name == "Microsoft.NETCore.App");
  82. // Test that the resolved version is greater than or equal to the latest runtime patch
  83. // we know about, so that when a new runtime patch is released the test doesn't
  84. // immediately start failing
  85. var minimumExpectedVersion = new NuGetVersion(TestContext.LatestRuntimePatchForNetCoreApp2_0);
  86. netCoreAppLibrary.Version.CompareTo(minimumExpectedVersion).Should().BeGreaterOrEqualTo(0,
  87. "the version resolved from a RuntimeFrameworkVersion of '{0}' should be at least {1}",
  88. testProject.RuntimeFrameworkVersion, TestContext.LatestRuntimePatchForNetCoreApp2_0);
  89. }
  90. private void It_targets_the_right_framework(
  91. string testIdentifier,
  92. string targetFramework,
  93. string runtimeFrameworkVersion,
  94. bool selfContained,
  95. bool isExe,
  96. string expectedPackageVersion,
  97. string expectedRuntimeVersion,
  98. string extraMSBuildArguments = null)
  99. {
  100. string runtimeIdentifier = null;
  101. if (selfContained)
  102. {
  103. runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
  104. }
  105. var testProject = new TestProject()
  106. {
  107. Name = "FrameworkTargetTest",
  108. TargetFrameworks = targetFramework,
  109. RuntimeFrameworkVersion = runtimeFrameworkVersion,
  110. IsExe = isExe,
  111. RuntimeIdentifier = runtimeIdentifier
  112. };
  113. var extraArgs = extraMSBuildArguments?.Split(' ') ?? Array.Empty<string>();
  114. var testAsset = _testAssetsManager.CreateTestProject(testProject, testIdentifier);
  115. NuGetConfigWriter.Write(testAsset.TestRoot, NuGetConfigWriter.DotnetCoreBlobFeed);
  116. var buildCommand = new BuildCommand(testAsset);
  117. buildCommand
  118. .Execute(extraArgs)
  119. .Should()
  120. .Pass();
  121. var outputDirectory = buildCommand.GetOutputDirectory(targetFramework, runtimeIdentifier: runtimeIdentifier);
  122. if (isExe)
  123. {
  124. // Self-contained apps don't write a framework version to the runtimeconfig, so only check this for framework-dependent apps
  125. if (!selfContained)
  126. {
  127. string runtimeConfigFile = Path.Combine(outputDirectory.FullName, testProject.Name + ".runtimeconfig.json");
  128. string runtimeConfigContents = File.ReadAllText(runtimeConfigFile);
  129. JObject runtimeConfig = JObject.Parse(runtimeConfigContents);
  130. string actualRuntimeFrameworkVersion = ((JValue)runtimeConfig["runtimeOptions"]["framework"]["version"]).Value<string>();
  131. actualRuntimeFrameworkVersion.Should().Be(expectedRuntimeVersion);
  132. }
  133. var runtimeconfigDevFileName = testProject.Name + ".runtimeconfig.dev.json";
  134. outputDirectory.Should()
  135. .HaveFile(runtimeconfigDevFileName);
  136. string devruntimeConfigContents = File.ReadAllText(Path.Combine(outputDirectory.FullName, runtimeconfigDevFileName));
  137. JObject devruntimeConfig = JObject.Parse(devruntimeConfigContents);
  138. var additionalProbingPaths = ((JArray)devruntimeConfig["runtimeOptions"]["additionalProbingPaths"]).Values<string>();
  139. // can't use Path.Combine on segments with an illegal `|` character
  140. var expectedPath = $"{Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "store")}{Path.DirectorySeparatorChar}|arch|{Path.DirectorySeparatorChar}|tfm|";
  141. additionalProbingPaths.Should().Contain(expectedPath);
  142. }
  143. LockFile lockFile = LockFileUtilities.GetLockFile(Path.Combine(buildCommand.ProjectRootPath, "obj", "project.assets.json"), NullLogger.Instance);
  144. var target = lockFile.GetTarget(NuGetFramework.Parse(targetFramework), null);
  145. var netCoreAppLibrary = target.Libraries.Single(l => l.Name == "Microsoft.NETCore.App");
  146. netCoreAppLibrary.Version.ToString().Should().Be(expectedPackageVersion);
  147. }
  148. [Theory]
  149. [InlineData(false)]
  150. [InlineData(true)]
  151. public void It_handles_mismatched_implicit_package_versions(bool allowMismatch)
  152. {
  153. var testProject = new TestProject()
  154. {
  155. Name = "MismatchFrameworkTest",
  156. TargetFrameworks = "netcoreapp2.0",
  157. IsExe = true,
  158. };
  159. if (!EnvironmentInfo.SupportsTargetFramework(testProject.TargetFrameworks))
  160. {
  161. return;
  162. }
  163. if (allowMismatch)
  164. {
  165. testProject.AdditionalProperties["VerifyMatchingImplicitPackageVersion"] = "false";
  166. }
  167. string runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(testProject.TargetFrameworks);
  168. testProject.AdditionalProperties["RuntimeIdentifiers"] = runtimeIdentifier;
  169. var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: allowMismatch.ToString())
  170. .Restore(Log, testProject.Name);
  171. var buildCommand = new BuildCommand(testAsset);
  172. var result = buildCommand.ExecuteWithoutRestore($"/p:RuntimeIdentifier={runtimeIdentifier}");
  173. if (allowMismatch)
  174. {
  175. result.Should().Pass();
  176. }
  177. else
  178. {
  179. result.Should().Fail();
  180. // Get everything after the {2} in the failure message so this test doesn't need to
  181. // depend on the exact version the app would be rolled forward to
  182. string expectedFailureMessage = Strings.MismatchedPlatformPackageVersion
  183. .Substring(Strings.MismatchedPlatformPackageVersion.IndexOf("{2}") + 3);
  184. result.Should().HaveStdOutContaining(expectedFailureMessage);
  185. }
  186. }
  187. [Fact]
  188. public void It_restores_only_ridless_tfm()
  189. {
  190. var testAsset = _testAssetsManager
  191. .CopyTestAsset("HelloWorld")
  192. .WithSource();
  193. var getValuesCommand = new GetValuesCommand(Log, testAsset.TestRoot,
  194. ToolsetInfo.CurrentTargetFramework, "TargetDefinitions", GetValuesCommand.ValueType.Item)
  195. {
  196. DependsOnTargets = "RunResolvePackageDependencies",
  197. Properties = { { "EmitLegacyAssetsFileItems", "true" } }
  198. };
  199. getValuesCommand
  200. .Execute()
  201. .Should()
  202. .Pass();
  203. // When RuntimeIdentifier is not specified, the assets file
  204. // should only contain one target with no RIDs
  205. var targetDefs = getValuesCommand.GetValues();
  206. targetDefs.Count.Should().Be(1);
  207. targetDefs.Should().Contain(ToolsetInfo.CurrentTargetFramework);
  208. }
  209. [Theory]
  210. [InlineData("net6.0")]
  211. [InlineData("net7.0")]
  212. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  213. public void It_runs_the_app_from_the_output_folder(string targetFramework)
  214. {
  215. RunAppFromOutputFolder("RunFromOutputFolder_" + targetFramework, false, false, targetFramework);
  216. }
  217. [Theory]
  218. [InlineData("net6.0")]
  219. [InlineData("net7.0")]
  220. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  221. public void It_runs_a_rid_specific_app_from_the_output_folder(string targetFramework)
  222. {
  223. RunAppFromOutputFolder("RunFromOutputFolderWithRID_" + targetFramework, true, false, targetFramework);
  224. }
  225. [Theory]
  226. [InlineData("net6.0")]
  227. [InlineData("net7.0")]
  228. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  229. public void It_runs_the_app_with_conflicts_from_the_output_folder(string targetFramework)
  230. {
  231. RunAppFromOutputFolder("RunFromOutputFolderConflicts_" + targetFramework, false, true, targetFramework);
  232. }
  233. [Theory]
  234. [InlineData("net6.0")]
  235. [InlineData("net7.0")]
  236. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  237. public void It_runs_a_rid_specific_app_with_conflicts_from_the_output_folder(string targetFramework)
  238. {
  239. RunAppFromOutputFolder("RunFromOutputFolderWithRIDConflicts_" + targetFramework, true, true, targetFramework);
  240. }
  241. private void RunAppFromOutputFolder(string testName, bool useRid, bool includeConflicts,
  242. string targetFramework = ToolsetInfo.CurrentTargetFramework)
  243. {
  244. var runtimeIdentifier = useRid ? EnvironmentInfo.GetCompatibleRid(targetFramework) : null;
  245. TestProject project = new()
  246. {
  247. Name = testName,
  248. TargetFrameworks = targetFramework,
  249. RuntimeIdentifier = runtimeIdentifier,
  250. IsExe = true,
  251. };
  252. string outputMessage = $"Hello from {project.Name}!";
  253. project.SourceFiles["Program.cs"] = @"
  254. using System;
  255. public static class Program
  256. {
  257. public static void Main()
  258. {
  259. TestConflictResolution();
  260. Console.WriteLine(""" + outputMessage + @""");
  261. }
  262. " + ConflictResolutionAssets.ConflictResolutionTestMethod + @"
  263. }
  264. ";
  265. var testAsset = _testAssetsManager.CreateTestProject(project, project.Name)
  266. .WithProjectChanges(p =>
  267. {
  268. if (includeConflicts)
  269. {
  270. var ns = p.Root.Name.Namespace;
  271. var itemGroup = new XElement(ns + "ItemGroup");
  272. p.Root.Add(itemGroup);
  273. foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies)
  274. {
  275. itemGroup.Add(new XElement(ns + "PackageReference",
  276. new XAttribute("Include", dependency.Item1),
  277. new XAttribute("Version", dependency.Item2)));
  278. }
  279. }
  280. });
  281. var buildCommand = new BuildCommand(testAsset);
  282. buildCommand
  283. .Execute()
  284. .Should()
  285. .Pass();
  286. string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks, runtimeIdentifier: runtimeIdentifier ?? "").FullName;
  287. new DotnetCommand(Log, Path.Combine(outputFolder, project.Name + ".dll"))
  288. .Execute()
  289. .Should()
  290. .Pass()
  291. .And
  292. .HaveStdOutContaining(outputMessage);
  293. }
  294. [Theory]
  295. [InlineData("netcoreapp2.0", true)]
  296. [InlineData("netcoreapp3.0", true)]
  297. [InlineData("net5.0", true)]
  298. [InlineData(ToolsetInfo.CurrentTargetFramework, false)]
  299. public void It_stops_generating_runtimeconfig_dev_json_after_net6(string targetFramework, bool shouldGenerateRuntimeConfigDevJson)
  300. {
  301. TestProject proj = new()
  302. {
  303. Name = "NetCoreApp",
  304. ProjectSdk = "Microsoft.NET.Sdk",
  305. IsExe = true,
  306. TargetFrameworks = targetFramework,
  307. IsSdkProject = true
  308. };
  309. var buildCommand = new BuildCommand(_testAssetsManager.CreateTestProject(proj, identifier: targetFramework));
  310. var runtimeconfigFile = Path.Combine(
  311. buildCommand.GetOutputDirectory(targetFramework).FullName,
  312. $"{proj.Name}.runtimeconfig.dev.json");
  313. buildCommand.Execute().StdOut
  314. .Should()
  315. .NotContain("NETSDK1048");
  316. File.Exists(runtimeconfigFile).Should().Be(shouldGenerateRuntimeConfigDevJson);
  317. }
  318. [RequiresMSBuildVersionTheory("17.0.0.32901")]
  319. [InlineData("netcoreapp2.0")]
  320. [InlineData("netcoreapp3.0")]
  321. [InlineData("net5.0")]
  322. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  323. public void It_stops_generating_runtimeconfig_dev_json_after_net6_allow_property_override(string targetFramework)
  324. {
  325. TestProject proj = new()
  326. {
  327. Name = "NetCoreApp",
  328. ProjectSdk = "Microsoft.NET.Sdk",
  329. IsExe = true,
  330. TargetFrameworks = targetFramework,
  331. IsSdkProject = true
  332. };
  333. var buildCommand = new BuildCommand(_testAssetsManager.CreateTestProject(proj, identifier: targetFramework));
  334. var runtimeconfigFile = Path.Combine(
  335. buildCommand.GetOutputDirectory(targetFramework).FullName,
  336. $"{proj.Name}.runtimeconfig.dev.json");
  337. // GenerateRuntimeConfigDevFile overrides default behavior
  338. buildCommand.Execute("/p:GenerateRuntimeConfigDevFile=true").StdOut
  339. .Should()
  340. .NotContain("NETSDK1048"); ;
  341. File.Exists(runtimeconfigFile).Should().BeTrue();
  342. buildCommand.Execute("/p:GenerateRuntimeConfigDevFile=false").StdOut
  343. .Should()
  344. .NotContain("NETSDK1048"); ;
  345. File.Exists(runtimeconfigFile).Should().BeFalse();
  346. }
  347. [Theory]
  348. [InlineData("netcoreapp2.0")]
  349. [InlineData(ToolsetInfo.CurrentTargetFramework)]
  350. public void It_trims_conflicts_from_the_deps_file(string targetFramework)
  351. {
  352. TestProject project = new()
  353. {
  354. Name = "NetCore2App",
  355. TargetFrameworks = targetFramework,
  356. IsExe = true,
  357. };
  358. project.SourceFiles["Program.cs"] = @"
  359. using System;
  360. public static class Program
  361. {
  362. public static void Main()
  363. {
  364. TestConflictResolution();
  365. Console.WriteLine(""Hello, World!"");
  366. }
  367. " + ConflictResolutionAssets.ConflictResolutionTestMethod + @"
  368. }
  369. ";
  370. var testAsset = _testAssetsManager.CreateTestProject(project, identifier: targetFramework)
  371. .WithProjectChanges(p =>
  372. {
  373. var ns = p.Root.Name.Namespace;
  374. var itemGroup = new XElement(ns + "ItemGroup");
  375. p.Root.Add(itemGroup);
  376. foreach (var dependency in ConflictResolutionAssets.ConflictResolutionDependencies)
  377. {
  378. itemGroup.Add(new XElement(ns + "PackageReference",
  379. new XAttribute("Include", dependency.Item1),
  380. new XAttribute("Version", dependency.Item2)));
  381. }
  382. });
  383. var buildCommand = new BuildCommand(testAsset);
  384. buildCommand
  385. .Execute()
  386. .Should()
  387. .Pass();
  388. string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks).FullName;
  389. using (var depsJsonFileStream = File.OpenRead(Path.Combine(outputFolder, $"{project.Name}.deps.json")))
  390. {
  391. var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream);
  392. dependencyContext.Should()
  393. .OnlyHaveRuntimeAssemblies("", project.Name)
  394. .And
  395. .HaveNoDuplicateRuntimeAssemblies("")
  396. .And
  397. .HaveNoDuplicateNativeAssets(""); ;
  398. }
  399. }
  400. [Theory]
  401. [InlineData(true)]
  402. [InlineData(false)]
  403. public void It_generates_rid_fallback_graph(bool isSelfContained)
  404. {
  405. var targetFramework = ToolsetInfo.CurrentTargetFramework;
  406. var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(targetFramework);
  407. TestProject project = new()
  408. {
  409. Name = "NetCore2App",
  410. TargetFrameworks = targetFramework,
  411. IsExe = true,
  412. RuntimeIdentifier = runtimeIdentifier
  413. };
  414. var testAsset = _testAssetsManager.CreateTestProject(project, identifier: isSelfContained.ToString());
  415. var buildCommand = new BuildCommand(testAsset);
  416. buildCommand
  417. .Execute($"/p:SelfContained={isSelfContained}")
  418. .Should()
  419. .Pass();
  420. string outputFolder = buildCommand.GetOutputDirectory(project.TargetFrameworks, runtimeIdentifier: runtimeIdentifier).FullName;
  421. using var depsJsonFileStream = File.OpenRead(Path.Combine(outputFolder, $"{project.Name}.deps.json"));
  422. var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream);
  423. var runtimeFallbackGraph = dependencyContext.RuntimeGraph;
  424. if (isSelfContained)
  425. {
  426. runtimeFallbackGraph.Should().NotBeEmpty();
  427. runtimeFallbackGraph
  428. .Any(runtimeFallback => !runtimeFallback.Runtime.Equals(runtimeIdentifier) && !runtimeFallback.Fallbacks.Contains(runtimeIdentifier))
  429. .Should()
  430. .BeFalse();
  431. }
  432. else
  433. {
  434. runtimeFallbackGraph.Should().BeEmpty();
  435. }
  436. }
  437. [Fact]
  438. public void There_are_no_conflicts_when_targeting_netcoreapp_1_1()
  439. {
  440. var testProject = new TestProject()
  441. {
  442. Name = "NetCoreApp1.1_Conflicts",
  443. TargetFrameworks = "netcoreapp1.1",
  444. IsExe = true
  445. };
  446. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
  447. var buildCommand = new BuildCommand(testAsset);
  448. buildCommand
  449. .Execute("/v:normal")
  450. .Should()
  451. .Pass()
  452. .And
  453. .NotHaveStdOutMatching("Encountered conflict", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
  454. }
  455. [Theory]
  456. [InlineData(true)]
  457. [InlineData(false)]
  458. public void It_publishes_package_satellites_correctly(bool crossTarget)
  459. {
  460. var testProject = new TestProject()
  461. {
  462. Name = "AppUsingPackageWithSatellites",
  463. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  464. IsExe = true
  465. };
  466. if (crossTarget)
  467. {
  468. testProject.Name += "_cross";
  469. }
  470. testProject.PackageReferences.Add(new TestPackageReference("Humanizer.Core.fr", "2.2.0"));
  471. testProject.PackageReferences.Add(new TestPackageReference("Humanizer.Core.pt", "2.2.0"));
  472. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name)
  473. .WithProjectChanges(project =>
  474. {
  475. if (crossTarget)
  476. {
  477. var ns = project.Root.Name.Namespace;
  478. var propertyGroup = project.Root.Elements(ns + "PropertyGroup").First();
  479. propertyGroup.Element(ns + "TargetFramework").Name += "s";
  480. }
  481. });
  482. var publishCommand = new PublishCommand(testAsset);
  483. publishCommand
  484. .Execute("/v:normal", $"/p:TargetFramework={testProject.TargetFrameworks}")
  485. .Should()
  486. .Pass()
  487. .And
  488. .NotHaveStdOutMatching("Encountered conflict", System.Text.RegularExpressions.RegexOptions.CultureInvariant | System.Text.RegularExpressions.RegexOptions.IgnoreCase)
  489. ;
  490. var outputDirectory = publishCommand.GetOutputDirectory(testProject.TargetFrameworks);
  491. outputDirectory.Should().NotHaveFile("Humanizer.resources.dll");
  492. outputDirectory.Should().HaveFile(Path.Combine("fr", "Humanizer.resources.dll"));
  493. }
  494. [Theory]
  495. [InlineData(true)]
  496. [InlineData(false)]
  497. public void It_uses_lowercase_form_of_the_target_framework_for_the_output_path(bool useStandardOutputPaths)
  498. {
  499. var testProject = new TestProject()
  500. {
  501. Name = "OutputPathCasing",
  502. // Force the actual TargetFramework to be included in the artifact pivots
  503. TargetFrameworks = "ignored;ignored2",
  504. IsExe = true
  505. };
  506. string[] extraArgs = new[] { $"/p:TargetFramework={ToolsetInfo.CurrentTargetFramework.ToUpper()}" };
  507. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name, identifier: useStandardOutputPaths.ToString());
  508. var buildCommand = new BuildCommand(testAsset);
  509. buildCommand
  510. .WithEnvironmentVariable("UseStandardOutputPaths", useStandardOutputPaths.ToString())
  511. .Execute(extraArgs)
  512. .Should()
  513. .Pass();
  514. if (useStandardOutputPaths)
  515. {
  516. buildCommand.GetOutputDirectory().Should().Exist();
  517. buildCommand.GetIntermediateDirectory().Should().Exist();
  518. }
  519. else
  520. {
  521. string outputFolderWithConfiguration = Path.Combine(buildCommand.ProjectRootPath, "bin", "Debug");
  522. Directory.GetDirectories(outputFolderWithConfiguration)
  523. .Select(Path.GetFileName)
  524. .Should()
  525. .BeEquivalentTo(ToolsetInfo.CurrentTargetFramework);
  526. string intermediateFolderWithConfiguration = Path.Combine(buildCommand.GetBaseIntermediateDirectory().FullName, "Debug");
  527. Directory.GetDirectories(intermediateFolderWithConfiguration)
  528. .Select(Path.GetFileName)
  529. .Should()
  530. .BeEquivalentTo(ToolsetInfo.CurrentTargetFramework);
  531. }
  532. }
  533. [Fact]
  534. public void BuildWithTransitiveReferenceToNetCoreAppPackage()
  535. {
  536. var testProject = new TestProject()
  537. {
  538. Name = "NetCoreAppPackageReference",
  539. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  540. IsExe = true
  541. };
  542. var referencedProject = new TestProject()
  543. {
  544. Name = "NetStandardProject",
  545. TargetFrameworks = "netstandard2.0",
  546. IsExe = false
  547. };
  548. // The SharpDX package depends on the Microsoft.NETCore.App package
  549. referencedProject.PackageReferences.Add(new TestPackageReference("SharpDX", "4.0.1"));
  550. testProject.ReferencedProjects.Add(referencedProject);
  551. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
  552. var buildCommand = new BuildCommand(testAsset);
  553. buildCommand
  554. .Execute()
  555. .Should()
  556. .Pass();
  557. }
  558. [WindowsOnlyFact]
  559. public void It_escapes_resolved_package_assets_paths()
  560. {
  561. var testProject = new TestProject()
  562. {
  563. Name = "ProjectWithPackageThatNeedsEscapes",
  564. TargetFrameworks = "net462",
  565. IsExe = true,
  566. };
  567. testProject.SourceFiles["ExampleReader.cs"] = @"
  568. using System;
  569. using System.Threading.Tasks;
  570. namespace ContentFilesExample
  571. {
  572. internal static class ExampleInternals
  573. {
  574. internal static Task<string> GetFileText(string fileName)
  575. {
  576. throw new NotImplementedException();
  577. }
  578. }
  579. }
  580. class Program
  581. {
  582. static void Main(string[] args)
  583. {
  584. Console.WriteLine(""Hello World!"");
  585. }
  586. }";
  587. // ContentFilesExample is an existing package that demonstrates the problem.
  588. // It contains assets with paths that have '%2B', which MSBuild will unescape to '+'.
  589. // Without the change to escape the asset paths, the asset will not be found inside the package.
  590. testProject.PackageReferences.Add(new TestPackageReference("ContentFilesExample", "1.0.2"));
  591. var testAsset = _testAssetsManager
  592. .CreateTestProject(testProject);
  593. var buildCommand = new BuildCommand(testAsset);
  594. buildCommand
  595. .Execute()
  596. .Should()
  597. .Pass();
  598. }
  599. [Fact(Skip = "https://github.com/dotnet/sdk/issues/3044")]
  600. public void ReferenceLegacyContracts()
  601. {
  602. var testProject = new TestProject()
  603. {
  604. Name = "ReferencesLegacyContracts",
  605. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  606. IsExe = true,
  607. RuntimeIdentifier = EnvironmentInfo.GetCompatibleRid(ToolsetInfo.CurrentTargetFramework)
  608. };
  609. // Dependencies on contracts from different 1.x "bands" can cause downgrades when building
  610. // with a RuntimeIdentifier.
  611. testProject.PackageReferences.Add(new TestPackageReference("System.IO.FileSystem", "4.0.1"));
  612. testProject.PackageReferences.Add(new TestPackageReference("System.Reflection", "4.3.0"));
  613. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
  614. var buildCommand = new BuildCommand(testAsset);
  615. buildCommand
  616. .Execute()
  617. .Should()
  618. .Pass();
  619. }
  620. [Fact]
  621. public void ItHasNoPackageReferences()
  622. {
  623. var testProject = new TestProject()
  624. {
  625. Name = "NoPackageReferences",
  626. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  627. IsExe = true
  628. };
  629. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
  630. string testDirectory = Path.Combine(testAsset.TestRoot, testProject.Name);
  631. var getPackageReferences = new GetValuesCommand(
  632. Log,
  633. testDirectory,
  634. testProject.TargetFrameworks,
  635. "PackageReference",
  636. GetValuesCommand.ValueType.Item);
  637. getPackageReferences.Execute().Should().Pass();
  638. List<string> packageReferences = getPackageReferences.GetValues();
  639. packageReferences
  640. .Should()
  641. .BeEmpty();
  642. }
  643. [WindowsOnlyFact]
  644. public void ItResolvesPackageAssetsMultiTargetingNetStandard()
  645. {
  646. var testProject = new TestProject()
  647. {
  648. Name = "MultiTargetedPackageReference",
  649. TargetFrameworks = ToolsetInfo.CurrentTargetFramework + ";netstandard2.1",
  650. RuntimeIdentifier = "win-x64",
  651. IsExe = true
  652. };
  653. testProject.PackageReferences.Add(new TestPackageReference("Nuget.Common", "6.5.7"));
  654. var testAsset = _testAssetsManager.CreateTestProject(testProject, testProject.Name);
  655. var buildCommand = new BuildCommand(testAsset);
  656. buildCommand
  657. .Execute()
  658. .Should()
  659. .Pass()
  660. .And
  661. .HaveStdOutContaining("NU1603");
  662. }
  663. [WindowsOnlyFact]
  664. public void It_builds_with_unicode_characters_in_path()
  665. {
  666. var testProject = new TestProject()
  667. {
  668. Name = "Prj_すおヸょー",
  669. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  670. IsExe = true,
  671. };
  672. var testAsset = _testAssetsManager
  673. .CreateTestProject(testProject);
  674. var buildCommand = new BuildCommand(testAsset);
  675. buildCommand
  676. .Execute()
  677. .Should()
  678. .Pass();
  679. }
  680. [Fact]
  681. public void It_regenerates_files_if_self_contained_changes()
  682. {
  683. const string TFM = ToolsetInfo.CurrentTargetFramework;
  684. var runtimeIdentifier = EnvironmentInfo.GetCompatibleRid(TFM);
  685. var testProject = new TestProject()
  686. {
  687. Name = "GenerateFilesTest",
  688. TargetFrameworks = TFM,
  689. RuntimeIdentifier = runtimeIdentifier,
  690. IsExe = true,
  691. SelfContained = "true"
  692. };
  693. var testAsset = _testAssetsManager
  694. .CreateTestProject(testProject);
  695. var buildCommand = new BuildCommand(testAsset);
  696. buildCommand
  697. .Execute()
  698. .Should()
  699. .Pass();
  700. var outputPath = buildCommand.GetOutputDirectory(targetFramework: TFM, runtimeIdentifier: runtimeIdentifier).FullName;
  701. var depsFilePath = Path.Combine(outputPath, $"{testProject.Name}.deps.json");
  702. var runtimeConfigPath = Path.Combine(outputPath, $"{testProject.Name}.runtimeconfig.json");
  703. var depsFileLastWriteTime = File.GetLastWriteTimeUtc(depsFilePath);
  704. var runtimeConfigLastWriteTime = File.GetLastWriteTimeUtc(runtimeConfigPath);
  705. WaitForUtcNowToAdvance();
  706. buildCommand
  707. .Execute("/p:SelfContained=false")
  708. .Should()
  709. .Pass();
  710. depsFileLastWriteTime.Should().NotBe(File.GetLastWriteTimeUtc(depsFilePath));
  711. runtimeConfigLastWriteTime.Should().NotBe(File.GetLastWriteTimeUtc(runtimeConfigPath));
  712. }
  713. [Fact]
  714. public void It_passes_when_building_single_file_app_without_rid()
  715. {
  716. GetBuildCommand()
  717. .Execute("/p:PublishSingleFile=true")
  718. .Should()
  719. .Pass();
  720. }
  721. [Fact]
  722. public void It_errors_when_publishing_single_file_without_apphost()
  723. {
  724. GetBuildCommand()
  725. .Execute("/p:PublishSingleFile=true", "/p:SelfContained=false", "/p:UseAppHost=false")
  726. .Should()
  727. .Pass();
  728. }
  729. [Theory]
  730. [InlineData(true)]
  731. [InlineData(false)]
  732. public void It_builds_the_project_successfully_with_only_reference_assembly_set(bool produceOnlyReferenceAssembly)
  733. {
  734. var testProject = new TestProject()
  735. {
  736. Name = "MainProject",
  737. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  738. IsSdkProject = true,
  739. IsExe = true
  740. };
  741. testProject.AdditionalProperties["ProduceOnlyReferenceAssembly"] = produceOnlyReferenceAssembly.ToString();
  742. var testProjectInstance = _testAssetsManager.CreateTestProject(testProject, identifier: produceOnlyReferenceAssembly.ToString());
  743. var buildCommand = new BuildCommand(testProjectInstance);
  744. buildCommand
  745. .Execute()
  746. .Should()
  747. .Pass();
  748. var outputPath = buildCommand.GetOutputDirectory(targetFramework: ToolsetInfo.CurrentTargetFramework).FullName;
  749. if (produceOnlyReferenceAssembly == true)
  750. {
  751. var refPath = Path.Combine(outputPath, "ref");
  752. Directory.Exists(refPath)
  753. .Should()
  754. .BeFalse();
  755. }
  756. else
  757. {
  758. // Reference assembly should be produced in obj
  759. var refPath = Path.Combine(
  760. buildCommand.GetIntermediateDirectory(targetFramework: ToolsetInfo.CurrentTargetFramework).FullName,
  761. "ref",
  762. "MainProject.dll");
  763. File.Exists(refPath)
  764. .Should()
  765. .BeTrue();
  766. }
  767. }
  768. private TestProject CreateProjectWithRidAssets(string targetFramework, string[] rids, bool addLibAssets, bool addNativeAssets)
  769. {
  770. var packageProject = new TestProject()
  771. {
  772. Name = "WithRidAssets",
  773. TargetFrameworks = targetFramework,
  774. };
  775. // Add assets for each RID. The test just needs the asset to exist, so it can just copy same output assembly.
  776. foreach (string rid in rids)
  777. {
  778. if (addLibAssets)
  779. {
  780. packageProject.AddItem("None",
  781. new Dictionary<string, string>()
  782. {
  783. { "Include", $"$(TargetPath)" },
  784. { "Pack", "true" },
  785. { "PackagePath", $@"runtimes\{rid}\lib\$(TargetFramework)" }
  786. });
  787. }
  788. if (addNativeAssets)
  789. {
  790. packageProject.AddItem("None",
  791. new Dictionary<string, string>()
  792. {
  793. { "Include", $"$(TargetPath)" },
  794. { "Pack", "true" },
  795. { "PackagePath", $@"runtimes\{rid}\native" }
  796. });
  797. }
  798. }
  799. return packageProject;
  800. }
  801. [Theory]
  802. // Non-portable RID should warn
  803. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "ubuntu.22.04-x64" }, true, true, null, true)]
  804. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "ubuntu.22.04-x64" }, true, false, null, true)]
  805. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "ubuntu.22.04-x64" }, false, true, null, true)]
  806. // Non-portable and portable RIDs should warn
  807. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "ubuntu.22.04-x64", "win7-x86", "unix" }, true, true, null, true)]
  808. // Portable RIDs only should not warn
  809. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "win-x86", "win", "linux", "linux-musl-x64", "osx", "osx-arm64", "unix", "browser", "browser-wasm", "ios-arm64" }, true, true, null, false)]
  810. // No RID assets should not warn
  811. [InlineData(ToolsetInfo.CurrentTargetFramework, new string[] { }, false, false, null, false)]
  812. // Below .NET 8 should not warn
  813. [InlineData("net7.0", new string[] { "ubuntu.22.04-x64", "win7-x86" }, true, true, null, false)]
  814. // Explicitly set to use RID graph should not warn
  815. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "alpine-x64" }, true, true, true, false)]
  816. // Explicitly set to not use RID graph should warn
  817. [InlineData(ToolsetInfo.CurrentTargetFramework, new[] { "alpine-x64" }, true, true, false, true)]
  818. public void It_warns_on_nonportable_rids(string targetFramework, string[] rids, bool addLibAssets, bool addNativeAssets, bool? useRidGraph, bool shouldWarn)
  819. {
  820. var packageProject = CreateProjectWithRidAssets(targetFramework, rids, addLibAssets, addNativeAssets);
  821. // Identifer based on test inputs to create test assets that are unique for each test case
  822. string assetIdentifier = $"{targetFramework}{string.Join(null, rids)}{addLibAssets}{addNativeAssets}{useRidGraph}{shouldWarn}";
  823. var packCommand = new PackCommand(_testAssetsManager.CreateTestProject(packageProject, assetIdentifier));
  824. packCommand.Execute().Should().Pass();
  825. var package = new TestPackageReference(packageProject.Name, "1.0.0", packCommand.GetNuGetPackage());
  826. var testProject = new TestProject()
  827. {
  828. Name = "NonPortableRid",
  829. TargetFrameworks = targetFramework,
  830. IsExe = true
  831. };
  832. // Reference the package, add it to restore sources, and use a test-specific packages folder
  833. testProject.PackageReferences.Add(package);
  834. testProject.AdditionalProperties["RestoreAdditionalProjectSources"] = Path.GetDirectoryName(package.NupkgPath);
  835. testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages";
  836. // The actual list comes from BundledVersions.props. For testing, we conditionally add a
  837. // subset of the list if it isn't already defined (so running on an older version)
  838. testProject.AddItem("_KnownRuntimeIdentiferPlatforms",
  839. new Dictionary<string, string>()
  840. {
  841. { "Include", "unix" },
  842. { "Condition", "'@(_KnownRuntimeIdentiferPlatforms)'==''" }
  843. });
  844. if (useRidGraph.HasValue)
  845. {
  846. testProject.AddItem("RuntimeHostConfigurationOption",
  847. new Dictionary<string, string>()
  848. {
  849. { "Include", "System.Runtime.Loader.UseRidGraph" },
  850. { "Value", useRidGraph.Value.ToString() },
  851. });
  852. }
  853. TestAsset testAsset = _testAssetsManager.CreateTestProject(testProject, assetIdentifier);
  854. var result = new BuildCommand(testAsset).Execute();
  855. result.Should().Pass();
  856. if (shouldWarn)
  857. {
  858. result.Should().HaveStdOutMatching($"NETSDK1206.*{package.ID}");
  859. }
  860. else
  861. {
  862. result.Should().NotHaveStdOutContaining("NETSDK1206");
  863. }
  864. }
  865. [Fact]
  866. public void It_does_not_warn_on_rids_if_no_framework_references()
  867. {
  868. var packageProject = CreateProjectWithRidAssets(ToolsetInfo.CurrentTargetFramework, new string[] { "unix", "win", "alpine-x64" }, true, true);
  869. var packCommand = new PackCommand(_testAssetsManager.CreateTestProject(packageProject));
  870. packCommand.Execute().Should().Pass();
  871. var package = new TestPackageReference(packageProject.Name, "1.0.0", packCommand.GetNuGetPackage());
  872. var testProject = new TestProject()
  873. {
  874. Name = "NoFrameworkReferences",
  875. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  876. IsExe = true
  877. };
  878. // Reference the package, add it to restore sources, and use a test-specific packages folder
  879. testProject.PackageReferences.Add(package);
  880. testProject.AdditionalProperties["RestoreAdditionalProjectSources"] = Path.GetDirectoryName(package.NupkgPath);
  881. testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages";
  882. // Disable implicit framework references and don't add any framework references.
  883. // This mimics the scenario of building runtime framework libraries. Since they are part of the
  884. // framework itself, they just directly reference the other framework libraries they need.
  885. testProject.AdditionalProperties["DisableImplicitFrameworkReferences"] = "true";
  886. testProject.AdditionalProperties["UseAppHost"] = "false";
  887. testProject.PackageReferences.Add(new TestPackageReference("NETStandard.Library", "1.6.1"));
  888. TestAsset testAsset = _testAssetsManager.CreateTestProject(testProject);
  889. var result = new BuildCommand(testAsset).Execute();
  890. result.Should().Pass()
  891. .And.NotHaveStdOutContaining("NETSDK1206");
  892. }
  893. [Theory]
  894. [InlineData(true, "TRACE DISABLED")]
  895. [InlineData(false, "TRACE ENABLED")]
  896. public void It_can_use_implicitly_defined_compilation_constants(bool disableTracing, string expectedOutput)
  897. {
  898. var testProj = new TestProject()
  899. {
  900. Name = "DisableTracing_" + disableTracing.ToString(),
  901. TargetFrameworks = ToolsetInfo.CurrentTargetFramework,
  902. IsExe = true,
  903. };
  904. if (disableTracing == true)
  905. {
  906. testProj.AdditionalProperties["DisableDiagnosticTracing"] = "true";
  907. }
  908. testProj.SourceFiles[$"{testProj.Name}.cs"] = @"
  909. using System;
  910. class Program
  911. {
  912. static void Main(string[] args)
  913. {
  914. #if TRACE
  915. Console.WriteLine(""TRACE ENABLED"");
  916. #endif
  917. #if !TRACE
  918. Console.WriteLine(""TRACE DISABLED"");
  919. #endif
  920. }
  921. }";
  922. var testAsset = _testAssetsManager.CreateTestProject(testProj, identifier: disableTracing.ToString());
  923. var buildCommand = new BuildCommand(Log, Path.Combine(testAsset.Path, testProj.Name));
  924. buildCommand
  925. .Execute()
  926. .Should()
  927. .Pass();
  928. var runCommand = new RunExeCommand(Log, Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, $"{testProj.Name}{EnvironmentInfo.ExecutableExtension}"));
  929. runCommand
  930. .Execute()
  931. .Should().HaveStdOut(expectedOutput);
  932. }
  933. }
  934. }