Skip to content

Commit bb041ea

Browse files
committed
repl: support hidden history file on Windows
On Windows when REPL history file has the hidden attribute node will fail when trying to open it in 'w' mode. This changes the mode to 'r+'. The file is guaranteed to exists because of earlier open call with 'a+'. Fixes: #5261 PR-URL: #12207 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f3f9dd7 commit bb041ea

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

lib/internal/repl.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,19 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
164164
}
165165
}
166166

167-
fs.open(historyPath, 'w', onhandle);
167+
fs.open(historyPath, 'r+', onhandle);
168168
}
169169

170170
function onhandle(err, hnd) {
171+
if (err) {
172+
return ready(err);
173+
}
174+
fs.ftruncate(hnd, 0, (err) => {
175+
return onftruncate(err, hnd);
176+
});
177+
}
178+
179+
function onftruncate(err, hnd) {
171180
if (err) {
172181
return ready(err);
173182
}

test/fixtures/.empty-hidden-repl-history-file

Whitespace-only changes.

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

+15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ const oldHistoryPath = path.join(fixtures, 'old-repl-history-file.json');
7676
const enoentHistoryPath = path.join(fixtures, 'enoent-repl-history-file.json');
7777
const emptyHistoryPath = path.join(fixtures, '.empty-repl-history-file');
7878
const defaultHistoryPath = path.join(common.tmpDir, '.node_repl_history');
79+
const emptyHiddenHistoryPath = path.join(fixtures,
80+
'.empty-hidden-repl-history-file');
7981

8082
const tests = [
8183
{
@@ -163,6 +165,19 @@ const tests = [
163165
test: [UP],
164166
expected: [prompt, replFailedRead, prompt, replDisabled, prompt]
165167
},
168+
{
169+
before: function before() {
170+
if (common.isWindows) {
171+
const execSync = require('child_process').execSync;
172+
execSync(`ATTRIB +H "${emptyHiddenHistoryPath}"`, (err) => {
173+
assert.ifError(err);
174+
});
175+
}
176+
},
177+
env: { NODE_REPL_HISTORY: emptyHiddenHistoryPath },
178+
test: [UP],
179+
expected: [prompt]
180+
},
166181
{ // Make sure this is always the last test, since we change os.homedir()
167182
before: function before() {
168183
// Mock os.homedir() failure

0 commit comments

Comments
 (0)