gulp-less task exits with zero on invalid styles - node.js

Firstly, here is a repo with very simple example of my problem: https://github.com/nkoder/example-of-gulp-less-zero-exit-on-error
I want to define gulp build pipeline which fails on any error. This pipeline would be used in automated process (eg. Continuous Integration) so I need the build command to return non-zero exit code on failure. It works for me with invalid Jade templates or non-passing tests run with Karma.
The problem is when I use gulp-less to prepare CSS from LESS. Whenever I add any pipeline step after gulp-less execution, all errors are "consumed" and the whole task exits with 0. But it shouldn't.
The first question is: How to force wrong task to fail with non-zero exit code? (solved, see answer below)
The second question is: Why error event emitted in less() call followed by piped stream is not making a whole task to return non-zero exit code? Non-zero is returned in some other similar cases, eg. in Jade or ESLint failure. (not solved yet)
Versions:
node 5.11.0
npm 3.8.6
gulp 3.9.1
gulp-less 3.1.0

Listen for the error event and then exit the process explicitly:
gulp.task('wrong', function () {
return gulp
.src('invalid-styles.less')
.pipe(less().on('error', function(error) {
console.log(error.message);
process.exit(1);
}))
.pipe(gulp.dest('generated-styles.css'));
});

Related

Worker thread postMessage() vs command line command

I recently learned about Worker threads in Node JS. I was trying to create a worker thread to run Stockfish chess engine in node js.
The npm package I am using for this is called stockfish. I tried using node-stockfish before this but it was not installing with npm as it was using an older version of the type definition for the "AbortSignal" variable apparently causing compatibility issues.
For the current npm package that I am using even though I was able to install it successfully, I could find very little documentation on how to use it. So I tried out a few ideas.
import { Worker } from "worker_threads";
const engine = new Worker("./node_modules/stockfish/src/stockfish.js")
engine.on('message', (data) => console.log(data))
engine.postMessage('position startpos move e2e4 e7e5')
engine.postMessage('go movetime 3000')
Here I tried to run the stockfish.js as a worker thread and send commands to it with the postMessage() function. This however did not work and it gave the following output:
worker.js received unknown command undefined
position startpos move e2e4 e7e5
worker.js received unknown command undefined
go movetime 3000
But I know these commands are valid commands if I run the same js from the command line like so:
It might be because I am using the flags --experimental-wasm-threads and --experimental-wasm-simd when I am running it from the command line. I found this command to run it from the little documentation that was present. But I don't know how to mention these flags when I run it through a worker thread.
Otherwise it could also be that I don't understand how worker threads work yet and postMessage() is not the same as sending it a command from the command line.
Any help is greatly appreciated.
I switched to using stockfish.wasm library instead. With this library I was able to achieve what I wanted and I don't need to use any worker threads for now. Maybe I can add this to a worker thread if required later. Here is a simple example:
const Stockfish = require("stockfish.wasm")
Stockfish().then((engine) => {
engine.addMessageListener((output) => {
console.log(output);
// Do something with the output data here
})
engine.postMessage("uci");
engine.postMessage("ucinewgame");
engine.postMessage("position startpos");
engine.postMessage("go depth 20");
});

Yarn / node command "error Command failed with exit code 1." no error

I've got a weird issue going on. I have a node service being started with yarn .... The app seems to work fine for some random amount of time before I get hit with a...
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
There is no error logged. My service already has the following two handlers, but they never log anything when the service dies like this.
process.on('unhandledRejection', (err: any) => {
logger.fatal({ err: err }, 'Process failed. Unhandled Rejection');
});
process.on('uncaughtException', (list) => {
logger.fatal({ err: list }, 'unhandledExceptionThrown');
});
I've read some other SO question (error Command failed with exit code 1. when I try to run yarn) and tried to clear node modules / clean my yarn cache, but so far that has not helped the issue.
Is there something else I can try to track down what's causing the issue?
After lots of digging around, I found something that was able to help move me in the right direction. Im not sure why the handlers above weren't catching the issue, however... when I added --unhandled-rejections=strict to my node command to start the service, the app would blow up as expected and actually would output some useful information.

Expect assertions type error -> expect(...).toExist is not a function

I'm testing a NodeJS app. I encountered this error when I ran the tests. The test script is below:
.expect((res) => {
expect(res.headers['x-auth']).toExist();
expect(res.body._id).toExist();
expect(res.body.email).toBe(email);
})
The error showed:
TypeError: expect(...).toExist is not a function
How can I resolve this issue?
The expect assertion library has changed ownership. It was handed over to the Jest team, who in their infinite wisdom, created a new API.
You must now use toBeTruthy()instead of toExist().
You can still install expect as before, npm install expect --save-dev, which is currently at version 21.2.1. Most methods names will remain unchanged except for a few, including toExist().
If you are using Jest you can also use 'toBeDefined()'

