summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoralaviss <alaviss@users.noreply.github.com>2019-10-16 11:03:11 +0000
committerAndreas Rumpf <rumpf_a@web.de>2019-10-16 13:03:11 +0200
commit37dfb7ecc6b482578bdd8778379f506032b961c9 (patch)
tree17f7b1b45391bc52566f98fd56caba66c69963ee
parent734da9e1dfdd5cb441a5c1461539f10ab331596a (diff)
downloadNim-37dfb7ecc6b482578bdd8778379f506032b961c9.tar.gz
compiler/options: only check the last folder for a candidate (#12421)
This prevents a nimble file from causing a different project to be
targeted instead of the desired one.
-rw-r--r--compiler/options.nim17
1 files changed, 8 insertions, 9 deletions
diff --git a/compiler/options.nim b/compiler/options.nim
index 3f7ca4720..d115a20bd 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -665,6 +665,7 @@ proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
   var
     candidates: seq[string] = @[]
     dir = pkg
+    prev = dir
     nimblepkg = ""
   let pkgname = pkg.lastPathPart()
   while true:
@@ -678,15 +679,12 @@ proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
           if ext == ".nimble":
             if nimblepkg.len == 0:
               nimblepkg = name
-              # Scan subfolders for package source since nimble supports that.
-              # To save time we only scan with the depth of one as that's the
-              # common scenario.
-              let x = x.extractFilename()
-              for k, d in os.walkDir(dir):
-                if k == pcDir:
-                  for k, f in os.walkDir(d, relative = true):
-                    if k == pcFile and f == x:
-                      candidates.add d / f
+              # Since nimble packages can have their source in a subfolder,
+              # check the last folder we were in for a possible match.
+              if dir != prev:
+                let x = prev / x.extractFilename()
+                if fileExists(x):
+                  candidates.add x
             else:
               # If we found more than one nimble file, chances are that we
               # missed the real project file, or this is an invalid nimble
@@ -697,6 +695,7 @@ proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
       if pkgname in c.extractFilename(): return c
     if candidates.len > 0:
       return candidates[0]
+    prev = dir
     dir = parentDir(dir)
     if dir == "": break
   return ""