Skip to content

Commit

Permalink
Merge pull request rust-lang#365 from segevfiner/fix-active-toolchain…
Browse files Browse the repository at this point in the history
…-detection

Fix active toolchain detection
  • Loading branch information
Xanewok authored Jul 12, 2018
2 parents 6630159 + 3219978 commit ebf3bf1
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/rustup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,27 @@ export function parseActiveToolchain(rustupOutput: string): string {
// There may a default entry under 'installed toolchains' section, so search
// for currently active/overridden one only under 'active toolchain' section
const activeToolchainsIndex = rustupOutput.search('active toolchain');
if (activeToolchainsIndex === -1) {
throw new Error(`couldn't find active toolchains`);
}
if (activeToolchainsIndex !== -1) {
rustupOutput = rustupOutput.substr(activeToolchainsIndex);

const matchActiveChannel = /^(\S*) \((?:default|overridden)/gm;
const match = matchActiveChannel.exec(rustupOutput);
if (match === null) {
throw new Error(`couldn't find active toolchain under 'active toolchains'`);
} else if (matchActiveChannel.exec(rustupOutput) !== null) {
throw new Error(`multiple active toolchains found under 'active toolchains'`);
}

rustupOutput = rustupOutput.substr(activeToolchainsIndex);
return match[1];
}

const matchActiveChannel = new RegExp(/^(\S*) \((?:default|overridden)/gm);
const match = matchActiveChannel.exec(rustupOutput);
if (match === null) {
throw new Error(`couldn't find active toolchain under 'active toolchains'`);
} else if (match.length > 2) {
throw new Error(`multiple active toolchains found under 'active toolchains'`);
// Try matching the third line as the active toolchain
const match = /^(?:.*\r?\n){2}(\S*) \((?:default|overridden)/.exec(rustupOutput);
if (match !== null) {
return match[1];
}

return match[1];
throw new Error(`couldn't find active toolchains`);
}

/**
Expand All @@ -219,9 +225,17 @@ export function getActiveChannel(rustupPath: string, wsPath: string): string {
// rustup info might differ depending on where it's executed
// (e.g. when a toolchain is locally overriden), so executing it
// under our current workspace root should give us close enough result
const output = child_process.execSync(`${rustupPath} show`, { cwd: wsPath }).toString();

const activeChannel = parseActiveToolchain(output);
let activeChannel;
try {
// `rustup show active-toolchain` is available since rustup 1.12.0
activeChannel = child_process.execSync(`${rustupPath} show active-toolchain`, { cwd: wsPath }).toString().trim();
} catch (e) {
// Possibly an old rustup version, so try rustup show
const showOutput = child_process.execSync(`${rustupPath} show`, { cwd: wsPath }).toString();
activeChannel = parseActiveToolchain(showOutput);
}

console.info(`Detected active channel: ${activeChannel} (since 'rust-client.channel' is unspecified)`);
return activeChannel;
}

0 comments on commit ebf3bf1

Please sign in to comment.