Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting failed error #376

Closed
michaelaye opened this issue Jan 12, 2018 · 33 comments · Fixed by #381
Closed

Formatting failed error #376

michaelaye opened this issue Jan 12, 2018 · 33 comments · Fixed by #381
Labels
external issue Issue with something else than LaTeX-Wokshop

Comments

@michaelaye
Copy link

Description

When I activate globally "editor.formatOnSave" (as is done for a while it seems in VSCode), I get this error, everytime I save a Latex file:

screenshot 2018-01-12 13 53 36

I suspect this has something to do wit the fact that I need to enable "formatOnSave" on a global level now, to have it working for my Python programming.

I guess your package does not support formatting, which is why I get this error?

I deactivated the error message by adding this language-specific setting to my settings, like so:

    "[latex]": {
        "editor.formatOnSave": false,
    }

So, I'm fine, I was just wondering if there is actually automatic formatting available for this extension somehow? I will close this issue immediately, but wanted to create the issue for somebody else to find.

@zoehneto
Copy link
Contributor

This extension actually supports formating using latexindent if you have it installed. The log should explain why it didn't work.

@michaelaye
Copy link
Author

and how do i find that mysterious log? ;)

@zoehneto
Copy link
Contributor

You can find it by clicking on View -> Output in the menu and then selecting LaTeX Workshop from the dropdown on the right. It should have an entry starting with Formatting failed: and the error message.

@thedadams
Copy link

I am having this same issue now after using the extension for some time. I have latexindent installed and the Output has no information in it.

@vnermolaev
Copy link

All right I found the problem. latexindent is distributed as a perl script while on Win you need an exe file. Thus:

  1. Download a zip distro of the package from CTAN
  2. Rename the latexindent folder to whatever you like, then create another latexindent folder and place the contents of the zip file there.
  3. Disable formatOnSave for tex files as pointed out in the first post, i.e.,
"[latex]": {
    "editor.formatOnSave": false,
}
  1. Configure the toolchain, mine looks as follows:
        "latex-workshop.latex.toolchain": [
        {
            "command": "c:/...path.../latexindent/latexindent.exe",
            "args": [
                "-w",
                "-s",
                "%DOC%"
            ]
        },
        {
            "command": "texify",
            "args": [
                "--synctex",
                "--pdf",
                "--tex-option=\"-interaction=nonstopmode\"",
                "--tex-option=\"-file-line-error\"",
                "%DOC%.tex"
            ]
        }
    ]
  1. Optionally specify in defaultSettings.yaml or your own conf file that you want one backup and whatever else you like to change.

@zoehneto
Copy link
Contributor

Do you see Can not find latexindent in PATH! in the extension log? As you said, the formatter implementation currently expects latexindent to be available as an exe file in your path on windows. If MiKTeX always distributes latexindent as a perl script then I could change the implementation to consider that. Then you won't have to make any changes to your toolchain and could use format on save.

@vnermolaev
Copy link

I've been looking for Formatting failed: as you suggested earlier. That's not in the log.

latexindent is in my PATH, but it's a different latexindent than the one I used in the config! The former weighs 11kb while the latter 11MB. If I replace latexindent in the config (as I posted above) with the one I have in PATH, the toolchain fails with the message

latexindent.exe: The script engine could not be found.
latexindent.exe: Data: scriptEngine="perl.exe"

There is something fishy with MiKTeX. Apparently the small latexindent in PATH is just wrapper calling per interpreter.

@thedadams
Copy link

I am working with TeXLive on macOS, so I don't think the above fix works for me.

I currently have editor.formatOnSave set to false so that the error won't pop up.

@vnermolaev
Copy link

you can still try to fall back on installing the perl interpreter and either it will work as is or employ approach similar to mine.

@zoehneto
Copy link
Contributor

There are two possible log messages: if the extension can't find latexindent at all (possible issue with MiKTeX) you should see Can not find latexindent in PATH! in the extension log. If latexindent is found in the PATH but doesn't work (for example if some perl modules are missing on linux) you should see Formatting failed: in the log with latexindents error message. Please check which one applies to you.

@thedadams
Copy link

Neither of those applies to me because the output log is empty (see my comment above).

@zoehneto
Copy link
Contributor

