Skip to content

Commit c20e87a

Browse files
committed
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 dc3bbb4 commit c20e87a

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

lib/internal/repl.js

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

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

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

+11
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)