Skip to content

Commit

Permalink
fixes #22763; nimcache in nim.cfg uses the relative path to the confi…
Browse files Browse the repository at this point in the history
…g file (#22764)

fixes #22763
  • Loading branch information
ringabout authored Sep 28, 2023
1 parent 4fffa09 commit 8761599
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions compiler/nimconf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -585,6 +587,7 @@ proc newConfigRef*(): ConfigRef =
maxLoopIterationsVM: 10_000_000,
vmProfileData: newProfileData(),
spellSuggestMax: spellSuggestSecretSauce,
currentConfigDir: ""
)
initConfigRefCommon(result)
setTargetFromSystem(result.target)
Expand Down
1 change: 1 addition & 0 deletions compiler/scriptconfig.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8761599

Please sign in to comment.