Skip to content

Commit bd323a7

Browse files
mscdexMylesBorins
authored andcommitted
repl: fix /dev/null history file regression
This fixes a regression from 83887f3 where ftruncate() fails on a file symlinked to /dev/null. PR-URL: #12762 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Bartosz Sosnowski <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b2acb81 commit bd323a7

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/internal/repl.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -173,25 +173,18 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
173173
return ready(err);
174174
}
175175
fs.ftruncate(hnd, 0, (err) => {
176-
return onftruncate(err, hnd);
176+
repl._historyHandle = hnd;
177+
repl.on('line', online);
178+
179+
// reading the file data out erases it
180+
repl.once('flushHistory', function() {
181+
repl.resume();
182+
ready(null, repl);
183+
});
184+
flushHistory();
177185
});
178186
}
179187

180-
function onftruncate(err, hnd) {
181-
if (err) {
182-
return ready(err);
183-
}
184-
repl._historyHandle = hnd;
185-
repl.on('line', online);
186-
187-
// reading the file data out erases it
188-
repl.once('flushHistory', function() {
189-
repl.resume();
190-
ready(null, repl);
191-
});
192-
flushHistory();
193-
}
194-
195188
// ------ history listeners ------
196189
function online() {
197190
repl._flushing = true;

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ const emptyHistoryPath = path.join(fixtures, '.empty-repl-history-file');
7878
const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history');
7979
const emptyHiddenHistoryPath = path.join(fixtures,
8080
'.empty-hidden-repl-history-file');
81+
const devNullHistoryPath = path.join(common.tmpDir,
82+
'.dev-null-repl-history-file');
8183

8284
const tests = [
8385
{
@@ -178,6 +180,15 @@ const tests = [
178180
test: [UP],
179181
expected: [prompt]
180182
},
183+
{
184+
before: function before() {
185+
if (!common.isWindows)
186+
fs.symlinkSync('/dev/null', devNullHistoryPath);
187+
},
188+
env: { NODE_REPL_HISTORY: devNullHistoryPath },
189+
test: [UP],
190+
expected: [prompt]
191+
},
181192
{ // Make sure this is always the last test, since we change os.homedir()
182193
before: function before() {
183194
// Mock os.homedir() failure

0 commit comments

Comments
 (0)