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

Infer output paths from latexmkrc if possible #968

Merged
merged 4 commits into from
Nov 28, 2023

Conversation

pfoerster
Copy link
Member

Fixes #907.

@gnull
Copy link
Contributor

gnull commented Nov 26, 2023

I've tested it with kak-lsp and Kakoune, seems to work!

My only question is: do we need to document this new behavior in Wiki? Wiki needs a separate PR, doesn't it?

@gnull
Copy link
Contributor

gnull commented Nov 26, 2023

Also, if we pass the values through print on stdout, could this mess with something that the user may be printing there?

Writing to temporary file would have been cleaner, but I nevertheless think that the current approach good enough (and simpler).

Ivan

@gnull
Copy link
Contributor

gnull commented Nov 26, 2023

I tested more and got this error.

Log from kak-lsp

The <<project-root>> and <<texlab-source>> are placeholders for paths I removed for privacy.

Nov 26 20:34:53.989 DEBG Searching for vars starting with KAK_LSP_PROJECT_ROOT_LATEX, module: kak_lsp::project_root:60
Nov 26 20:34:53.989 INFO Found project root "/home/io/<<project-root>>" because it contains ".latexmkrc", module: kak_lsp::project_root:43
Nov 26 20:34:53.989 DEBG Routing editor request to Route { session: "150345", server_name: "<<texlab-source>>/target/debug/texlab", root: "/home/io/<<project-root>>" }, module: kak_lsp::session:120
Nov 26 20:34:53.989 DEBG To server <<texlab-source>>/target/debug/texlab: {"jsonrpc":"2.0","method":"textDocument/hover","params":{"position":{"character":24,"line":101},"textDocument":{"uri":"file:///home/io/<<project-root>>/contents.tex"}},"id":1}, module: kak_lsp::language_server_transport:198
Nov 26 20:34:54.302 DEBG Language server closed pipe, stopping reading, module: kak_lsp::language_server_transport:146
Nov 26 20:34:54.302 ERRO Language server error: thread 'main' panicked at crates/line-index/src/lib.rs:117:27:
index out of bounds: the len is 10 but the index is 125
stack backtrace:
   0: rust_begin_unwind
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/panicking.rs:72:14
   2: core::panicking::panic_bounds_check
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/panicking.rs:189:5
   3: <usize as core::slice::index::SliceIndex<[T]>>::index
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/slice/index.rs:258:10
   4: core::slice::index::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/slice/index.rs:18:9
   5: <alloc::vec::Vec<T,A> as core::ops::index::Index<I>>::index
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/alloc/src/vec/mod.rs:2732:9
   6: line_index::LineIndex::offset
             at <<texlab-source>>/crates/line-index/src/lib.rs:117:27
   7: <diagnostics::build_log::BuildErrors as diagnostics::DiagnosticSource>::update::{{closure}}
             at <<texlab-source>>/crates/diagnostics/src/build_log.rs:52:30
   8: core::option::Option<T>::unwrap_or_else
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/option.rs:976:21
   9: <diagnostics::build_log::BuildErrors as diagnostics::DiagnosticSource>::update
             at <<texlab-source>>/crates/diagnostics/src/build_log.rs:50:25
  10: <diagnostics::DiagnosticManager as diagnostics::DiagnosticSource>::update
             at <<texlab-source>>/crates/diagnostics/src/lib.rs:72:13
  11: texlab::server::Server::update_workspace
             at <<texlab-source>>/crates/texlab/src/server.rs:272:13
  12: texlab::server::Server::did_open
             at <<texlab-source>>/crates/texlab/src/server.rs:391:9
  13: texlab::server::Server::process_messages::{{closure}}
             at <<texlab-source>>/crates/texlab/src/server.rs:1087:72
  14: texlab::server::dispatch::NotificationDispatcher::on
             at <<texlab-source>>/crates/texlab/src/server/dispatch.rs:23:21
  15: texlab::server::Server::process_messages
             at <<texlab-source>>/crates/texlab/src/server.rs:1079:29
  16: texlab::server::Server::run
             at <<texlab-source>>/crates/texlab/src/server.rs:1129:9
  17: texlab::main
             at <<texlab-source>>/crates/texlab/src/main.rs:31:5
  18: core::ops::function::FnOnce::call_once
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread '<unnamed>' panicked at /home/io/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lsp-server-0.7.4/src/stdio.rs:29:37:
receiver was dropped, failed to send a message: "SendError(..)"
stack backtrace:
   0: rust_begin_unwind
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/panicking.rs:72:14
   2: core::result::unwrap_failed
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/result.rs:1649:5
   3: core::result::Result<T,E>::expect
             at /rustc/1e9dda77b5b8e690c7e21871bbd2dcf182e1a841/library/core/src/result.rs:1030:23
   4: lsp_server::stdio::stdio_transport::{{closure}}
             at /home/io/.cargo/registry/src/index.crates.io-6f17d22bba15001f/lsp-server-0.7.4/src/stdio.rs:29:13
