Skip to content

tools.ps1 should ensure sdk.txt is always created if createSdkLocationFile is true #871

Closed
dotnet/arcade
#4546

Description

@cshung

I think I have spotted a bug in the dotnet.cmd/tools.ps1 interaction:

Here is the relevant content of dotnet.cmd:

runtime/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.

# 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).

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

  1. added
    untriaged
    on Dec 14, 2019
  2. ViktorHofer commented on Dec 16, 2019

    @ViktorHofer
    Member

    Submitted a PR to fix this dotnet/arcade#4546

  3. removed
    untriaged
    on Dec 16, 2019
  4. added this to the 5.0 milestone on Dec 16, 2019
  5. ViktorHofer commented on Dec 16, 2019

    @ViktorHofer
    Member

    Thanks for reporting @cshung

  6. ghost locked as resolved and limited conversation to collaborators on Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions