Build fails if working directory is not <coreclr-root>
Building from outside root used to work fine. I'm not sure when or why it regressed.
Repro
Run <coreclr-root>\build.cmd or <coreclr-root>/build.sh from src, src/jit, etc. Presumably anywhere other than the root of coreclr.
Output (Windows)
C:\git\coreclr\src>c:\git\coreclr\build.cmd x64 release
Starting Build at 15:41:44.19
BUILD: Commencing CoreCLR Repo build
BUILD: Checking prerequisites
BUILD: Using environment: "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\\VsDevCmd.bat"
Tools are already initialized.
Running: c:\git\coreclr\Tools\dotnetcli\dotnet.exe c:\git\coreclr\Tools\run.exe c:\git\coreclr\config.json build -Project=c:\git\coreclr\build.proj -generateHeaderWindows -NativeVersionHeaderFile="c:\git\coreclr\bin\obj\_version.h" -BuildOS=Windows_NT -BuildType=Release -BuildArch=x64
Running: c:\git\coreclr\Tools\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /maxcpucount /nodeReuse:false /l:BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log c:\git\coreclr\build.proj /p:__BuildType=Release /p:__BuildArch=x64 /p:__BuildOS=Windows_NT /t:GenerateVersionHeader /p:GenerateVersionHeader=true /p:NativeVersionHeaderFile=c:\git\coreclr\bin\obj\_version.h
MSBUILD : error MSB1021: Cannot create an instance of the logger. Could not load file or assembly 'Tools\/net46\/Microsoft.DotNet.Build.Tasks.dll' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
Switch: BinClashLogger,Tools/net46/Microsoft.DotNet.Build.Tasks.dll;LogFile=binclash.log
Command execution failed with exit code 1.
BUILD: Restoring the OptimizationData Package
Tools are already initialized.
Running: c:\git\coreclr\Tools\dotnetcli\dotnet.exe c:\git\coreclr\Tools\run.exe c:\git\coreclr\config.json sync -optdata
Running: c:\git\coreclr\Tools\msbuild.cmd /nologo /verbosity:minimal /clp:Summary /maxcpucount /nodeReuse:false /flp:v=detailed;LogFile=sync.log /t:RestoreOptData /p:RestoreDuringBuild=true ./build.proj
C:\git\coreclr\src\build.proj : error MSB4057: The target "RestoreOptData" does not exist in the project.
C:\git\coreclr\src\build.proj : error MSB4057: The target "RestoreOptData" does not exist in the project.
Command execution failed with exit code 1.
BUILD: Error: Failed to restore the optimization data package.
C:\git\coreclr\src>
@gkhanna79 @wtgodbe I only briefly investigated this, but it looks like run.exe is invoking msbuild with a reference to ".\build.proj". I don't know if that's the problem, but it looks suspicious.
@wtgodbe PTAL at this before you leave :)
I believe the issues lie here (with 3 other such incriminating lines just below): https://github.com/dotnet/coreclr/blob/master/config.json#L439
And here: https://github.com/dotnet/coreclr/blob/master/config.json#L43
With another potential for error here: https://github.com/dotnet/coreclr/blob/master/config.json#L569
Running sync.cmd from coreclr/src locally confirms.
There should probably be some variable that defines the path of the repo root, which we can prepend to these values - @maririos do we already have such a thing here or in CoreFX?
More: https://github.com/dotnet/coreclr/blob/master/config.json#L611 & https://github.com/dotnet/coreclr/blob/master/config.json#L615
Also cc @chcosta @Priya91
@MattGal
@wtgodbe It's the same behavior in corefx as well, build scripts work only when invoked from root. This is the behavior of run tool, not repo specific.
E:\corefx\src>..\build.cmd
Tools are already initialized.
The specified config file 'E:\corefx\src\config.json' does not exist.
Error: Could not load Json configuration file.
@Priya91 Why do we have this constraint?
@Priya91 Why do we have this constraint?
The infra team will have more info on the current behavior of buildtools.
This is simply a limitation of how this was originally implemented; fixing to adapt for this (i.e. looking in the assembly's directory versus the current directory for config.json) is not tough, but the point of run.exe and config was not specifically to be the primary workflow for users building a single project from a command line, but rather a common set of entry points for building the product, tests, etc.
/cc: @hseok-oh @jyoungyun
Shouldn't be hard to make this tweak, but do note that I'd expect building build.cmd from a different directory is likely to fail later on.
@mattgal To be clear, this isn't about building single projects. This was found because we have a tool that invokes the coreclr build as a subset of its operation. That tool previously didn't have to change the working directory to make sure the build would succeed.
That's not terribly important, though. The key is whether run.exe (or whatever tool(s) involved) relies on ambient state, e.g. current working directory, for correct operation by design. If so, we'll need to adapt coreclr's build system to accommodate that new interface restriction because IMO we don't want the build to rely on ambient state. If not, then can you open an appropriate issue against buildtools and mention it here?
Reading the source, I'd expect what it was doing to ALREADY be smart about config.json location. I will at least get this investigated today; going on vacation for 10 days starting tomorrow either way.
@MattGal Did anything come of your investigation?
Apologies for not updating, I did not get to this before I had to leave for vacation. As I noted before, it looks like (from https://github.com/dotnet/buildtools/blob/0e059aeb88f91ac633e5853c68ee6fb362ced87d/src/Run/Executor.cs#L30 ) that this should already be working the way you'd expect.
Even if we fix this however, build.cmd will not work as it makes lots of assumptions about its path; why not simply insert a "pushd %~dp0" in the beginning of CoreCLR's build.cmd?
@RussKeldorph after investigating, I was correct; the problem as described has already been fixed, which is why it looks like it should already be past the error listed.
Run.exe just runs what it finds in config.json, and when it hits stuff like this: https://github.com/dotnet/coreclr/blob/master/config.json#L43 ... it doesn't know what to do, since the path to the logging task given has no way to be resolved relative to the path of config.json.
There's lots of ways to address this, such as defining an environment variable representing the base path of the repo and making all commands use this, but I think given the many other challenges faced simply pushing into the directory of build.cmd when calling it is the easiest course.
@mattgal Ok. I just wanted to know whether the problem was with Run.exe or was something each repo needs to address. Thanks.
@wtgodbe We've worked around this problem in the code formatting tool where this was initially encountered, so I don't see an urgent need to fix this, but it might be nice someday.
Closing as I suspect this may be addressed by upcoming cleanup work and, at any rate, doesn't seem to have caused many problems thus far.