, module: kak_lsp::language_server_transport:68
Nov 26 20:34:54.302 INFO Waiting for Messages to language server to finish..., module: kak_lsp::thread_worker:19
Nov 26 20:34:54.302 DEBG Received signal to stop language server, closing pipe, module: kak_lsp::language_server_transport:209

Does it look like texlab is looking at the wrong logfile?

There were more errors of similar sort, I can carefully make a more detailed report later.

@gnull
Copy link
Contributor

gnull commented Nov 27, 2023

From latexmk docs:

image

Getting the directories where latexmk will put its files is more nuanced, apparently. If only $out_dir is set in latexmkrc, latexmk will assume that aux_dir has the same value. There's these lines in latexmk source:

if ($out_dir eq '' ){
    # Default to cwd
    $out_dir = '.';
}
if ( $aux_dir eq '' ){
    # Default to out_dir
    #  ?? This is different than MiKTeX
    $aux_dir = $out_dir;
}

I read the docs more carefully, and found the option -dir-report which reports the actual directories that latexmk is about to use. Seems much better and cleaner than injecting Perl code.

The problem is, it then proceeds to build the document. Is that ok for texlab that we read the out_dir and aux_dir variables only after the build has finished? I guessing it's not, since we may want to open pdf with forward search even if we didn't build it during this run of texlab. And for that we need to know where the pdf is.

$ ls main.tex .latexmkrc
.latexmkrc  main.tex
$ time latexmk -dir-report | grep 'Normalized'
Latexmk: Normalized aux dir and out dir: 'build', 'build'

real	0m10.354s
user	0m9.652s
sys	0m0.747s

In this example, latexmk immediately prints the (actual) directory names, but then spends 10s to build the whole document.

Ivan

@gnull
Copy link
Contributor

gnull commented Nov 27, 2023

One more update. I wrote to John Collins, the author of Latexmk, and asked if there's a clean way to do what we want. Here's what he says:

  1. He'll add -dir-report-only option to the next release of Latexmk. It will report the variables just like -dir-report, but skip building anything.

  2. Until then, John suggests we do latexmk -dir-report NONEXISTENT.tex. This will throw an error about file not existing, but also print the variables we need.

We don't know when the new release is coming, and how long it will take it to reach all the distros. So, probably, the best solution is to proceed with 2 for now. And later implement 1.

Ivan

@gnull
Copy link
Contributor

gnull commented Nov 27, 2023

https://github.com/gnull/texlab/tree/feature/latexmkrc-auto-config

I tried implementing approach 2 here over your commits, @pfoerster . What do you think?

It seems to work and I don't get those stack traces anymore.

Ivan

@pfoerster
Copy link
Member Author

The problem is, it then proceeds to build the document. Is that ok for texlab that we read the out_dir and aux_dir variables only after the build has finished? I guessing it's not

Yeah, I think so too because parsing blocks the main thread so we need to be quite fast here. Waiting for the entire document to build (which could also be stuck in an infinite loop if you are unlucky) is not ideal as it makes the server unresponsive.

I tried implementing approach 2 here over your commits, @pfoerster . What do you think?

Looks very good! Do you mind creating a PR? :)

I tested more and got this error.

The error that you are seeing is from another issue which is unrelated to the changes made in the PR. It has to do with the LineIndex struct that is used to translate UTF8 offets to UTF16 line/column positions and vice-versa (see #967 (comment)).

@gnull
Copy link
Contributor

gnull commented Nov 27, 2023

Done!

PRing into this feature branch so that this PR tracks all the progress. Hope this is what you meant.

Ivan

@pfoerster pfoerster force-pushed the feature/latexmkrc-auto-config branch from 3396a8e to a14efe0 Compare November 27, 2023 20:22
gnull and others added 3 commits November 27, 2023 21:42
)

* Reliably determine latexmk's aux_dir and out_dir using -dir-report

This commit makes texlab determine aux_dir and out_dir variables by
calling

  latexmkrc -dir-report $TMPDIR/NONEXISTENT.tex

Passing NONEXISTENT file is a hack to prevent latexmk from building
anything. And the $TMPDIR part should ensure 100% that this file does
not exist (to avoid event rarest cases when user may have a file called
NONEXISTENT.tex in current working directory).

This should be a more correct than `latexmk -r $TMPDIR/latexmkrc`, since
-dir-report was intended exactly for this and it prints normalized
values.

* remove a reduntant check
@pfoerster pfoerster merged commit 4aabfa1 into master Nov 28, 2023
5 checks passed
@pfoerster pfoerster deleted the feature/latexmkrc-auto-config branch November 28, 2023 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Setup auxDirectory accroding to the .latexmkrc
2 participants