<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - temp_directory_path() returns path with trailing slash"
   href="https://bugs.llvm.org/show_bug.cgi?id=45307">45307</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>temp_directory_path() returns path with trailing slash
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ldionne@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program works when run in an empty environment, but not when
TEMPDIR or some other temporary-directory-pointing environment variable is
defined:

    $ cat <<EOF | clang++ -xc++ -std=c++17 - && ./a.out
    #include <filesystem>
    #include <iostream>
    namespace fs = std::filesystem;
    int main() {
        fs::path tmp = fs::temp_directory_path();
        fs::path p = tmp / "hello";
        std::cout << "tmp = " << tmp << std::endl;
        std::cout << "p.parent_path() = " << p.parent_path() << std::endl;
        assert(p.parent_path() == tmp);
    }
    EOF

When run as `./a.out`, the program fails:

    tmp = "/var/folders/10/r6bw68bs5b9gwjtrnl9dz0vm0000gn/T/"
    p.parent_path() = "/var/folders/10/r6bw68bs5b9gwjtrnl9dz0vm0000gn/T"
    Assertion failed: (p.parent_path() == tmp), function main, file <stdin>,
line 9.

Unsurprisingly, the environment contains the following entry:

    TMPDIR=/var/folders/10/r6bw68bs5b9gwjtrnl9dz0vm0000gn/T/

When run as `env -i ./a.out`, the program succeeds and prints the following:

    tmp = "/tmp"
    p.parent_path() = "/tmp"


Comparison of paths is based on comparing the file-names exposed by begin()
through end(), and that is specified to yield an empty element at the end if
the path ends with a directory separator. So the comparison appears to be
behaving correctly.

However, I believe temp_directory_path() should normalize its return value and
not return a path with a trailing slash if the path it picks up from the
environment has a trailing slash. The reason is that the Standard says
(<a href="http://eel.is/c++draft/fs.op.temp.dir.path#5">http://eel.is/c++draft/fs.op.temp.dir.path#5</a>):

    Example: For POSIX-based operating systems, an implementation might return
the path supplied by the first environment variable found in the list TMPDIR,
TMP, TEMP, TEMPDIR, or if none of these are found, "/tmp".

It seems weird that we'd return a path with a trailing slash when found in an
environment variable, but just "/tmp" without a trailing slash otherwise.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>