If you don't use editor.formatOnSave you can open your latex file you want to format, right click on the content and select Format Document. You should then see the result in the log.

@vnermolaev
Copy link

Neither of those applies to me either. As I said latexindent is in my PATH but it is apparently just a wrapper around the perl script and it does not work if executed in command line. Format Document fails with no output except the aforementioned error pop-up.

PS: Win 7, MiKTex 2.9.

@zoehneto
Copy link
Contributor

Do you use Latex Workshop 3.12.1 or later (logging for formatting was added with 3.12.1) ? Latexindent in your PATH means latexindent.exe in your path and found by the where command on windows, otherwise the extension won't recognize it.

@zoehneto
Copy link
Contributor

So I just installed MiKTeX on Windows to reproduce this issue. Here is how I got it to work:

  • According to this StackExchange post, MiKTeX stopped distributing Perl a couple of years ago so you will need to download and install Perl from here
  • Then you need to install the latexindent package using the MiKTeX Package Manager
  • Afterwards you have to install the missing perl modules from cpan using cpan on the command line. In my case those were Log::Log4perl , Log::Dispatch::FileShared, YAML::Tiny and Unicode::GCString (the exact missing packages can be found by executing the formatter a mentioned above and checking the log)
  • At last there was one bug I found which I will submit a PR for shortly

@vnermolaev
Copy link

Thanks for looking into that. As I said latexindent.exe is in my %PATH% and the corresponding file is an exe file, but it weighs only 55 kb and fails with

latexindent.exe: The script engine could not be found.
latexindent.exe: Data: scriptEngine="perl.exe"

So, I assume, it requires the modules that you listed above.

The exe file that one extracts from the latexindent package downloaded from CTAN is 11 MB and it works as is. So there are 2 options: stick to my fix, or replace the original latexindent.exe (found with where) with the one from CTAN.

@zoehneto
Copy link
Contributor

I would guess that the latexindent in your PATH is the one provided by MiKTeX or the placeholder provided by MiKTeX (this would require you to install perl as described above). It would be interesting whether format on save works if you delete the latexindent provided by MiKTeX and add the one you downloaded to the PATH. If this works one possible solution would be to provide a config option for Latex Studio to specify the directory where latexindent is installed.

@vnermolaev
Copy link

I wrote exactly this a couple of messages ago.
I would rather have it configurable in Latex Studio.

@zoehneto
Copy link
Contributor

I will look into adding a config option. Your message described a workaround using the toolchain though and I was interested whether the detection would work if you have the downloaded version in your PATH. With a config option this won't be an issue though so it might be the best solution for Windows.

@zoehneto
Copy link
Contributor

I've created a PR for the config option, so once PRs #381 and #383 are merged you can set the config option latex-workshop.latexindent.path to point your downloaded latexindent package and the formatting should work on save and with standard vs code methods instead of requiring you to add it to the toolchain.

@zoehneto
Copy link
Contributor

@quicky84 James just released a new version which contains the fixes described above. Could you please test whether the issue is now resolved?

@vnermolaev
Copy link

I am afraid I can't as I moved from win to mac.

@yan-kuan
Copy link

@thedadams , can u format ur code? (Not saving after formatting) Or u already solved it? It seems it's just one case of failure.

I'm a Mac User too. My issue is unable to format codes. As @quicky84 said, the problem might be in latexindent not LaTeX-Workshop. Here is my error message:

[18:36:40] Formatting failed: Command failed: latexindent -c "/Users/yenWu/git/report-poly" "/Users/yenWu/git/report-poly/__latexindent_temp.tex" -y="defaultIndent: '    '"
Can't locate File/HomeDir.pm in @INC (you may need to install the File::HomeDir module) (@INC contains: /usr/local/texlive/2017/texmf-dist/scripts/latexindent /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm line 23.
BEGIN failed--compilation aborted at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/GetYamlSettings.pm line 23.
Compilation failed in require at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm line 19.
BEGIN failed--compilation aborted at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/LogFile.pm line 19.
Compilation failed in require at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/Document.pm line 25.
BEGIN failed--compilation aborted at /usr/local/texlive/2017/texmf-dist/scripts/latexindent/LatexIndent/Document.pm line 25.
Compilation failed in require at /Library/TeX/texbin/latexindent line 27.
BEGIN failed--compilation aborted at /Library/TeX/texbin/latexindent line 27.

