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

Revert #17398 and #17402 #18480

Merged
merged 3 commits into from
Jul 18, 2021
Merged
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
3 changes: 0 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
underlying code is also updated the same way.
- Custom pragma values have now an API for use in macros.

- In `std/os`, `getHomeDir`, `expandTilde`, `getTempDir`, `getConfigDir` now do not include trailing `DirSep`,
unless `-d:nimLegacyHomeDir` is specified (for a transition period).

- On POSIX systems, the default signal handlers used for Nim programs (it's
used for printing the stacktrace on fatal signals) will now re-raise the
signal for the OS default handlers to handle.
Expand Down
40 changes: 15 additions & 25 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,9 @@ proc getHomeDir*(): string {.rtl, extern: "nos$1",
## * `setCurrentDir proc <#setCurrentDir,string>`_
runnableExamples:
assert getHomeDir() == expandTilde("~")
# `getHomeDir()` doesn't end in `DirSep` even if `$HOME` (on posix) or
# `$USERPROFILE` (on windows) does, unless `-d:nimLegacyHomeDir` is specified.
from std/strutils import endsWith
assert not getHomeDir().endsWith DirSep

when defined(windows): result = getEnv("USERPROFILE")
else: result = getEnv("HOME")
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
when defined(windows): return getEnv("USERPROFILE") & "\\"
else: return getEnv("HOME") & "/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So previously the code used a trailing dir on purpose. Calling this a "bugfix" then is a bit of stretch...


proc getConfigDir*(): string {.rtl, extern: "nos$1",
tags: [ReadEnvEffect, ReadIOEffect].} =
Expand All @@ -917,21 +912,23 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
## On non-Windows OSs, this proc conforms to the XDG Base Directory
## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
## variable if it is set, otherwise it returns the default configuration
## directory ("~/.config").
## directory ("~/.config/").
##
## An OS-dependent trailing slash is always present at the end of the
## returned string: `\\` on Windows and `/` on all other OSs.
##
## See also:
## * `getHomeDir proc <#getHomeDir>`_
## * `getTempDir proc <#getTempDir>`_
## * `getCacheDir proc <#getCacheDir>`_
runnableExamples:
from std/strutils import endsWith
# See `getHomeDir` for behavior regarding trailing DirSep.
assert not getConfigDir().endsWith DirSep
## * `expandTilde proc <#expandTilde,string>`_
## * `getCurrentDir proc <#getCurrentDir>`_
## * `setCurrentDir proc <#setCurrentDir,string>`_
when defined(windows):
result = getEnv("APPDATA")
else:
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
result.normalizePathEnd(trailingSep = true)


proc getCacheDir*(): string =
## Returns the cache directory of the current user for applications.
Expand Down Expand Up @@ -1006,11 +1003,9 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
## See also:
## * `getHomeDir proc <#getHomeDir>`_
## * `getConfigDir proc <#getConfigDir>`_
## * `getCacheDir proc <#getCacheDir>`_
runnableExamples:
from std/strutils import endsWith
# See `getHomeDir` for behavior regarding trailing DirSep.
assert not getTempDir().endsWith(DirSep)
## * `expandTilde proc <#expandTilde,string>`_
## * `getCurrentDir proc <#getCurrentDir>`_
## * `setCurrentDir proc <#setCurrentDir,string>`_
const tempDirDefault = "/tmp"
when defined(tempDir):
const tempDir {.strdefine.}: string = tempDirDefault
Expand All @@ -1031,7 +1026,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
getTempDirImpl(result)
if result.len == 0:
result = tempDirDefault
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))
normalizePathEnd(result, trailingSep=true)

proc expandTilde*(path: string): string {.
tags: [ReadEnvEffect, ReadIOEffect].} =
Expand All @@ -1041,8 +1036,6 @@ proc expandTilde*(path: string): string {.
## Windows: this is still supported despite the Windows platform not having this
## convention; also, both ``~/`` and ``~\`` are handled.
##
## .. warning:: `~bob` and `~bob/` are not yet handled correctly.
##
## See also:
## * `getHomeDir proc <#getHomeDir>`_
## * `getConfigDir proc <#getConfigDir>`_
Expand All @@ -1053,9 +1046,6 @@ proc expandTilde*(path: string): string {.
assert expandTilde("~" / "appname.cfg") == getHomeDir() / "appname.cfg"
assert expandTilde("~/foo/bar") == getHomeDir() / "foo/bar"
assert expandTilde("/foo/bar") == "/foo/bar"
assert expandTilde("~") == getHomeDir()
from std/strutils import endsWith
assert not expandTilde("~").endsWith(DirSep)

if len(path) == 0 or path[0] != '~':
result = path
Expand Down
6 changes: 3 additions & 3 deletions tests/stdlib/tos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -594,12 +594,12 @@ block getTempDir:
if existsEnv("TMPDIR"):
let origTmpDir = getEnv("TMPDIR")
putEnv("TMPDIR", "/mytmp")
doAssert getTempDir() == "/mytmp"
doAssert getTempDir() == "/mytmp/"
delEnv("TMPDIR")
doAssert getTempDir() == "/tmp"
doAssert getTempDir() == "/tmp/"
putEnv("TMPDIR", origTmpDir)
else:
doAssert getTempDir() == "/tmp"
doAssert getTempDir() == "/tmp/"

block: # getCacheDir
doAssert getCacheDir().dirExists
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/ttempfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ block: # createTempDir

doAssert dirExists(dir1)
doAssert dir1.lastPathPart.contains(re"^D20210502T100442(\w+).tmp$")
doAssert dir1.parentDir == getTempDir()
doAssert dir1.parentDir == getTempDir().normalizePathEnd()

block:
let dir3 = createTempDir(prefix, "_mytmp", ".")
Expand Down