From 4efc88c78e1318a9256f51f35137bf15e462f1cd Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Fri, 27 Dec 2024 15:01:45 +1100 Subject: [PATCH 1/5] Add test case Might only work on unix systems though --- changelog.md | 2 +- compiler/commands.nim | 8 +++++++- compiler/options.nim | 2 ++ compiler/pipelines.nim | 2 +- tests/tools/tloadstdin.nim | 12 ++++++++++++ tests/tools/tloadstdin.nims | 1 + 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/tools/tloadstdin.nim create mode 100644 tests/tools/tloadstdin.nims 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..421d62ba45042 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,12 @@ 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..3d752e48ef4bf 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..440c1022fd2d5 100644 --- a/compiler/pipelines.nim +++ b/compiler/pipelines.nim @@ -234,7 +234,7 @@ 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 + if not graph.config.projectIsStdin: # it could be a stdinfile 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..080e137f8401c --- /dev/null +++ b/tests/tools/tloadstdin.nim @@ -0,0 +1,12 @@ +discard """ + action: "compile" + cmd: "cat $file | nim check --stdinfile:$file -" +""" + +import std/assertions + +# Test the nimscript config is loaded +assert defined(nimscriptConfigLoaded) + +{.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 From 95ce92135e9061893b4a6a706454e364cf07fbb2 Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Fri, 27 Dec 2024 15:04:31 +1100 Subject: [PATCH 2/5] Fix test and syntax issue --- compiler/options.nim | 2 +- tests/tools/tloadstdin.nim | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/options.nim b/compiler/options.nim index 3d752e48ef4bf..c6d5ef87d1efb 100644 --- a/compiler/options.nim +++ b/compiler/options.nim @@ -581,7 +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" + 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/tests/tools/tloadstdin.nim b/tests/tools/tloadstdin.nim index 080e137f8401c..6d07e30e19343 100644 --- a/tests/tools/tloadstdin.nim +++ b/tests/tools/tloadstdin.nim @@ -1,6 +1,6 @@ discard """ action: "compile" - cmd: "cat $file | nim check --stdinfile:$file -" + cmd: "cat $file | $nim check --stdinfile:$file -" """ import std/assertions From 7edfe1f0a45aaab580051c77a5d05daddd709425 Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Sun, 29 Dec 2024 09:11:21 +1100 Subject: [PATCH 3/5] Add another test --- tests/tools/tloadstdin.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/tools/tloadstdin.nim b/tests/tools/tloadstdin.nim index 6d07e30e19343..dc6ad7c199e4c 100644 --- a/tests/tools/tloadstdin.nim +++ b/tests/tools/tloadstdin.nim @@ -3,10 +3,12 @@ discard """ cmd: "cat $file | $nim check --stdinfile:$file -" """ -import std/assertions +import std/[assertions, paths] # Test the nimscript config is loaded assert defined(nimscriptConfigLoaded) +assert currentSourcePath() == $(getCurrentDir()/Path"tloadstdin.nim") + {.warning: "Hello".} #[tt.Warning ^ Hello]# From d11046325397a4368283ded4687a5cb28a2fb5c0 Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Wed, 1 Jan 2025 15:03:33 +1100 Subject: [PATCH 4/5] Disable test on windwos --- compiler/commands.nim | 6 ++---- tests/tools/tloadstdin.nim | 2 ++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/commands.nim b/compiler/commands.nim index 421d62ba45042..ba3d5eadc8329 100644 --- a/compiler/commands.nim +++ b/compiler/commands.nim @@ -937,10 +937,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo; 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) + 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/tests/tools/tloadstdin.nim b/tests/tools/tloadstdin.nim index dc6ad7c199e4c..27e62b4da4738 100644 --- a/tests/tools/tloadstdin.nim +++ b/tests/tools/tloadstdin.nim @@ -1,6 +1,8 @@ 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] From cf05b0d4b3dc18cff9f7958b9095e52deff8c26c Mon Sep 17 00:00:00 2001 From: Jake Leahy Date: Wed, 1 Jan 2025 20:04:12 +1100 Subject: [PATCH 5/5] Add back in the file check for cmdfile We leave in the `projectIsStdin` check since it can now point to an actual file --- compiler/pipelines.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/pipelines.nim b/compiler/pipelines.nim index 440c1022fd2d5..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 not graph.config.projectIsStdin: # 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)