LaTeX-Utilities icon indicating copy to clipboard operation
LaTeX-Utilities copied to clipboard

fix(texdef): improve error handling, set CWD, and add `--tex` config

Open maxchang3 opened this issue 8 months ago • 0 comments

Fixes #407. For reproduction steps, see #407 and texdef-fontspec-repro.

Before and After

Before After

Description

While investigating why texdef couldn’t resolve macros defined in .cls files within the same folder, I manually ran the logged texdef command and confirmed it worked as expected. However, in the extension, the spawned process exited immediately without any output.

Debugging revealed the following issues:

  • Issue 1: We were listening on stdout.on('error'), but error messages from texdef are emitted via stderr.on('data'), so errors were never caught.
    https://github.com/tecosaur/LaTeX-Utilities/blob/a3265cc6827bd383eac4945ea8b914c0b4896fa0/src/providers/macroDefinitions.ts#L105-L108
  • Issue 2: The spawned process did not have the correct working directory (cwd), which prevented it from accessing files in the workspace folder.
    • Based on testing, the default cwd on macOS is /, while on Windows, it defaults to the VS Code directory.
    • https://github.com/tecosaur/LaTeX-Utilities/blob/a3265cc6827bd383eac4945ea8b914c0b4896fa0/src/providers/macroDefinitions.ts#L96
  • Issue 3: Some packages (e.g., fontspec) require an engine like xelatex, but texdef defaults to latex.

Summary of Changes

  • Fixed error handling by switching from stdout.on('error') to stderr.on('data'), since stdout and stderr can appear simultaneously (confirmed by testing), so no early return on stderr.
  • Added cwd to spawn options to ensure texdef runs in the correct context.
  • Introduced a new configuration option latex-utilities.texdef.tex to set the --tex argument.

Future Improvements

  • Automatically detect the correct engine based on the project context.
  • Provide user feedback if texdef fails due to an incompatible engine.
  • Should we cache the result of texdef?

maxchang3 avatar Apr 25 '25 16:04 maxchang3