diff --git a/src/compile/recipe.ts b/src/compile/recipe.ts index 2b3a1aa7d..2b712bd3e 100644 --- a/src/compile/recipe.ts +++ b/src/compile/recipe.ts @@ -95,13 +95,19 @@ function createOutputSubFolders(rootFile: string) { outDir = path.resolve(rootDir, outDir) } logger.log(`outDir: ${outDir} .`) - lw.cache.getIncludedTeX(rootFile).forEach(file => { + lw.cache.getIncludedTeX(rootFile).forEach(async file => { const relativePath = path.dirname(file.replace(rootDir, '.')) const fullOutDir = path.resolve(outDir, relativePath) // To avoid issues when fullOutDir is the root dir // Using fs.mkdir() on the root directory even with recursion will result in an error try { - if (! (lw.external.existsSync(fullOutDir) && lw.external.statSync(fullOutDir)?.isDirectory())) { + const fileStat = await lw.file.exists(fullOutDir) + if ( + !fileStat || + ![vscode.FileType.Directory, vscode.FileType.Directory | vscode.FileType.SymbolicLink].includes( + fileStat.type + ) + ) { lw.external.mkdirSync(fullOutDir, { recursive: true }) } } catch (e) { diff --git a/src/core/file.ts b/src/core/file.ts index 5ff626a44..31edede2b 100644 --- a/src/core/file.ts +++ b/src/core/file.ts @@ -490,23 +490,22 @@ async function read(filePath: string, raise: boolean = false): Promise} - A promise that resolves to `true` if the file * or directory exists, and `false` otherwise. */ -async function exists(uri: vscode.Uri | string): Promise { +async function exists(uri: vscode.Uri | string): Promise { if (typeof(uri) === 'string') { uri = vscode.Uri.file(uri) } try { - await lw.external.stat(uri) - return true + return await lw.external.stat(uri) } catch { return false } diff --git a/src/lw.ts b/src/lw.ts index 43186ab21..230d916b7 100644 --- a/src/lw.ts +++ b/src/lw.ts @@ -50,9 +50,7 @@ export const lw = { spawn: wrapper(cs.spawn), sync: wrapper(cs.sync), stat: wrapper(vscode.workspace.fs.stat), - existsSync: wrapper(fs.existsSync), mkdirSync: wrapper(fs.mkdirSync), - statSync: wrapper(fs.statSync), chmodSync: wrapper(fs.chmodSync) }, onConfigChange, diff --git a/test/units/06_compile_recipe.test.ts b/test/units/06_compile_recipe.test.ts index 3f3f86458..b7dd0a000 100644 --- a/test/units/06_compile_recipe.test.ts +++ b/test/units/06_compile_recipe.test.ts @@ -1,6 +1,5 @@ import * as vscode from 'vscode' import * as path from 'path' -import * as fs from 'fs' import Sinon, * as sinon from 'sinon' import { assert, get, log, mock, set, sleep } from './utils' import { lw } from '../../src/lw' @@ -209,15 +208,13 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { const rootFile = set.root(fixture, 'main.tex') const relativeOutDir = 'output' const expectedOutDir = path.resolve(path.dirname(rootFile), relativeOutDir) - const existsStub = sinon.stub(lw.external, 'existsSync').returns(false) - const statStub = sinon.stub(lw.external, 'statSync').returns(fs.statSync(get.path(fixture))) const mkdirStub = sinon.stub(lw.external, 'mkdirSync').returns(undefined) + const stub = sinon.stub(lw.file, 'exists').resolves(false) getOutDirStub.returns(relativeOutDir) await build(rootFile, 'latex', async () => {}) - existsStub.restore() - statStub.restore() mkdirStub.restore() + stub.restore() assert.pathStrictEqual(mkdirStub.getCalls()[0].args[0].toString(), expectedOutDir) assert.deepStrictEqual(mkdirStub.getCalls()[0].args[1], { recursive: true }) @@ -226,8 +223,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { it('should not create the output directory if it already exists', async () => { const rootFile = set.root(fixture, 'main.tex') const relativeOutDir = 'output' - const existsStub = sinon.stub(lw.external, 'existsSync').returns(true) - const statStub = sinon.stub(lw.external, 'statSync').returns(fs.statSync(get.path(fixture))) + const stub = sinon.stub(lw.file, 'exists').resolves({ type: vscode.FileType.Directory, ctime: 0, mtime: 0, size: 0 }) const mkdirStub = sinon.stub(lw.external, 'mkdirSync').returns(undefined) getOutDirStub.returns(relativeOutDir) @@ -235,9 +231,8 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { mkdirStub.resetHistory() await build(rootFile, 'latex', async () => {}) - existsStub.restore() - statStub.restore() mkdirStub.restore() + stub.restore() assert.ok(!mkdirStub.called) })