Skip to content

Commit 304d812

Browse files
fix: fall back to portable RID for bundled CLI lookup on Linux (#424) (#482)
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 8598dc3 commit 304d812

2 files changed

Lines changed: 40 additions & 9 deletions

File tree

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: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,27 @@
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 portable RID from explicit RuntimeIdentifier or build host -->
77
<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>
8+
<!-- Determine OS: from RID prefix if set, otherwise from build host -->
9+
<_CopilotOs Condition="'$(RuntimeIdentifier)' != '' And $(RuntimeIdentifier.StartsWith('win'))">win</_CopilotOs>
10+
<_CopilotOs Condition="'$(_CopilotOs)' == '' And '$(RuntimeIdentifier)' != '' And $(RuntimeIdentifier.StartsWith('osx'))">osx</_CopilotOs>
11+
<_CopilotOs Condition="'$(_CopilotOs)' == '' And '$(RuntimeIdentifier)' != ''">linux</_CopilotOs>
12+
13+
<!-- Determine arch: from RID suffix if set, otherwise from build host -->
14+
<_CopilotArch Condition="'$(RuntimeIdentifier)' != '' And $(RuntimeIdentifier.EndsWith('-x64'))">x64</_CopilotArch>
15+
<_CopilotArch Condition="'$(_CopilotArch)' == '' And '$(RuntimeIdentifier)' != '' And $(RuntimeIdentifier.EndsWith('-arm64'))">arm64</_CopilotArch>
16+
17+
<!-- When no RuntimeIdentifier is set, use the SDK's portable RID for the build host -->
18+
<_CopilotRid Condition="'$(_CopilotOs)' != '' And '$(_CopilotArch)' != ''">$(_CopilotOs)-$(_CopilotArch)</_CopilotRid>
19+
<_CopilotRid Condition="'$(_CopilotRid)' == '' And '$(RuntimeIdentifier)' == ''">$(NETCoreSdkPortableRuntimeIdentifier)</_CopilotRid>
1520
</PropertyGroup>
1621

22+
<!-- Fail if we couldn't determine a portable RID from the given RuntimeIdentifier -->
23+
<Target Name="_ValidateCopilotRid" BeforeTargets="BeforeBuild" Condition="'$(RuntimeIdentifier)' != '' And '$(_CopilotRid)' == ''">
24+
<Error Text="Could not determine a supported portable RID from RuntimeIdentifier '$(RuntimeIdentifier)'. Supported RIDs: win-x64, win-arm64, linux-x64, linux-arm64, osx-x64, osx-arm64." />
25+
</Target>
26+
1727
<!-- Map RID to platform name used in npm packages -->
1828
<PropertyGroup>
1929
<_CopilotPlatform Condition="'$(_CopilotRid)' == 'win-x64'">win32-x64</_CopilotPlatform>

0 commit comments

Comments
 (0)