Skip to content

Commit 1a549b8

Browse files
authored
Set default local auth type for msrustup, add configurable installation directory (#612)
1 parent 4103696 commit 1a549b8

File tree

6 files changed

+58
-30
lines changed

6 files changed

+58
-30
lines changed

src/Cargo/CargoTask.cs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,6 @@ namespace Microsoft.Build.Cargo
2323
/// </summary>
2424
public class CargoTask : Task
2525
{
26-
private static readonly string? _tempPath = Environment.GetEnvironmentVariable("TEMP") ?? throw new Exception("%TEMP% directory not defined");
27-
private static readonly string _rustUpBinary = Path.Combine(_tempPath, "cargohome", "bin", "rustup.exe");
28-
private static readonly string _cargoPath = Path.Combine(_tempPath, "cargohome", "bin", "cargo.exe");
29-
private static readonly string _rustInstallPath = Path.Combine(_tempPath, "rustinstall");
30-
private static readonly string _rustUpInitBinary = Path.Combine(_rustInstallPath, "rustup-init.exe");
31-
private static readonly string _cargoHome = Path.Combine(_tempPath, "cargohome");
32-
private static readonly string _rustUpHome = Path.Combine(_tempPath, "rustuphome");
33-
private static readonly string _cargoHomeBin = Path.Combine(_tempPath, "cargohome", "bin");
34-
private static readonly string _msRustUpBinary = Path.Combine(_tempPath, "cargohome", "bin", "msrustup.exe");
35-
private static readonly Dictionary<string, string> _envVars = new () { { "CARGO_HOME", _cargoHome }, { "RUSTUP_HOME", _rustUpHome } };
36-
private static readonly string _rustUpDownloadLink = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe";
37-
private static readonly string _checkSumVerifyUrl = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe.sha256";
3826
private static readonly string _rustToolChainFileName = "rust-toolchain.toml";
3927
private static readonly string _cargoConfigFilePath = Path.Combine(".cargo", "config.toml");
4028
private static readonly string _cargoFileName = "cargo.toml";
@@ -43,18 +31,34 @@ public class CargoTask : Task
4331
private static readonly string _installCommand = "install";
4432
private static readonly string _fetchCommand = "fetch";
4533
private static readonly string _loginCommand = "login";
34+
private static readonly string _rustUpDownloadLink = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe";
35+
private static readonly string _checkSumVerifyUrl = "https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe.sha256";
4636
private string? _rustUpFile = Environment.GetEnvironmentVariable("MSRUSTUP_FILE");
4737
private bool _shouldCleanRustPath = false;
4838
private bool _isMsRustUp = false;
4939
private string? _currentRustUpInitExeCheckSum;
5040
private List<string> _cargoRegistries = new ();
41+
private string _rustUpBinary = string.Empty;
42+
private string _cargoPath = string.Empty;
43+
private string _rustInstallPath = string.Empty;
44+
private string _rustUpInitBinary = string.Empty;
45+
private string _cargoHome = string.Empty;
46+
private string _rustUpHome = string.Empty;
47+
private string _cargoHomeBin = string.Empty;
48+
private string _msRustUpBinary = string.Empty;
49+
private Dictionary<string, string> _envVars = new ();
5150

5251
private enum ExitCode
5352
{
5453
Succeeded,
5554
Failed,
5655
}
5756

57+
/// <summary>
58+
/// Gets or sets installation root path for rust.
59+
/// <inheritdoc/>
60+
public string CargoInstallationRoot { get; set; } = Environment.GetEnvironmentVariable("TEMP") ?? Path.GetTempPath();
61+
5862
/// <summary>
5963
/// Gets or sets a cargo command to execute.
6064
/// </summary>
@@ -87,17 +91,36 @@ private enum ExitCode
8791
/// </summary>
8892
public string Configuration { get; set; } = string.Empty;
8993

94+
/// <summary>
95+
/// Gets or sets the MSRustup Authentication type.
96+
/// </summary>
97+
public string MsRustupAuthType { get; set; } = string.Empty;
98+
9099
/// <inheritdoc/>
91100
public override bool Execute()
92101
{
102+
if (string.IsNullOrEmpty(CargoInstallationRoot))
103+
{
104+
throw new InvalidOperationException("CargoInstallationRoot cannot be null or empty.");
105+
}
106+
107+
_rustUpBinary = Path.Combine(CargoInstallationRoot, "cargohome", "bin", "rustup.exe");
108+
_cargoPath = Path.Combine(CargoInstallationRoot, "cargohome", "bin", "cargo.exe");
109+
_rustInstallPath = Path.Combine(CargoInstallationRoot, "rustinstall");
110+
_rustUpInitBinary = Path.Combine(_rustInstallPath, "rustup-init.exe");
111+
_cargoHome = Path.Combine(CargoInstallationRoot, "cargohome");
112+
_rustUpHome = Path.Combine(CargoInstallationRoot, "rustuphome");
113+
_cargoHomeBin = Path.Combine(CargoInstallationRoot, "cargohome", "bin");
114+
_msRustUpBinary = Path.Combine(CargoInstallationRoot, "cargohome", "bin", "msrustup.exe");
115+
_envVars = new () { { "CARGO_HOME", _cargoHome }, { "RUSTUP_HOME", _rustUpHome } };
93116
return ExecuteAsync().GetAwaiter().GetResult();
94117
}
95118

96-
private static void CleanupRustPath()
119+
private void CleanupRustPath()
97120
{
98-
if (Directory.Exists(_rustUpInitBinary))
121+
if (File.Exists(_rustUpInitBinary))
99122
{
100-
Directory.Delete(_rustUpInitBinary, true);
123+
File.Delete(_rustUpInitBinary);
101124
}
102125
}
103126

@@ -108,7 +131,6 @@ private async Task<bool> ExecuteAsync()
108131
_isMsRustUp = File.Exists(Path.Combine(RepoRoot, _rustToolChainFileName)) && IsMSToolChain(Path.Combine(RepoRoot, _rustToolChainFileName));
109132
}
110133

111-
// download & install rust if necessary
112134
if (Command.Equals(_installCommand, StringComparison.InvariantCultureIgnoreCase))
113135
{
114136
return await DownloadAndInstallRust();
@@ -120,6 +142,7 @@ private async Task<bool> ExecuteAsync()
120142
if (string.IsNullOrEmpty(_rustUpFile) || !File.Exists(_rustUpFile))
121143
{
122144
Log.LogMessage($"MSRUSTUP_FILE environment variable is not set or the file does not exist. Assuming local build.");
145+
_envVars.Add("ADO_CREDENTIAL_PROVIDER", MsRustupAuthType);
123146
}
124147
else
125148
{
@@ -199,7 +222,6 @@ private async Task<ExitCode> CargoRunCommandAsync(string command, string args)
199222
return await ExecuteProcessAsync(customCargoBin!, $"{command} {args} --offline {(isDebugConfiguration ? string.Empty : "--" + Configuration.ToLowerInvariant())} --config {Path.Combine(RepoRoot, _cargoConfigFilePath)}", ".", _envVars);
200223
}
201224

202-
// if we don't have the toolchain, we need to install it.
203225
return ExitCode.Failed;
204226
}
205227

@@ -219,7 +241,7 @@ private async Task<bool> DownloadAndInstallRust()
219241
}
220242
}
221243

