summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-09-29 00:09:58 +0800
committerGitHub <noreply@github.com>2023-09-28 18:09:58 +0200
commit8761599aade64f5953ef7d3c4cea005a0c459355 (patch)
treee31167b9c6a6ec437756f983da87c78210e8ec84
parent4fffa0960f6c17e9e1e6a20665c97ac34ea678bb (diff)
downloadNim-8761599aade64f5953ef7d3c4cea005a0c459355.tar.gz
fixes #22763; nimcache in nim.cfg uses the relative path to the config file (#22764)
fixes #22763
-rw-r--r--changelog.md1
-rw-r--r--compiler/commands.nim9
-rw-r--r--compiler/nimconf.nim5
-rw-r--r--compiler/options.nim3
-rw-r--r--compiler/scriptconfig.nim1
5 files changed, 16 insertions, 3 deletions
diff --git a/changelog.md b/changelog.md
index e0eb34470..795386144 100644
--- a/changelog.md
+++ b/changelog.md
@@ -29,6 +29,7 @@ slots when enlarging a sequence.
 
 ## Compiler changes
 
+- `--nimcache` using a relative path as the argument in a config file is now relative to the config file instead of the current directory.
 
 ## Tool changes
 
diff --git a/compiler/commands.nim b/compiler/commands.nim
index 8c1da3cd3..f36d82306 100644
--- a/compiler/commands.nim
+++ b/compiler/commands.nim
@@ -608,6 +608,13 @@ proc processMemoryManagementOption(switch, arg: string, pass: TCmdLinePass,
       defineSymbol(conf.symbols, "gcregions")
     else: localError(conf, info, errNoneBoehmRefcExpectedButXFound % arg)
 
+proc pathRelativeToConfig(arg: string, pass: TCmdLinePass, conf: ConfigRef): string =
+  if pass == passPP and not isAbsolute(arg):
+    assert isAbsolute(conf.currentConfigDir), "something is wrong with currentConfigDir"
+    result = conf.currentConfigDir / arg
+  else:
+    result = arg
+
 proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
                     conf: ConfigRef) =
   var key = ""
@@ -653,7 +660,7 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
     # refs bug #18674, otherwise `--os:windows` messes up with `--nimcache` set
     # in config nims files, e.g. via: `import os; switch("nimcache", "/tmp/somedir")`
     if conf.target.targetOS == osWindows and DirSep == '/': arg = arg.replace('\\', '/')
-    conf.nimcacheDir = processPath(conf, arg, info, notRelativeToProj=true)
+    conf.nimcacheDir = processPath(conf, pathRelativeToConfig(arg, pass, conf), info, notRelativeToProj=true)
   of "out", "o":
     expectArg(conf, switch, arg, pass, info)
     let f = splitFile(processPath(conf, arg, info, notRelativeToProj=true).string)
diff --git a/compiler/nimconf.nim b/compiler/nimconf.nim
index f7bae4b36..78215d281 100644
--- a/compiler/nimconf.nim
+++ b/compiler/nimconf.nim
@@ -162,7 +162,7 @@ proc checkSymbol(L: Lexer, tok: Token) =
     lexMessage(L, errGenerated, "expected identifier, but got: " & $tok)
 
 proc parseAssignment(L: var Lexer, tok: var Token;
-                     config: ConfigRef; condStack: var seq[bool]) =
+                     config: ConfigRef; filename: AbsoluteFile; condStack: var seq[bool]) =
   if tok.ident != nil:
     if tok.ident.s == "-" or tok.ident.s == "--":
       confTok(L, tok, config, condStack)           # skip unnecessary prefix
@@ -205,6 +205,7 @@ proc parseAssignment(L: var Lexer, tok: var Token;
       checkSymbol(L, tok)
       val.add($tok)
       confTok(L, tok, config, condStack)
+  config.currentConfigDir = parentDir(filename.string)
   if percent:
     processSwitch(s, strtabs.`%`(val, config.configVars,
                                 {useEnvironment, useEmpty}), passPP, info, config)
@@ -224,7 +225,7 @@ proc readConfigFile*(filename: AbsoluteFile; cache: IdentCache;
     tok.tokType = tkEof       # to avoid a pointless warning
     var condStack: seq[bool] = @[]
     confTok(L, tok, config, condStack)           # read in the first token
-    while tok.tokType != tkEof: parseAssignment(L, tok, config, condStack)
+    while tok.tokType != tkEof: parseAssignment(L, tok, config, filename, condStack)
     if condStack.len > 0: lexMessage(L, errGenerated, "expected @end")
     closeLexer(L)
     return true
diff --git a/compiler/options.nim b/compiler/options.nim
index c18ca92dc..a2f50b12b 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -421,6 +421,8 @@ type
     expandNodeResult*: string
     expandPosition*: TLineInfo
 
+    currentConfigDir*: string # used for passPP only; absolute dir
+
 
 proc parseNimVersion*(a: string): NimVer =
   # could be moved somewhere reusable
@@ -585,6 +587,7 @@ proc newConfigRef*(): ConfigRef =
     maxLoopIterationsVM: 10_000_000,
     vmProfileData: newProfileData(),
     spellSuggestMax: spellSuggestSecretSauce,
+    currentConfigDir: ""
   )
   initConfigRefCommon(result)
   setTargetFromSystem(result.target)
diff --git a/compiler/scriptconfig.nim b/compiler/scriptconfig.nim
index 1a686fd20..c89767296 100644
--- a/compiler/scriptconfig.nim
+++ b/compiler/scriptconfig.nim
@@ -160,6 +160,7 @@ proc setupVM*(module: PSym; cache: IdentCache; scriptName: string;
   cbconf getCommand:
     setResult(a, conf.command)
   cbconf switch:
+    conf.currentConfigDir = vthisDir
     processSwitch(a.getString 0, a.getString 1, passPP, module.info, conf)
   cbconf hintImpl:
     processSpecificNote(a.getString 0, wHint, passPP, module.info,