Skip to content

Commit d57d6fd

Browse files
authored
Pre-empt EADDRINUSE and show useful error. (#407)
1 parent 7e9be6c commit d57d6fd

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

lib/api.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path');
55
const istanbul = require('istanbul');
66
const util = require('util');
77
const assert = require('assert');
8+
const detect = require('detect-port');
89

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

125+
// Check for port-in-use
126+
if (await detect(this.port) !== this.port){
127+
throw new Error(this.ui.generate('server-fail', [this.port]))
128+
}
129+
124130
if(!this.client) this.client = client; // Prefer client from options
125131

126132
this.collector = new DataCollector(this.instrumenter.instrumentationData);

lib/ui.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class AppUI extends UI {
103103
'istanbul-fail': `${c.red('Istanbul coverage reports could not be generated. ')}`,
104104

105105
'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`,
106+
107+
'server-fail': `${c.red('Port')} ${args[0]} ${c.red('is already in use.\n')}` +
108+
`${c.red('\tRun: "lsof -i" to find the pid of the process using it.\n')}` +
109+
`${c.red('\tRun: "kill -9 <pid>" to kill it.\n')}`
106110
}
107111

108112
return this._format(kinds[kind])

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"chalk": "^2.4.2",
2929
"death": "^1.1.0",
3030
"fs-extra": "^8.1.0",
31+
"detect-port": "^1.3.0",
3132
"ganache-core-sc": "2.7.0-sc.0",
3233
"ghost-testrpc": "^0.0.2",
3334
"global-modules": "^2.0.0",

test/units/truffle/errors.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('Truffle Plugin: error cases', function() {
126126
});
127127

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

@@ -139,7 +139,11 @@ describe('Truffle Plugin: error cases', function() {
139139
await plugin(truffleConfig);
140140
assert.fail();
141141
} catch(err){
142-
assert(err.message.includes('EADDRINUSE: address already in use :::8545'))
142+
assert(
143+
err.message.includes('is already in use') &&
144+
err.message.includes('lsof'),
145+
`Should error on port-in-use with advice: ${err.message}`
146+
)
143147
}
144148

145149
await pify(server.close)();

yarn.lock

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ accepts@~1.3.7:
288288
mime-types "~2.1.24"
289289
negotiator "0.6.2"
290290

291+
address@^1.0.1:
292+
version "1.1.2"
293+
resolved "https://registry.yarnpkg.com/address/-/address-1.1.2.tgz#bf1116c9c758c51b7a933d296b72c221ed9428b6"
294+
integrity sha512-aT6camzM4xEA54YVJYSqxz1kv4IHnQZRtThJJHhUMRExaU5spC7jX5ugSwTaTgJliIgs4VhZOk7htClvQ/LmRA==
295+
291296
292297
version "3.0.0"
293298
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d"
@@ -1828,7 +1833,7 @@ death@^1.1.0:
18281833
resolved "https://registry.yarnpkg.com/death/-/death-1.1.0.tgz#01aa9c401edd92750514470b8266390c66c67318"
18291834
integrity sha1-AaqcQB7dknUFFEcLgmY5DGbGcxg=
18301835

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

2037+
detect-port@^1.3.0:
2038+
version "1.3.0"
2039+
resolved "https://registry.yarnpkg.com/detect-port/-/detect-port-1.3.0.tgz#d9c40e9accadd4df5cac6a782aefd014d573d1f1"
2040+
integrity sha512-E+B1gzkl2gqxt1IhUzwjrxBKRqx1UzC3WLONHinn8S3T6lwV/agVCyitiFOsGJ/eYuEUBvD71MZHy3Pv1G9doQ==
2041+
dependencies:
2042+
address "^1.0.1"
2043+
debug "^2.6.0"
2044+
20322045
20332046
version "3.5.0"
20342047
resolved "https://registry.yarnpkg.com/diff/-/diff-3.5.0.tgz#800c0dd1e0a8bfbc95835c202ad220fe317e5a12"

0 commit comments

Comments
 (0)