Skip to content

Locate dotnet host for illink on desktop#3192

Merged
nguerrera merged 2 commits into
dotnet:masterfrom
sbomer:locateHostForLinker
May 17, 2019
Merged

Locate dotnet host for illink on desktop#3192
nguerrera merged 2 commits into
dotnet:masterfrom
sbomer:locateHostForLinker

Conversation

@sbomer
Copy link
Copy Markdown
Member

@sbomer sbomer commented May 1, 2019

When running on full framework MSBuild, the environment variable DOTNET_HOST_PATH is not set, causing the linker to fail. This was not caught by the SDK tests because they are started by a test process that uses the dotnet cli, so this includes a change to prevent DOTNET_HOST_PATH from propagating to the MSBuild process.

For the desktop MSBuild case, I believe the correct thing to do is to use the dotnet host that comes with the SDK that is being used to build the app, so I'm trying to explicitly pass the location of that host (rather than searching the PATH, for example). @dsplaisted and @nguerrera does that sound right to you, or should I be doing something else?

The current fix has a problem: the relative path from the targets file to the dotnet host is correct for an actual product SDK, but because the tests run against a different layout, the computed location is incorrect when tests are run. @dsplaisted, @nguerrera do you have any suggestions? Perhaps the SDK already has some information about the dotnet host location that I can reuse.

<_DotNetHostDirectory>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)../../../../../))</_DotNetHostDirectory>
<_DotNetHostFileName>dotnet</_DotNetHostFileName>
<_DotNetHostFileName Condition=" '$(OS)' == 'Windows_NT' ">dotnet.exe</_DotNetHostFileName>
</PropertyGroup>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This ../../.. won't work in our test environment for full framework, where we set up things to point at the build output.

Thinking about the best approach here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the SDK always sets a property DotNetHost similar to AppHost executable, I think that would be useful.

@sbomer
Copy link
Copy Markdown
Member Author

sbomer commented May 9, 2019

@fadimounir @swaroop-sridhar this is the env var issue I mentioned today.

@nguerrera
Copy link
Copy Markdown
Contributor

Ah, forgot about this, sorry. Haven't figured out what to do about the issue above yet, but back to thinking about this.

@sbomer
Copy link
Copy Markdown
Member Author

sbomer commented May 14, 2019

@nguerrera any more thoughts on this?

@nguerrera
Copy link
Copy Markdown
Contributor

nguerrera commented May 14, 2019

How about this:

Set up NETCoreRoot next to NETCoreTargetingPackRoot here:

https://github.com/dotnet/core-sdk/blob/94503cff19d14a89ad36e9aeaf6bd9c07b292ba8/src/redist/targets/GenerateBundledVersions.targets#L154

This makes one fewer place in sdk where we have to .. (we already do that to find these bundled versions), and has prior art.

We can optimize a bit and make NETCoreTargetingPackRoot a function of NETCoreRoot.

Edit: it will also take care of the testing problem because we already override the bundled props location appropriately when testing.

@dsplaisted What do you think?

@sbomer
Copy link
Copy Markdown
Member Author

sbomer commented May 14, 2019

Ah, so that bundled props file is the place to put this. Thanks @nguerrera! If @dsplaisted agrees, I can go ahead and make a PR with the change in core-sdk.

@dsplaisted
Copy link
Copy Markdown
Member

Sounds good. I do wonder if there are interactions between environment variables such as DOTNET_HOST or DOTNET_ROOT and this new MSBuild property that we should consider.

@nguerrera
Copy link
Copy Markdown
Contributor

I thought about those a bit, it's why I hesitated here. The problem I see is that there are existing uses of DOTNET_HOST_PATH meant to detect if running CLI or not, and DOTNET_ROOT has a bunch of baggage too. I think this is separate and just the location of the dotnet hive that has the bundled versions. Maybe there's a better name?

@sbomer
Copy link
Copy Markdown
Member Author

sbomer commented May 14, 2019

