Repository: neovim
Issue: vim.fn.stdpath() can return unsupported 8.3 DOS filenames
Labels: bug,platform:windows

### Problem

- Windows will truncate some long usernames into `6ch~N` names.
- When calling `vim.fn.stdpath("cache")` in these cases, we get a path containing these truncated names
  - eg: `C:/User/RUNNER~1/AppData/Local/Temp/nvim`
  - Quirk: `stdpath("config")` does not truncate the username portion.
  - This effect is **only** observed when running nvim under `cmd.exe`, not in `powershell.exe` and not in `cmd.exe` spawned from a powershell instance.
- After adding these "truncated paths" to the RTP, `nvim_get_runtime_file` will fail to find the files under them.
- Forcing `RUNNER~1` to be `runneradmin` (the true username) will let `nvim_get_runtime_file` find files, ~~but its not practical, except if its possible to force `stdpath("cache")` to return "full paths".~~ 
  - Calling `uv.fs_realpath` will convert to the long name.
- You can list "short names" via `dir /X` when running in `cmd.exe` **not powershell**.

Below is a walkthrough of the results of the reproduction script included in this post.

*Setup, create dirs and files*

```lua
-- run.lua
vim.cmd("version")
-- Script expects
local cache = vim.fn.stdpath("cache")
local lua_dir = vim.fs.normalize(cache .. "/some-dir/lua")
local lua_file = vim.fs.normalize(lua_dir .. "/abc.lua")

vim.fn.mkdir(lua_dir, "p")
local fd = assert(io.open(lua_file, "w"))
fd:write("true")
fd:close()

-- check file exists
assert(vim.loop.fs_access(lua_file, "R"), "Failed to create lua file")
```

```lua
print("Cache:", cache)
print("Lua file:", lua_file)
print("RTP", vim.go.rtp)
```


Observe the truncated cache path, note we have not yet attempted to add the
cache path to the RTP

```
Cache: C:\Users\RUNNER~1\AppData\Local\Temp\nvim

Lua file: C:/Users/RUNNER~1/AppData/Local/Temp/nvim/some-dir/lua/abc.lua

RTP C:\Users\runneradmin\AppData\Local\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\share\nvim\runtime,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\lib\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site\after,C:\Users\runneradmin\AppData\Local\nvim\after
```


Show all RTP dir, look for lua/abc.lua file. Expected to fail as we have not
added it to the RTP yet.

```lua
-- Try to find file (should fail, we have not added it to rtp yet)
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))
```

```
{ "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\share\\nvim\\runtime", "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\lib\\nvim" }

{}
```

Now add the truncated path to the RTP


```lua
-- Add cache to rtp
vim.opt.runtimepath:prepend(cache.."/*")
-- Now we have RUNNER~1/temp in the RTP
print("RTP", vim.go.rtp)
```

```
RTP C:\Users\RUNNER~1\AppData\Local\Temp\nvim/*,C:\Users\runneradmin\AppData\Local\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\share\nvim\runtime,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\lib\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site\after,C:\Users\runneradmin\AppData\Local\nvim\after
```

Search for file, not we find nothing and the cache dir is not shown in the rtp dirs.

```lua
-- try to find files, should now work as we have added to rtp
-- but it will fail because the dir contains ~ or possibly fails
-- some "does it exist?" check.
-- 
-- For ref, windows creates two entries, users/RUNNER~1 (invisible) and
-- users/runneradmin, both pointing to the same dir.
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))
```

```
{ "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\share\\nvim\\runtime", "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\lib\\nvim" }

{}
```

Manually swap truncated RUNNER~1 to runneradmin,

```lua
-- convert `RUNNER~1` into runneradmin in RTP
vim.go.rtp = vim.go.rtp:gsub("RUNNER~1", "runneradmin", 1)
print("RTP", vim.go.rtp)
```

