diff --git a/package-lock.json b/package-lock.json index b1dcbb1..ec0157c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "hasInstallScript": true, "license": "MIT", "dependencies": { + "command-exists": "^1.2.9", "sax": "^1.2.4" }, "devDependencies": { @@ -271,6 +272,12 @@ "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", "dev": true }, + "node_modules/command-exists": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", + "integrity": "sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==", + "license": "MIT" + }, "node_modules/commander": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", @@ -1789,6 +1796,11 @@ "integrity": "sha512-72fSenhMw2HZMTVHeCA9KCmpEIbzWiQsjN+BHcBbS9vr1mtt+vJjPdksIBNUmKAW8TFUDPJK5SUU3QhE9NEXDw==", "dev": true }, + "command-exists": { + "version": "1.2.9", + "resolved": "https://registry.npmjs.org/command-exists/-/command-exists-1.2.9.tgz", + "integrity": "sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==" + }, "commander": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", diff --git a/package.json b/package.json index 5ce9058..568d350 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "theme": "dark" }, "dependencies": { + "command-exists": "^1.2.9", "sax": "^1.2.4" }, "categories": [ diff --git a/src/extension.ts b/src/extension.ts index c49688f..183766a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -5,6 +5,7 @@ import {MODES, ALIAS} from './clangMode'; import {getBinPath} from './clangPath'; import sax = require('sax'); +import commandExists = require('command-exists'); export let outputChannel = vscode.window.createOutputChannel('Clang-Format'); @@ -251,17 +252,19 @@ export class ClangDocumentFormattingEditProvider implements vscode.DocumentForma workingPath = path.dirname(document.fileName); } + const formatCommandBinPathExists = commandExists.sync(formatCommandBinPath); + if (!formatCommandBinPathExists) { + vscode.window.showInformationMessage('The \'' + formatCommandBinPath + '\' command is not available. Please check your clang-format.executable user setting and ensure it is installed.'); + return reject(null); + } + let stdout = ''; let stderr = ''; - let child = cp.spawn(formatCommandBinPath, formatArgs, { cwd: workingPath }); + let child = cp.spawn(formatCommandBinPath, formatArgs, { cwd: workingPath, shell: true }); child.stdin.end(codeContent); child.stdout.on('data', chunk => stdout += chunk); child.stderr.on('data', chunk => stderr += chunk); child.on('error', err => { - if (err && (err).code === 'ENOENT') { - vscode.window.showInformationMessage('The \'' + formatCommandBinPath + '\' command is not available. Please check your clang-format.executable user setting and ensure it is installed.'); - return resolve(null); - } return reject(err); }); child.on('close', code => {