August 07, 2023
https://issues.dlang.org/show_bug.cgi?id=24076

          Issue ID: 24076
           Summary: DMD fails to correctly detect Visual Studio 2022
                    (17.5.3) folder on Windows
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: me@gzgz.dev

I've been working on a Bazel ruleset for D, during which I found that D doesn't seem to pickup the my Visual Studio installation (2022 Community Edition). The exact command, environment variables, and error outputs are follows:

```
SUBCOMMAND: # //app:app [action 'Generating interface file for app/main.d',
configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3, execution
platform: @gzgz_rules_d~override//d/platforms:dmd]
cd /d C:/users/zjwga/_bazel_zjwga/3zn6tpc7/execroot/_main
external\gzgz_rules_d~override~d~d_dmd_x86_64-pc-windows-msvc\windows\bin64\dmd.exe
-conf=external/gzgz_rules_d~override~d~d_dmd_x86_64-pc-windows-msvc/windows/bin64/sc.ini
app/main.d -o- -Hf=bazel-out/x64_windows-fastbuild/bin/app/main.di
# Configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3
# Execution platform: @gzgz_rules_d~override//d/platforms:dmd
SUBCOMMAND: # //app:app [action 'Compiling D src app/main.d to object file
bazel-out/x64_windows-fastbuild/bin/app/main.o', configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3, execution
platform: @gzgz_rules_d~override//d/platforms:dmd]
cd /d C:/users/zjwga/_bazel_zjwga/3zn6tpc7/execroot/_main
external\gzgz_rules_d~override~d~d_dmd_x86_64-pc-windows-msvc\windows\bin64\dmd.exe
-conf=external/gzgz_rules_d~override~d~d_dmd_x86_64-pc-windows-msvc/windows/bin64/sc.ini
-of=bazel-out/x64_windows-fastbuild/bin/app/main.o -c app/main.d
# Configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3
# Execution platform: @gzgz_rules_d~override//d/platforms:dmd
SUBCOMMAND: # //app:app [action 'Linking app/app.exe', configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3, execution
platform: @gzgz_rules_d~override//d/platforms:dmd]
cd /d C:/users/zjwga/_bazel_zjwga/3zn6tpc7/execroot/_main
  SET LIB=C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\ATLMFC\lib\x64;C:\Program
Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\lib\x64;C:\Program Files
(x86)\Windows Kits\NETFXSDK\4.8\lib\um\x64;C:\Program Files (x86)\Windows
Kits\10\lib\10.0.19041.0\ucrt\x64;C:\Program Files (x86)\Windows
Kits\10\\lib\10.0.19041.0\\um\x64
    SET PATH=C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\VC\VCPackages;C:\Program Files\Microsoft
Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team
Explorer;C:\Program Files\Microsoft Visual
Studio\2022\Community\MSBuild\Current\bin\Roslyn;C:\Program Files\Microsoft
Visual Studio\2022\Community\Team Tools\Performance Tools\x64;C:\Program
Files\Microsoft Visual Studio\2022\Community\Team Tools\Performance
Tools;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8
Tools\x64\;C:\Program Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\FSharp\Tools;C:\Program
Files (x86)\Windows Kits\10\bin\10.0.19041.0\\x64;C:\Program Files
(x86)\Windows Kits\10\bin\\x64;C:\Program Files\Microsoft Visual
Studio\2022\Community\\MSBuild\Current\Bin\amd64;C:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program
Files\Microsoft Visual Studio\2022\Community\Common7\IDE\;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\Tools\;;C:\WINDOWS\system32;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja;C:\Program
Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\VC\Linux\bin\ConnectionManagerExe
    SET PWD=/proc/self/cwd
    SET RUNFILES_MANIFEST_ONLY=1
    SET TEMP=C:\Users\ZJWGa\AppData\Local\Temp
    SET TMP=C:\Users\ZJWGa\AppData\Local\Temp
  C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64\link.exe
@bazel-out/x64_windows-fastbuild/bin/app/app.exe-2.params
# Configuration:
5ee4290ce8148d9b5d85725c7428265bfd44887b0d546a3868c7a26be51e40f3
# Execution platform: @gzgz_rules_d~override//d/platforms:dmd
ERROR: C:/users/zjwga/cs/bazel/gzgz_rules_d/e2e/smoke/app/BUILD:5:9: Linking
app/app.exe failed: (Exit 1104): link.exe failed: error executing command (from
target //app:app) C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64\link.exe
@bazel-out/x64_windows-fastbuild/bin/app/app.exe-2.params
LINK : fatal error LNK1104: cannot open file 'msvcrt120.lib'
```

The parameters passed to `link.exe` are:
```
/nologo
/OUT:bazel-out/x64_windows-fastbuild/bin/app/app.exe
bazel-out/x64_windows-fastbuild/bin/app/main.o
external/gzgz_rules_d~override~d~d_dmd_x86_64-pc-windows-msvc/windows/lib64/phobos64.lib
/SUBSYSTEM:CONSOLE
/MACHINE:X64
/DEFAULTLIB:msvcrt.lib
/DEBUG:FASTLINK
/INCREMENTAL:NO
```

This is what DMD invokes as the link flag on my system:
```
C:\Program Files\Microsoft Visual
Studio\2022\Community\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64\link.exe
/NOLOGO "main.obj"   /DEFAULTLIB:phobos64  /LIBPATH:"C:\Program Files\Microsoft
Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\lib\x64"
legacy_stdio_definitions.lib /LIBPATH:"C:\Program Files (x86)\Windows
Kits\10\Lib\10.0.19041.0\ucrt\x64" /LIBPATH:"C:\Program Files (x86)\Windows
Kits\10\lib\10.0.19041.0\um\x64"
```

I tried to search for occurence of `msvcrt120` and `legacy_stdio_definitions.lib` in the DMD codebase, and noticed that they seem to get set when `getVCLibDir` (defined in `compiler/src/dmd/vsoptions.d`) returns null, which indicates that my VS Installation is probably not being detected. I don't know if this is true, since DMD doesn't show any info related to VS detection even in verbose output.

In addition, a skim of `vsoptions.d` tells me that DMD seems to make a lot of assumptions based on VS2017, which further increases my suspicion that newer VS installations may not get detected correctly.

I've been trying to build DMD from source to check, but I can't even build it locally with VS2022, so I thought I'd report this issue here and see if anyone who can build DMD on VS2022 may be able to verify if newer VS installations are being detected or not.

--