Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log gulp error to Chart.js #5143

Merged
merged 13 commits into from Jan 13, 2018
Merged

Log gulp error to Chart.js #5143

merged 13 commits into from Jan 13, 2018

Conversation

loicbourgois
Copy link
Contributor

@loicbourgois loicbourgois commented Jan 13, 2018

If gulp encounters an error when building, it just stops, which means the Chart.js files are not updated with the error, and that gulp would have to be restarted manually.
This PR takes care of it.

Gulp log before

[12:14:33] Starting 'build'...
events.js:136
      throw er; // Unhandled 'error' event
      ^

Error: Parsing file /home/user/Chart.js/src/chart.js: Unexpected token (5:5)
    at Deps.parseDeps (/home/user/Chart.js/node_modules/module-deps/index.js:481:28)
    at getDeps (/home/user/Chart.js/node_modules/module-deps/index.js:414:40)
    ...

Gulp log after

[12:10:29] Finished 'default' after 20 μs
[12:10:46] Starting 'build'...
[12:10:46] [Error] Error: Parsing file /home/user/Chart.js/src/chart.js: Unexpected token (5:5)
[12:10:46] [Error] Error: Parsing file /home/user/Chart.js/src/chart.js: Unexpected token (5:5)
[12:10:46] Finished 'build' after 42 ms

It's possible to have the full trace using simply util.log(err)

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you post part of the log of the error before and after this change?