222-
return downloadSuccess && installSuccess;
244+
return true;
223245
}
224246

225247
private async Task<bool> FetchCratesAsync(string project)
@@ -463,6 +485,7 @@ private async Task<bool> InstallRust()
463485
bool msRustupToolChainExists = useMsRustUp && !string.IsNullOrEmpty(GetCustomToolChainCargoPath());
464486
bool cargoPathAndRustPathsExists = Directory.Exists(_cargoHome) && Directory.Exists(_rustUpHome);
465487
bool cargoBinaryExists = File.Exists(_cargoPath);
488+
466489
if ((msRustupToolChainExists && cargoPathAndRustPathsExists && useMsRustUp) || cargoPathAndRustPathsExists && cargoBinaryExists && !useMsRustUp)
467490
{
468491
return true;
@@ -517,6 +540,7 @@ private async Task<bool> InstallRust()
517540
if (string.IsNullOrEmpty(_rustUpFile) || !File.Exists(_rustUpFile))
518541
{
519542
Log.LogMessage($"MSRUSTUP_FILE environment variable is not set or the file does not exist. Assuming local build.");
543+
_envVars.Add("ADO_CREDENTIAL_PROVIDER", MsRustupAuthType);
520544
}
521545
else
522546
{

src/Cargo/Microsoft.Build.Cargo.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
<ItemGroup>
2020
<PackageReference Include="Microsoft.Build.Utilities.Core" VersionOverride="17.11.4" />
2121
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" VersionOverride="17.11.4" />
22-
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime"
23-
VersionOverride="17.11.4" />
22+
<PackageReference Include="Microsoft.Build.Framework" ExcludeAssets="runtime" VersionOverride="17.11.4" />
2423
<PackageReference Include="System.Net.Http" NoWarn="RT0003" />
2524
</ItemGroup>
2625
<ItemGroup>

src/Cargo/dist/msrustup.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
# Requires MSRUSTUP_ACCESS_TOKEN or MSRUSTUP_PAT environment variables to be set with a token.
1010
# See https://aka.ms/rust for more information.
1111

12+
param (
13+
[string]$destinationDirectory
14+
)
15+
1216
$ErrorActionPreference = "Stop"
13-
$destinationDirectory = $env:Temp + "\cargohome\bin"
1417

1518
# Create directory if it doesn't exist
1619
Write-Host $destinationDirectory

src/Cargo/sdk/CargoInstall.proj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net8.0\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' == 'Core'" />
44
<UsingTask TaskName="Microsoft.Build.Cargo.CargoTask" AssemblyFile="$(MSBuildThisFileDirectory)..\build\net472\Microsoft.Build.Cargo.dll" Condition="'$(MSBuildRuntimeType)' != 'Core'" />
55
<Target Name="CargoInstall">
6-
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(MSBuildStartupDirectory)" Command="install" RepoRoot="$(RepoRoot)" />
6+
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(MSBuildStartupDirectory)" Command="install" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
77
</Target>
88
</Project>

src/Cargo/sdk/Sdk.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<DocCommandArgs Condition="'$(DocCommandArgs)' == ''"></DocCommandArgs>
8585
<StartupProj Condition="'$(StartupProj)' == ''">$(MSBuildProjectFullPath)</StartupProj>
8686
<RepoRoot Condition="'$(RepoRoot)' == ''">$(EnlistmentRoot)</RepoRoot>
87+
<CargoInstallationRoot Condition="'$(CargoInstallationRoot)' == ''"></CargoInstallationRoot>
88+
<MsRustupAuthType Condition="'$(MsRustupAuthType)' == ''">AzureAuth</MsRustupAuthType>
8789
</PropertyGroup>
8890
<ItemGroup>
8991
<None Include="**\*.rs" />

src/Cargo/sdk/Sdk.targets

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,29 +112,29 @@
112112
<MSBuild
113113
Projects="$(MSBuildThisFileDirectory)CargoInstall.proj"
114114
Targets="CargoInstall"
115-
Properties="EnableTelemetryLoggerCopy=$(EnableTelemetryLoggerCopy);TelemetryLoggerLocation=$(TelemetryLoggerLocation);TelemetryLoggerSourcePath=$(TelemetryLoggerSourcePath);TelemetryLoggerInstallId=$(TelemetryLoggerInstallId);RepoRoot=$(RepoRoot)"
115+
Properties="EnableTelemetryLoggerCopy=$(EnableTelemetryLoggerCopy);TelemetryLoggerLocation=$(TelemetryLoggerLocation);TelemetryLoggerSourcePath=$(TelemetryLoggerSourcePath);TelemetryLoggerInstallId=$(TelemetryLoggerInstallId);RepoRoot=$(RepoRoot);CargoInstallationRoot=$(CargoInstallationRoot);MsRustupAuthType=$(MsRustupAuthType)"
116116
RemoveProperties="NuGetInteractive;MSBuildRestoreSessionId;TargetFramework;RuntimeIdentifier" />
117117
</Target>
118118
<Target Name="CargoFetch" AfterTargets="CargoInstall">
119-
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="fetch" RepoRoot="$(RepoRoot)" />
119+
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="fetch" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
120120
</Target>
121121

122122
<Target Name="CargoBuild" AfterTargets="CoreCompile">
123-
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="build" CommandArgs="$(BuildCommandArgs)" Configuration="$(Configuration)" RepoRoot="$(RepoRoot)" />
123+
<CargoTask EnableAuth="$(AuthMode)" StartupProj="$(StartupProj)" Command="build" CommandArgs="$(BuildCommandArgs)" Configuration="$(Configuration)" RepoRoot="$(RepoRoot)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
124124
</Target>
125125
<Target Name="Test" DependsOnTargets="Cargo">
126-
<CargoTask StartupProj="$(StartupProj)" Command="test" CommandArgs="$(TestCommandArgs)" />
126+
<CargoTask StartupProj="$(StartupProj)" Command="test" CommandArgs="$(TestCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
127127
</Target>
128128
<Target Name="Clean">
129-
<CargoTask StartupProj="$(StartupProj)" Command="clean" CommandArgs="$(CleanCommandArgs)" />
129+
<CargoTask StartupProj="$(StartupProj)" Command="clean" CommandArgs="$(CleanCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
130130
</Target>
131131
<Target Name="Run" DependsOnTargets="Cargo">
132-
<CargoTask StartupProj="$(StartupProj)" Command="run" CommandArgs="$(RunCommandArgs)" />
132+
<CargoTask StartupProj="$(StartupProj)" Command="run" CommandArgs="$(RunCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
133133
</Target>
134134
<Target Name="Doc" DependsOnTargets="Cargo">
135-
<CargoTask StartupProj="$(StartupProj)" Command="doc" CommandArgs="$(DocCommandArgs)" />
135+
<CargoTask StartupProj="$(StartupProj)" Command="doc" CommandArgs="$(DocCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
136136
</Target>
137137
<Target Name="ClearCargoCache">
138-
<CargoTask StartupProj="$(StartupProj)" Command="clearcargocache" CommandArgs="$(DocCommandArgs)" />
138+
<CargoTask StartupProj="$(StartupProj)" Command="clearcargocache" CommandArgs="$(DocCommandArgs)" CargoInstallationRoot="$(CargoInstallationRoot)" MsRustupAuthType="$(MsRustupAuthType)" />
139139
</Target>
140140
</Project>

0 commit comments

Comments
 (0)