Skip to content

Commit 46cc2f8

Browse files
fix: fall back to portable RID for bundled CLI lookup on Linux (#424)
On Linux distros that install .NET from distribution packages (Ubuntu, Fedora, RHEL, etc.), RuntimeInformation.RuntimeIdentifier returns distro-specific RIDs like ubuntu.24.04-x64 instead of the portable linux-x64. The bundled CLI is placed under runtimes/linux-x64/native/, so the lookup fails and throws. Fix both the runtime lookup and build-time RID resolution: - Client.cs: GetBundledCliPath now falls back to the portable RID (e.g., linux-x64) when the distro-specific RID path doesn't exist. - GitHub.Copilot.SDK.targets: Always use portable RIDs derived from OS/architecture detection instead of the project's RuntimeIdentifier, which may be distro-specific. Fixes #424 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent e40d57c commit 46cc2f8

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

dotnet/src/Client.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,11 +997,32 @@ private async Task VerifyProtocolVersionAsync(Connection connection, Cancellatio
997997
private static string? GetBundledCliPath(out string searchedPath)
998998
{
999999
var binaryName = OperatingSystem.IsWindows() ? "copilot.exe" : "copilot";
1000-
var rid = Path.GetFileName(System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier);
1000+
// Always use portable RID (e.g., linux-x64) to match the build-time placement,
1001+
// since distro-specific RIDs (e.g., ubuntu.24.04-x64) are normalized at build time.
1002+
var rid = GetPortableRid()
1003+
?? Path.GetFileName(System.Runtime.InteropServices.RuntimeInformation.RuntimeIdentifier);
10011004
searchedPath = Path.Combine(AppContext.BaseDirectory, "runtimes", rid, "native", binaryName);
10021005
return File.Exists(searchedPath) ? searchedPath : null;
10031006
}
10041007

1008+
private static string? GetPortableRid()
1009+
{
1010+
string os;
1011+
if (OperatingSystem.IsWindows()) os = "win";
1012+
else if (OperatingSystem.IsLinux()) os = "linux";
1013+
else if (OperatingSystem.IsMacOS()) os = "osx";
1014+
else return null;
1015+
1016+
var arch = System.Runtime.InteropServices.RuntimeInformation.OSArchitecture switch
1017+
{
1018+
System.Runtime.InteropServices.Architecture.X64 => "x64",
1019+
System.Runtime.InteropServices.Architecture.Arm64 => "arm64",
1020+
_ => null,
1021+
};
1022+
1023+
return arch != null ? $"{os}-{arch}" : null;
1024+
}
1025+
10051026
private static (string FileName, IEnumerable<string> Args) ResolveCliCommand(string cliPath, IEnumerable<string> args)
10061027
{
10071028
var isJsFile = cliPath.EndsWith(".js", StringComparison.OrdinalIgnoreCase);

dotnet/src/build/GitHub.Copilot.SDK.targets

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@
33
<!-- CopilotCliVersion is imported from GitHub.Copilot.SDK.props (generated at SDK build time, packaged alongside) -->
44
<Import Project="$(MSBuildThisFileDirectory)GitHub.Copilot.SDK.props" Condition="'$(CopilotCliVersion)' == '' And Exists('$(MSBuildThisFileDirectory)GitHub.Copilot.SDK.props')" />
55

6-
<!-- Resolve RID: use explicit RuntimeIdentifier, or infer from current machine -->
6+
<!-- Resolve RID: honor explicit portable RuntimeIdentifier for cross-platform builds,
7+
fall back to OS/arch detection only when RuntimeIdentifier is not set -->
78
<PropertyGroup>
8-
<_CopilotRid Condition="'$(RuntimeIdentifier)' != ''">$(RuntimeIdentifier)</_CopilotRid>
9-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">win-x64</_CopilotRid>
10-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">win-arm64</_CopilotRid>
11-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">linux-x64</_CopilotRid>
12-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">linux-arm64</_CopilotRid>
13-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">osx-x64</_CopilotRid>
14-
<_CopilotRid Condition="'$(_CopilotRid)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">osx-arm64</_CopilotRid>
9+
<!-- Use explicit RuntimeIdentifier if it matches a supported portable RID -->
10+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'win-x64'">win-x64</_CopilotRid>
11+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'win-arm64'">win-arm64</_CopilotRid>
12+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'linux-x64'">linux-x64</_CopilotRid>
13+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'linux-arm64'">linux-arm64</_CopilotRid>
14+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'osx-x64'">osx-x64</_CopilotRid>
15+
<_CopilotRid Condition="'$(RuntimeIdentifier)' == 'osx-arm64'">osx-arm64</_CopilotRid>
16+
<!-- Detect from build host only when no explicit RuntimeIdentifier is set -->
17+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">win-x64</_CopilotRid>
18+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('Windows')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">win-arm64</_CopilotRid>
19+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">linux-x64</_CopilotRid>
20+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('Linux')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">linux-arm64</_CopilotRid>
21+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64'">osx-x64</_CopilotRid>
22+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == '' And $([MSBuild]::IsOSPlatform('OSX')) And '$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'Arm64'">osx-arm64</_CopilotRid>
1523
</PropertyGroup>
1624

25+
<!-- Fail early if an explicit RID was given but not recognized -->
26+
<Target Name="_ValidateCopilotRid" BeforeTargets="BeforeBuild" Condition="'$(RuntimeIdentifier)' != '' And '$(_CopilotRid)' == ''">
27+
<Error Text="RuntimeIdentifier '$(RuntimeIdentifier)' is not a supported portable RID for the Copilot CLI. Supported RIDs: win-x64, win-arm64, linux-x64, linux-arm64, osx-x64, osx-arm64." />
28+
</Target>
29+
1730
<!-- Map RID to platform name used in npm packages -->
1831
<PropertyGroup>
1932
<_CopilotPlatform Condition="'$(_CopilotRid)' == 'win-x64'">win32-x64</_CopilotPlatform>

0 commit comments

Comments
 (0)