| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import { hasCommand, spawnCommand } from './commands';
- import { Language } from './localization';
- import { logWarning } from './logs';
- import {
- assertIsValidVersion,
- compareVersions,
- getVersionAndVersionWithoutPatch,
- getVersionFromStdout,
- ResolvedVersion,
- Version,
- VersionWithoutPatch,
- } from './versionCore';
- export async function detectRustVersion(): Promise<Version | undefined> {
- const hasRustc = await hasCommand('rustc');
- if (!hasRustc) {
- return undefined;
- }
- return getVersionFromStdout(spawnCommand('rustc', ['--version']));
- }
- export function resolveRustVersion(
- language: Language,
- solanaVersion: ResolvedVersion,
- inputVersion: string | undefined,
- detectedVersion: Version | undefined
- ): ResolvedVersion {
- const solanaToRustMap: Record<VersionWithoutPatch, Version> = {
- '1.17': '1.75.0',
- '1.18': '1.75.0',
- '2.0': '1.75.0',
- '2.1': '1.79.0',
- '2.2': '1.79.0',
- };
- const fallbackVersion =
- solanaToRustMap[solanaVersion.withoutPatch] ?? '1.79.0';
- const version = inputVersion ?? detectedVersion ?? fallbackVersion;
- assertIsValidVersion(language, 'Rust', version);
- const [full, withoutPatch] = getVersionAndVersionWithoutPatch(version);
- const rustVersion = { full, withoutPatch, detected: detectedVersion };
- warnAboutSolanaRustVersionMismatch(language, rustVersion, solanaVersion);
- return rustVersion;
- }
- function warnAboutSolanaRustVersionMismatch(
- language: Language,
- rustVersion: ResolvedVersion,
- solanaVersion: ResolvedVersion
- ) {
- const minimumViableRustVersionPerSolanaVersion: Record<
- VersionWithoutPatch,
- Version
- > = {
- '1.17': '1.68.0',
- '1.18': '1.75.0',
- '2.0': '1.75.0',
- '2.1': '1.79.0',
- '2.2': '1.79.0',
- };
- const minimumViableRustVersion: Version | undefined =
- minimumViableRustVersionPerSolanaVersion[solanaVersion.withoutPatch];
- if (!minimumViableRustVersion) return;
- if (compareVersions(rustVersion.full, minimumViableRustVersion) < 0) {
- logWarning(
- language.errors.rustVersionIncompatibleWithSolana
- .replace('$solanaVersion', solanaVersion.withoutPatch)
- .replace('$minimumRustVersion', minimumViableRustVersion)
- .replace('$rustVersion', rustVersion.full)
- );
- }
- }
|