microsoft / microsoft/STL

`<filesystem>`: std::filesystem::relative not accounting for drive letter case

Open
#5,715 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug filesystem
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Describe the bug

std::filesystem::relative does not take into account drive letter capitalization rules. In Windows, the drive letter C: or c: are equivalent. However std:filesystem::relative() does not respect this rule.

Test case

#include <filesystem>
#include <iostream>

int main()
{
    std::filesystem::path a("C:\\a\\b\\path");
    std::filesystem::path b("c:\\a\\x\\y\\file");

    std::cout << "Relative between a and b is " << std::filesystem::relative(a, b) << "\n";
}

Expected behavior

I would expect this function to return "Relative between a and b is ..\..\x\y\file which is the output if a use the windows API

#include <windows.h>
#include <iostream>
#include "Shlwapi.h"

void main(void)
{
    char szOut[MAX_PATH] = "";
    char szFrom[ ] = "C:\\a\\b\\path";
    char szTo[ ] = "c:\\a\\x\\y\\file";

    std::cout  <<  "The relative path is relative from: ";
    std::cout  <<  szFrom;
    std::cout  <<  "\n";

    std::cout  <<  "The relative path is relative to: ";
    std::cout  <<  szTo;
    std::cout  <<  "\n";

    PathRelativePathTo(szOut,
                       szFrom,
                       FILE_ATTRIBUTE_DIRECTORY,
                       szTo,
                       FILE_ATTRIBUTE_NORMAL);

    std::cout  <<  "The relative path is: ";
    std::cout  <<  szOut;
    std::cout  <<  "\n";
}

Output from above is

The relative path is relative from: C:\a\b\path
The relative path is relative to: c:\a\x\y\file
The relative path is: ..\..\x\y\file

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Windows case with std::filesystem::relative using the paths shown, then locate the implementation and tests for that entry point in the STL repository. Done means drive-letter case is treated equivalently and the result matches the expected ....\x\y\file path.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.