As far as I can tell, DOTNET_ROOT is only read by the fx-dependent apphost, so would be set only in cases that are independent of this msbuild property. DOTNET_HOST_PATH, though, is set by the cli when invoking msbuild. So they will interact like this:

  • For "dotnet msbuild", ILLink will use DOTNET_HOST_PATH, which points to the same dotnet that was invoked. In this case, NETCoreRoot would point to the directory of the same executable (I'm not aware of any scenarios where "dotnet" would go off and find an SDK in a different directory, so they would be the same).
  • For "msbuild.exe", ILLink will use NETCoreRoot. This will be the root of the SDK install location that is chosen by msbuild's SDK resolver. DOTNET_HOST_PATH might be set if the "msbuild.exe" process was run indirectly from a "dotnet" process. In this case, DOTNET_HOST_PATH would be irrelevant to the linker because it could potentially be a completely different dotnet install, one we don't care about.

If I haven't missed anything, these interactions seem sensible to me. If we wanted to, I think we could even replace uses of DOTNET_HOST_PATH in msbuild tasks with NETCoreRoot, and they would continue working the same way.

sbomer added 2 commits May 16, 2019 16:12
When running on full framework MSBuild, the environment variable
DOTNET_HOST_PATH is not set. This was not caught by the SDK tests
because they are started by a test process that uses the dotnet cli,
so this includes a change to prevent DOTNET_HOST_PATH from propagating
to the MSBuild process.
@sbomer sbomer force-pushed the locateHostForLinker branch from 8176b5d to cd96a1d Compare May 16, 2019 23:16
@sbomer
Copy link
Copy Markdown
Member Author

sbomer commented May 17, 2019

@nguerrera @dsplaisted PTAL

@nguerrera nguerrera merged commit 9fd3e0b into dotnet:master May 17, 2019
wli3 pushed a commit that referenced this pull request Feb 7, 2020
…015.2 (#3192)

- Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19515.2
ViktorHofer added a commit that referenced this pull request May 7, 2026
The harness was wiping DOTNET_HOST_PATH from the msbuild.exe environment to
mirror the customer scenario where bare msbuild.exe is launched outside a
Developer Command Prompt. That clear was added in 2019 by #3192 ("Locate
dotnet host for illink on desktop") to expose ILLink's dependence on the
env var, and ILLink got a project-level fallback at the same time.

That customer scenario no longer reflects reality. Since #50066 (commit
ea55de9, 2025-07-19, "set env vars from resolver APIs"),
Microsoft.DotNet.MSBuildSdkResolver returns DOTNET_HOST_PATH via the
EnvironmentVariablesToAdd API whenever it resolves Microsoft.NET.Sdk. The
MSBuild engine then injects it into the resolver's process environment, so
by the time tasks (Csc, Microsoft.DotNet.ApiCompat.Task, etc.) execute,
DOTNET_HOST_PATH is set even if msbuild.exe was launched without it. Real
customers running bare msbuild.exe today are auto-fixed by the resolver;
Arcade's eng/common scripts also rely on this and never set
DOTNET_HOST_PATH explicitly.

Removing the clear restores natural propagation through the test harness:
dotnet exec sets DOTNET_HOST_PATH on the test process (per Muxer.cs), the
test process inherits it, and msbuild.exe inherits it from the test
process. This unblocks .NETCoreApp tasks declared with `Runtime="NET"`
(notably the new ApiCompat / GenAPI tasks introduced when removing
.NET Framework support from src/Compatibility) when running on the
FullFramework Helix leg, without needing per-test env-var workarounds.

Also revert two ineffective workarounds previously added while diagnosing
this:
- The DOTNET_HOST_PATH set in build/SetupHelixEnvironment.cmd (it never
  reached msbuild.exe because the harness immediately cleared it).
- The temporary _DiagnoseTaskHostProps target in
  test/TestAssets/TestProjects/PackageValidationTestProject.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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