Thread overview
How to work with long paths on Windows?
Sep 13, 2022
Preetpal
Sep 14, 2022
Preetpal
Nov 14, 2022
Preetpal
Nov 14, 2022
Imperatorn
Nov 14, 2022
Imperatorn
Nov 14, 2022
Preetpal
Nov 14, 2022
Imperatorn
September 13, 2022

In Windows 10, Version 1607 (and later), you can enable long paths which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use npm.

Example code:

import std.file;
import std.stdio;

/// Command line tool to find files in directories
int main(string[] args) {
    if (args.length < 2) {
        writefln("Usage: %s wildcard", args[0]);
        return 1;
    }
    string wildcard = args[1];

    auto results = dirEntries(".", wildcard, SpanMode.depth);
    foreach(string result; results) {
        writeln(result);
    }
    return 0;
}

Example problem:

If you run it in a directory with paths containing exceeding MAX_PATH, it fails.

std.file.FileException@std\file.d(4648): .\my_webproject\my_webproject\my_webproject\node_modules\bootswatch\docs\3\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\har-validator\node_modules\chalk\node_modules\has-ansi\node_modules\ansi-regex: The system cannot find the path specified.

How do you work around this issue on Windows? The issue has already been reported.

September 14, 2022

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:

>

In Windows 10, Version 1607 (and later), you can enable long paths which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use npm.

Example code:

import std.file;
import std.stdio;

/// Command line tool to find files in directories
int main(string[] args) {
    if (args.length < 2) {
        writefln("Usage: %s wildcard", args[0]);
        return 1;
    }
    string wildcard = args[1];

    auto results = dirEntries(".", wildcard, SpanMode.depth);
    foreach(string result; results) {
        writeln(result);
    }
    return 0;
}

This issue technically isn't an issue as things are working as expected IMO. All you have to do is create an manifest file for the executable (this changes the behavior of how the file management functions in the Windows API (which are being used in std.file) deal with the MAX_PATH restriction). This manifest file can either be named exactly the same name as the executable and placed in the same directory (in this case if you named the program executable "find_file.exe" you would name the manifest file "find_file.exe.manifest") or it can be embedded in the executable (did not test this myself). The manifest file is just an XML file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
    <asmv3:application>
    <asmv3:windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
      <ws2:longPathAware>true</ws2:longPathAware>
    </asmv3:windowsSettings>
    <asmv3:windowsSettings xmlns:ws3="http://schemas.microsoft.com/SMI/2019/WindowsSettings">
      <ws3:activeCodePage>UTF-8</ws3:activeCodePage>
    </asmv3:windowsSettings>
  </asmv3:application>
  <assemblyIdentity type="win32" name="YourOrganization.file_name" version="1.0.0.7"></assemblyIdentity>
</assembly>
>

Example problem:

If you run it in a directory with paths containing exceeding MAX_PATH, it fails.

std.file.FileException@std\file.d(4648): .\my_webproject\my_webproject\my_webproject\node_modules\bootswatch\docs\3\node_modules\bower\node_modules\update-notifier\node_modules\request\node_modules\har-validator\node_modules\chalk\node_modules\has-ansi\node_modules\ansi-regex: The system cannot find the path specified.

How do you work around this issue on Windows? The issue has already been reported.

If you use the manifest file for your executable, you will be able to deal with long paths on Windows. By using the manifest file, my program (see example code) no longer threw an exception when dealing with long paths. I think that the open issue should be closed as you have to opt-in to make your program long path aware.

November 14, 2022

In case anyone wants to see a working example of how to use long paths on Windows, I uploaded a gist (see my comment in the gist for how to make it work). It is an upgraded version of the original command line tool shown in the example code. I actually use this program to find files and directories sometimes (it matches against relative child paths in the search directory). The command line tool should work on FreeBSD, Linux and macOS.

November 14, 2022

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:

>

In Windows 10, Version 1607 (and later), you can enable long paths which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use npm.

[...]

Have you set longPathAware in the applications manifest?

November 14, 2022

On Monday, 14 November 2022 at 10:44:11 UTC, Imperatorn wrote:

>

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:

>

In Windows 10, Version 1607 (and later), you can enable long paths which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use npm.

[...]

Have you set longPathAware in the applications manifest?

"If possible, you should embed the application manifest as a resource in your application's .exe file or .dll. If you can't do that, then you can place the application manifest file in the same directory as the .exe or .dll"

"By convention an application manifest should have the same name as your app's executable file, with the .manifest extension appended to it"

Here's an example

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings xmlns:ws2="http://schemas.microsoft.com/SMI/2016/WindowsSettings">
        <ws2:longPathAware>true</ws2:longPathAware>
    </windowsSettings>
</application>
November 14, 2022

On Monday, 14 November 2022 at 10:44:11 UTC, Imperatorn wrote:

>

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:

>

In Windows 10, Version 1607 (and later), you can enable long paths which bypasses the MAX_PATH limitation for local paths (e.g., C:\Users\you\log.txt). Currently if you iterate over a directory with a file exceeding the MAX_PATH limitation for local paths, an exception is thrown. There is no limitation on Linux (tested using GDC on the Windows Subsystem for Linux) and this issue occurs when using either the LDC2 or DMD compilers on Windows. It's very common to have these sorts of paths if you use npm.

[...]

Have you set longPathAware in the applications manifest?

Yeah that's how I dealt with the issue. I just replied to my own question with a working example that people who might find this post can refer to: gist (see my comment in the gist for how to make it work).

November 14, 2022

On Monday, 14 November 2022 at 14:43:50 UTC, Preetpal wrote:

>

On Monday, 14 November 2022 at 10:44:11 UTC, Imperatorn wrote:

>

On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:

>

[...]

Have you set longPathAware in the applications manifest?

Yeah that's how I dealt with the issue. I just replied to my own question with a working example that people who might find this post can refer to: gist (see my comment in the gist for how to make it work).

👍