If so, here is a shortcut.

sudo cpan Unicode::GCString
sudo cpan App::cpanminus
sudo cpan YAML::Tiny
sudo perl -MCPAN -e 'install "File::HomeDir"'

More details u can found in this issue in Atom-beaultifier.

@thedadams
Copy link

@yenwu Yes, I fixed it. I was missing some perl modules. Thanks for checking.

@Stephen-X
Copy link

Stephen-X commented Apr 13, 2018

I encountered the same problem on Windows, and I'm using MiKTeX and Strawberry Perl distributions. Turns out it's due to two perl modules missing: Log::Log4perl and Log::Dispatch (you can see the error messages mentioning them in LaTex Workshop's output: View -> Output -> Select "LaTex Workshop" from drop-down menu). You can simply run the following two commands in your command line terminal to resolve this problem (note that you have to force install Log::Log4perl as one of its tests keeps failing on Windows even though the module is perfectly functional):

cpan -fi Log::Log4perl
cpan Log::Dispatch

@him229
Copy link

him229 commented Jun 20, 2018

Encountered the same problem on Mac. Here are the packages I installed to make it work.

sudo cpan Log::Log4perl
sudo cpan Log::Dispatch
sudo cpan YAML::Tiny
sudo cpan File::HomeDir
sudo cpan Unicode::GCString

@drmattprior
Copy link

drmattprior commented Jun 20, 2018

Installed all these packages and get the error

Cannot write to 'indent.log': Permission denied at /Library/Perl/5.18/Log/Dispatch/File.pm line 100.

@yzy1996
Copy link

yzy1996 commented Oct 24, 2018

After I tried many many times in two days I solved it.

Solution is that : "latex-workshop.latexindent.path" in vs code settings you can change to your path ,just like this
"latex-workshop.latexindent.path": "C:\\Users\\Jerry\\Downloads\\latexindent\\latexindent.exe"

Before of this, you should download latexindent.exe from latexindent . I find that installing from MikTeX console packages isn't working, so you'd best installed by yourself.

@TheFabbiusCorp
Copy link

After I tried many many times in two days I solved it.

Solution is that : "latex-workshop.latexindent.path" in vs code settings you can change to your path ,just like this
"latex-workshop.latexindent.path": "C:\\Users\\Jerry\\Downloads\\latexindent\\latexindent.exe"

Before of this, you should download latexindent.exe from latexindent . I find that installing from MikTeX console packages isn't working, so you'd best installed by yourself.

This worked for me on Windows 10. Thank you!
Hope a fix will be released sooner or later.

@jlelong
Copy link
Collaborator

jlelong commented Jan 20, 2019

@fabiocapasso93 unfortunately this cannot be fixed on the extension side. This is up to the user to set the paths of the different tools properly on his installation.

Also, see the wiki page. Feel free to suggest improvements for this page if you think it is detailed enough.

@gavinxgu
Copy link

gavinxgu commented Jan 24, 2019

File::HomeDir

Encountered the same problem on Mac. Here are the packages I installed to make it work.

sudo cpan Log::Log4perl
sudo cpan Log::Dispatch
sudo cpan YAML::Tiny
sudo cpan File::HomeDir
sudo cpan Unicode::GCString

In my case, File::HomeDir needs force install

sudo cpan -fi File::HomeDir

@admercs
Copy link

admercs commented Feb 23, 2019

Thank you, @him229! Your solution worked for me for latexindent.

However, LaTeX Workshop formatting still does not work on MacOS without additional configuration. Does anyone know how to get the current version to work? I have not found a complete solution.

@ghost
Copy link

ghost commented Aug 14, 2019

@adam-erickson Provided you already have latexindent installed on your Mac, In Visual Studio Code's settings, go to Latex-workshop > Latexindent: Path, then set the path to something like /usr/local/texlive/2018/texmf-dist/scripts/latexindent/latexindent.pl. Hope this helps.

Repository owner locked as resolved and limited conversation to collaborators Dec 22, 2019
@tamuratak tamuratak added the external issue Issue with something else than LaTeX-Wokshop label Mar 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
external issue Issue with something else than LaTeX-Wokshop
Projects
None yet
Development

Successfully merging a pull request may close this issue.