Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit 9e01d29

Browse files
committed
Refactor _checkToolchain split & use async/await
Reduce error/warn logging for non-errors/warnings
1 parent acebc2b commit 9e01d29

File tree

1 file changed

+129
-111
lines changed

1 file changed

+129
-111
lines changed

lib/index.js

Lines changed: 129 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ async function exec(command) {
3636
})
3737
}
3838

39+
function logErr(e, logFn=console.warn) {
40+
const message = e && '' + e
41+
message && logFn(message)
42+
}
43+
3944
function clearIdeRustInfos() {
4045
for (const note of atom.notifications.getNotifications()) {
4146
if (note.getOptions()._src === 'ide-rust') {
@@ -119,7 +124,7 @@ async function serverEnv(toolchain) {
119124
env.RUST_SRC_PATH = path.join(sysroot, "/lib/rustlib/src/rust/src/")
120125
}
121126
catch (e) {
122-
console.error("Failed to find sysroot: " + e)
127+
console.warn("Failed to find sysroot: " + e)
123128
}
124129
}
125130
return env
@@ -267,7 +272,7 @@ class RustLanguageClient extends AutoLanguageClient {
267272
.then(() => this._checkToolchain())
268273
.then(() => checkRls())
269274
.then(() => this._restartLanguageServers(`Updated Rls toolchain`))
270-
.catch(e => console.error(e))
275+
.catch(logErr)
271276

272277
if (this.busySignalService) {
273278
this.busySignalService.reportBusyWhile(
@@ -281,115 +286,128 @@ class RustLanguageClient extends AutoLanguageClient {
281286
}
282287

283288
/**
284-
* Checks for rustup and nightly toolchain
285-
* If not found, asks to install. If user declines, throws error
286-
* @param {BusySignalService} [busySignalService]
287-
* @return {Promise<*>} toolchain is ok
289+
* Checks for rustup, toolchain & rls components
290+
* If not found prompts to fix & throws error
288291
*/
289-
_checkToolchain(busySignalService) {
290-
return new Promise((resolve, reject) => {
291-
exec(`rustup run ${configToolchain()} rustc --version`)
292-
.then(resolve)
293-
.catch(() => checkHasRls(configToolchain()).then(async hasRls => {
294-
// Toolchain not found, prompt to install
295-
let toolchain = configToolchain()
296-
const title = `\`rustup\` missing ${toolchain} toolchain`
297-
298-
if (hasRls) {
299-
atomPrompt(title, {
300-
detail: `rustup toolchain install ${toolchain}`,
301-
buttons: [{
302-
text: 'Install',
303-
onDidClick: () => {
304-
clearIdeRustInfos()
305-
let installPromise = installCompiler()
306-
.then(() => this._checkToolchain())
307-
.then(() => this._restartLanguageServers(`Installed Rls toolchain`))
308-
.catch(e => {
309-
console.warn(e)
310-
clearIdeRustInfos()
311-
let err = (e + '').split('\n')
312-
err = err.length && err[0] || `Error installing rust \`${toolchain}\``
313-
atom.notifications.addError(err, {
314-
detail: 'Check the toolchain is valid & connection is available',
315-
dismissable: true
316-
})
317-
})
318-
319-
if (busySignalService) {
320-
busySignalService.reportBusyWhile(
321-
`Installing rust \`${toolchain}\``,
322-
() => installPromise
323-
)
324-
}
325-
}
326-
}],
327-
})
328-
}
329-
else {
330-
const note = {
331-
description: '**Warning**: This toolchain is unavilable or missing Rls.',
332-
buttons: [{
333-
text: 'Configure',
334-
onDidClick: () => atom.workspace.open('atom://config/packages/ide-rust')
335-
}],
336-
}
292+
async _checkToolchain() {
293+
const toolchain = configToolchain()
337294

338-
if (toolchain === 'nightly') {
339-
note.description += ' Try using a previous _dated_ nightly.'
340-
}
341-
else if (toolchain.startsWith('nightly')) {
342-
note.description += ' Try using another nightly version.'
343-
}
295+
try {
296+
await exec(`rustup run ${toolchain} rustc --version`)
297+
clearIdeRustInfos()
298+
}
299+
catch (e) {
300+
this._handleMissingToolchain(toolchain)
301+
throw e
302+
}
303+
}
304+
305+
/**
306+
* Takes appropriate action when missing a toolchain
307+
* @param {string} toolchain
308+
*/
309+
async _handleMissingToolchain(toolchain) {
310+
try {
311+
if (await checkHasRls(toolchain)) {
312+
let clicked = await atomPrompt(`\`rustup\` missing ${toolchain} toolchain`, {
313+
detail: `rustup toolchain install ${toolchain}`,
314+
}, ['Install'])
344315

345-
let suggestChannel = toolchain.startsWith('beta') && 'beta' ||
346-
toolchain.startsWith('stable') && 'stable' ||
347-
'nightly'
348-
349-
try {
350-
let suggestedVersion = await suggestChannelOrDated(suggestChannel)
351-
if (suggestedVersion) {
352-
note.buttons.push({
353-
text: `Use ${suggestedVersion}`,
354-
className: 'btn-success',
355-
onDidClick: () => {
356-
clearIdeRustInfos()
357-
atom.config.set('ide-rust.rlsToolchain', suggestedVersion)
358-
}
359-
})
360-
}
361-
} catch (e) {
316+
if (clicked === 'Install') {
317+
clearIdeRustInfos()
318+
const installPromise = installCompiler()
319+
.then(() => this._checkToolchain())
320+
.then(() => this._restartLanguageServers(`Installed Rls toolchain`))
321+
.catch(e => {
362322
console.warn(e)
363-
}
323+
clearIdeRustInfos()
324+
let err = (e + '').split('\n')
325+
err = err.length && err[0] || `Error installing rust \`${toolchain}\``
326+
atom.notifications.addError(err, {
327+
detail: 'Check the toolchain is valid & connection is available',
328+
dismissable: true
329+
})
330+
})
364331

365-
atomPrompt(title, note)
332+
if (this.busySignalService) {
333+
this.busySignalService.reportBusyWhile(
334+
`Installing rust \`${toolchain}\``,
335+
() => installPromise
336+
)
366337
}
367-
}))
368-
.catch(e => {
369-
e && console.warn(e)
370-
// Missing rustup, prompt to install
371-
atomPrompt(
372-
"`rustup` is not available",
373-
{
374-
description: "From https://www.rustup.rs/",
375-
detail: "curl https://sh.rustup.rs -sSf | sh"
376-
},
377-
["Install"]
378-
).then(response => {
379-
if (response === "Install") {
380-
// Install rustup and try again
381-
installRustup()
382-
.then(() => this._checkToolchain())
383-
.then(resolve)
384-
.catch(reject)
385-
} else {
386-
reject()
387-
}
388-
})
389-
})
390-
.then(() => reject())
391-
})
392-
.then(() => clearIdeRustInfos())
338+
}
339+
}
340+
else {
341+
this._handleMissingToolchainMissingRls(toolchain)
342+
}
343+
}
344+
catch (e) {
345+
logErr(e)
346+
this._handleMissingRustup()
347+
}
348+
}
349+
350+
/**
351+
* Takes appropriate action when missing a toolchain that itself is missing
352+
* or missing vital components
353+
* @param {string} toolchain
354+
*/
355+
async _handleMissingToolchainMissingRls(toolchain) {
356+
const note = {
357+
description: '**Warning**: This toolchain is unavilable or missing Rls.',
358+
buttons: [{
359+
text: 'Configure',
360+
onDidClick: () => atom.workspace.open('atom://config/packages/ide-rust')
361+
}],
362+
}
363+
364+
if (toolchain === 'nightly') {
365+
note.description += ' Try using a previous _dated_ nightly.'
366+
}
367+
else if (toolchain.startsWith('nightly')) {
368+
note.description += ' Try using another nightly version.'
369+
}
370+
371+
let suggestChannel = toolchain.startsWith('beta') && 'beta' ||
372+
toolchain.startsWith('stable') && 'stable' ||
373+
'nightly'
374+
375+
try {
376+
let suggestedVersion = await suggestChannelOrDated(suggestChannel)
377+
if (suggestedVersion) {
378+
note.buttons.push({
379+
text: `Use ${suggestedVersion}`,
380+
className: 'btn-success',
381+
onDidClick: () => {
382+
clearIdeRustInfos()
383+
atom.config.set('ide-rust.rlsToolchain', suggestedVersion)
384+
}
385+
})
386+
}
387+
} catch (e) {
388+
console.warn(e)
389+
}
390+
391+
atomPrompt(`\`rustup\` missing ${toolchain} toolchain`, note)
392+
}
393+
394+
/** Takes appropriate action when missing rustup */
395+
async _handleMissingRustup() {
396+
try {
397+
let clicked = await atomPrompt("`rustup` is not available", {
398+
description: "From https://www.rustup.rs/",
399+
detail: "curl https://sh.rustup.rs -sSf | sh"
400+
}, ["Install"])
401+
402+
if (clicked === "Install") {
403+
// Install rustup and try again
404+
await installRustup()
405+
this._checkToolchain().catch(logErr)
406+
}
407+
}
408+
catch (e) {
409+
e && console.warn(e)
410+
}
393411
}
394412

395413
activate() {
@@ -414,25 +432,25 @@ class RustLanguageClient extends AutoLanguageClient {
414432
// Watch config toolchain changes -> switch, install & update toolchains, restart servers
415433
this.disposables.add(atom.config.onDidChange('ide-rust.rlsToolchain',
416434
_.debounce(({ newValue }) => {
417-
this._checkToolchain(this.busySignalService)
435+
return this._checkToolchain()
418436
.then(() => checkRls(this.busySignalService))
419437
.then(() => this._restartLanguageServers(`Switched Rls toolchain to \`${newValue}\``))
420438
.then(() => this._promptToUpdateToolchain())
421-
.catch(e => e && console.warn(e))
439+
.catch(e => logErr(e, console.info))
422440
}, 1000)
423441
))
424442

425443
// watch config toolchain updates -> check for updates if enabling
426444
this.disposables.add(atom.config.onDidChange('ide-rust.checkForToolchainUpdates',
427445
({ newValue: enabled }) => {
428-
if (enabled) this._promptToUpdateToolchain().catch(e => e && console.warn(e))
446+
if (enabled) this._promptToUpdateToolchain().catch(logErr)
429447
}
430448
))
431449

432450
// check for updates (if enabled) every so often
433451
let periodicUpdateTimeoutId
434452
const periodicUpdate = async () => {
435-
await this._promptToUpdateToolchain().catch(e => e && console.warn(e))
453+
await this._promptToUpdateToolchain().catch(logErr)
436454
periodicUpdateTimeoutId = setTimeout(periodicUpdate, PERIODIC_UPDATE_CHECK_MILLIS)
437455
}
438456
this.disposables.add(new Disposable(() => {
@@ -509,7 +527,7 @@ class RustLanguageClient extends AutoLanguageClient {
509527
}
510528

511529
try {
512-
await this._checkToolchain(this.busySignalService)
530+
await this._checkToolchain()
513531
await checkRls(this.busySignalService)
514532
let toolchain = configToolchain()
515533
return cp.spawn("rustup", ["run", toolchain, "rls"], {
@@ -518,7 +536,7 @@ class RustLanguageClient extends AutoLanguageClient {
518536
})
519537
}
520538
catch (e) {
521-
throw e || new Error("failed to start server")
539+
throw new Error("failed to start server: " + e)
522540
}
523541
}
524542
}

0 commit comments

Comments
 (0)