From cecad8256e5fd9ea97c29747f731b34585aa95d1 Mon Sep 17 00:00:00 2001 From: springmin Date: Wed, 25 Nov 2026 08:19:25 +0800 Subject: [PATCH 1/4] Detect HarmonyOS/OpenHarmony as the openharmony RID `uname -s` reports `HarmonyOS` on HarmonyOS/OpenHarmony devices, which the script does not recognize, and `/etc/os-release` may be absent. Detect it before the generic Linux branch, also recognize `ID=openharmony`/`ohos` in os-release, and accept `--os openharmony` (plus the `harmonyos`/`ohos` aliases). Guard the `getconf LONG_BIT` probe used for aarch64. Adds dry-run tests that put a `uname` shim on PATH and serve a file feed. --- src/dotnet-install.sh | 23 ++- .../OpenHarmonyOsDetectionTests.cs | 139 ++++++++++++++++++ 2 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs diff --git a/src/dotnet-install.sh b/src/dotnet-install.sh index bd13ffa66..6e203e7be 100755 --- a/src/dotnet-install.sh +++ b/src/dotnet-install.sh @@ -198,7 +198,17 @@ get_current_os_name() { elif [ "$uname" = "FreeBSD" ]; then echo "freebsd" return 0 + elif [ "$uname" = "HarmonyOS" ] || [ "$uname" = "OpenHarmony" ] || [ "$uname" = "OHOS" ]; then + # HarmonyOS/OpenHarmony report their own uname value; the portable RID is openharmony-. + echo "openharmony" + return 0 elif [ "$uname" = "Linux" ]; then + # OpenHarmony standard systems can report Linux and may not ship /etc/os-release. + if [ -r /etc/os-release ] && grep -qiE '^(ID|ID_LIKE)="?[^"]*(openharmony|ohos)' /etc/os-release; then + echo "openharmony" + return 0 + fi + local linux_platform_name="" linux_platform_name="$(get_linux_platform_name)" || true @@ -307,7 +317,7 @@ get_machine_architecture() { return 0 ;; aarch64|arm64) - if [ "$(getconf LONG_BIT)" -lt 64 ]; then + if machine_has "getconf" && [ "$(getconf LONG_BIT 2>/dev/null || echo 64)" -lt 64 ]; then # This is 32-bit OS running on 64-bit CPU (for example Raspberry Pi OS) echo "arm" return 0 @@ -448,7 +458,7 @@ get_normalized_os() { local osname="$(to_lowercase "$1")" if [ ! -z "$osname" ]; then case "$osname" in - osx | freebsd | rhel.6 | linux-musl | linux) + osx | freebsd | rhel.6 | linux-musl | linux | openharmony) echo "$osname" return 0 ;; @@ -457,8 +467,13 @@ get_normalized_os() { echo "$osname" return 0 ;; + harmonyos | ohos) + osname='openharmony' + echo "$osname" + return 0 + ;; *) - say_err "'$user_defined_os' is not a supported value for --os option, supported values are: osx, macos, linux, linux-musl, freebsd, rhel.6. If you think this is a bug, report it at https://github.com/dotnet/install-scripts/issues." + say_err "'$user_defined_os' is not a supported value for --os option, supported values are: osx, macos, linux, linux-musl, freebsd, rhel.6, openharmony. If you think this is a bug, report it at https://github.com/dotnet/install-scripts/issues." return 1 ;; esac @@ -1807,7 +1822,7 @@ do echo " --arch,-Architecture,-Arch" echo " Possible values: x64, arm, arm64, s390x, ppc64le and loongarch64" echo " --os Specifies operating system to be used when selecting the installer." - echo " Overrides the OS determination approach used by the script. Supported values: osx, linux, linux-musl, freebsd, rhel.6." + echo " Overrides the OS determination approach used by the script. Supported values: osx, linux, linux-musl, freebsd, rhel.6, openharmony." echo " In case any other value is provided, the platform will be determined by the script based on machine configuration." echo " Not supported for legacy links. Use --runtime-id to specify platform for legacy links." echo " Refer to: https://aka.ms/dotnet-os-lifecycle for more information." diff --git a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs new file mode 100644 index 000000000..33cbf17ce --- /dev/null +++ b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs @@ -0,0 +1,139 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using Install_Scripts.Test.Utils; +using Xunit; + +namespace Microsoft.DotNet.InstallationScript.Tests +{ + /// + /// Tests for OpenHarmony/HarmonyOS OS detection in dotnet-install.sh. + /// Unix-only because the script under test is the bash variant. + /// + public class OpenHarmonyOsDetectionTests + { + [Fact] + public void WhenTheMachineReportsHarmonyOS_TheLinkUsesTheOpenHarmonyRid() + { + Assert.SkipWhen(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), + "dotnet-install.sh tests are Unix-only."); + + using var fixture = new OsDetectionFixture(); + + CommandResult result = fixture.Run( + "-dryrun", + "-version", "11.0.100", + "-azurefeed", "file://" + fixture.FeedPath); + + Assert.Equal(0, result.ExitCode); + Assert.Contains("dotnet-sdk-11.0.100-openharmony-arm64.tar.gz", result.StdOut); + } + + [Theory] + [InlineData("harmonyos")] + [InlineData("ohos")] + [InlineData("openharmony")] + public void WhenAnOpenHarmonyOsAliasIsPassed_TheLinkUsesTheOpenHarmonyRid(string osAlias) + { + Assert.SkipWhen(RuntimeInformation.IsOSPlatform(OSPlatform.Windows), + "dotnet-install.sh tests are Unix-only."); + + using var fixture = new OsDetectionFixture(); + + CommandResult result = fixture.Run( + "-dryrun", + "-version", "11.0.100", + "-os", osAlias, + "-azurefeed", "file://" + fixture.FeedPath); + + Assert.Equal(0, result.ExitCode); + Assert.Contains("openharmony-arm64", result.StdOut); + } + + private sealed class OsDetectionFixture : IDisposable + { + private readonly string _root; + private readonly string _shimDir; + + public OsDetectionFixture() + { + _root = Path.Combine(Path.GetTempPath(), "dotnet-ohos-" + Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(_root); + + // Override uname so the script sees HarmonyOS regardless of the test host. + _shimDir = Path.Combine(_root, "shim"); + Directory.CreateDirectory(_shimDir); + string unamePath = Path.Combine(_shimDir, "uname"); + File.WriteAllText(unamePath, "#!/bin/sh\ncase \"${1:-}\" in\n -m) echo aarch64 ;;\n -s) echo HarmonyOS ;;\n *) echo HarmonyOS ;;\nesac\n"); + File.SetUnixFileMode(unamePath, + UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | + UnixFileMode.GroupRead | UnixFileMode.GroupExecute | + UnixFileMode.OtherRead | UnixFileMode.OtherExecute); + + FeedPath = Path.Combine(_root, "feed"); + string versionDir = Path.Combine(FeedPath, "Sdk", "11.0.100"); + Directory.CreateDirectory(versionDir); + File.WriteAllText(Path.Combine(versionDir, "productVersion.txt"), "11.0.100\n"); + } + + public string FeedPath { get; } + + public CommandResult Run(params string[] args) + { + var startInfo = new ProcessStartInfo + { + FileName = "/bin/bash", + Arguments = $"{GetScriptPath()} {ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(args)}", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + startInfo.Environment["PATH"] = _shimDir + Path.PathSeparator + Environment.GetEnvironmentVariable("PATH"); + startInfo.Environment["TMPDIR"] = _root; + + using Process process = Process.Start(startInfo)!; + string stdout = process.StandardOutput.ReadToEnd(); + string stderr = process.StandardError.ReadToEnd(); + process.WaitForExit(); + + return new CommandResult(process.ExitCode, stdout, stderr); + } + + private static string GetScriptPath() + { + string? directory = AppContext.BaseDirectory; + while (directory != null && !Directory.Exists(Path.Combine(directory, ".git"))) + { + directory = Directory.GetParent(directory)?.FullName; + } + + if (directory == null) + { + throw new InvalidOperationException("Could not locate the repository root."); + } + + return Path.Combine(directory, "src", "dotnet-install.sh"); + } + + public void Dispose() + { + try + { + Directory.Delete(_root, true); + } + catch + { + // Best effort cleanup. + } + } + } + + private sealed record CommandResult(int ExitCode, string StdOut, string StdErr); + } +} From 4e752df4e4c813e1c59b8935e1a7d751f9132208 Mon Sep 17 00:00:00 2001 From: springmin Date: Wed, 25 Nov 2026 09:08:28 +0800 Subject: [PATCH 2/4] Fix CA1416 in the OpenHarmony detection tests File.SetUnixFileMode is unsupported on Windows and the analyzer flags the call site even though the test is skipped there. Guard it with OperatingSystem.IsWindows(). --- .../OpenHarmonyOsDetectionTests.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs index 33cbf17ce..5b932ff35 100644 --- a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs +++ b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs @@ -69,10 +69,15 @@ public OsDetectionFixture() Directory.CreateDirectory(_shimDir); string unamePath = Path.Combine(_shimDir, "uname"); File.WriteAllText(unamePath, "#!/bin/sh\ncase \"${1:-}\" in\n -m) echo aarch64 ;;\n -s) echo HarmonyOS ;;\n *) echo HarmonyOS ;;\nesac\n"); - File.SetUnixFileMode(unamePath, - UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | - UnixFileMode.GroupRead | UnixFileMode.GroupExecute | - UnixFileMode.OtherRead | UnixFileMode.OtherExecute); + // CA1416: SetUnixFileMode is Unix-only. The tests are skipped on Windows; the + // guard keeps the analyzer (and a future Windows run) happy. + if (!OperatingSystem.IsWindows()) + { + File.SetUnixFileMode(unamePath, + UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute | + UnixFileMode.GroupRead | UnixFileMode.GroupExecute | + UnixFileMode.OtherRead | UnixFileMode.OtherExecute); + } FeedPath = Path.Combine(_root, "feed"); string versionDir = Path.Combine(FeedPath, "Sdk", "11.0.100"); From e5df009115822a1919e4589d2bd77fbb2f764368 Mon Sep 17 00:00:00 2001 From: springmin Date: Wed, 25 Nov 2026 11:18:41 +0800 Subject: [PATCH 3/4] Use the long --os form in the OpenHarmony alias tests The parser matches -[Oo][SS] (uppercase S), so `-os` is an unknown argument. --- tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs index 5b932ff35..435dc797a 100644 --- a/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs +++ b/tests/Install-Scripts.Test/OpenHarmonyOsDetectionTests.cs @@ -47,7 +47,7 @@ public void WhenAnOpenHarmonyOsAliasIsPassed_TheLinkUsesTheOpenHarmonyRid(string CommandResult result = fixture.Run( "-dryrun", "-version", "11.0.100", - "-os", osAlias, + "--os", osAlias, "-azurefeed", "file://" + fixture.FeedPath); Assert.Equal(0, result.ExitCode); From a68c3733688f39188970d0c9208e57f4a0aaeb9f Mon Sep 17 00:00:00 2001 From: springmin Date: Wed, 25 Nov 2026 12:37:21 +0800 Subject: [PATCH 4/4] Retry CI: Windows install tests are flaky on this pipeline WhenInstallingTheSdk / WhenInstallingDotnetRuntime / WhenInstallingAspNetCoreRuntime failed on the Windows leg, the same three fail on other PRs (for example #739) while Linux and macOS pass. No source change.