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

Add possibility to explicitly signal root directory #838

Closed

Conversation

leon-richardt
Copy link

@leon-richardt leon-richardt commented Jan 11, 2023

An attempt to tackle #826.

Scenario

I ran into #826 with a project structure like this:

Project Structure
meeting-notes
├── .git
├── .gitignore
├── 2022-07-13
│   └── notes.md
├── 2022-07-27
│   └── notes.md
├── 2022-08-10
│   ├── notes.md
│   ├── graphs.pdf
│   ├── multi-graph.pdf
│   └── simple-graph.pdf
├── 2022-08-24
│   ├── graphs.pdf
│   ├── document.pdf -> ../tex-doc/document.pdf
│   └── notes.md
├── 2022-09-07
│   ├── document.pdf -> ../tex-doc/document.pdf
│   └── notes.md
├── 2022-09-21
│   ├── document.pdf -> ../tex-doc/document.pdf
│   ├── notes.md
├── 2022-10-05
│   └── notes.md
├── 2022-10-19
│   └── notes.md
├── 2022-11-02
│   └── notes.md
├── 2022-11-16
│   └── notes.md
├── 2022-11-30
│   ├── document.pdf -> ../tex-doc/document.pdf
│   └── notes.md
├── 2022-12-14
│   └── notes.md
├── 2023-01-11
│   ├── document.pdf -> ../tex-doc/document.pdf
│   └── notes.md
└── tex-doc
    ├── document.aux
    ├── document.fdb_latexmk
    ├── document.fls
    ├── document.log
    ├── document.out
    ├── document.pdf
    ├── document.synctex.gz
    └── document.tex

I keep notes on biweekly meetings as Markdown files in a Git directory (see tree output above). Each meeting gets its own subdirectory so that I can also include other files that were relevant for the meeting. Additionally, some of these meeting directories have symlinks to a document located in the tex-doc/ directory.

When editing tex-doc/document.tex and compiling using TexLab, TexLab tries to determine the root directory from file path. Since meeting-notes/ is the first directory that includes one of the "special files" (the .git/ directory in this case), it is set as the root for the project. Thus, the build artifacts are placed into meeting-notes/ directly instead of tex-doc/, where I would like them to be.

Proposed Change & Justification

Prior to this commit, TexLab searches for the following paths to determine "rootness" of a directory:

  • .git/
  • Tectonic.toml
  • .latexmkrc
  • latexmkrc
  • .chktexrc
  • chktexrc

The Change

My proposed solution is a simple one: in addition to the files above, TexLab also looks for the files .texlabroot and texlabroot. These serve no purpose other than signaling to TexLab that this directory should be considered the project root. With this change, the project structure from above would look like this:

meeting-notes
├── .git
├── .gitignore
├── [omitted for brevity]
└── tex-doc
    ├── .texlabroot
    ├── document.aux
    ├── document.fdb_latexmk
    ├── document.fls
    ├── document.log
    ├── document.out
    ├── document.pdf
    ├── document.synctex.gz
    └── document.tex

Compilations of tex-doc/document.tex will then place the build artifacts into tex-doc/ as desired.

Commit Message

When a root directory is not directly specified in the config, TexLab traverses upwards in the file system hierarchy upwards until a .git directory or a latexmk/chktex/Tectonic configuration file is found. The first directory where one of the search items is found is then considered the root directory. This is a reasonable default but may not work for all project structures.

To provide more flexibility, this commit adds the possibility to explicitly signal when TexLab should stop searching for the root directory via empty ".texlabroot" or "texlabroot" files. These serve no purpose other than signaling to TexLab that it should stop traversing upwards at this point.

The Justification

This solution is ...

  • flexible: Users have complete control over when TexLab should stop looking for a root directory.
  • backwards-compatible: For existing project structures, TexLab will still behave the same as it did before this change (i.e., nothing will break).
  • easy to maintain: Since this solution leverages the existing root discovery algorithm, little additional code maintenance is required.

I think this solution would also help with the scenarios described in @har7an and @xlucn in #826, maybe they can chime in with some additional opinions. 😄

Open Questions & Closing Words

If this PR ends up being merged, I feel like this behavior should definitely be documented somewhere. However, I don't know where this documentation should live. If you point me towards your preferred location, I would be happy to write some docs regarding the behavior of the project root discovery. (In fact, I think the current behavior of the project root algorithm should be documented somewhere as well — maybe it already is and I just haven't found it.)

