From 29b6b098a3e93a3b3c473d7e3042ad677c7ff80c Mon Sep 17 00:00:00 2001 From: rikki cattermole Date: Sun, 6 Feb 2022 03:59:48 +1300 Subject: [PATCH] Fix Issue 22738 - std.file.tempDir adds an addition / even when it already has one --- std/file.d | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/std/file.d b/std/file.d index c974ada2ae8..6c091c508d9 100644 --- a/std/file.d +++ b/std/file.d @@ -5274,7 +5274,21 @@ Returns: */ string tempDir() @trusted { - import std.path : dirSeparator; + // We must check that the end of a path is not a seperator, before adding another + // If we don't we end up with https://issues.dlang.org/show_bug.cgi?id=22738 + static string addSeperator(string input) + { + import std.path : dirSeparator; + import std.algorithm.searching : endsWith; + + // It is very rare a directory path will reach this point with a directory seperator at the end + // However on OSX this can happen, so we must verify least we break user code i.e. https://github.com/dlang/dub/pull/2208 + if (!input.endsWith(dirSeperator)) + return input ~ dirSeperator; + else + return input; + } + static string cache; if (cache is null) { @@ -5294,7 +5308,7 @@ string tempDir() @trusted static string findExistingDir(T...)(lazy T alternatives) { foreach (dir; alternatives) - if (!dir.empty && exists(dir)) return dir ~ dirSeparator; + if (!dir.empty && exists(dir)) return addSeperator(dir); return null; } @@ -5309,7 +5323,7 @@ string tempDir() @trusted if (cache is null) { - cache = getcwd() ~ dirSeparator; + cache = addSeperator(getcwd()); } } return cache; @@ -5338,6 +5352,9 @@ string tempDir() @trusted import std.algorithm.searching : endsWith; import std.path : dirSeparator; assert(tempDir.endsWith(dirSeparator)); + + // https://issues.dlang.org/show_bug.cgi?id=22738 + assert(!tempDir.endsWith(dirSeparator ~ dirSeparator)); } /**