From 9f68bc5660c0ff1a29ab6d7d6fd729fbd544705f Mon Sep 17 00:00:00 2001 From: Jakob Ackermann Date: Fri, 5 Jul 2024 18:29:04 +0200 Subject: [PATCH] Merge pull request #19296 from overleaf/jpa-issue-19290-3 [clsi] atomic writing of LaTeXMk output GitOrigin-RevId: d81c497370587b98fc7ad282035cd59b0ae09ec8 --- services/clsi/app/js/LatexRunner.js | 13 ++++++++----- services/clsi/test/unit/js/LatexRunnerTests.js | 15 +++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/services/clsi/app/js/LatexRunner.js b/services/clsi/app/js/LatexRunner.js index 51286dce91..d956ee4949 100644 --- a/services/clsi/app/js/LatexRunner.js +++ b/services/clsi/app/js/LatexRunner.js @@ -110,11 +110,14 @@ function _writeLogOutput(projectId, directory, output, callback) { // internal method for writing non-empty log files function _writeFile(file, content, cb) { if (content && content.length > 0) { - fs.writeFile(file, content, err => { - if (err) { - logger.error({ err, projectId, file }, 'error writing log file') // don't fail on error - } - cb() + fs.unlink(file, () => { + fs.writeFile(file, content, { flag: 'wx' }, err => { + if (err) { + // don't fail on error + logger.error({ err, projectId, file }, 'error writing log file') + } + cb() + }) }) } else { cb() diff --git a/services/clsi/test/unit/js/LatexRunnerTests.js b/services/clsi/test/unit/js/LatexRunnerTests.js index 4da40837c4..ca36d7308f 100644 --- a/services/clsi/test/unit/js/LatexRunnerTests.js +++ b/services/clsi/test/unit/js/LatexRunnerTests.js @@ -23,6 +23,9 @@ describe('LatexRunner', function () { } this.fs = { writeFile: sinon.stub().yields(), + unlink: sinon + .stub() + .yields(new Error('ENOENT: no such file or directory, unlink ...')), } this.LatexRunner = SandboxedModule.require(MODULE_PATH, { requires: { @@ -99,11 +102,19 @@ describe('LatexRunner', function () { it('should record the stdout and stderr', function () { this.fs.writeFile.should.have.been.calledWith( this.directory + '/' + 'output.stdout', - 'this is stdout' + 'this is stdout', + { flag: 'wx' } ) this.fs.writeFile.should.have.been.calledWith( this.directory + '/' + 'output.stderr', - 'this is stderr' + 'this is stderr', + { flag: 'wx' } + ) + this.fs.unlink.should.have.been.calledWith( + this.directory + '/' + 'output.stdout' + ) + this.fs.unlink.should.have.been.calledWith( + this.directory + '/' + 'output.stderr' ) })