fixes #22065; do not search path for relative imports (#22073)

* fixes #22065; do not search path for "./"

* simplify

* fixes

* fixes

* allow ".."

* cleanup

* add a test case

* slightly modify the import

* adds a changelog
This commit is contained in:
ringabout
2023-06-13 19:23:11 +08:00
committed by GitHub
parent fda8b6f193
commit cca5e5ffb9
3 changed files with 13 additions and 1 deletions

View File

@@ -260,6 +260,8 @@
- `strutils.split` and `strutils.rsplit` now forbid an empty separator.
- Relative imports will not resolve to stdlib anymore, e.g. `import ./tables` now reports an error properly.
## Standard library additions and changes
[//]: # "Changes:"

View File

@@ -910,6 +910,7 @@ proc findFile*(conf: ConfigRef; f: string; suppressStdlib = false): AbsoluteFile
proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFile =
# returns path to module
var m = addFileExt(modulename, NimExt)
var hasRelativeDot = false
if m.startsWith(pkgPrefix):
result = findFile(conf, m.substr(pkgPrefix.len), suppressStdlib = true)
else:
@@ -923,7 +924,11 @@ proc findModule*(conf: ConfigRef; modulename, currentModule: string): AbsoluteFi
else: # If prefixed with std/ why would we add the current module path!
let currentPath = currentModule.splitFile.dir
result = AbsoluteFile currentPath / m
if not fileExists(result):
if m.startsWith('.') and not fileExists(result):
result = AbsoluteFile ""
hasRelativeDot = true
if not fileExists(result) and not hasRelativeDot:
result = findFile(conf, m)
patchModule(conf)

5
tests/import/t22065.nim Normal file
View File

@@ -0,0 +1,5 @@
discard """
errormsg: "cannot open file: ./sugar"
"""
import ./sugar