|
4 | 4 | using System;
|
5 | 5 | using System.CommandLine.Parsing;
|
6 | 6 | using System.IO;
|
| 7 | +using System.Linq; |
| 8 | +using Microsoft.DotNet.Cli.Utils; |
| 9 | +using NuGet.Common; |
| 10 | +using NuGet.Configuration; |
7 | 11 |
|
8 | 12 | namespace Microsoft.DotNet.Cli
|
9 | 13 | {
|
@@ -33,7 +37,57 @@ public static void OverrideEnvironmentVariableToTmp(ParseResult parseResult)
|
33 | 37 | if (!OperatingSystem.IsWindows() && IsRunningUnderSudo() && IsRunningWorkloadCommand(parseResult))
|
34 | 38 | {
|
35 | 39 | Directory.CreateDirectory(SudoHomeDirectory);
|
| 40 | + |
| 41 | + var homeBeforeOverride = Path.Combine(Environment.GetEnvironmentVariable("HOME")); |
36 | 42 | Environment.SetEnvironmentVariable("HOME", SudoHomeDirectory);
|
| 43 | + |
| 44 | + CopyUserNuGetConfigToOverridenHome(homeBeforeOverride); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// To make NuGet honor the user's NuGet config file. |
| 50 | + /// Copying instead of using the file directoy to avoid existing file being set higher permission |
| 51 | + /// Try to delete the existing NuGet config file in "/tmp/dotnet_sudo_home/" |
| 52 | + /// to avoid different user's NuGet config getting mixed. |
| 53 | + /// </summary> |
| 54 | + private static void CopyUserNuGetConfigToOverridenHome(string homeBeforeOverride) |
| 55 | + { |
| 56 | + // https://github.com/NuGet/NuGet.Client/blob/dev/src/NuGet.Core/NuGet.Common/PathUtil/NuGetEnvironment.cs#L139 |
| 57 | + // home is cache in NuGet we cannot directly use the call |
| 58 | + var userSettingsDir = Path.Combine(homeBeforeOverride, ".nuget", "NuGet"); |
| 59 | + |
| 60 | + string userNuGetConfig = Settings.OrderedSettingsFileNames |
| 61 | + .Select(fileName => Path.Combine(userSettingsDir, fileName)) |
| 62 | + .FirstOrDefault(f => File.Exists(f)); |
| 63 | + |
| 64 | + var overridenSettingsDir = NuGetEnvironment.GetFolderPath(NuGetFolderPath.UserSettingsDirectory); |
| 65 | + var overridenNugetConfig = Path.Combine(overridenSettingsDir, Settings.DefaultSettingsFileName); |
| 66 | + |
| 67 | + if (File.Exists(overridenNugetConfig)) |
| 68 | + { |
| 69 | + try |
| 70 | + { |
| 71 | + FileAccessRetrier.RetryOnIOException( |
| 72 | + () => File.Delete(overridenNugetConfig)); |
| 73 | + } |
| 74 | + catch |
| 75 | + { |
| 76 | + // best effort to remove |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (userNuGetConfig != default) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + FileAccessRetrier.RetryOnIOException( |
| 85 | + () => File.Copy(userNuGetConfig, overridenNugetConfig, overwrite: true)); |
| 86 | + } |
| 87 | + catch |
| 88 | + { |
| 89 | + // best effort to copy |
| 90 | + } |
37 | 91 | }
|
38 | 92 | }
|
39 | 93 |
|
|
0 commit comments