Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -390,31 +390,22 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool

proc processPath(conf: ConfigRef; path: string, info: TLineInfo,
notRelativeToProj = false): AbsoluteDir =
let p = if os.isAbsolute(path) or '$' in path:
path
elif notRelativeToProj:
conf.getCurrentDir() / path
else:
conf.projectPath.string / path
let base = if notRelativeToProj: conf.getCurrentDir() else: conf.projectPath.string
try:
result = AbsoluteDir pathSubs(conf, p, toFullPath(conf, info).splitFile().dir)
result = AbsoluteDir pathSubs(conf, path, toFullPath(conf, info).splitFile().dir, base)
except ValueError:
localError(conf, info, "invalid path: " & p)
result = AbsoluteDir p
localError(conf, info, "invalid path: " & path)
result = AbsoluteDir path

proc processCfgPath(conf: ConfigRef; path: string, info: TLineInfo): AbsoluteDir =
let path = if path.len > 0 and path[0] == '"': strutils.unescape(path)
else: path
let basedir = toFullPath(conf, info).splitFile().dir
let p = if os.isAbsolute(path) or '$' in path:
path
else:
basedir / path
try:
result = AbsoluteDir pathSubs(conf, p, basedir)
result = AbsoluteDir pathSubs(conf, path, basedir, basedir)
except ValueError:
localError(conf, info, "invalid path: " & p)
result = AbsoluteDir p
localError(conf, info, "invalid path: " & path)
result = AbsoluteDir path

const
errInvalidNumber = "$1 is not a valid number"
Expand Down
8 changes: 8 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,14 @@ proc pathSubs*(conf: ConfigRef; p, config: string): string =
"projectdir", conf.projectPath.string,
"nimcache", getNimcacheDir(conf).string]).expandTilde

proc pathSubs*(conf: ConfigRef; p, config, base: string): string =
## Like `pathSubs`, but a relative path is resolved against `base`. For a
## path with `$` variables this happens after the substitution, so that
## e.g. `build/$projectname` also becomes absolute (#26296).
result = pathSubs(conf, if isAbsolute(p) or '$' in p: p else: base / p, config)
if '$' in p and not isAbsolute(result):
result = base / result

iterator nimbleSubs*(conf: ConfigRef; p: string): string =
let pl = p.toLowerAscii
if "$nimblepath" in pl or "$nimbledir" in pl:
Expand Down
5 changes: 5 additions & 0 deletions tests/misc/m26296.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import std/compilesettings

static:
echo querySetting(nimcacheDir)
echo querySetting(outDir)
13 changes: 13 additions & 0 deletions tests/misc/trunner.nim
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ tests/newconfig/bar/mfoo.nims""".splitLines
let cmd = fmt"{nim} check --hints:off {file}"
check execCmdEx(cmd) == ("", 0)

block: # bug #26296
# relative paths containing `$` are made absolute like those without `$`
let file = testsDir / "misc/m26296.nim"
let dir = "build/D26296"
let nimcacheArg = quoteShell("--nimcache:" & dir & "/nimcache/$projectName")
let outdirArg = quoteShell("--outdir:" & dir & "/$projectName")
let cmd = fmt"{nim} check --hints:off {nimcacheArg} {outdirArg} {file}"
let (outp, exitCode) = execCmdEx(cmd, workingDir = nimRootDir)
doAssert exitCode == 0, outp
let base = nimRootDir / dir.unixToNativePath
let expected = base / "nimcache" / "m26296" & "\n" & base / "m26296" & "\n"
doAssert outp == expected, outp

block: # mfoo2.customext
let filename = testsDir / "newconfig/foo2/mfoo2.customext"
let cmd = fmt"{nim} e --hint:conf {filename}"
Expand Down
Loading