gulpfile.js Outdated
.on('error', function (err) {
util.log(util.colors.red('[Error]'), err.toString())
fs.writeFileSync(outDir+'Chart.bundle.js', 'console.error("Gulp: ' + err.toString() + '")');
fs.writeFileSync(outDir+'Chart.bundle.min.js', 'console.error("Gulp: ' + err.toString() + '")');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not alter Chart.bundle(.min)?.js, simply log the error to console.

gulpfile.js Outdated
@@ -96,6 +102,12 @@ function buildTask() {
.ignore('moment')
.plugin(collapse)
.bundle()
.on('error', function (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid code duplication by refactoring this method:

var errorHandler = function (err) {
   util.log(util.colors.red('[Error]'), err.toString());
   this.emit('end');
}
// ...

.bundle()
.on('error', errorHandler)
// ...
.on('error', errorHandler)

@loicbourgois
Copy link
Contributor Author

loicbourgois commented Jan 13, 2018

I added the logs in the top comment

@simonbrunel
Copy link
Member

simonbrunel commented Jan 13, 2018

I'm reading again your description: does that mean the gulp build process doesn't fail anymore in case of parsing errors?

gulpfile.js Outdated
fs.writeFileSync(outDir+'Chart.js', browserError);
fs.writeFileSync(outDir+'Chart.min.js', browserError);
fs.writeFileSync(outDir+'Chart.bundle.js', browserError);
fs.writeFileSync(outDir+'Chart.bundle.min.js', browserError);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simonbrunel if possible, i would like to be able to see the errors directly in the browser. I prefer to work in the editor and the browser only, and not have to check the terminal.

@loicbourgois
Copy link
Contributor Author

Yes, gulp continues to run.
If it's a problem, maybe we could add a task build-dev, that wouldn't fail completely on errors.

@loicbourgois
Copy link
Contributor Author

loicbourgois commented Jan 13, 2018

Maybe the travis log could be helpful https://travis-ci.org/chartjs/Chart.js/builds/328424793

@simonbrunel
Copy link
Member

These changes are not safe: first the build task must fail and return an error code in order to stop the Travis build and report the failed status. In that case, tests should not run, neither other tasks such as docs, package, etc. Then, in case of a failing build, no artifact should be generated because that could confuse some other tasks and deploy unwanted files.

I'm not fan of the build-dev task because it will enforce your workflow, but also because it duplicates logic and code. Instead, I would adopt another approach by introducing new build options:

  • --force-output: that would output the error in the dist files
  • --silent-errors: that would prevent the process to fail

Indeed, the Travis build fails, but that's not controlled, which is a door open on more issues.

@simonbrunel
Copy link
Member

Though, I plan to refactor the gulp file for a better watch logic and thus make sure it keeps watching on error (so that would deprecate --silent-errors).

@loicbourgois
Copy link
Contributor Author

I followed your advice and used options.
With your plan to refactor, is it still ok to keep --silent-errors for now ?
Also, should I update contributing.md#building-and-testing ?

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep --silent-errors for now.

I don't think I would update contributing.md right now because with the number of options increasing, it will be complicated to maintain. Instead, we will use the yargs help method (gulp --help), which is planned in the upcoming refactor and thus another PR.

gulpfile.js Outdated
@@ -34,6 +36,9 @@ var header = "/*!\n" +
" * https://github.com/chartjs/Chart.js/blob/master/LICENSE.md\n" +
" */\n";

var options = minimist(process.argv.slice(2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already depend on yargs, no need for minimist:

var argv = require('yargs')
  .option('force-output', {default: 'false'})
  .option('silent-errors', {default: 'false'})
  .option('verbose', {default: 'false'})
  .argv;

// ...

if (argv.forceOutput) { ... }
if (argv.silentErrors) { ... }

gulpfile.js Outdated
@@ -135,15 +157,15 @@ function lintTask() {
// NOTE(SB) codeclimate has 'complexity' and 'max-statements' eslint rules way too strict
// compare to what the current codebase can support, and since it's not straightforward
// to fix, let's turn them as warnings and rewrite code later progressively.
var options = {
var eslintOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to rename this variable, we can keep var argv = ...

gulpfile.js Outdated
@@ -20,6 +20,8 @@ var collapse = require('bundle-collapser/plugin');
var argv = require('yargs').argv
var path = require('path');
var package = require('./package.json');
var fs = require('fs');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] can we move var fs before var package since the later one is a local file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes
while i'm at it, is there any logic in the order for the non local dependencies ? alphabetical, date of addition ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No guideline for the order because some require() may need other ones. I'm used to gather related dependencies (e.g. gulp-*) and declare the package.json at the end.

gulpfile.js Outdated
@@ -79,9 +84,25 @@ function bowerTask() {

function buildTask() {

var errorHandler = function (err) {
util.log(util.colors.red('[Error]'), err.toString());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move that line before this.emit('end');, it's only useful when ignoring exceptions

gulpfile.js Outdated
@@ -34,6 +36,9 @@ var header = "/*!\n" +
" * https://github.com/chartjs/Chart.js/blob/master/LICENSE.md\n" +
" */\n";

var options = minimist(process.argv.slice(2));
util.log("Gulp running with options: "+JSON.stringify(options, null, 2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (argv.verbose) {
  util.log("Gulp running with options: " + JSON.stringify(argv, null, 2));
}

gulpfile.js Outdated
@@ -17,8 +17,13 @@ var browserify = require('browserify');
var source = require('vinyl-source-stream');
var merge = require('merge-stream');
var collapse = require('bundle-collapser/plugin');
var argv = require('yargs').argv
var argv = require('yargs')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though, since this one will grow, I would keep it outside the others for a better readability of dependencies :)

Maybe:

var collapse = require('bundle-collapser/plugin');
var yargs = require('yargs');
var path = require('path');
var package = require('./package.json');

var argv = yargs
  .option('force-output', {default: 'false'})
  // ..
  .argv;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but I think I prefer the first solution: var yargs = require('yargs');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think it makes more sense to keep all the dependencies small and together.

gulpfile.js Outdated
var package = require('./package.json');

var argv = yargs
.option('force-output', {default: 'false'})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad, I think it should be {default: false} (boolean instead of string)

Copy link
Member

@simonbrunel simonbrunel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @loicbourgois (and sorry for all these iterations)

@etimberg etimberg merged commit e585c75 into chartjs:master Jan 13, 2018
@loicbourgois loicbourgois deleted the gulp branch January 13, 2018 17:36
@simonbrunel simonbrunel added this to the Version 2.7.2 milestone Jan 13, 2018
exwm pushed a commit to exwm/Chart.js that referenced this pull request Apr 30, 2021
* Log errors and skip for buildTask

* Write gulp error to Chart.js
+ Add intentional error to core to check if travis fails

* Remove unused require

* Remove error + Proper require fs

* Fix newline

* Refactor

* Put back browser errors

* Use options

* Fix intentional error

* Use yargs + Refactor

* remove space

* Fefactor

* Use booleans
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants