Fix cargo command not found after Rust installation
This commit is contained in:
4
bin/builders/BaseBuilder.ts
vendored
4
bin/builders/BaseBuilder.ts
vendored
@@ -4,7 +4,7 @@ import chalk from 'chalk';
|
||||
import prompts from 'prompts';
|
||||
|
||||
import { PakeAppOptions } from '@/types';
|
||||
import { checkRustInstalled, installRust } from '@/helpers/rust';
|
||||
import { checkRustInstalled, ensureRustEnv, installRust } from '@/helpers/rust';
|
||||
import { mergeConfig } from '@/helpers/merge';
|
||||
import tauriConfig from '@/helpers/tauriConfig';
|
||||
import { generateIdentifierSafeName } from '@/utils/name';
|
||||
@@ -77,6 +77,8 @@ export default abstract class BaseBuilder {
|
||||
logger.warn('✼ See more in https://tauri.app/start/prerequisites/.');
|
||||
}
|
||||
|
||||
ensureRustEnv();
|
||||
|
||||
if (!checkRustInstalled()) {
|
||||
const res = await prompts({
|
||||
type: 'confirm',
|
||||
|
||||
63
bin/helpers/rust.ts
vendored
63
bin/helpers/rust.ts
vendored
@@ -1,3 +1,6 @@
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fsExtra from 'fs-extra';
|
||||
import chalk from 'chalk';
|
||||
import { execaSync } from 'execa';
|
||||
|
||||
@@ -6,6 +9,64 @@ import { IS_WIN } from '@/utils/platform';
|
||||
import { shellExec } from '@/utils/shell';
|
||||
import { isChinaDomain } from '@/utils/ip';
|
||||
|
||||
function normalizePathForComparison(targetPath: string) {
|
||||
const normalized = path.normalize(targetPath);
|
||||
return IS_WIN ? normalized.toLowerCase() : normalized;
|
||||
}
|
||||
|
||||
function getCargoHomeCandidates(): string[] {
|
||||
const candidates = new Set<string>();
|
||||
if (process.env.CARGO_HOME) {
|
||||
candidates.add(process.env.CARGO_HOME);
|
||||
}
|
||||
const homeDir = os.homedir();
|
||||
if (homeDir) {
|
||||
candidates.add(path.join(homeDir, '.cargo'));
|
||||
}
|
||||
if (IS_WIN && process.env.USERPROFILE) {
|
||||
candidates.add(path.join(process.env.USERPROFILE, '.cargo'));
|
||||
}
|
||||
return Array.from(candidates).filter(Boolean);
|
||||
}
|
||||
|
||||
function ensureCargoBinOnPath() {
|
||||
const currentPath = process.env.PATH || '';
|
||||
const segments = currentPath.split(path.delimiter).filter(Boolean);
|
||||
const normalizedSegments = new Set(
|
||||
segments.map((segment) => normalizePathForComparison(segment)),
|
||||
);
|
||||
|
||||
const additions: string[] = [];
|
||||
let cargoHomeSet = Boolean(process.env.CARGO_HOME);
|
||||
|
||||
for (const cargoHome of getCargoHomeCandidates()) {
|
||||
const binDir = path.join(cargoHome, 'bin');
|
||||
if (
|
||||
fsExtra.pathExistsSync(binDir) &&
|
||||
!normalizedSegments.has(normalizePathForComparison(binDir))
|
||||
) {
|
||||
additions.push(binDir);
|
||||
normalizedSegments.add(normalizePathForComparison(binDir));
|
||||
}
|
||||
|
||||
if (!cargoHomeSet && fsExtra.pathExistsSync(cargoHome)) {
|
||||
process.env.CARGO_HOME = cargoHome;
|
||||
cargoHomeSet = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (additions.length) {
|
||||
const prefix = additions.join(path.delimiter);
|
||||
process.env.PATH = segments.length
|
||||
? `${prefix}${path.delimiter}${segments.join(path.delimiter)}`
|
||||
: prefix;
|
||||
}
|
||||
}
|
||||
|
||||
export function ensureRustEnv() {
|
||||
ensureCargoBinOnPath();
|
||||
}
|
||||
|
||||
export async function installRust() {
|
||||
const isActions = process.env.GITHUB_ACTIONS;
|
||||
const isInChina = await isChinaDomain('sh.rustup.rs');
|
||||
@@ -22,6 +83,7 @@ export async function installRust() {
|
||||
IS_WIN ? rustInstallScriptForWindows : rustInstallScriptForMac,
|
||||
);
|
||||
spinner.succeed(chalk.green('✔ Rust installed successfully!'));
|
||||
ensureRustEnv();
|
||||
} catch (error) {
|
||||
spinner.fail(chalk.red('✕ Rust installation failed!'));
|
||||
console.error(error.message);
|
||||
@@ -30,6 +92,7 @@ export async function installRust() {
|
||||
}
|
||||
|
||||
export function checkRustInstalled() {
|
||||
ensureCargoBinOnPath();
|
||||
try {
|
||||
execaSync('rustc', ['--version']);
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user