James-Yu / LaTeX-Workshop

Boost LaTeX typesetting efficiency with preview, compile, autocomplete, colorize, and more.
MIT License
10.75k stars 535 forks source link

Cleaning with latex-workshop.latex.clean.command (latexmk) fails on Windows 11 #3679

Closed t4rf9 closed 1 year ago

t4rf9 commented 1 year ago

Preliminary questions [Required]

Disable all the other extensions except for LaTeX Workshop, restart VS Code, and check that you still see this issue. [Required]

You still see this issue?: Yes

Make sure to visit the wiki FAQ before filling an issue.

You visited the wiki?: Yes

If your issue is with compiling a document (not having to do with finding the root file of a project), check first that you can compile manually.

You can compile a TeX document manually?: Yes

Describe the bug [Required]

When I try to configure latex-workshop to clean with latexmk -c -auxdir=%OUTDIR%/.aux %TEX% (compiled with latexmk -auxdir=%OUTDIR%/.aux), I added "-c", "-auxdir=%OUTDIR%/.aux" and "%TEX%" to latex-workshop.latex.clean.args.

Then, it works well on macOS 13, but fails on Windows 11.

After investigating the source code, I found the problem comes from https://github.com/James-Yu/LaTeX-Workshop/blob/master/src/components/cleaner.ts#L160, where cross-spawn.spawn is used. Using cross-spawn-async.spawn would work well. That's because after parsing, cross-spawn.spawn directly returns the cleaning command latexmk, namely

  command: 'latexmk',
  args: [
    '-c',
    '-bibtex',
    '-auxdir=c:/Users/t4rf9/Programs/test/.aux',
    'c:\\Users\\t4rf9\\Programs\\test\\main.tex'
  ],
  options: {
    cwd: 'c:\\Users\\t4rf9\\Programs\\test',
    detached: true,
    windowsVerbatimArguments: true
  }

whilst cross-spawn-async.spawn returns cmd.exe as the command, namely

  command: 'C:\\WINDOWS\\system32\\cmd.exe',
  args: [
    '/s',
    '/c',
    '"latexmk "-c" "-bibtex" "-auxdir=c:/Users/t4rf9/Programs/test/.aux" "c:\\Users\\t4rf9\\Programs\\test\\main.tex""'
  ],
  options: {
    cwd: 'c:\\Users\\t4rf9\\Programs\\test',
    detached: true,
    windowsVerbatimArguments: true
  }

The latter wraps the cleaning command in a separated CMD, I think that's what makes it a success. However, I have no time to dig deeper. Hope this helps solving the bug.

To Reproduce

Steps to reproduce the behavior:

  1. Add "-c", "-auxdir=%OUTDIR%/.aux" and "%TEX%" to latex-workshop.latex.clean.args.
  2. Compile a tex file with latexmk -auxdir=%OUTDIR%/.aux
  3. Click "Clean up auxilliary files"
  4. Fails on Windows

Expected behavior

The cleaning should be successful.

Logs [Required]

Please paste the whole log messages here, not parts of ones. The log should start with Initializing LaTeX Workshop. It is very important to identify problems.

LaTeX Workshop Output [Required]

To access the log, click the 'TeX' icon on the Activity Bar on the left side, select 'View Log Messages', then select 'View LaTeX Workshop extension log'.

