summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-06-13 19:23:11 +0800
committerGitHub <noreply@github.com>2023-06-13 13:23:11 +0200
commitcca5e5ffb92d10b56e0d57940bd52c632d6651a4 (patch)
treed46bbf795f07b2df67d31653c2d6b93b7b2d83ca
parentfda8b6f193e2e229488f76f18089f01eb08272fb (diff)
downloadNim-cca5e5ffb92d10b56e0d57940bd52c632d6651a4.tar.gz
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
-rw-r--r--changelogs/changelog_2_0_0.md2
-rw-r--r--compiler/options.nim7
-rw-r--r--tests/import/t22065.nim5
3 files changed, 13 insertions, 1 deletions
diff --git a/changelogs/changelog_2_0_0.md b/changelogs/changelog_2_0_0.md
index 42c25946a..76f1a2c92 100644
--- a/changelogs/changelog_2_0_0.md
+++ b/changelogs/changelog_2_0_0.md
@@ -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:"
diff --git a/compiler/options.nim b/compiler/options.nim
index 7e9e37384..082caedf1 100644
--- a/compiler/options.nim
+++ b/compiler/options.nim
@@ -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)
 
diff --git a/tests/import/t22065.nim b/tests/import/t22065.nim
new file mode 100644
index 000000000..dfd87a107
--- /dev/null
+++ b/tests/import/t22065.nim
@@ -0,0 +1,5 @@
+discard """
+  errormsg: "cannot open file: ./sugar"
+"""
+
+import ./sugar
\ No newline at end of file