```
RTP C:\Users\runneradmin\AppData\Local\Temp\nvim/*,C:\Users\runneradmin\AppData\Local\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\share\nvim\runtime,D:\a\nvim-tilde-rtp\nvim-tilde-rtp\nvim-win64\lib\nvim,C:\Users\runneradmin\AppData\Local\nvim-data\site\after,C:\Users\runneradmin\AppData\Local\nvim\after
```

Repeat the previous search, we find the file now

```lua
-- Search now succeeds
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))
```

```
{ "C:\\Users\\runneradmin\\AppData\\Local\\Temp\\nvim\\some-dir", "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\share\\nvim\\runtime", "D:\\a\\nvim-tilde-rtp\\nvim-tilde-rtp\\nvim-win64\\lib\\nvim" }

{ "C:\\Users\\runneradmin\\AppData\\Local\\Temp\\nvim\\some-dir\\lua\\abc.lua" }
```

### Steps to reproduce

See:

- https://github.com/rktjmp/nvim-tilde-rtp/actions/runs/6076103075
- https://github.com/rktjmp/nvim-tilde-rtp

Note: You must have a username long enough (over 8chars?) to invoke the `~1` truncation. By default the GH action "runneradmin" satisfies this.

Workflow and script reproduced below:

**Github workflow**
```yaml
name: Run Test

on:
  push:
  workflow_dispatch:

jobs:
  test_stable:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2 # Checking out the repo
    - name: Install Neovim stable
      run: |
        choco install neovim
        Import-Module $env:ChocolateyInstall\helpers\chocolateyProfile.psm1
        refreshenv
    - name: Run stable test
      run: |
        c:\tools\neovim\nvim-win64\bin\nvim +"set columns=1000" -l run.lua
  test_latest:
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v2 # Checking out the repo
    - name: Install Neovim latest
      run: |
        Invoke-WebRequest -Uri "https://github.com/neovim/neovim/releases/download/nightly/nvim-win64.zip" -OutFile "nvim-win64.zip"
        Expand-Archive -Path "nvim-win64.zip" -DestinationPath .
    - name: Run latest test
      run: |
        .\nvim-win64\bin\nvim +"set columns=1000" -l run.lua
```

**lua script**
```lua
vim.cmd("version")
-- Script expects
local cache = vim.fn.stdpath("cache")
local lua_dir = vim.fs.normalize(cache .. "/some-dir/lua")
local lua_file = vim.fs.normalize(lua_dir .. "/abc.lua")

vim.fn.mkdir(lua_dir, "p")
local fd = assert(io.open(lua_file, "w"))
fd:write("true")
fd:close()

-- check file exists
assert(vim.loop.fs_access(lua_file, "R"), "Failed to create lua file")

print("Cache:", cache)
print("Lua file:", lua_file)
print("RTP", vim.go.rtp)

-- Try to find file (should fail, we have not added it to rtp yet)
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))

-- Add cache to rtp
vim.opt.runtimepath:prepend(cache.."/*")
-- Now we have RUNNER~1/temp in the RTP
print("RTP", vim.go.rtp)

-- try to find files, should now work as we have added to rtp
-- but it will fail because the dir contains ~ or possibly fails
-- some "does it exist?" check.
-- 
-- For ref, windows creates two entries, users/RUNNER~1 (invisible) and
-- users/runneradmin, both pointing to the same dir.
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))

-- convert `RUNNER~1` into runneradmin in RTP
vim.go.rtp = vim.go.rtp:gsub("RUNNER~1", "runneradmin", 1)
print("RTP", vim.go.rtp)

-- Search now succeeds
print(vim.inspect(vim.api.nvim_get_runtime_file("", true)))
print(vim.inspect(vim.api.nvim_get_runtime_file("lua/abc.lua", true)))
```

### Expected behavior

`nvim_get_runtime_files` can search in `RUNNER~1` style paths.

### Neovim version (nvim -v)

0.9.1, 0.10.0-dev-1031+gc431d820e

### Vim (not Nvim) behaves the same?

NA

### Operating system/version

Windows 11

### Terminal name/version

NA

### $TERM environment variable

NA

### Installation

Chocolatey, Repo latest release