[01:32:14][Extension] Initializing LaTeX Workshop.
[01:32:14][Extension] Extension root: c:\Users\t4rf9\.vscode\extensions\james-yu.latex-workshop-9.6.0
[01:32:14][Extension] $PATH: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\libnvvp;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\dotnet\;C:\Program Files\ojdkbuild\java-11-openjdk-11.0.15-1\bin;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.2.0\;C:\Program Files\zlib123dllx64\dll_x64;C:\Program Files (x86)\GnuWin32\bin\;C:\Program Files\NVIDIA\CUDNN\v8.4;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;C:\Program Files\CMake\bin;C:\Program Files\Docker\Docker\resources\bin;C:\Program Files\Git\cmd;C:\Program Files\nodejs\;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\Users\t4rf9\AppData\Local\Programs\Python\Python310\Scripts\;C:\Users\t4rf9\AppData\Local\Programs\Python\Python310\;C:\Users\t4rf9\.cargo\bin;C:\Users\t4rf9\AppData\Local\Microsoft\WindowsApps;C:\Users\t4rf9\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\t4rf9\AppData\Local\Android\Sdk\platform-tools;C:\Users\t4rf9\AppData\Local\Programs\oh-my-posh\bin;C:\Users\t4rf9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\Scripts;C:\Users\t4rf9\AppData\Roaming\npm;C:\Users\t4rf9\AppData\Local\Programs\MiKTeX\miktex\bin\x64\
[01:32:14][Extension] $SHELL: undefined
[01:32:14][Extension] $LANG: undefined
[01:32:14][Extension] $LC_ALL: undefined
[01:32:14][Extension] process.platform: win32
[01:32:14][Extension] process.arch: x64
[01:32:14][Extension] vscode.env.appName: Visual Studio Code
[01:32:14][Extension] vscode.env.remoteName: undefined
[01:32:14][Extension] vscode.env.uiKind: 1
[01:32:14][Extension] LaTeX Workshop initialized.
[01:32:14][Format][Bib] Bibtex format config: {"tab":"    ","case":"lowercase","left":"{","right":"}","trailingComma":false,"sort":["key"],"alignOnEqual":true,"sortFields":false,"fieldsOrder":[],"firstEntries":["string","xdata"]}
[01:32:14][Extension] Trigger characters for intellisense of LaTeX documents: ["\\",",","{"]
[01:32:14][Manager] Current workspace folders: ["file:///c:/Users/t4rf9/Programs/test"]
[01:32:14][Manager] Found root file from active editor: %WS1%\main.tex
[01:32:15][Manager] Root file changed: from undefined to %WS1%\main.tex
[01:32:15][Manager] Start to find all dependencies.
[01:32:15][Manager] Root file languageId: latex
[01:32:15][Cacher][Watcher] Reset.
[01:32:15][Cacher] Adding %WS1%\main.tex .
[01:32:15][Cacher] Caching %WS1%\main.tex .
[01:32:15][Cacher] Updated inputs of %WS1%\main.tex .
[01:32:15][Cacher][Watcher] Watched %WS1%\main.tex .
[01:32:15][DupLabel] Checking for duplicate labels: %WS1%\main.tex .
[01:32:15][Cacher] Updated elements of %WS1%\main.tex .
[01:32:15][Cacher] Updated bibs of %WS1%\main.tex .
[01:32:15][Cacher] Cached %WS1%\main.tex .
[01:32:15][Cacher] Parsing .fls %WS1%\main.fls .
[01:32:15][Cacher] Adding C:\WINDOWS\Fonts\msyh.ttc .
[01:32:15][Cacher] Adding C:\WINDOWS\Fonts\simsun.ttc .
[01:32:15][Cacher] Found .aux %WS1%\main.tex from .fls %WS1%\main.fls , parsing.
[01:32:15][Cacher] Parsed .aux %WS1%\main.tex .
[01:32:15][Cacher] Parsed .fls %WS1%\main.fls .
[01:32:15][Cacher][Watcher] Watched C:\WINDOWS\Fonts\msyh.ttc .
[01:32:15][Cacher][Watcher] Watched C:\WINDOWS\Fonts\simsun.ttc .
[01:32:22][Commander] CLEAN command invoked.
[01:32:22][Manager] Current workspace folders: ["file:///c:/Users/t4rf9/Programs/test"]
[01:32:22][Manager] Found root file from active editor: %WS1%\main.tex
[01:32:22][Manager] Keep using the same root file: %WS1%\main.tex
[01:32:22][Cleaner] Clean temporary files command The command is latexmk:["-c","-bibtex","-auxdir=c:/Users/t4rf9/Programs/test/.aux","c:\\Users\\t4rf9\\Programs\\test\\main.tex"].
[01:32:22][Cleaner] The clean command failed. Context: 3221226505.

Developer Tools Console [Required]

To access the log, click 'help' -> 'Toggle Developer Tools' -> 'Console'. Paste anything suspicious.
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.tex.
Old grammar file: file:///c%3A/Users/t4rf9/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/latex/syntaxes/TeX.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/TeX.tmLanguage.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.tex.latex.
Old grammar file: file:///c%3A/Users/t4rf9/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/latex/syntaxes/LaTeX.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/LaTeX.tmLanguage.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.bibtex.
Old grammar file: file:///c%3A/Users/t4rf9/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/latex/syntaxes/Bibtex.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/Bibtex.tmLanguage.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.tex.markdown_latex_combined.
Old grammar file: file:///c%3A/Users/t4rf9/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/latex/syntaxes/markdown-latex-combined.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/markdown-latex-combined.tmLanguage.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope source.cpp.embedded.latex.
Old grammar file: file:///c%3A/Users/t4rf9/AppData/Local/Programs/Microsoft%20VS%20Code/resources/app/extensions/latex/syntaxes/cpp-grammar-bailout.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/cpp-grammar-bailout.tmLanguage.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.bibtex.
Old grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/Bibtex.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/torn4dom4n.latex-support-3.10.0/grammars/Bibtex.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.tex.latex.
Old grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/LaTeX.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/torn4dom4n.latex-support-3.10.0/grammars/LaTeX.json
register @ workbench.desktop.main.js:2053
workbench.desktop.main.js:2053 Overwriting grammar scope name to file mapping for scope text.tex.
Old grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/syntax/TeX.tmLanguage.json.
New grammar file: file:///c%3A/Users/t4rf9/.vscode/extensions/torn4dom4n.latex-support-3.10.0/grammars/TeX.json
register @ workbench.desktop.main.js:2053
DevTools failed to load source map: Could not load content for https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/t4rf9/.vscode/extensions/james-yu.latex-workshop-9.6.0/node_modules/pdfjs-dist/build/pdf.js.map: Connection error: net::ERR_NAME_NOT_RESOLVED

Screenshots

