diff --git a/changelog.md b/changelog.md index b9671147f08f9..d764cce1df01a 100644 --- a/changelog.md +++ b/changelog.md @@ -71,4 +71,4 @@ errors. ## Tool changes - +- Added `--stdinfile` flag to name of the file used when running program from stdin (defaults to `stdinfile.nim`) diff --git a/compiler/commands.nim b/compiler/commands.nim index 879a995882b6e..ba3d5eadc8329 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -459,7 +459,7 @@ template handleStdinOrCmdInput = conf.outDir = getNimcacheDir(conf) proc handleStdinInput*(conf: ConfigRef) = - conf.projectName = "stdinfile" + conf.projectName = conf.stdinFile.string conf.projectIsStdin = true handleStdinOrCmdInput() @@ -935,6 +935,10 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; var value: int = 0 discard parseSaturatedNatural(arg, value) conf.errorMax = if value == 0: high(int) else: value + of "stdinfile": + expectArg(conf, switch, arg, pass, info) + conf.stdinFile = if os.isAbsolute(arg): AbsoluteFile(arg) + else: AbsoluteFile(getCurrentDir() / arg) of "verbosity": expectArg(conf, switch, arg, pass, info) let verbosity = parseInt(arg) diff --git a/compiler/options.nim b/compiler/options.nim index 0ef65a14048e9..c6d5ef87d1efb 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -404,6 +404,7 @@ type projectPath*: AbsoluteDir # holds a path like /home/alice/projects/nim/compiler/ projectFull*: AbsoluteFile # projectPath/projectName projectIsStdin*: bool # whether we're compiling from stdin + stdinFile*: AbsoluteFile # Filename to use in messages for stdin lastMsgWasDot*: set[StdOrrKind] # the last compiler message was a single '.' projectMainIdx*: FileIndex # the canonical path id of the main module projectMainIdx2*: FileIndex # consider merging with projectMainIdx @@ -580,6 +581,7 @@ proc newConfigRef*(): ConfigRef = projectPath: AbsoluteDir"", # holds a path like /home/alice/projects/nim/compiler/ projectFull: AbsoluteFile"", # projectPath/projectName projectIsStdin: false, # whether we're compiling from stdin + stdinFile: AbsoluteFile"stdinfile", projectMainIdx: FileIndex(0'i32), # the canonical path id of the main module command: "", # the main command (e.g. cc, check, scan, etc) commandArgs: @[], # any arguments after the main command diff --git a/compiler/pipelines.nim b/compiler/pipelines.nim index 5fddb046f0743..94268c4cae1c5 100644 --- a/compiler/pipelines.nim +++ b/compiler/pipelines.nim @@ -234,7 +234,8 @@ proc compilePipelineModule*(graph: ModuleGraph; fileIdx: FileIndex; flags: TSymF result = moduleFromRodFile(graph, fileIdx, cachedModules) let path = toFullPath(graph.config, fileIdx) let filename = AbsoluteFile path - if fileExists(filename): # it could be a stdinfile + # it could be a stdinfile/cmdfile + if fileExists(filename) and not graph.config.projectIsStdin: graph.cachedFiles[path] = $secureHashFile(path) if result == nil: result = newModule(graph, fileIdx) diff --git a/tests/tools/tloadstdin.nim b/tests/tools/tloadstdin.nim new file mode 100644 index 0000000000000..27e62b4da4738 --- /dev/null +++ b/tests/tools/tloadstdin.nim @@ -0,0 +1,16 @@ +discard """ + action: "compile" + cmd: "cat $file | $nim check --stdinfile:$file -" + # Don't believe cat and pipes works on windows + disabled: "win" +""" + +import std/[assertions, paths] + +# Test the nimscript config is loaded +assert defined(nimscriptConfigLoaded) + +assert currentSourcePath() == $(getCurrentDir()/Path"tloadstdin.nim") + +{.warning: "Hello".} #[tt.Warning + ^ Hello]# diff --git a/tests/tools/tloadstdin.nims b/tests/tools/tloadstdin.nims new file mode 100644 index 0000000000000..58b9142ca04b1 --- /dev/null +++ b/tests/tools/tloadstdin.nims @@ -0,0 +1 @@ +--d:nimscriptConfigLoaded