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

Fix Issue 22738 - std.file.tempDir adds an addition / even when it already has one #8373

Merged
merged 5 commits into from Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 separator, before adding another
// If we don't we end up with https://issues.dlang.org/show_bug.cgi?id=22738
static string addSeparator(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 separator at the end
// However on OSX this can happen, so we must verify lest we break user code i.e. https://github.com/dlang/dub/pull/2208
if (!input.endsWith(dirSeparator))
return input ~ dirSeparator;
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 addSeparator(dir);
return null;
}

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

if (cache is null)
{
cache = getcwd() ~ dirSeparator;
cache = addSeparator(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