If applicable, add screenshots to help explain your problem.

Desktop [Required]

Please write exact version numbers. Please don't write latest instead of exact numbers.

Additional questions

Are you using VSCodium?

No

Are you using the Snap or Flatpack versions of VS Code?

No

Are you using LaTeX Workshop with VS Code Remote?

No

James-Yu commented 1 year ago

This might be a bigger issue than it looks. I will check the details.

James-Yu commented 1 year ago

Using cross-spawn-async instead is not an option as it was deprecated and superceded by cross-spawn, according to https://www.npmjs.com/package/cross-spawn-async .

James-Yu commented 1 year ago

I suspect it to be a local problem or the naming of .aux on win. You may take a look into https://github.com/James-Yu/LaTeX-Workshop/actions/runs/4060600101/jobs/6989848291 on test [104] and https://github.com/James-Yu/LaTeX-Workshop/blob/ab357e31ac2f2b3ada99e3461d2cc9c893cc7677/test/suites/10_cleaner.test.ts#L79-L90 It just tests your use case.

t4rf9 commented 1 year ago

In further attempts, I find that it has nothing to do with -auxdir... Even if I configure to run latexmk -c %TEX%, the same error is produced with no file removed. I'll try to explore this in depth.

t4rf9 commented 1 year ago

On my device, child_process.spawn just fails when trying to run latexmk which is from MiKTeX and returns an exit-code of 3221226505, but works fine for other commands like node main.js. latexmk works fine from PowerShell or CMD manually. That might be a node.js problem or MiKTeX problem.

t4rf9 commented 1 year ago

Adding shell: true to the options paramater in https://github.com/James-Yu/LaTeX-Workshop/blob/master/src/components/cleaner.ts#L160 can be a solution, but a shell window may pop out.

t4rf9 commented 1 year ago

Using

command = 'perl';
args = [
        'C:\\Users\\t4rf9\\AppData\\Local\\Programs\\MiKTeX\\scripts\\latexmk\\latexmk.pl',
        '-c',
        '-bibtex',
        '-auxdir=c:/Users/t4rf9/Programs/test/.aux',
        'c:/Users/t4rf9/Programs/test/main.tex'
];
options = { cwd: 'c:\\Users\\t4rf9\\Programs\\test', detached: true };

works fine.

So I think the problem comes from latexmk.exe, which is installed by MiKTeX from CTAN.

t4rf9 commented 1 year ago

spawning a new process with subprocess.run in python executes latexmk successfully, so the problem seems to lie in nodejs

t4rf9 commented 1 year ago

Using

command = 'latexmk';
args = [
        '-c',
        '-bibtex',
        '-auxdir=c:/Users/t4rf9/Programs/test/.aux',
        'c:/Users/t4rf9/Programs/test/main.tex'
];
options = { cwd: 'c:\\Users\\t4rf9\\Programs\\test', detached: false };

also works.

I am not sure whether it's necessary to set detached as true.

t4rf9 commented 1 year ago

The following python script fails with the same return code, which proves that it's spawning a detached latexmk process that causes the issue.

import subprocess
p = subprocess.Popen(["latexmk", "-v"], creationflags=subprocess.DETACHED_PROCESS)
p.wait()
print(p.returncode)
t4rf9 commented 1 year ago

@James-Yu I find out that settting detached: true in the spawning options may be the cause of the problem and setting detached: false avoids the problem by using cross-spawn to spawn latexmk -c -bibtex -auxdir=c:/Users/t4rf9/Programs/test/.aux c:/Users/t4rf9/Programs/test/main.tex in a nodejs terminal.

If there is no other problems that may be caused, I think it's ok to try detached: false.

BTW, your tests are run with TeXLive, while in my case I used MiKTeX.

t4rf9 commented 1 year ago

This is my code for the mentioned attempt of detached: false, which works well for me.

var cs = require('cross-spawn');

let command = 'latexmk';
let args = [
    '-c',
    '-bibtex',
    '-auxdir=c:/Users/t4rf9/Programs/test/.aux',
    'c:/Users/t4rf9/Programs/test/main.tex'
];
let options = { cwd: 'c:\\Users\\t4rf9\\Programs\\test', detached: false };

let proc = cs.spawn(command, args, options);

let output = '';
proc.stdout.setEncoding('utf8')
proc.stdout.on('data', function (data) {
    output += data.toString();
});

proc.stderr.setEncoding('utf8')
proc.stderr.on('data', function (data) {
    output += data.toString();
});

proc.on('close', function (code) {
    console.log('exit code: ' + code);
    console.log();
    console.log(output);
});
t4rf9 commented 1 year ago

@James-Yu Besides, when using options = { cwd: 'c:\\Users\\t4rf9\\Programs\\test', detached: true, shell: true }, everything works as well. This can be a better choice than detached: false in case the parent process exits, although I think the case is quite unlikely to happen.

I installed TeXLive on my device and find out that the latexmk.exe generated by TeXLive always runs in a new shell, so it works with the original options = { cwd: 'c:\\Users\\t4rf9\\Programs\\test', detached: true }.