www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to work with long paths on Windows?

reply Preetpal <preetpal.sohal gmail.com> writes:
In Windows 10, Version 1607 (and later), you can [enable long 
paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limit
tion?tabs=registry) 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](https://www.npmjs.com/).

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](https://issues.dlang.org/show_bug.cgi?id=8967).
Sep 13 2022
next sibling parent reply Preetpal <preetpal.sohal gmail.com> writes:
On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:
 In Windows 10, Version 1607 (and later), you can [enable long 
 paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limit
tion?tabs=registry) 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](https://www.npmjs.com/).

 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](https://issues.dlang.org/show_bug.cgi?id=8967).
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](https://issues.dlang.org/show_bug.cgi?id=8967) should be closed as you have to opt-in to make your program long path aware.
Sep 13 2022
parent Preetpal <preetpal.sohal gmail.com> writes:
In case anyone wants to see a working example of how to use long 
paths on Windows, I uploaded a 
[gist](https://gist.github.com/preetpalS/2fd6c6bf05a94734f89b70b679716bf3) (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.
Nov 13 2022
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
On Tuesday, 13 September 2022 at 19:54:15 UTC, Preetpal wrote:
 In Windows 10, Version 1607 (and later), you can [enable long 
 paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limit
tion?tabs=registry) 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](https://www.npmjs.com/).

 [...]
Have you set longPathAware in the applications manifest?
Nov 14 2022
next sibling parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limit
tion?tabs=registry) 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](https://www.npmjs.com/).

 [...]
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 ```xml <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> ```
Nov 14 2022
prev sibling parent reply Preetpal <preetpal.sohal gmail.com> writes:
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](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limit
tion?tabs=registry) 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](https://www.npmjs.com/).

 [...]
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](https://gist.github.com/preetpalS/2fd6c6bf05a94734f89b70b679716bf3) (see my comment in the gist for how to make it work).
Nov 14 2022
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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](https://gist.github.com/preetpalS/2fd6c6bf05a94734f89b70b679716bf3) (see my comment in the gist for how to make it work).
👍
Nov 14 2022
parent James Carter <jamescarter.overstate358 passinbox.com> writes:
On Monday, 14 November 2022 at 18:45:40 UTC, Imperatorn wrote:
 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](https://gist.github.com/preetpalS/2fd6c6bf05a94734f89b70b679716bf3) (see my comment in the gist for how to make it work).
👍
Don't get in the weeds, it really did my my head this path thing, but I found a way, I tried LongPath Tool Program and that sorted it.
Aug 02