Skip to content

Commit

Permalink
fix nim-lang#17393 getHomeDir and expandTilde should not include trai…
Browse files Browse the repository at this point in the history
…ling `/` (nim-lang#17398)

* fix nim-lang#17393 getHomeDir and expandTilde should not include trailing `/`

* changelog
  • Loading branch information
timotheecour authored Mar 17, 2021
1 parent 4d3f351 commit bebf2ce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@
- Added `ZZZ` and `ZZZZ` patterns to `times.nim` `DateTime` parsing, to match time
zone offsets without colons, e.g. `UTC+7 -> +0700`.

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


## Language changes
Expand Down
14 changes: 12 additions & 2 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -901,9 +901,14 @@ 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): return getEnv("USERPROFILE") & "\\"
else: return getEnv("HOME") & "/"
when defined(windows): result = getEnv("USERPROFILE")
else: result = getEnv("HOME")
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))

proc getConfigDir*(): string {.rtl, extern: "nos$1",
tags: [ReadEnvEffect, ReadIOEffect].} =
Expand Down Expand Up @@ -999,6 +1004,8 @@ proc expandTilde*(path: string): string {.
##
## Windows: this is still supported despite 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>`_
Expand All @@ -1010,6 +1017,9 @@ 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

0 comments on commit bebf2ce

Please sign in to comment.