How to abort if command fails in Unix shell?

I'm not very great with shell scripting and still couldn't find a solution to this. I want to run a command gulp tslint and if it returns errors, abort the script.
So far I tried gulp tslint || exit 1 with no success. I think it somehow is returning true, even though it has errors when i run it on command prompt e.g.
[12:30:14] Starting 'tslint'...
[12:30:14] Finished 'tslint' after 9.49 ms
[12:30:16] [gulp-tslint] error (quotemark) payment-types.ts[18, 53]: " should be '
How can i make it work?
Any help appreciated. Thanks!
This looks like a gulp question, rather than a shell question. As gulp completes successfully (i.e. reaches an exit without raising an error), it doesn't tell the shell that anything is wrong. The best way to address this is to reconfigure gulp to interpret lint failures as errors.
Could you post your gulpfile.js? Specifically your tslint task.
It probably looks like
gulp.task('tslint', function() {
gulp.src("**/*.ts")
.pipe(tslint.report('prose'))
});
You can instruct your reporter to fail on error by changing this to:
gulp.task('tslint', function() {
gulp.src("**/*.ts")
.pipe(tslint.report('prose', {emitError: true}))
});
edit: look at the gulp-tslint README for more details on emitError

PhantomJS exited unexpectedly with exit code -1073741819

I run a bunch of Jasmine specs with PhantomJS (via Grunt) on a Windows 7 PC, and I happen to get the following error:
Testing jasmine specs via phantom
......
Running PhantomJS...ERROR
>> 0 [ '' ]
Warning: PhantomJS exited unexpectedly with exit code -1073741819. Use --force to continue.
Aborted due to warnings.
The error does not appear if I delete a bunch of tests; however I have no idea what causes the error.
What I also find strange, it that it only occurs now and then.
Any idea why this happens?
We are running AngularJS tests via jasmine and grunt, and had this very same problem. For us it turned out to be due to our custom $exceptionHandler function.
We were missing the throw exception line that is in the example handler. By not throwing the exception, the test would pass but it would randomly cause PhantomJS to crash.
This means that some of our tests were failing and we didn't know it!
Here's the sample handler from the $exceptionHandler documentation page:
angular.module('exceptionOverride', []).factory('$exceptionHandler', function () {
return function (exception, cause) {
exception.message += ' (caused by "' + cause + '")';
throw exception; // <--- This is important
};
});
So I'd look and see if you have custom exception handling too and are possibly missing the throw exception line.
I hope it's as simple as that for you too!
(Sadly, I don't have an answer. I would post as a comment, but I don't have the rep.)
I'm getting the same error trying to create html snapshots with grunt-html-snapshot.
Running PhantomJS...ERROR
>> 0 [ '' ]
Warning: PhantomJS exited unexpectedly with exit code -1073741819. Use --force to continue.
Aborted due to warnings.
I'm feeding grunt a list of URLs and I "randomly" get this crash somewhere in the list. I think I have traced it to the child process spawn call that launches phantom.exe with the bridge.js script. It doesn't seem to get as far as trying load the next page before the phantom.exe child process exits with the error. I haven't been able to find any documentation on the error code.
I received the same error message as Helge after renaming a folder and running jasmine in grunt.
The folder contained all the JavaScript files I wanted to minify into one file, and I renamed the folder to match the final file name. For example the folder, \src\modules\ was renamed \src\modules.js\.
Removing .js from the folder name solved the problem.
Using PhantomJS to generate snapshots for AngularJS.
The snapshots script goes through an array of urls of which it has to take snapshots.
Narrowed down to the possible cause and as it turned out there was a bug in my latest fix in AngularJS. This bug resulted in the constantly switching the home page and the page where the error occurred.
Code Visualization:
$state.go('home state');
...
$state.go('an other state');
...
$state.go('home state');
...
$state.go('an other state');
...
$state.go('home state');
...
...
=> Error: PhantomJS exited unexpectedly with exit code -1073741819
In short:
For me this error was caused by an infinite redirect loop.
The warning PhantomJS exited unexpectedly with exit code -1073741819 is also displayed when your local PhantomJS is incompatible or corrupt.
With npm list -g phantomjs you can check your PhantomJS version.
If you think that it is corrupt, just reinstall it with:
npm uninstall phantomjs -g
npm install phantomjs -g

Resources