versionRust.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. };
  31. const fallbackVersion =
  32. solanaToRustMap[solanaVersion.withoutPatch] ?? '1.75.0';
  33. const version = inputVersion ?? detectedVersion ?? fallbackVersion;
  34. assertIsValidVersion(language, 'Rust', version);
  35. const [full, withoutPatch] = getVersionAndVersionWithoutPatch(version);
  36. const rustVersion = { full, withoutPatch, detected: detectedVersion };
  37. warnAboutSolanaRustVersionMismatch(language, rustVersion, solanaVersion);
  38. return rustVersion;
  39. }
  40. function warnAboutSolanaRustVersionMismatch(
  41. language: Language,
  42. rustVersion: ResolvedVersion,
  43. solanaVersion: ResolvedVersion
  44. ) {
  45. const minimumViableRustVersionPerSolanaVersion: Record<
  46. VersionWithoutPatch,
  47. Version
  48. > = {
  49. '1.17': '1.68.0',
  50. '1.18': '1.75.0',
  51. '2.0': '1.75.0',
  52. };
  53. const minimumViableRustVersion: Version | undefined =
  54. minimumViableRustVersionPerSolanaVersion[solanaVersion.withoutPatch];
  55. if (!minimumViableRustVersion) return;
  56. if (compareVersions(rustVersion.full, minimumViableRustVersion) < 0) {
  57. logWarning(
  58. language.errors.rustVersionIncompatibleWithSolana
  59. .replace('$solanaVersion', solanaVersion.withoutPatch)
  60. .replace('$minimumRustVersion', minimumViableRustVersion)
  61. .replace('$rustVersion', rustVersion.full)
  62. );
  63. }
  64. }