From cdc57051c9be532c386374815e1d5f4c1db3d18b Mon Sep 17 00:00:00 2001 From: White-Oak Date: Fri, 2 Sep 2016 23:40:03 +0300 Subject: [PATCH 1/2] Fixed the way json errors are requested from cargo & rustc Closes #69 Also fixed the way envinronment variables were passed to BufferedProcess -- according to [the documentation](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback) (*`BufferedProcess` is actually a wrapper above node's exec*) they should be passed in an `env` subobject of `options`, but they were passed as raw in options object. Adding to these environment variables accordingly set `RUSTFLAGS` variable, when JSON errors are available, makes cargo tell compiler to output JSON errors. When using `rustc` simply providing `--error-format=json` is enough to make compiler do what we want. --- lib/linter-rust.coffee | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/linter-rust.coffee b/lib/linter-rust.coffee index 5b5cccd..459be98 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -21,8 +21,9 @@ class LinterRust file = @initCmd do textEditor.getPath curDir = path.dirname file PATH = path.dirname @cmd[0] - options = JSON.parse JSON.stringify process.env - options.PATH = PATH + path.delimiter + options.PATH + options = new Object + options.env = JSON.parse JSON.stringify process.env + options.env.PATH = PATH + path.delimiter + options.env.PATH options.cwd = curDir command = @cmd[0] args = @cmd.slice 1 @@ -34,6 +35,11 @@ class LinterRust atom.notifications.addError "Invalid specified features", detail: "#{err}" dismissable: true + else + if do atom.inDevMode + atom.notifications.addError "Something wrong", + detail: "#{err}" + dismissable: true results.push err exit = (code) => @@ -49,6 +55,9 @@ class LinterRust else resolve [] + if do @ableToJSONErrors + additional = if options.env.RUSTFLAGS? then ' ' + options.env.RUSTFLAGS else '' + options.env.RUSTFLAGS = '--error-format=json' + additional @lintProcess = new BufferedProcess({command, args, options, stdout, stderr, exit}) @lintProcess.onWillThrowError ({error, handle}) -> atom.notifications.addError "Failed to run #{command}", @@ -192,6 +201,7 @@ class LinterRust @cmd.push path.join path.dirname(cargoManifestPath), @cargoDependencyDir @cmd = @cmd.concat @compilationFeatures(false) @cmd = @cmd.concat [editingFile] + @cmd = @cmd.concat ['--error-format=json'] if do @ableToJSONErrors return editingFile else @cmd = @buildCargoPath cargoPath @@ -199,7 +209,6 @@ class LinterRust .concat ['-j', @config('jobsNumber')] @cmd = @cmd.concat @compilationFeatures(true) @cmd = @cmd.concat ['--manifest-path', cargoManifestPath] - @cmd = @cmd.concat ['--','--error-format=json'] if do @ableToJSONErrors return cargoManifestPath compilationFeatures: (cargo) => From d61d6ee42548c6324e94d3c1e61e0549d8d99a19 Mon Sep 17 00:00:00 2001 From: White-Oak Date: Sat, 3 Sep 2016 00:39:08 +0300 Subject: [PATCH 2/2] Added an ability to cache 'ability to output json errors' Changed notifications in dev mode. --- lib/linter-rust.coffee | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/linter-rust.coffee b/lib/linter-rust.coffee index 459be98..40be712 100644 --- a/lib/linter-rust.coffee +++ b/lib/linter-rust.coffee @@ -8,6 +8,7 @@ spawn = require ('child_process') class LinterRust cargoDependencyDir: "target/debug/deps" lintProcess: null + cachedAbleToJsonErrors: null pattern: XRegExp('(?[^\n\r]+):(?\\d+):(?\\d+):\\s*\ (?\\d+):(?\\d+)\\s+\ ((?error|fatal error)|(?warning)|(?note|help)):\\s+\ @@ -21,12 +22,14 @@ class LinterRust file = @initCmd do textEditor.getPath curDir = path.dirname file PATH = path.dirname @cmd[0] - options = new Object - options.env = JSON.parse JSON.stringify process.env + options = + env: JSON.parse JSON.stringify process.env options.env.PATH = PATH + path.delimiter + options.env.PATH options.cwd = curDir command = @cmd[0] args = @cmd.slice 1 + @cachedAbleToJsonErrors = null + @cachedAbleToJsonErrors = do @ableToJSONErrors stdout = (data) -> console.log data if do atom.inDevMode @@ -37,8 +40,9 @@ class LinterRust dismissable: true else if do atom.inDevMode - atom.notifications.addError "Something wrong", + atom.notifications.addWarning "Output from stderr while linting", detail: "#{err}" + description: "This is shown because Atom is running in dev-mode and probably not an actual error" dismissable: true results.push err @@ -223,6 +227,7 @@ class LinterRust result ableToJSONErrors: () => + return @cachedAbleToJsonErrors if @cachedAbleToJsonErrors? rustcPath = (@config 'rustcPath').trim() result = spawn.execSync rustcPath + ' --version', {stdio: 'pipe' } match = XRegExp.exec result, @patternRustcVersion