From 6892af4e94dade12aa64ffcdd29d1e03e054fffc Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Sat, 2 Jan 2021 01:33:59 -0800 Subject: [PATCH] fix #16206, `nim r / nim -r` recompiles if cwd changes (#16349) --- compiler/extccomp.nim | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/extccomp.nim b/compiler/extccomp.nim index 14b961ee1e517..87b38092e46e6 100644 --- a/compiler/extccomp.nim +++ b/compiler/extccomp.nim @@ -1026,6 +1026,8 @@ proc writeJsonBuildInstructions*(conf: ConfigRef) = lit $(%* conf.projectIsCmd) lit ",\L\"cmdInput\": " lit $(%* conf.cmdInput) + lit ",\L\"currentDir\": " + lit $(%* getCurrentDir()) if optRun in conf.globalOptions or isDefined(conf, "nimBetterRun"): lit ",\L\"cmdline\": " @@ -1046,14 +1048,23 @@ proc changeDetectedViaJsonBuildInstructions*(conf: ConfigRef; projectfile: Absol result = false try: let data = json.parseFile(jsonFile.string) - if not data.hasKey("depfiles") or not data.hasKey("cmdline"): + for key in "depfiles cmdline stdinInput currentDir".split: + if not data.hasKey(key): return true + if getCurrentDir() != data["currentDir"].getStr: + # fixes bug #16271 + # Note that simply comparing `expandFilename(projectFile)` would + # not be sufficient in case other flags depend implicitly on `getCurrentDir`, + # and would require much more care. Simply re-compiling is safer for now. + # A better strategy for future work would be to cache (with an LRU cache) + # the N most recent unique build instructions, as done with `rdmd`, + # which is both robust and avoids recompilation when switching back and forth + # between projects, see https://github.com/timotheecour/Nim/issues/199 return true let oldCmdLine = data["cmdline"].getStr if conf.commandLine != oldCmdLine: return true if hashNimExe() != data["nimexe"].getStr: return true - if not data.hasKey("stdinInput"): return true let stdinInput = data["stdinInput"].getBool let projectIsCmd = data["projectIsCmd"].getBool if conf.projectIsStdin or stdinInput: