Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update BuildEnvironmentHelper to locate .NET Core app directory #1069

Merged
merged 4 commits into from
Sep 23, 2016

Conversation

jeffkl
Copy link
Contributor

@jeffkl jeffkl commented Sep 22, 2016

In .NET Core, the AppContext.BaseDirectory can be used to locate the application's dependencies.

Related to #1039

In .NET Core, the AppContext.BaseDirectory can be used to locate the application's dependencies.
@rainersigwald
Copy link
Member

Can you elaborate on why this helps? I don't think I fully understand the scenario.

@jeffkl
Copy link
Contributor Author

jeffkl commented Sep 22, 2016

When you execute dotnet run, the .NET Core runtime loads references from NuGet package locations. This means Microsoft.Build.Framework.dll is loaded from one place, while Microsoft.Build.dll is loaded from another.

However, when you do dotnet publish, all of the assemblies are placed in the output folder and loaded form it. In these two cases, AppContext.BaseDirectory is the location of the executing app which would reference Microsoft.Build.Runtime package and get the .props and .targets in their output directory. So this change makes it work that your app's directory is used as the location of MSBuild. Does that make sense?

@rainersigwald
Copy link
Member

Ahh, yes it does make sense! So with the new package, the toolset files will be in AppContext.BaseDirectory even though the DLLs are loaded from elsewhere.

Can you update the comment for this case to be explicit about that scenario and why loading from that directory is right?


#if !CLR2COMPATIBILITY // Assemblies compiled against anything older than .NET 4.0 won't have a System.AppContext
// Try the base directory that the assembly resolver uses to probe for assemblies.
() => TryFromFolder(AppContext.BaseDirectory, runningTests, runningInVisualStudio, visualStudioPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see what's up, I think this should be above the current-working-directory path. If someone's redistributing an MSBuild runtime, that should be preferred over something that happens to be in the CWD, just as it would be if we found it next to our MSBuild.exe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on this too. Technically if I see MSBuild.exe (and the runtime) in the current directory, I sort of expect it to be used. That would make sense for an end user but not necessarily for the app developer. It seems rare that someone would change the current directory to some place that has MSBuild and then run a completely different app...

@jeffkl
Copy link
Contributor Author

jeffkl commented Sep 22, 2016

Yeah but _only_ if you can help me understand why the test fails... Is it because my change needs to be mocked to not return a valid location?

@jeffkl
Copy link
Contributor Author

jeffkl commented Sep 23, 2016

I have updated the comment, let me know about the test fix when you have a chance.

@rainersigwald
Copy link
Member

I think I get the test failure. Inside the test run I have AppContext.BaseDirectory set to C:\src\msbuild\bin\Debug-NetCore\Windows_NT_Deployment_Test\, which is accurate--that's where it's running from. But that folder also contains a full toolset--which your new fallback will find. Then a toolset will be found and the test for "don't find toolsets in condition X" will fail.

You should be able to fix this by adding a new delegate like the ones that are already there to return AppContext.BaseDirectory . . . unless you've overridden it to return null in the test, which you should also do.

@@ -105,7 +105,7 @@ public void BuildEnvironmentDetectsVisualStudioByEnvironment()
{
Environment.SetEnvironmentVariable("VSINSTALLDIR", env.TempFolderRoot);
Environment.SetEnvironmentVariable("VisualStudioVersion", "15.0");
BuildEnvironmentHelper.ResetInstance_ForUnitTestsOnly();
BuildEnvironmentHelper.ResetInstance_ForUnitTestsOnly(getAppContextBaseDirectory: ReturnNull);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to specify this? Seems like it should be redundant (since it's after the env-var fallback).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried if the order ever changed then the test could start to fail so I put this in here to remove the AppContext.BaseDirectory from the equation for all cases. I can remove it if you'd like

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd rather the tests fail in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll update the test

@jeffkl jeffkl force-pushed the BuildEnvironmentHelper branch from 77e9cff to 33cb003 Compare September 23, 2016 15:55
@jeffkl
Copy link
Contributor Author

jeffkl commented Sep 23, 2016

@dotnet-bot test OSX Build for CoreCLR please

@jeffkl jeffkl force-pushed the BuildEnvironmentHelper branch from 33cb003 to a8d9a62 Compare September 23, 2016 16:45
@jeffkl
Copy link
Contributor Author

jeffkl commented Sep 23, 2016

@AndyGerlicher @rainersigwald do you guys approve?

@jeffkl jeffkl merged commit c09995d into dotnet:xplat Sep 23, 2016
@jeffkl jeffkl deleted the BuildEnvironmentHelper branch September 23, 2016 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants