Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set EnableWindowsTargeting to true when updating the lock file #11037

Merged
merged 7 commits into from
Dec 20, 2024

Conversation

na1307
Copy link
Contributor

@na1307 na1307 commented Dec 2, 2024

What are you trying to accomplish?

I'm trying to prevent the lock file update from failing when updating NuGet packages for Windows specific projects.

Fix #11036

Anything you want to highlight for special attention from reviewers?

No

How will you know you've accomplished your goal?

Added -p:EnableWindowsTargeting=true to dotnet restore command. This prevents dotnet cli from failing when restoring Windows specific projects.

Checklist

  • I have run the complete test suite to ensure all tests and linters pass.
  • I have thoroughly tested my code changes to ensure they work as expected, including adding additional tests for new functionality.
  • I have written clear and descriptive commit messages.
  • I have provided a detailed description of the changes in the pull request, including the problem it addresses, how it fixes the problem, and any relevant details about the implementation.
  • I have ensured that the code is well-documented and easy to understand.

@na1307 na1307 requested a review from a team as a code owner December 2, 2024 12:30
@github-actions github-actions bot added the L: dotnet:nuget NuGet packages via nuget or dotnet label Dec 2, 2024
@JamieMagee
Copy link
Contributor

Are there any downsides to optimistically setting this property? This is the only documentation I can find on it:

@na1307
Copy link
Contributor Author

na1307 commented Dec 3, 2024

Are there any downsides to optimistically setting this property? This is the only documentation I can find on it:

As far as I know, there isn't any.

@brettfo
Copy link
Contributor

brettfo commented Dec 4, 2024

@na1307 The change looks good and the documentation makes sense. Could you add a unit test verifying the behavior? All CI (and production) runs in a Linux container, so you should get the appropriate behavior.

@na1307
Copy link
Contributor Author

na1307 commented Dec 5, 2024

@na1307 The change looks good and the documentation makes sense. Could you add a unit test verifying the behavior? All CI (and production) runs in a Linux container, so you should get the appropriate behavior.

I know roughly how the tests are written, but I don't know what to do because of the contentHash in the lock file. Should I just use Some.Package like other tests, or should I use an actual existing package?

@na1307
Copy link
Contributor Author

na1307 commented Dec 8, 2024

@brettfo

@na1307
Copy link
Contributor Author

na1307 commented Dec 17, 2024

Why is there still no answer... @brettfo

@brettfo
Copy link
Contributor

brettfo commented Dec 18, 2024

You can write a unit test that uses real NuGet packages and I'll fix it up to use fake local packages later. As for verification, you might need to update the test methods to allow for a custom verification action or regular expression match.

If my description wasn't helpful (they often aren't :) ) and if you can point me to some example projects/files/packages I can help with writing the test.

@na1307
Copy link
Contributor Author

na1307 commented Dec 19, 2024

@brettfo I've added tests. I don't know if this will be enough.

@brettfo
Copy link
Contributor

brettfo commented Dec 19, 2024

@na1307 Thank you for the tests. I don't have access to push the test changes, but you can save the following file as tests.patch and run the following command locally to apply these. Command: git apply --verbose tests.patch

`tests.patch` contents:
From 87cf71c28643d4980ca89bdce3b2825ded56d320 Mon Sep 17 00:00:00 2001
From: "Brett V. Forsgren" <[email protected]>
Date: Thu, 19 Dec 2024 11:25:23 -0700
Subject: [PATCH] use local packages for tests

---
 .../Update/UpdateWorkerTestBase.cs            |  18 +-
 .../Update/UpdateWorkerTests.LockFile.cs      | 280 ++++++------------
 2 files changed, 113 insertions(+), 185 deletions(-)

diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs
index 3372eceb7..9785bfcc1 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTestBase.cs
@@ -47,7 +47,7 @@ public abstract class UpdateWorkerTestBase : TestBase
     {
         return useSolution
             ? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [(projectFilePath, projectContents)], projectFilesExpected: [(projectFilePath, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager)
-            : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile: (projectFilePath, projectContents), expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager);
+            : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile: (projectFilePath, projectContents), expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, additionalChecks: null, packages: packages, experimentsManager: experimentsManager);
     }
 
     protected static Task TestUpdate(
@@ -65,7 +65,7 @@ public abstract class UpdateWorkerTestBase : TestBase
     {
         return useSolution
             ? TestUpdateForSolution(dependencyName, oldVersion, newVersion, projectFiles: [projectFile], projectFilesExpected: [(projectFile.Path, expectedProjectContents)], isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager)
-            : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile, expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, packages, experimentsManager);
+            : TestUpdateForProject(dependencyName, oldVersion, newVersion, projectFile, expectedProjectContents, isTransitive, additionalFiles, additionalFilesExpected, additionalChecks: null, packages: packages, experimentsManager: experimentsManager);
     }
 
     protected static Task TestNoChangeforProject(
@@ -101,6 +101,7 @@ public abstract class UpdateWorkerTestBase : TestBase
         bool isTransitive = false,
         TestFile[]? additionalFiles = null,
         TestFile[]? additionalFilesExpected = null,
+        Action<string>? additionalChecks = null,
         MockNuGetPackage[]? packages = null,
         ExperimentsManager? experimentsManager = null,
         string projectFilePath = "test-project.csproj",
@@ -114,6 +115,7 @@ public abstract class UpdateWorkerTestBase : TestBase
             isTransitive,
             additionalFiles,
             additionalFilesExpected,
+            additionalChecks,
             packages,
             experimentsManager,
             expectedResult);
@@ -127,6 +129,7 @@ public abstract class UpdateWorkerTestBase : TestBase
         bool isTransitive = false,
         TestFile[]? additionalFiles = null,
         TestFile[]? additionalFilesExpected = null,
+        Action<string>? additionalChecks = null,
         MockNuGetPackage[]? packages = null,
         ExperimentsManager? experimentsManager = null,
         ExpectedUpdateOperationResult? expectedResult = null)
@@ -156,6 +159,17 @@ public abstract class UpdateWorkerTestBase : TestBase
             {
                 ValidateUpdateOperationResult(expectedResult, actualResult!);
             }
+
+            if (additionalChecks is not null)
+            {
+                var sourcesDirectory = temporaryDirectory;
+                if (placeFilesInSrc)
+                {
+                    sourcesDirectory = Path.Combine(temporaryDirectory, "src");
+                }
+
+                additionalChecks(sourcesDirectory);
+            }
         });
 
         var expectedResultFiles = additionalFilesExpected.Prepend((projectFilePath, expectedProjectContents)).ToArray();
diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs
index ca4f13a24..efa02e929 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.LockFile.cs
@@ -9,8 +9,12 @@ public partial class UpdateWorkerTests
         [Fact]
         public async Task UpdateSingleDependency()
         {
-            // update Newtonsoft.Json from 13.0.1 to 13.0.3
-            await TestUpdateForProject("Newtonsoft.Json", "13.0.1", "13.0.3",
+            await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0",
+                packages:
+                [
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"),
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"),
+                ],
                 // initial
                 projectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -20,10 +24,14 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
+                        <PackageReference Include="Some.Package" Version="1.0.0" />
                       </ItemGroup>
                     </Project>
                     """,
+                additionalFiles:
+                [
+                    ("packages.lock.json", "{}")
+                ],
                 // expected
                 expectedProjectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -33,54 +41,27 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+                        <PackageReference Include="Some.Package" Version="2.0.0" />
                       </ItemGroup>
                     </Project>
                     """,
-                additionalFiles:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 1,
-                          "dependencies": {
-                            "net8.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.1, )",
-                                "resolved": "13.0.1",
-                                "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A=="
-                              }
-                            }
-                          }
-                        }
-                        """)
-                ],
-                additionalFilesExpected:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 1,
-                          "dependencies": {
-                            "net8.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.3, )",
-                                "resolved": "13.0.3",
-                                "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
-                              }
-                            }
-                          }
-                        }
-                        """)
-                ]
+                additionalChecks: path =>
+                {
+                    var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json"));
+                    Assert.Contains("\"resolved\": \"2.0.0\"", lockContents);
+                }
             );
         }
-        
+
         [Fact]
         public async Task UpdateSingleDependency_CentralPackageManagement()
         {
-            // update Newtonsoft.Json from 13.0.1 to 13.0.3
-            await TestUpdateForProject("Newtonsoft.Json", "13.0.1", "13.0.3",
+            await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0",
+                packages:
+                [
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"),
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"),
+                ],
                 // initial
                 projectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -90,10 +71,25 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" />
+                        <PackageReference Include="Some.Package" />
                       </ItemGroup>
                     </Project>
                     """,
+                additionalFiles:
+                [
+                    ("packages.lock.json", "{}"),
+                    ("Directory.Packages.props", """
+                        <Project>
+                          <PropertyGroup>
+                            <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
+                          </PropertyGroup>
+                    
+                          <ItemGroup>
+                            <PackageVersion Include="Some.Package" Version="1.0.0" />
+                          </ItemGroup>
+                        </Project>
+                        """)
+                ],
                 // expected
                 expectedProjectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -103,56 +99,12 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" />
+                        <PackageReference Include="Some.Package" />
                       </ItemGroup>
                     </Project>
                     """,
-                additionalFiles:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 2,
-                          "dependencies": {
-                            "net8.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.1, )",
-                                "resolved": "13.0.1",
-                                "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A=="
-                              }
-                            }
-                          }
-                        }
-                        """),
-                    ("Directory.Packages.props", """
-                    <Project>
-                      <PropertyGroup>
-                        <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
-                      </PropertyGroup>
-                    
-                      <ItemGroup>
-                        <PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
-                      </ItemGroup>
-                    </Project>
-                    """)
-                ],
                 additionalFilesExpected:
                 [
-                    ("packages.lock.json", """
-                        {
-                          "version": 2,
-                          "dependencies": {
-                            "net8.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.3, )",
-                                "resolved": "13.0.3",
-                                "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
-                              }
-                            }
-                          }
-                        }
-                        """),
                     ("Directory.Packages.props", """
                         <Project>
                           <PropertyGroup>
@@ -160,19 +112,28 @@ public partial class UpdateWorkerTests
                           </PropertyGroup>
                         
                           <ItemGroup>
-                            <PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
+                            <PackageVersion Include="Some.Package" Version="2.0.0" />
                           </ItemGroup>
                         </Project>
                         """)
-                ]
+                ],
+                additionalChecks: path =>
+                {
+                    var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json"));
+                    Assert.Contains("\"resolved\": \"2.0.0\"", lockContents);
+                }
             );
         }
-        
+
         [Fact]
         public async Task UpdateSingleDependency_WindowsSpecific()
         {
-            // update Newtonsoft.Json from 13.0.1 to 13.0.3
-            await TestUpdateForProject("Newtonsoft.Json", "13.0.1", "13.0.3",
+            await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0",
+                packages:
+                [
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"),
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"),
+                ],
                 // initial
                 projectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -183,10 +144,14 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
+                        <PackageReference Include="Some.Package" Version="1.0.0" />
                       </ItemGroup>
                     </Project>
                     """,
+                additionalFiles:
+                [
+                    ("packages.lock.json", "{}")
+                ],
                 // expected
                 expectedProjectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -197,54 +162,27 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
+                        <PackageReference Include="Some.Package" Version="2.0.0" />
                       </ItemGroup>
                     </Project>
                     """,
-                additionalFiles:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 1,
-                          "dependencies": {
-                            "net8.0-windows7.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.1, )",
-                                "resolved": "13.0.1",
-                                "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A=="
-                              }
-                            }
-                          }
-                        }
-                        """)
-                ],
-                additionalFilesExpected:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 1,
-                          "dependencies": {
-                            "net8.0-windows7.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.3, )",
-                                "resolved": "13.0.3",
-                                "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
-                              }
-                            }
-                          }
-                        }
-                        """)
-                ]
+                additionalChecks: path =>
+                {
+                    var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json"));
+                    Assert.Contains("\"resolved\": \"2.0.0\"", lockContents);
+                }
             );
         }
-        
+
         [Fact]
         public async Task UpdateSingleDependency_CentralPackageManagement_WindowsSpecific()
         {
-            // update Newtonsoft.Json from 13.0.1 to 13.0.3
-            await TestUpdateForProject("Newtonsoft.Json", "13.0.1", "13.0.3",
+            await TestUpdateForProject("Some.Package", "1.0.0", "2.0.0",
+                packages:
+                [
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "1.0.0", "net8.0"),
+                    MockNuGetPackage.CreateSimplePackage("Some.Package", "2.0.0", "net8.0"),
+                ],
                 // initial
                 projectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -255,10 +193,25 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" />
+                        <PackageReference Include="Some.Package" />
                       </ItemGroup>
                     </Project>
                     """,
