compiler/options: improve project file detection (#12404)
The compiler can now correctly find the project file of a nimble package. The algorithm is based on what used by nim.nvim, which has yielded good results.
This commit is contained in:
parent
f957266574
commit
94c1695be6
1 changed files with 26 additions and 6 deletions
|
|
@ -662,20 +662,40 @@ proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFi
|
||||||
|
|
||||||
proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
|
proc findProjectNimFile*(conf: ConfigRef; pkg: string): string =
|
||||||
const extensions = [".nims", ".cfg", ".nimcfg", ".nimble"]
|
const extensions = [".nims", ".cfg", ".nimcfg", ".nimble"]
|
||||||
var candidates: seq[string] = @[]
|
var
|
||||||
var dir = pkg
|
candidates: seq[string] = @[]
|
||||||
|
dir = pkg
|
||||||
|
nimblepkg = ""
|
||||||
|
let pkgname = pkg.lastPathPart()
|
||||||
while true:
|
while true:
|
||||||
for k, f in os.walkDir(dir, relative=true):
|
for k, f in os.walkDir(dir, relative = true):
|
||||||
if k == pcFile and f != "config.nims":
|
if k == pcFile and f != "config.nims":
|
||||||
let (_, name, ext) = splitFile(f)
|
let (_, name, ext) = splitFile(f)
|
||||||
if ext in extensions:
|
if ext in extensions:
|
||||||
let x = changeFileExt(dir / name, ".nim")
|
let x = changeFileExt(dir / name, ".nim")
|
||||||
if fileExists(x):
|
if fileExists(x):
|
||||||
candidates.add x
|
candidates.add x
|
||||||
|
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
|
||||||
|
else:
|
||||||
|
# If we found more than one nimble file, chances are that we
|
||||||
|
# missed the real project file, or this is an invalid nimble
|
||||||
|
# package. Either way, bailing is the better choice.
|
||||||
|
return ""
|
||||||
|
let pkgname = if nimblepkg.len > 0: nimblepkg else: pkgname
|
||||||
for c in candidates:
|
for c in candidates:
|
||||||
# nim-foo foo or foo nfoo
|
if pkgname in c.extractFilename(): return c
|
||||||
if (pkg in c) or (c in pkg): return c
|
if candidates.len > 0:
|
||||||
if candidates.len >= 1:
|
|
||||||
return candidates[0]
|
return candidates[0]
|
||||||
dir = parentDir(dir)
|
dir = parentDir(dir)
|
||||||
if dir == "": break
|
if dir == "": break
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue