Skip to content

Commit b09bf51

Browse files
bzozevanlucas
authored andcommitted
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 <[email protected]>
1 parent 7d87edc commit b09bf51

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
@@ -165,10 +165,19 @@ function setupHistory(repl, historyPath, oldHistoryPath, ready) {
165165
}
166166
}
167167

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

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

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)