Skip to content

Conversation

@spalger
Copy link
Contributor

@spalger spalger commented Jul 9, 2018

node scripts/tslint is used to run the linter on all projects, but isn't currently reporting a non-zero status code when there are failures. This PR ensures that the status code is always 1 when an error occurs in the linters.

To test make a change to a TypeScript file that violates the linter rules and run yarn grunt run:tslint to make sure that grunt is seeing the non-zero exit code and fatal-ing correctly.

yarn grunt run:tslint
echo $? # should be 1

@spalger spalger added review Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v7.0.0 v6.4.0 v6.3.2 labels Jul 9, 2018
@spalger spalger requested review from rhoboat and timroes July 9, 2018 16:14
@spalger spalger requested a review from epixa July 9, 2018 16:22

/**
* Lints a list of files with eslint. eslint reports are written to the log
* Lints a list of files with tslint. tslint reports are written to the log
Copy link

Choose a reason for hiding this comment

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

nice. :)

log.error('Unhandled execption!');
log.error(error);
process.exit(1);
process.exit();
Copy link

Choose a reason for hiding this comment

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

Can you explain the reason for this way?

Copy link

Choose a reason for hiding this comment

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

Does setting process.exitCode have the side-effect that even if error.errors == true, it will exit with error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, setting process.exitCode tells node that when it naturally exits to use that exit code. I could also just call process.exit(1); in both branches of the .catch() handler, but I personally prefer to use the natural exit method as it's a little less brute-force and doesn't hide things like child processes or file descriptors that are left open and preventing the process from naturally exiting.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That said, calling process.exit() in the case that an unhandled exception is caught is important, because an unhandled exception could mean that we hit a failure before we were able to properly clean up. But in the case below, were we are iterating error.errors, all cleanup should have run so this sorta relies on that and will fail to exit if that changes in the future (intentionally, hopefully preventing us from committing changes that break cleanup in some way)

Copy link

Choose a reason for hiding this comment

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

node's natural exit is nice to leverage here.

is there a reason we can't really follow the same pattern in this tslint script, as we do in eslint script? it looks like in that one, we throw error and let it bubble up. can we do something like that here? and handle the exit higher?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could if there was a higher-level component, but this is the highest level script for typescript

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See https://github.com/spalger/kibana/blob/e4e809180de56db64776b0b8767258f79afe19b8/scripts/tslint.js#L21. scripts/tslint calls this function directly and it is responsible for exposing the CLI interface

Copy link

Choose a reason for hiding this comment

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

Ah, I see. TY!

Copy link
Contributor

@timroes timroes left a comment

Choose a reason for hiding this comment

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

Code looks good to me, and make sense (as long as we don't reset process.exitCode somewhere.

@elasticmachine
Copy link
Contributor

💚 Build Succeeded

@timroes
Copy link
Contributor

timroes commented Jul 9, 2018

It seems to actually work: #20546 (comment)

@epixa epixa removed their request for review July 9, 2018 18:13
@epixa
Copy link
Contributor

epixa commented Jul 9, 2018

I won't have time to review this in the short term, but it looks like you have coverage.

Copy link

@rhoboat rhoboat left a comment

Choose a reason for hiding this comment

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

LGTM

@spalger spalger merged commit 52e7939 into elastic:master Jul 12, 2018
spalger pushed a commit to spalger/kibana that referenced this pull request Jul 12, 2018
`node scripts/tslint` is used to run the linter on all projects, but isn't currently reporting a non-zero status code when there are failures. This PR ensures that the status code is always 1 when an error occurs in the linters.

To test make a change to a TypeScript file that violates the linter rules and run `yarn grunt run:tslint` to make sure that `grunt` is seeing the non-zero exit code and fatal-ing correctly.

```sh
yarn grunt run:tslint
echo $? # should be 1
```
@spalger spalger removed the v6.3.2 label Jul 12, 2018
spalger pushed a commit that referenced this pull request Jul 13, 2018
Backports the following commits to 6.x:
 - [tslint] use exitCode 1 when linter errors  (#20567)
@spalger
Copy link
Contributor Author

spalger commented Jul 13, 2018

6.4/6.x: d6a4331

@spalger spalger deleted the fix/tslint-exit-code branch July 13, 2018 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review Team:Core Platform Core services: plugins, logging, config, saved objects, http, ES client, i18n, etc t// v6.4.0 v7.0.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants