Repository: hannah-sten/texify-idea
Issue: Indexing of TEXINPUTS

### Discussed in https://github.com/Hannah-Sten/TeXiFy-IDEA/discussions/3035

<div type='discussions-op-text'>

<sup>Originally posted by **mvscheven** April 14, 2023</sup>
We are using a lot of custom classes and style files to keep the preamble of the main tex-file clean. If these custom files are in the source directory, they are known by TeXiFy and included commands are recognized. But if these files are in a location specified by the TEXINPUTS environment variable this does not work. Is it possible to index also files located at TEXINPUTS?

Thanks for any advice!

### Minimal Example:
#### main.tex
```
\\documentclass[11pt]{scrartcl}
\\RequirePackage{my_style}

\\begin{document}
    \\begin{align}
        \\sigma = E \\varepsilon
    \\end{align}
\\end{document}
```
#### my_style.sty
```
\\RequirePackage{amsmath}
```
If my_style.sty is in the source directory, it is linked in the margin next to the RequirePackage command and align is recognized as valid environment.
If my_style.sty is in a TEXINPUTS directory, it is marked as "not found" and for align an error is produced: "Environment requires amsmath package"

### System:
Windows 11, MikTex, IntelliJ IDEA 2023.1, TexiFy IDEA 0.7.28
</div>

Changed files:
src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt, src/nl/hannahsten/texifyidea/reference/InputFileReference.kt

diff --git a/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt b/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt
index 65a6a900e..430839b5b 100644
--- a/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt
+++ b/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt
@@ -17,11 +17,8 @@ import nl.hannahsten.texifyidea.psi.LatexCommands
 import nl.hannahsten.texifyidea.psi.LatexPsiHelper
 import nl.hannahsten.texifyidea.run.latex.LatexRunConfiguration
 import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
-import nl.hannahsten.texifyidea.util.LatexmkRcFileFinder
-import nl.hannahsten.texifyidea.util.expandCommandsOnce
+import nl.hannahsten.texifyidea.util.*
 import nl.hannahsten.texifyidea.util.files.*
-import nl.hannahsten.texifyidea.util.includedPackages
-import nl.hannahsten.texifyidea.util.isTestProject
 import nl.hannahsten.texifyidea.util.magic.CommandMagic
 
 /**
@@ -122,15 +119,17 @@ class InputFileReference(
 
         // Check environment variables
         val runManager = RunManagerImpl.getInstanceImpl(element.project) as RunManager
-        val texInputPath = runManager.allConfigurationsList
+        val texinputsVariable = runManager.allConfigurationsList
             .filterIsInstance<LatexRunConfiguration>()
             .firstOrNull { it.mainFile in rootFiles }
             ?.environmentVariables
             ?.envs
             ?.getOrDefault("TEXINPUTS", null)
+            // Not sure which of these takes precedence, or if they are joined together
             ?: LatexmkRcFileFinder.getTexinputsVariable(element.containingFile, null)
+            ?: runCommand("kpsewhich", "--expand-var", "'\\$TEXINPUTS'")
 
-        if (texInputPath != null) {
+        for (texInputPath in texinputsVariable?.split(":")?.filter { it.isNotBlank() } ?: emptyList()) {
             val path = texInputPath.trimEnd(':')
             searchPaths.add(path.trimEnd('/'))
             // See the kpathsea manual, // expands to subdirs
diff --git a/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt b/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
index 4d933f10d..6096a13ff 100644
--- a/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
+++ b/src/nl/hannahsten/texifyidea/util/LatexmkRcFileFinder.kt
@@ -107,7 +107,7 @@ object LatexmkRcFileFinder {
      * Get TEXINPUTS from latexmkrc.
      */
     private fun getTexinputs(file: VirtualFile): String? {
-        return """ensure_path\\('TEXINPUTS',\\s*'(?<path>[^']+)'\\)""".toRegex().find(file.inputStream.reader().readText())?.groups?.get("path")?.value
+        return """ensure_path\\(\\s*'TEXINPUTS',\\s*'(?<path>[^']+)'\\s*\\)""".toRegex().find(file.inputStream.reader().readText())?.groups?.get("path")?.value
     }
 
     private var usesLatexmkrc: Boolean? = null