Skip to content

Commit 5073da0

Browse files
evanlucasrvagg
authored andcommitted
repl: don't crash if cannot open history file
Previously, if we are unable to open the history file, an error would be thrown. Now, print an error message that we could not open the history file, but don't fail. Fixes: #3610 PR-URL: #3630 Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 8152248 commit 5073da0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/internal/repl.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,16 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
9292

9393
function oninit(err, hnd) {
9494
if (err) {
95-
return ready(err);
95+
// Cannot open history file.
96+
// Don't crash, just don't persist history.
97+
repl._writeToOutput('\nError: Could not open history file.\n' +
98+
'REPL session history will not be persisted.\n');
99+
repl._refreshLine();
100+
debug(err.stack);
101+
102+
repl._historyPrev = _replHistoryMessage;
103+
repl.resume();
104+
return ready(null, repl);
96105
}
97106
fs.close(hnd, onclose);
98107
}

test/sequential/test-repl-persistent-history.js

+9
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,13 @@ const convertMsg = '\nConverting old JSON repl history to line-separated ' +
6464
path.join(common.tmpDir, '.node_repl_history') + '.\n';
6565
const homedirErr = '\nError: Could not get the home directory.\n' +
6666
'REPL session history will not be persisted.\n';
67+
const replFailedRead = '\nError: Could not open history file.\n' +
68+
'REPL session history will not be persisted.\n';
6769
// File paths
6870
const fixtures = path.join(common.testDir, 'fixtures');
6971
const historyFixturePath = path.join(fixtures, '.node_repl_history');
7072
const historyPath = path.join(common.tmpDir, '.fixture_copy_repl_history');
73+
const historyPathFail = path.join(common.tmpDir, '.node_repl\u0000_history');
7174
const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
7275
const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json');
7376
const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history');
@@ -147,6 +150,12 @@ const tests = [{
147150
test: [UP, UP, UP, CLEAR],
148151
expected: [prompt, convertMsg, prompt, prompt + '\'=^.^=\'', prompt]
149152
},
153+
{
154+
env: { NODE_REPL_HISTORY: historyPathFail,
155+
NODE_REPL_HISTORY_SIZE: 1 },
156+
test: [UP],
157+
expected: [prompt, replFailedRead, prompt, replDisabled, prompt]
158+
},
150159
{ // Make sure this is always the last test, since we change os.homedir()
151160
before: function mockHomedirFailure() {
152161
// Mock os.homedir() failure

0 commit comments

Comments
 (0)