Overall, if you don't feel comfortable with this change or if it doesn't fit the philosophy of the project, please feel free to deny this PR. No hard feelings in that case.

Thank you for your work on TexLab, it has been a joy to use so far!

When a root directory is not directly specified in the config, TexLab
traverses upwards in the file system hierarchy upwards until a .git
directory or a latexmk/chktex/Tectonic configuration file is found. The
first directory where one of the search items is found is then
considered the root directory. This is a reasonable default but may not
work for all project structures.

To provide more flexibility, this commit adds the possibility to
explicitly signal when TexLab should stop searching for the root
directory via empty ".texlabroot" or "texlabroot" files. These serve no
purpose other than signaling to TexLab that it should stop traversing
upwards at this point.
@xlucn
Copy link

xlucn commented Jan 12, 2023

Thanks for pinging me here. Here's my two cents. I think an empty file with any name of .latexmkrc, latexmkrc, .chktexrc or chktexrc also does the same job as this PR.

What I had in mind is more automatic solution. For example, if current file has \begin{document} and \end{document}, then use the folder of current file as root directory. In other words, I hope texlab can find the main document and use its parent folder as root directory. This behavior is intuitive IMHO. (Maybe I should continue the discussion in #826 or in a new issue?)

Here is the reason of the proposal. Texlab is, I guess, executing latexmk from the root directory. So, if the root directory is different from the folder where the main document is, not only the output files are in the wrong place, but texlab also cannot find some included files such as included graphics or tex files. Therefore, I think it's better to determine root directory by the main document, not .git folder or some configuration files.

@leon-richardt
Copy link
Author

leon-richardt commented Jan 12, 2023

I think an empty file with any name of .latexmkrc, latexmkrc, .chktexrc or chktexrc also does the same job as this PR.

You are correct, that would result in the same behavior. However, I still think the .texlabroot approach would be a better solution for two reasons:

  1. Explicitness: The semantic meaning of an empty .latexmkrc/.chktexrc is unclear whereas .texlabroot only ever serves one purpose. (Although one could argue that a explanatory comment could be added in the .latexmkrc.)
  2. Sovereignty: If we depend on configuration files of other tools to signal rootness of a directory, we might get undesired behavior if these tools decide to change their conventions at any point in the future. With a dedicated .texlabroot file, we would have full control over how to interpret it.

(Maybe I should continue the discussion in #826 or in a new issue?)

I agree, I think it's better to stay on topic here — I have replied to you in #826.

@leon-richardt
Copy link
Author

leon-richardt commented Jan 18, 2023

Superseded by #839.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Apr 27, 2023
## [5.5.0] - 2023-04-16

### Added

- Allow optionally passing cursor position to `textDocument/build` request for use in forward search after building.
  Previously, the server had to guess the cursor position ([#475](latex-lsp/texlab#475))
- Add experimental `texlab.experimental.citationCommands` setting to allow extending the list of citation commands
  ([#832](latex-lsp/texlab#832))
- Add support for escaping placeholders in build arguments similar to forward search
- Allow configuring completion matching algorithm ([#872](latex-lsp/texlab#872))

### Fixed

- Fix regression introduced in `v5.4.2` involving `texlab.cleanArtifacts` command.

## [5.4.2] - 2023-04-11

### Fixed

- Fix memory leak when editing documents over a long time ([#856](latex-lsp/texlab#856))
- Fix parsing parentheses in file paths ([#874](latex-lsp/texlab#874))

## [5.4.1] - 2023-03-26

### Fixed

- Do not return symbols with empty names (e. g. sections without name) ([#870](latex-lsp/texlab#870))
- Repair `textDocument/formatting` request ([#871](latex-lsp/texlab#871))

## [5.4.0] - 2023-03-12

### Added

- Add experimental settings to allow extending the list of special environments:
  - `texlab.experimental.mathEnvironments`
  - `texlab.experimental.enumEnvironments`
  - `texlab.experimental.verbatimEnvironments`
- Add `texlab.changeEnvironment` workspace command ([#849](latex-lsp/texlab#849))
- Add `texlab.showDependencyGraph` workspace command

### Changed

- Do not show caption or section names in label inlay hints ([#858](latex-lsp/texlab#858))
- Include more user-defined commands in command completion

### Fixed

- Parse nested `\iffalse` blocks correctly ([#853](latex-lsp/texlab#853))
- Parse commands with multi-byte characters correctly ([#857](latex-lsp/texlab#857))
- Fix checking whether a document can be a root file

## [5.3.0] - 2023-02-25

### Added

- Allow filtering `textDocument/documentSymbols` using regular expressions specified via
  `texlab.symbols.allowedPatterns` and `texlab.symbols.ignoredPatterns`
  ([#851](latex-lsp/texlab#851))

### Fixed

- Do not use percent-encoded path when searching for PDF files during forward search
  ([#848](latex-lsp/texlab#848))
- Always return an empty list of code actions instead of returning "method not found" ([#850](latex-lsp/texlab#850))

## [5.2.0] - 2023-01-29

### Added

- Include line numbers in build warnings when available ([#840](latex-lsp/texlab#840))
- Add `none` formatter to `texlab.latexFormatter` and `texlab.bibtexFormatter` options
  to allow disabling formatting ([#846](latex-lsp/texlab#846))

### Fixed

- Concatenate more than two lines of maximum length in build diagnostics ([#842](latex-lsp/texlab#842))
- Apply the correct range of references to labels when renaming ([#841](latex-lsp/texlab#841))
- Use `document` environment to detect root file instead of `\documentclass` ([#845](latex-lsp/texlab#845))

## [5.1.0] - 2023-01-21

### Added

- Allow manually overriding the root directory using a `texlabroot`/`.texlabroot` marker file.
  See the wiki for more information.
  ([#826](latex-lsp/texlab#826), [#838](latex-lsp/texlab#838))

### Deprecated

- Deprecate `texlab.rootDirectory` setting in favor of `.texlabroot` files

### Fixed

- Do not use `.git`, `.chktexrc`, `.latexmkrc` files/directories to determine the root directory
  ([#826](latex-lsp/texlab#826))
- Fix building documents without an explicit root directory ([#837](latex-lsp/texlab#837))

## [5.0.0] - 2022-12-29

### Changed

- _BREAKING_: `texlab.rootDirectory` is now used as the folder path from which the compiler is executed
  relative to the main document. By default it is equal to `"."`. For more information, please visit the wiki.
- Improve performance of completion by a huge margin due to a faster filtering method used internally
- Do not discover project files beyond the provided workspace folders
- Try to guess the root directory by checking for files such as `.latexmkrc` or `Tectonic.toml` if `texlab.rootDirectory` is not set

### Fixed

- Update positions of reported build diagnostics when editing the affected line
- Do not treat links to files as bidirectional by default. This prevents issues where `texlab` ends up compiling the wrong file
  in projects with shared files ([#806](latex-lsp/texlab#806), [#757](latex-lsp/texlab#757), [#679](latex-lsp/texlab#679))
- Fix coverage of directories which need to be watched for changes ([#502](latex-lsp/texlab#502), [#491](latex-lsp/texlab#491))
- Resolve links of the `import` package correctly
- Use `filterText` of completion items when filtering internally ([#829](latex-lsp/texlab#829))

## [4.3.2] - 2022-11-20

### Fixed

- Do not try to run the TeX engine on package files and fail the build instead ([#801](latex-lsp/texlab#801))
- Handle URIs with URL-encoded drive letters on Windows ([#802](latex-lsp/texlab#802))
- Parse BibTeX entries with unbalanced quotes correctly ([#809](latex-lsp/texlab#809))
- Provide completion for more acronym commands ([#813](latex-lsp/texlab#813))
- Fix parsing acronym definitions ([#813](latex-lsp/texlab#813))

## [4.3.1] - 2022-10-22

### Fixed

- Do not crash with a stack overflow when trying to load packages with many internal dependencies ([#793](latex-lsp/texlab#793))
- Normalize drive letters of all document URIs
- Fix parsing commands that take file paths as arguments ([#789](latex-lsp/texlab#789))
- Use the correct working directory and command line arguments when calling `latexindent` ([#645](latex-lsp/texlab#645))
- Fix publishing to CTAN

## [4.3.0] - 2022-09-25

### Added

- Add inlay hints for `\label{...}` ([#753](latex-lsp/texlab#753))

### Fixed

- Improve accuracy of the error locations reported by the TeX engine ([#738](latex-lsp/texlab#738))
- Reduce number of false positive errors reported by `texlab` ([#745](latex-lsp/texlab#745))
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.

2 participants