Skip to content

Pre-empt EADDRINUSE and show useful error. #407

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

Merged
merged 3 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path');
const istanbul = require('istanbul');
const util = require('util');
const assert = require('assert');
const detect = require('detect-port');

const ConfigValidator = require('./validator');
const Instrumenter = require('./instrumenter');
Expand Down Expand Up @@ -121,6 +122,11 @@ class API {
let retry = false;
let address = `http://${this.host}:${this.port}`;

// Check for port-in-use
if (await detect(this.port) !== this.port){
throw new Error(this.ui.generate('server-fail', [this.port]))
}

if(!this.client) this.client = client; // Prefer client from options

this.collector = new DataCollector(this.instrumenter.instrumentationData);
Expand Down
4 changes: 4 additions & 0 deletions lib/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class AppUI extends UI {
'istanbul-fail': `${c.red('Istanbul coverage reports could not be generated. ')}`,

'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`,

'server-fail': `${c.red('Port')} ${args[0]} ${c.red('is already in use.\n')}` +
`${c.red('\tRun: "lsof -i" to find the pid of the process using it.\n')}` +
`${c.red('\tRun: "kill -9 <pid>" to kill it.\n')}`
}

return this._format(kinds[kind])
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"chalk": "^2.4.2",
"death": "^1.1.0",
"fs-extra": "^8.1.0",
"detect-port": "^1.3.0",
"ganache-core-sc": "2.7.0-sc.0",
"ghost-testrpc": "^0.0.2",
"global-modules": "^2.0.0",
Expand Down
8 changes: 6 additions & 2 deletions test/units/truffle/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Truffle Plugin: error cases', function() {
});

// This case *does* throw an error, but it's uncatch-able;
it.skip('tries to launch with a port already in use', async function(){
it('tries to launch with a port already in use', async function(){
verify.cleanInitialState();
const server = ganache.server();

Expand All @@ -139,7 +139,11 @@ describe('Truffle Plugin: error cases', function() {
await plugin(truffleConfig);
assert.fail();
} catch(err){
assert(err.message.includes('EADDRINUSE: address already in use :::8545'))
assert(
err.message.includes('is already in use') &&
err.message.includes('lsof'),
`Should error on port-in-use with advice: ${err.message}`
)
}

await pify(server.close)();
Expand Down
15 changes: 14 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ accepts@~1.3.7:
mime-types "~2.1.24"
negotiator "0.6.2"

address@^1.0.1:
version "1.1.2"
resolved "https://registry.yarnpkg.com/address/-/address-1.1.2.tgz#bf1116c9c758c51b7a933d296b72c221ed9428b6"
integrity sha512-aT6camzM4xEA54YVJYSqxz1kv4IHnQZRtThJJHhUMRExaU5spC7jX5ugSwTaTgJliIgs4VhZOk7htClvQ/LmRA==

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d"
Expand Down Expand Up @@ -1828,7 +1833,7 @@ death@^1.1.0:
resolved "https://registry.yarnpkg.com/death/-/death-1.1.0.tgz#01aa9c401edd92750514470b8266390c66c67318"
integrity sha1-AaqcQB7dknUFFEcLgmY5DGbGcxg=

[email protected], debug@^2.2.0, debug@^2.6.8, debug@^2.6.9:
[email protected], debug@^2.2.0, debug@^2.6.0, debug@^2.6.8, debug@^2.6.9:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
Expand Down Expand Up @@ -2029,6 +2034,14 @@ detect-indent@^4.0.0:
dependencies:
repeating "^2.0.0"

detect-port@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/detect-port/-/detect-port-1.3.0.tgz#d9c40e9accadd4df5cac6a782aefd014d573d1f1"
integrity sha512-E+B1gzkl2gqxt1IhUzwjrxBKRqx1UzC3WLONHinn8S3T6lwV/agVCyitiFOsGJ/eYuEUBvD71MZHy3Pv1G9doQ==
dependencies:
address "^1.0.1"
debug "^2.6.0"

[email protected]:
version "3.5.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"
Expand Down