microsoft / microsoft/STL

`<filesystem>`: `std::filesystem::exists` returns false for path longer than 260 symbols

Open
#6,353 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug

std::filesystem::exists returns false for an existing path longer than 260 characters and doesn't return an error (the error code is 0).

Command-line test case

C:\Temp>type repro.cpp
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <string>
#include <system_error>
#include <format>
#include <cassert>

int main()
{
    // File name longer than 260 characters
    const std::filesystem::path filePath = LR"(Reproduction_scenario_uniquie_folder\dddddddddddddddddddddddddddddd\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.txt)";

    std::wcout << std::format(L"filePath length: {}\n", filePath.wstring().size()); // 262

    // Create the folder path recursively if it is not already there.
    const auto mkdirResult = std::system(std::format("cmd.exe /c if not exist \"{}\" mkdir \"{}\"", filePath.parent_path().string(), filePath.parent_path().string()).c_str());
    assert(mkdirResult == 0);

    // Create the file
    const auto echoResult = std::system(std::format("cmd.exe /c echo AAAA> \"{}\"", filePath.string()).c_str());
    assert(echoResult == 0);

    // Check if the file exists
    std::error_code errorCode;
    bool exists = std::filesystem::exists(filePath, errorCode);

    std::cout << std::format("exists: {}\n", exists);
    std::cout << std::format("errorCode value: {}\n", errorCode.value());
    std::cout << std::format("errorCode category: {}\n", errorCode.category().name());
    std::cout << std::format("errorCode message: {}\n", errorCode.message());

    return 1;
}

C:\Temp>cl /EHsc /W4 /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.51.36248 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.51.36248.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>.\repro.exe
filePath length: 262
exists: false
errorCode value: 0
errorCode category: system
errorCode message: The operation completed successfully.

Expected behavior

std::filesystem::exists should return true if the file with a path longer than 260 characters exists.

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 compiling and running the supplied Windows/MSVC reproducer to confirm the behavior for a 262-character path. Then locate the std::filesystem::exists implementation and its Windows path-handling entry point; done means an existing path longer than 260 characters returns true with a clear, successful error-code result.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.