Repository navigation
Description
I think I have spotted a bug in the dotnet.cmd/tools.ps1 interaction:
Here is the relevant content of dotnet.cmd:
Lines 3 to 10 in 496c2a6
| powershell -ExecutionPolicy ByPass -NoProfile -Command "& { . '%~dp0eng\common\tools.ps1'; InitializeDotNetCli $true $true }" | |
| if NOT [%ERRORLEVEL%] == [0] ( | |
| echo Failed to install or invoke dotnet... 1>&2 | |
| exit /b %ERRORLEVEL% | |
| ) | |
| set /p dotnetPath=<%~dp0artifacts\toolset\sdk.txt |
Apparently, dotnet.cmd assumes tools.ps1 will ensure sdk.txt exists after InitializeDotNetCli $true $true is ran. Now let's look into the relevant code in tools.ps1.
Lines 132 to 165 in 496c2a6
| # Use dotnet installation specified in DOTNET_INSTALL_DIR if it contains the required SDK version, | |
| # otherwise install the dotnet CLI and SDK to repo local .dotnet directory to avoid potential permission issues. | |
| if ((-not $globalJsonHasRuntimes) -and ($env:DOTNET_INSTALL_DIR -ne $null) -and (Test-Path(Join-Path $env:DOTNET_INSTALL_DIR "sdk\$dotnetSdkVersion"))) { | |
| $dotnetRoot = $env:DOTNET_INSTALL_DIR | |
| } else { | |
| $dotnetRoot = Join-Path $RepoRoot '.dotnet' | |
| if (-not (Test-Path(Join-Path $dotnetRoot "sdk\$dotnetSdkVersion"))) { | |
| if ($install) { | |
| InstallDotNetSdk $dotnetRoot $dotnetSdkVersion | |
| } else { | |
| Write-PipelineTelemetryError -Category 'InitializeToolset' -Message "Unable to find dotnet with SDK version '$dotnetSdkVersion'" | |
| ExitWithExitCode 1 | |
| } | |
| } | |
| $env:DOTNET_INSTALL_DIR = $dotnetRoot | |
| if ($createSdkLocationFile) { | |
| # Create a temporary file under the toolset dir and rename it to sdk.txt to avoid races. | |
| do { | |
| $sdkCacheFileTemp = Join-Path $ToolsetDir $([System.IO.Path]::GetRandomFileName()) | |
| } | |
| until (!(Test-Path $sdkCacheFileTemp)) | |
| Set-Content -Path $sdkCacheFileTemp -Value $dotnetRoot | |
| try { | |
| Rename-Item -Force -Path $sdkCacheFileTemp 'sdk.txt' | |
| } catch { | |
| # Somebody beat us | |
| Remove-Item -Path $sdkCacheFileTemp | |
| } | |
| } | |
| } |
If we didn't get a chance to reach the comparison on line 150 (e.g. we entered the branch of line 135), then we will not create the sdk.txt. If sdk.txt was never created (e.g. a clean clone), then the build would fail with a file not found error.
This is not just theoretical, this could actually happen if dotnet.cmd is invoked inside the build process (e.g. when build.cmd of coreclr trying to publish crossgen2).
Line 659 in 496c2a6
| call %__RepoRootDir%\dotnet.cmd publish --self-contained -r win-%__BuildArch% -c %__BuildType% -o "%__BinDir%\crossgen2" "%__ProjectDir%\src\tools\crossgen2\crossgen2\crossgen2.csproj" /p:BuildArch=%__BuildArch% |
In that case, the DOTNET_INSTALL_DIR environment variable is already set somewhere upstream, therefore the script actually enters line 135 and never creates the sdk.txt file, leading to downstream failures.
IMHO, we should fix tools.ps1 so that it ensures sdk.txt is created under all conditions.
Activity
Move SDK file location creation to right branch
ViktorHofer commentedon Dec 16, 2019
Submitted a PR to fix this dotnet/arcade#4546
ViktorHofer commentedon Dec 16, 2019
Thanks for reporting @cshung
Move SDK file location creation to right branch (#4546)