versionRust.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { hasCommand, spawnCommand } from './commands';
  2. import { Language } from './localization';
  3. import { logWarning } from './logs';
  4. import {
  5. assertIsValidVersion,
  6. compareVersions,
  7. getVersionAndVersionWithoutPatch,
  8. getVersionFromStdout,
  9. ResolvedVersion,
  10. Version,
  11. VersionWithoutPatch,
  12. } from './versionCore';
  13. export async function detectRustVersion(): Promise<Version | undefined> {
  14. const hasRustc = await hasCommand('rustc');
  15. if (!hasRustc) {
  16. return undefined;
  17. }
  18. return getVersionFromStdout(spawnCommand('rustc', ['--version']));
  19. }
  20. export function resolveRustVersion(
  21. language: Language,
  22. solanaVersion: ResolvedVersion,
  23. inputVersion: string | undefined,
  24. detectedVersion: Version | undefined
  25. ): ResolvedVersion {
  26. const solanaToRustMap: Record<VersionWithoutPatch, Version> = {
  27. '1.17': '1.75.0',
  28. '1.18': '1.75.0',
  29. '2.0': '1.75.0',
  30. '2.1': '1.79.0',
  31. '2.2': '1.79.0',
  32. };
  33. const fallbackVersion =
  34. solanaToRustMap[solanaVersion.withoutPatch] ?? '1.79.0';
  35. const version = inputVersion ?? detectedVersion ?? fallbackVersion;
  36. assertIsValidVersion(language, 'Rust', version);
  37. const [full, withoutPatch] = getVersionAndVersionWithoutPatch(version);
  38. const rustVersion = { full, withoutPatch, detected: detectedVersion };
  39. warnAboutSolanaRustVersionMismatch(language, rustVersion, solanaVersion);
  40. return rustVersion;
  41. }
  42. function warnAboutSolanaRustVersionMismatch(
  43. language: Language,
  44. rustVersion: ResolvedVersion,
  45. solanaVersion: ResolvedVersion
  46. ) {
  47. const minimumViableRustVersionPerSolanaVersion: Record<
  48. VersionWithoutPatch,
  49. Version
  50. > = {
  51. '1.17': '1.68.0',
  52. '1.18': '1.75.0',
  53. '2.0': '1.75.0',
  54. '2.1': '1.79.0',
  55. '2.2': '1.79.0',
  56. };
  57. const minimumViableRustVersion: Version | undefined =
  58. minimumViableRustVersionPerSolanaVersion[solanaVersion.withoutPatch];
  59. if (!minimumViableRustVersion) return;
  60. if (compareVersions(rustVersion.full, minimumViableRustVersion) < 0) {
  61. logWarning(
  62. language.errors.rustVersionIncompatibleWithSolana
  63. .replace('$solanaVersion', solanaVersion.withoutPatch)
  64. .replace('$minimumRustVersion', minimumViableRustVersion)
  65. .replace('$rustVersion', rustVersion.full)
  66. );
  67. }
  68. }