Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifiying path to use for stdin error messages #24595

Open
wants to merge 5 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
6 changes: 5 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion compiler/pipelines.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions tests/tools/tloadstdin.nim
Original file line number Diff line number Diff line change
@@ -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]#
1 change: 1 addition & 0 deletions tests/tools/tloadstdin.nims
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--d:nimscriptConfigLoaded