Repository: hannah-sten/texify-idea
Issue: BibTeX can't find aux file.

#### Type of JetBrains IDE (IntelliJ, PyCharm, etc.) and version
IntelliJ IDEA 2022.1.1 (Ultimate Edition)

#### Operating System 
Windows 10

#### TeXiFy IDEA version
0.7.19

#### What I did (steps to reproduce)

1. Create new project (MiKTeX Windows SDK (MiKTeX 21.10), Configure with BibTeX support)
2. Project format .idea (directory-based)
3. Add some text to the default `main.tex` file.
4. Add an entry to the default `main.bib` file.
5. Use the `\\cite{}` command in the `main.tex` file.
6. Run 'main'

The result is `I couldn't open file name 'main.aux'` on the bibtex command, and undefined references in the produced PDF.  Note that the bibtex command is 
`bibtex -include-directory=C:/Users/User/Documents/LaTeX_Projects/bibibibib/src -include-directory=C:/Users/User/Documents/LaTeX_Projects/bibibibib/src main`
It seems to me that instead of including the src and auxil directory, the src is included twice, though I have tried running the command manually with the auxil directory included and I get the same result. Copying the `main.aux` file into the out folder makes the bibtex command work and the bibliography is printed, but the references are still undefined, copying it again after the second compilation makes the references work as well.




Changed files:
src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
src/nl/hannahsten/texifyidea/run/latex/LatexDistributionType.kt

diff --git a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
index f4dff51fd..5fecb1f3c 100644
--- a/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
+++ b/src/nl/hannahsten/texifyidea/run/compiler/LatexCompiler.kt
@@ -43,12 +43,12 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
             command.add("-output-directory=$outputPath")
 
             // -aux-directory only exists on MiKTeX
-            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex()) {
+            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(project = runConfig.project)) {
                 command.add("-aux-directory=$auxilPath")
             }
 
             // Prepend root paths to the input search path
-            if (runConfig.getLatexDistributionType().isMiktex()) {
+            if (runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
                 moduleRoots.forEach {
                     command.add("-include-directory=${it.path}")
                 }
@@ -126,7 +126,7 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
 
             command.add("-output-directory=$outputPath")
 
-            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex()) {
+            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
                 command.add("-aux-directory=$auxilPath")
             }
 
@@ -164,12 +164,12 @@ enum class LatexCompiler(private val displayName: String, val executableName: St
 
             command.add("-output-directory=$outputPath")
 
-            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex()) {
+            if (auxilPath != null && runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
                 command.add("-aux-directory=$auxilPath")
             }
 
             // Prepend root paths to the input search path
-            if (runConfig.getLatexDistributionType().isMiktex()) {
+            if (runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
                 moduleRoots.forEach {
                     command.add("-include-directory=${it.path}")
                 }
diff --git a/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt b/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
index a6eb1646c..9431debf4 100644
--- a/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
+++ b/src/nl/hannahsten/texifyidea/run/latex/LatexCommandLineState.kt
@@ -51,7 +51,7 @@ open class LatexCommandLineState(environment: ExecutionEnvironment, private val
         }
 
         firstRunSetup(compiler)
-        if (!runConfig.getLatexDistributionType().isMiktex()) {
+        if (!runConfig.getLatexDistributionType().isMiktex(runConfig.project)) {
             runConfig.outputPath.updateOutputSubDirs()
         }
 
diff --git a/src/nl/hannahsten/texifyidea/run/latex/LatexDistributionType.kt b/src/nl/hannahsten/texifyidea/run/latex/LatexDistributionType.kt
index 408a905cb..9ae3255f9 100644
--- a/src/nl/hannahsten/texifyidea/run/latex/LatexDistributionType.kt
+++ b/src/nl/hannahsten/texifyidea/run/latex/LatexDistributionType.kt
@@ -18,9 +18,8 @@ enum class LatexDistributionType(val displayName: String) {
     DOCKER_MIKTEX("Dockerized MiKTeX"),
     PROJECT_SDK("Use project SDK");
 
-    fun isMiktex() = this == MIKTEX || this == DOCKER_MIKTEX
-
-    fun isTexlive() = this == TEXLIVE || this == WSL_TEXLIVE
+    private fun isMiktex() = this == MIKTEX || this == DOCKER_MIKTEX
+    fun isMiktex(project: Project) = this == MIKTEX || this == DOCKER_MIKTEX || (this == PROJECT_SDK && LatexSdkUtil.getLatexProjectSdkType(project)?.getLatexDistributionType()?.isMiktex() == true)
 
     fun isAvailable(project: Project) = LatexSdkUtil.isAvailable(this, project)
 
diff --git a/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt b/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
index 735a3484f..5e0c2c63e 100644
--- a/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
+++ b/src/nl/hannahsten/texifyidea/run/latex/LatexRunConfiguration.kt
@@ -391,7 +391,7 @@ class LatexRunConfiguration constructor(
         bibtexRunConfiguration.setSuggestedName()
 
         // On non-MiKTeX systems, add bibinputs for bibtex to work
-        if (!latexDistribution.isMiktex()) {
+        if (!latexDistribution.isMiktex(project)) {
             // Only if default, because the user could have changed it after creating the run config but before running
             if (mainFile != null && outputPath.virtualFile != mainFile.parent) {
                 // As seen in issue 2165, appending a colon (like with TEXINPUTS) may not work on Windows,
@@ -529,7 +529,7 @@ class LatexRunConfiguration constructor(
      * @return The auxil folder when MiKTeX used, or else the out folder when used.
      */
     fun getAuxilDirectory(): VirtualFile? {
-        return if (latexDistribution.isMiktex()) {
+        return if (latexDistribution.isMiktex(project)) {
             auxilPath.getAndCreatePath()
         }
         else {