Skip to content

Commit

Permalink
Fix Issue 22738 - std.file.tempDir adds an addition / even when it al…
Browse files Browse the repository at this point in the history
…ready has one
  • Loading branch information
rikki cattermole committed Feb 5, 2022
1 parent 9b12b98 commit 29b6b09
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions std/file.d
Expand Up @@ -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)
{
Expand All @@ -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;
}

Expand All @@ -5309,7 +5323,7 @@ string tempDir() @trusted

if (cache is null)
{
cache = getcwd() ~ dirSeparator;
cache = addSeperator(getcwd());
}
}
return cache;
Expand Down Expand Up @@ -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));
}

/**
Expand Down

0 comments on commit 29b6b09

Please sign in to comment.