diff --git a/changelog.md b/changelog.md index e0eb34470f..7953861445 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 8c1da3cd32..f36d82306f 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 f7bae4b368..78215d281a 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 c18ca92dc1..a2f50b12bd 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 1a686fd20f..c89767296b 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,