Cannot call curl from MSBuild
Steps to reproduce
Windows 10 now comes with curl included (usually under C:\Windows\System32) however that also means that the utility cannot be called from inside x64 MSBuild:
Project file (download.proj)
<Project>
<Target Name="Download">
<Exec Command="curl -L $(from) -o $(to)" />
</Target>
</Project>
Command line
msbuild download.proj /p:from="some url" /p:to="some path"
Expected behavior
The curl is called, file downloaded.
Actual behavior
Error 9009, curl could not be found.
Environment data
msbuild /version output:
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Framework
OS info: Windows 10 Enterprise (build 1803, 17134.1304)
No matter what I try curl is never found. Absolute path, setting PATH inside the command to contain it etc. Nothing works. And on the command line conversely everything is fine. I even have system32 in system PATH. So I eventually found out the reason, it seems to be this:
When trying to reproduce your problem I tried a couple of things (running curl, running 64bit curl, running when it's in the PATH, running with specifying full path, running an executable from the system32 diretory), but the one thing I didn't try out of principle is copying curl to the system32 directory: doing that is almost never the correct solution for a problem, for a variety of reasons which can be found on the internet. And yes, today we found yet another reason why it's bad :] MsBuild's Exec task works by creating a temporary file including the Command to run, and then launching cmd.exe /q /c <path/to/tempfile>. The task does not just start cmd.exe though, but specifies the the full path by prepending it with Environment.GetFolderPath(Environment.SpecialFolder.System). Which returns C:\windows\SysWOW64 or similar when called from a 32bit process (i.e. msbuild) on a 64bit system. As a result msbuild starts the 32bit cmd from C:\windows\SysWOW64\cmd.exe. Being a 32bit process, the file system redirection gets invoked: if you tell a 32bit process to look in c:\windows\system32 it won't do that, but look in c:\windows\SysWOW64. And curl.exe is not there since you put it in system32. And so you get a code 9009. Long story short: be a good Windows citizen and don't put curl.exe in sSystem32 or SysWOW64, and then either specify the full path to it when using Exec or add the directory where it is located to your PATH.
Except I have not put curl.exe to system32, Microsoft did. I can of course put it into some other location but what gives? Who thought this behaviour to be reasonable? Silent redirection like this is crazy. And especially when it applies to every access of system32 be it relative, absolute, from PATH etc. There is literally no way I can call curl from inside 64-bit MSBuild.
I am not even sure this can be fixed in MSBuild itself as it sounds more like a Windows issue. But perhaps there is a workaround?
Thanks.
Addendum: The issue seems to be related to the OS version. On Windows 10 (1909) it works fine.
@Resurr3ction what's the output of where.exe curl on the failing machine? According to https://techcommunity.microsoft.com/t5/containers/tar-and-curl-come-to-windows/ba-p/382409, it should be available in 1803 (17063+).
On the command line it is:
c:\Windows\System32\curl.exe
From inside MSBuild using CurlDownload.proj
<?xml version="1.0" encoding="utf-8"?>
<Project>
<Target Name="Build">
<Exec Command="where curl.exe" />
</Target>
</Project>
The output is:
INFO: Could not find files for the given pattern(s). error MSB3073: The command "where curl.exe" exited with code 1.
Full console output:
c:\dev\projects\CFR\rpos\Build>where curl
C:\Windows\System32\curl.exe
c:\dev\projects\CFR\rpos\Build>msbuild CurlDownload.proj
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.
Build started 07-Apr-20 08:47:05.
Project "c:\dev\projects\CFR\rpos\Build\CurlDownload.proj" on node 1 (default targets).
Build:
where curl.exe
INFO: Could not find files for the given pattern(s).
c:\dev\projects\CFR\rpos\Build\CurlDownload.proj(4,9): error MSB3073: The command "where curl.exe" exited with code 1.
Done Building Project "c:\dev\projects\CFR\rpos\Build\CurlDownload.proj" (default targets) -- FAILED.
Build FAILED.
"c:\dev\projects\CFR\rpos\Build\CurlDownload.proj" (default target) (1) ->
(Build target) ->
c:\dev\projects\CFR\rpos\Build\CurlDownload.proj(4,9): error MSB3073: The command "where curl.exe" exited with code 1
.
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.66
c:\dev\projects\CFR\rpos\Build>
I've got a theory. Can you try this project on that machine, @Resurr3ction?
<Project>
<Target Name="Download">
<Exec Command="C:\Windows\sysnative\curl.exe -L $(from) -o $(to)" />
</Target>
</Project>
Sorry I must have missed the notification. I have tried it and it works! Thanks!
However what is sysnative anyway? There is no such directory in the Windows directory. Yet it works. Another silent replacement in path I guess? Should I add that to PATH? I mean I guess I would be content if it worked on all machines.
EDIT: It works only from inside MSBuild. On the command line that path does not work. Hmm.
Alright so I did some reading. This turned out to be helpful:
https://www.samlogic.net/articles/sysnative-folder-64-bit-windows.htm
While it is completely mad (64-bit apps in system32 and 32-bit ones in SysWOW64 ???). Sysnative is only visible from 32-bit apps (???). There is literally nothing that could point one to the problem because the system just tells you that "c:/Windows/system32/whatever" does not exist even when it clearly does. And you are left unsuspecting that some silent redirection happened and it does not bother to tell you about it. Also x64 VS 2019 command line is running 32-bit MSBuild. Another surprise.
I have seen my fair share of weirdness in Software but this is some entirely different level to be honest.
But thank you! I would never have thought it could be something like this. Will be wary of this in the future.
I already entered a community feedback item about the MsBuild 32-bits running under the "x64 command prompt" : https://developercommunity.visualstudio.com/content/problem/784372/msbuild-architecture-not-the-same-as-developer-com.html However I suspect changing that would break even more things that unknownlingly "rely" on having 32-bits MsBuild.
On a completely different note: Recent MSBuild versions also have a https://docs.microsoft.com/en-us/visualstudio/msbuild/downloadfile-task?view=vs-2019 task. It's relatively (compared to other MSBuild stuff) new so not many people have learned about it yet - so if you don't want to mess around with manual curl invocations, that could be useful too. (But of course, curl can be used for much more than downloading files)
@dasMulli Thanks for pointing that out. I am aware of the DownloadFile task. It's a good thing but we use curl to access Artifactory with a token so DownloadTask cannot be used unfortunately. Otherwise I am all for staying in bounds of my build system when I can.
.. seems like a reasonable feature request for <DownloadFile> though - setting an authorization header.
(especially since that's a thing i'd be setting as an env var so i could use it as an msbuild property)
However I suspect changing that would break even more things that unknownlingly "rely" on having 32-bits MsBuild.
Yup :(
.. seems like a reasonable feature request for
<DownloadFile>though - setting an authorization header.
Feel free to file a feature request. One complexity here is that properties and task inputs are logged, while you probably don't want to have your secrets logged. Ideally, they'd be retrieved from a secret store just in time, and we probably don't want to make MSBuild dependent on any specific secret store.
Closing this because it's a (confusing, surprising) Windows behavior.