Skip to content

Commit 486d287

Browse files
evanlucasMyles Borins
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 b3b55a5 commit 486d287

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎lib/internal/repl.js‎

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

9393
functiononinit(err,hnd){
9494
if(err){
95-
returnready(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+
returnready(null,repl);
96105
}
97106
fs.close(hnd,onclose);
98107
}

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

Lines changed: 9 additions & 0 deletions
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
consthomedirErr='\nError: Could not get the home directory.\n'+
6666
'REPL session history will not be persisted.\n';
67+
constreplFailedRead='\nError: Could not open history file.\n'+
68+
'REPL session history will not be persisted.\n';
6769
// File paths
6870
constfixtures=path.join(common.testDir,'fixtures');
6971
consthistoryFixturePath=path.join(fixtures,'.node_repl_history');
7072
consthistoryPath=path.join(common.tmpDir,'.fixture_copy_repl_history');
73+
consthistoryPathFail=path.join(common.tmpDir,'.node_repl\u0000_history');
7174
constoldHistoryPath=path.join(fixtures,'old-repl-history-file.json');
7275
constenoentHistoryPath=path.join(fixtures,'enoent-repl-history-file.json');
7376
constdefaultHistoryPath=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: functionmockHomedirFailure(){
152161
// Mock os.homedir() failure

0 commit comments

Comments
(0)