+                additionalFiles:
+                [
+                    ("packages.lock.json", "{}"),
+                    ("Directory.Packages.props", """
+                        <Project>
+                          <PropertyGroup>
+                            <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
+                          </PropertyGroup>
+                    
+                          <ItemGroup>
+                            <PackageVersion Include="Some.Package" Version="1.0.0" />
+                          </ItemGroup>
+                        </Project>
+                        """)
+                ],
                 // expected
                 expectedProjectContents: $"""
                     <Project Sdk="Microsoft.NET.Sdk">
@@ -269,56 +222,12 @@ public partial class UpdateWorkerTests
                       </PropertyGroup>
 
                       <ItemGroup>
-                        <PackageReference Include="Newtonsoft.Json" />
+                        <PackageReference Include="Some.Package" />
                       </ItemGroup>
                     </Project>
                     """,
-                additionalFiles:
-                [
-                    ("packages.lock.json", """
-                        {
-                          "version": 2,
-                          "dependencies": {
-                            "net8.0-windows7.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.1, )",
-                                "resolved": "13.0.1",
-                                "contentHash": "ppPFpBcvxdsfUonNcvITKqLl3bqxWbDCZIzDWHzjpdAHRFfZe0Dw9HmA0+za13IdyrgJwpkDTDA9fHaxOrt20A=="
-                              }
-                            }
-                          }
-                        }
-                        """),
-                    ("Directory.Packages.props", """
-                    <Project>
-                      <PropertyGroup>
-                        <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
-                      </PropertyGroup>
-                    
-                      <ItemGroup>
-                        <PackageVersion Include="Newtonsoft.Json" Version="13.0.1" />
-                      </ItemGroup>
-                    </Project>
-                    """)
-                ],
                 additionalFilesExpected:
                 [
-                    ("packages.lock.json", """
-                        {
-                          "version": 2,
-                          "dependencies": {
-                            "net8.0-windows7.0": {
-                              "Newtonsoft.Json": {
-                                "type": "Direct",
-                                "requested": "[13.0.3, )",
-                                "resolved": "13.0.3",
-                                "contentHash": "HrC5BXdl00IP9zeV+0Z848QWPAoCr9P3bDEZguI+gkLcBKAOxix/tLEAAHC+UvDNPv4a2d18lOReHMOagPa+zQ=="
-                              }
-                            }
-                          }
-                        }
-                        """),
                     ("Directory.Packages.props", """
                         <Project>
                           <PropertyGroup>
@@ -326,11 +235,16 @@ public partial class UpdateWorkerTests
                           </PropertyGroup>
                         
                           <ItemGroup>
-                            <PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
+                            <PackageVersion Include="Some.Package" Version="2.0.0" />
                           </ItemGroup>
                         </Project>
                         """)
-                ]
+                ],
+                additionalChecks: path =>
+                {
+                    var lockContents = File.ReadAllText(Path.Combine(path, "packages.lock.json"));
+                    Assert.Contains("\"resolved\": \"2.0.0\"", lockContents);
+                }
             );
         }
     }
-- 
2.47.1.windows.1

@na1307
Copy link
Contributor Author

na1307 commented Dec 20, 2024

@brettfo I applied the patch. Thank you very much for your help.

@randhircs randhircs merged commit 1cb627b into dependabot:main Dec 20, 2024
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
L: dotnet:nuget NuGet packages via nuget or dotnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lock file update fails if project is windows specific
4 participants