Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,12 @@
"default": true,
"markdownDescription": "Force the use the recipe mechanism even if some magic comments are present."
},
"latex-workshop.latex.build.fromWorkspaceFolder": {
"scope": "resource",
"type": "boolean",
"default": false,
"markdownDescription": "Run the recipe from the workspace folder. If false, the recipe is run from the directory containing the root file. The setting has no effect on external commands `#latex-workshop.latex.external.build.command`."
},
"latex-workshop.latex.outDir": {
"scope": "resource",
"type": "string",
Expand Down
10 changes: 5 additions & 5 deletions src/compile/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,30 @@ function spawnProcess(step: Step): ProcessEnv {
if (step.index === 0 || configuration.get('latex.build.clearLog.everyRecipeStep.enabled') as boolean) {
logger.clearCompilerMessage()
}
let cwd = step.cwd

logger.refreshStatus('sync~spin', 'statusBar.foreground', undefined, undefined, ' ' + queue.getStepString(step))
logger.logCommand(`Recipe step ${step.index + 1}`, step.command, step.args)
logger.log(`env: ${JSON.stringify(step.env)}`)
logger.log(`root: ${step.rootFile}`)
logger.log(`cwd: ${cwd}`)

const env: ProcessEnv = { ...process.env, ...step.env }
env['max_print_line'] = lw.constant.MAX_PRINT_LINE

if (!step.isExternal &&
(step.name.startsWith(lw.constant.TEX_MAGIC_PROGRAM_NAME) ||
step.name.startsWith(lw.constant.BIB_MAGIC_PROGRAM_NAME))) {
logger.log(`cwd: ${path.dirname(step.rootFile)}`)

const args = step.args
if (args && !step.name.endsWith(lw.constant.MAGIC_PROGRAM_ARGS_SUFFIX)) {
// All optional arguments are given as a unique string (% !TeX options) if any, so we use {shell: true}
lw.compile.process = lw.external.spawn(`${step.command} ${args[0]}`, [], {cwd: path.dirname(step.rootFile), env, shell: true})
lw.compile.process = lw.external.spawn(`${step.command} ${args[0]}`, [], {cwd, env, shell: true})
} else {
lw.compile.process = lw.external.spawn(step.command, args ?? [], {cwd: path.dirname(step.rootFile), env})
lw.compile.process = lw.external.spawn(step.command, args ?? [], {cwd, env})
}
} else if (!step.isExternal) {
let cwd = path.dirname(step.rootFile)
if (step.command === 'latexmk' && step.rootFile === lw.root.subfiles.path && lw.root.dir.path) {
if (step.command === 'latexmk' && step.rootFile === lw.root.subfiles.path && lw.root.dir.path && cwd === path.dirname(step.rootFile)) {
cwd = lw.root.dir.path
}
logger.log(`cwd: ${cwd}`)
Expand Down
11 changes: 5 additions & 6 deletions src/compile/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ const stepQueue: StepQueue = { steps: [], nextSteps: [] }
* @param {string} recipeName - The name of the recipe to which the tool
* belongs.
* @param {number} timestamp - The timestamp when the recipe is called.
* @param {boolean} [isExternal=false] - Whether the tool is an external
* command.
* @param {string} [cwd] - The current working directory if the tool is an
* external command.
* @param {boolean} isExternal - Whether the tool is an external command.
* @param {string} [cwd] - The current working directory.
*/
function add(tool: Tool, rootFile: string | undefined, recipeName: string, timestamp: number, isExternal: boolean = false, cwd?: string) {
function add(tool: Tool, rootFile: string | undefined, recipeName: string, timestamp: number, isExternal: boolean, cwd: string) {
// Wrap the tool as a RecipeStep or ExternalStep
let step: Step
if (!isExternal && rootFile !== undefined) {
Expand All @@ -32,12 +30,13 @@ function add(tool: Tool, rootFile: string | undefined, recipeName: string, times
step.isRetry = false
step.isExternal = false
step.isSkipped = false
step.cwd = cwd
} else {
step = tool as ExternalStep
step.recipeName = 'External'
step.timestamp = timestamp
step.isExternal = true
step.cwd = cwd || ''
step.cwd = cwd
}

// Add the step to the appropriate queue (steps or nextSteps)
Expand Down
10 changes: 9 additions & 1 deletion src/compile/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ function setDockerPath() {
*/
export async function build(rootFile: string, langId: string, buildLoop: () => Promise<void>, recipeName?: string) {
logger.log(`Build root file ${rootFile}`)
let cwd: string = path.dirname(lw.file.toUri(rootFile).fsPath)
const configuration = vscode.workspace.getConfiguration('latex-workshop')
if (configuration.get('latex.build.fromWorkspaceFolder')) {
const workspaceFolder = vscode.workspace.getWorkspaceFolder(lw.file.toUri(rootFile))
if (workspaceFolder) {
cwd = workspaceFolder.uri.fsPath
}
}

// Save all open files in the workspace
await vscode.workspace.saveAll()
Expand All @@ -75,7 +83,7 @@ export async function build(rootFile: string, langId: string, buildLoop: () => P

// Add tools to the queue with timestamp
const timestamp = Date.now()
tools.forEach(tool => queue.add(tool, rootFile, recipeName || 'Build', timestamp))
tools.forEach(tool => queue.add(tool, rootFile, recipeName || 'Build', timestamp, false, cwd))

// #4513 If the recipe contains a forced latexmk compilation, don't set the
// compiledPDFPath so that PDF refresh is handled by file watcher.
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export type RecipeStep = Tool & {
index: number,
isExternal: false,
isRetry: boolean,
isSkipped: boolean
isSkipped: boolean,
cwd: string
}

export type ExternalStep = Tool & {
Expand Down
15 changes: 15 additions & 0 deletions test/suites/01_build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ suite.skip('Build TeX files test suite', () => {
await vscode.workspace.getConfiguration('latex-workshop').update('latex.outDir', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.recipes', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.build.forceRecipeUsage', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.build.fromWorkspaceFolder', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.doNotPrompt', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.rootFile.useSubFile', undefined)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.search.rootFiles.include', undefined)
Expand Down Expand Up @@ -279,4 +280,18 @@ suite.skip('Build TeX files test suite', () => {
assert.ok(fs.existsSync(path.resolve(fixture, 'out space/copy.pdf')))
}, ['win32'])

test.run('build from workspace folder', async (fixture: string) => {
const texFile = path.join(path.basename(fixture), 'tex', 'main.tex')
const outdir = path.join(fixture, 'build')

const tools = [{name: 'latexmk', command: 'latexmk', args: [ '-synctex=1', '-interaction=nonstopmode', '-file-line-error', '-pdf', `-outdir=${outdir}`, texFile ]}]
await vscode.workspace.getConfiguration('latex-workshop').update('latex.tools', tools)
await vscode.workspace.getConfiguration('latex-workshop').update('latex.build.fromWorkspaceFolder', true)
await test.load(fixture, [
{src: 'base.tex', dst: 'tex/main.tex'}
], {skipCache: true})
await test.build(fixture, 'tex/main.tex')
assert.ok(fs.existsSync(path.resolve(outdir, 'main.pdf')))
})

})
30 changes: 15 additions & 15 deletions test/units/05_compile_queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should clear the queue', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')
assert.ok(queue.getStep())

queue.clear()
assert.strictEqual(queue.getStep(), undefined)
})

it('should get the next step from the queue', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'bibtex', command: 'bibtex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')
queue.add({ name: 'bibtex', command: 'bibtex' }, 'main.tex', 'Recipe1', Date.now(), false, '')

const step1 = queue.getStep()
const step2 = queue.getStep()
Expand All @@ -39,7 +39,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should add a Tool as a RecipeStep to the queue', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now())
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')

const step = queue.getStep()
assert.ok(step)
Expand All @@ -59,11 +59,11 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should prepend a Tool as a RecipeStep to the queue', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now())
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')

let step = queue.getStep()
queue.clear()
queue.add({ name: 'latex', command: 'pdflatex' }, 'alt.tex', 'Recipe1', Date.now())
queue.add({ name: 'latex', command: 'pdflatex' }, 'alt.tex', 'Recipe1', Date.now(), false, '')

assert.ok(step)
queue.prepend(step)
Expand All @@ -76,22 +76,22 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should check if the last step in the queue', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')

let step = queue.getStep()
assert.ok(step)
assert.ok(queue.isLastStep(step))

queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now() + 1, false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now() + 1, false, '')

step = queue.getStep()
assert.ok(step)
assert.ok(queue.isLastStep(step))
})

it('should get the formatted string representation of a step', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')

const step = queue.getStep()
assert.ok(step)
Expand All @@ -100,8 +100,8 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {
})

it('should get correct step repr with multiple recipes', () => {
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now() + 1, false)
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now(), false, '')
queue.add({ name: 'latex', command: 'pdflatex' }, 'main.tex', 'Recipe1', Date.now() + 1, false, '')
const step = queue.getStep()
assert.ok(step)
const stepString = queue.getStepString(step)
Expand All @@ -110,8 +110,8 @@ describe(path.basename(__filename).split('.')[0] + ':', () => {

it('should get correct step repr with multiple tools in one recipe', () => {
const recipeTime = Date.now()
queue.add({ name: 'latex1', command: 'pdflatex' }, 'main.tex', 'Recipe1', recipeTime, false)
queue.add({ name: 'latex2', command: 'pdflatex' }, 'main.tex', 'Recipe1', recipeTime, false)
queue.add({ name: 'latex1', command: 'pdflatex' }, 'main.tex', 'Recipe1', recipeTime, false, '')
queue.add({ name: 'latex2', command: 'pdflatex' }, 'main.tex', 'Recipe1', recipeTime, false, '')

let step = queue.getStep()
assert.ok(step)
Expand Down
Loading