node-sass icon indicating copy to clipboard operation
node-sass copied to clipboard

CLI: Error output style

Open demrks opened this issue 8 years ago • 10 comments

This might be a dumb question/request, but it seems that there's no information about that:

If you are using the CLI to compile SCSS to CSS and there's an error in your code, the error is outputted in json format:

{
  "formatted": "Error: Undefined variable: \"$variable\".\n        on line X of X\n>> \n   ----^\n",
  "message": "Undefined variable: \"$variable\".",
  "column": 1,
  "line": 1,
  "file": "FILE_NAME",
  "status": 1
} 

For some error messages this is really hard to read. Is there any way to output error message as "plain text" or, if not, any plans to implement this feature?

demrks avatar Apr 29 '16 21:04 demrks

Thanks for the suggestion @demrks. This is something I have thought about.

@am11 any idea whey we spit out json to standout? I have a feeling it changed in 3.3 ish.

xzyfer avatar Apr 30 '16 05:04 xzyfer

I'm would like to change this output be more useful. This will likely have to come on a major version since someone in inevitably relying on parsing this json.

xzyfer avatar Apr 30 '16 05:04 xzyfer

@xzyfer In order to be backwards compatible, you could also add an additional parameter (defaults to json format) to the CLI, so people can choose what output they prefer. Something like

node-sass --error-output-style json

demrks avatar Apr 30 '16 09:04 demrks

@xzyfer, this was a v2.0 change (https://github.com/sass/node-sass/commit/9f7a78d870cede1a55e6ab342ab133815e62bbf9), when libsass implemented JSON serialization and we decided to get error as JSON string from libsass, serialize it as JS Error object and output the object in CLI scenario (errObj.toString()). At that point, formatted option was not added to the error object, so that was the simple way to convey the error with line/file info to the user.

Now that we have formatted option from libsass plus we do not follow that convention everywhere, i.e. some errors are not serialized as JSON:

  • "No input file was found"
    • when .sass or .scss file path is provided, but file does not exist on FS
  • "An output directory must be specified when compiling a directory"
    • when some path is provided, which does not have .sass or .scss ext. and -o option is missing
  • "Provide a Sass file to render"
    • when no input path is provided

it probably makes sense to introduce a breaking change in render.js: emitter.emit('error', chalk.red(JSON.stringify(error, null, 2))); to emitter.emit('error', chalk.red(error.formatted));. Also, for those three string errors which are directly sent to emitter without ever being real Error object, those should use the chalk.red for consistency.

am11 avatar May 01 '16 08:05 am11

Thanks for the feedback @am11. Always appreciated.

xzyfer avatar May 10 '16 10:05 xzyfer

Will this change be implemented, @am11? It would really help.

robrecord avatar Jul 18 '16 15:07 robrecord

@robrecord this will land in node-sass@v4 because it's a breaking change. Node Sass 4 will likely land when LibSass reaches 3.4.0

xzyfer avatar Jul 19 '16 04:07 xzyfer

In the mean time if you want better formatted errors then pass stderr to prettyjson. Something like this: node-sass source/scss/main.scss public/css/main.css 2>&1 | prettyjson

darren131 avatar Jan 31 '17 23:01 darren131

@darren131 I tried this approach, and it does work as long as there is an actual error, since prettyjson gets passed valid json. But when there is no error 2>&1 pipes a string ("Rendering Complete, saving .css file...") causing an error in prettyjson.

Being a command line rookie, I couldn't quite figure out how to fix this, any suggestions?

eivindmjelde avatar Sep 06 '17 10:09 eivindmjelde

So are we going to change the error style?

PolyPik avatar Mar 28 '20 03:03 PolyPik