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

Unregister does not reset IsRegistered to false #77

Closed
yaakov-h opened this issue Aug 29, 2019 · 13 comments
Closed

Unregister does not reset IsRegistered to false #77

yaakov-h opened this issue Aug 29, 2019 · 13 comments

Comments

@yaakov-h
Copy link

If I call RegisterInstance(...) and then Unregister(), IsRegistered continues to return true.

Any code using this to perform conditional registration checks will subsequently not register, and then crash when attempting to resolve an MSBuild assembly.

@Forgind
Copy link
Member

Forgind commented Dec 23, 2020

You're right—IsRegistered is more like "was once registered" than "is currently registered." Can you explain why you need to call Unregister in the first place?

@yaakov-h
Copy link
Author

I have an object that sets this up at the start of its lifecycle, so ideally it should also clean it up at the end of its lifecycle.

In practice though, since you can't unload assemblies in Framework, it can't really revert everything back to the original state...

@Forgind
Copy link
Member

Forgind commented Dec 23, 2020

Makes sense. I think all you'd have to do to resolve this is add s_registeredHandler = null; to Unregister(), but I'm reluctant to actually do that, partially because s_registeredHandler caches assemblies it's loaded, so it would also worsen perf a little should you want to register again—though, to be fair, that would be true right now anyway if it didn't error.

@rainersigwald
Copy link
Member

In practice though, since you can't unload assemblies in Framework, it can't really revert everything back to the original state...

Yeah, when reviewing #107 I came to the same realization--there's no real point to Unregister any more (since we don't have a finite set of assemblies that will be loaded) and I think we should just make it a no-op. Arguably we never should have exposed it in the first place.

@asoifer
Copy link

asoifer commented Mar 3, 2021

Hi everyone,
I found this library completely useful. But I'm having a hard problem to solve.

Let's consider I have a method "ExecuteCommand" for executing commands (using the classical ProcessStartInfo and Process instances).
Then, I create a project programmatically using dotnet commands, after that, I want to open that solution, and after that, I want to run again the solution.
This is the scenario in my source code:

ExecuteCommand("dotnet new sln -n MySolution");
ExecuteCommand("dotnet new console -n MyProject -f netcoreapp3.1");
ExecuteCommand("dotnet sln MySolution.sln add MyProject\\MyProject.csproj");
ExecuteCommand("dotnet run"); // In this case everything goes fine!

Here I want to analyze the solution (or doing some code analysis stuff)
MSBuildLocator.RegisterDefaults();
var msBuildWorkspace = MSBuildWorkspace.Create();
var mySolution = msBuildWorkspace.OpenSolutionAsync("MySolution.sln").Result;

Here I have my solution, and that's okay, but I want to run again the same solution
ExecuteCommand("dotnet run"); // In this case when I execute I have the following exception...

C:\Program Files\dotnet\sdk\3.1.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(194,5): error MSB4018: Unexpected error in the task "GenerateDepsFile". [C:\Users\myuser\AppData\Local\Temp\TestProject\TestProject\TestProject.csproj]
C:\Program Files\dotnet\sdk\3.1.301\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(194,5): error MSB4018: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.DotNet.PlatformAbstractions, Version=2.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system can not found the specified file. ...

After analyzing everything, the problem starts with "MSBuildLocator.RegisterDefaults();".
If I execute "dotnet run" immediately after this line I have the same error.
In this case, I wanted to unregister everything related to MSBuildLocator and I can't, even if I add MSBuildLocation.Unregister().

Anyone knows how can I solve this situation?
Thanks in advance :)

@asoifer
Copy link

asoifer commented Mar 3, 2021

Sorry I'm using this space for my issue, but after many hours I found how to solve the last problem I posted.

The idea is to remove the environment variables in your child process before executing dotnet run again.

Let's procStartInfo my instance of a ProcessStartInfo, then...

if (procStartInfo.EnvironmentVariables.ContainsKey("MSBuildSDKsPath"))
{
    procStartInfo.EnvironmentVariables.Remove("MSBuildSDKsPath");
    procStartInfo.EnvironmentVariables.Remove("MSBuildExtensionsPath");
    procStartInfo.EnvironmentVariables.Remove("MSBUILD_EXE_PATH");
}

And adding this fragment of code, I managed to combine dotnet commands with this library.
I hope this will be useful for anyone.
Regards.

@yaakov-h
Copy link
Author

yaakov-h commented Mar 3, 2021

It would probably be more "useful for anyone" if it was in a separate Issue with a name that described the scenario, rather than existing in a completely unrelated hijacked Issue. 😉

@Forgind
Copy link
Member

Forgind commented Mar 17, 2023

This looks resolved to me? Let me know if I'm wrong.

@Forgind Forgind closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2023
@yaakov-h
Copy link
Author

I don't believe this has been resolved. The opinion seems to be that instead of Unregister unregistering, there shouldn't be an Unregister, but there has been no action in either direction.

@Forgind
Copy link
Member

Forgind commented Mar 29, 2023

Well, we can't actually remove Unregister because whether or not it unregisters, people still call it, and removing it would be a breaking change for no reason. But as you said, we don't think Unregister should unregister, so I'd say there's no action to take.

@yaakov-h
Copy link
Author

Not quite. As it is now, it kind-of-Unregisters, which can cause consumers to crash.

If we change Unregister to a no-op, it should resolve the crash described in the original post.

I'd say that's actionable.

@Forgind
Copy link
Member

Forgind commented Mar 30, 2023

You're right; I was convinced we'd already done that, but I was wrong. I'll reopen this and make a PR to do that.

@Forgind Forgind reopened this Mar 30, 2023
@Forgind
Copy link
Member

Forgind commented Mar 30, 2023

#204

YuliiaKovalova added a commit that referenced this issue Aug 29, 2023
* Bump Microsoft.NET.Test.Sdk from 15.9.0 to 17.3.1 (#180)

Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 15.9.0 to 17.3.1.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Commits](microsoft/vstest@v15.9.0...v17.3.1)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump xunit from 2.4.1 to 2.4.2 (#172)

Bumps [xunit](https://github.com/xunit/xunit) from 2.4.1 to 2.4.2.
- [Release notes](https://github.com/xunit/xunit/releases)
- [Commits](xunit/xunit@2.4.1...2.4.2)

---
updated-dependencies:
- dependency-name: xunit
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump xunit.runner.visualstudio from 2.4.1 to 2.4.5 (#155)

Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.4.1 to 2.4.5.
- [Release notes](https://github.com/xunit/visualstudio.xunit/releases)
- [Commits](https://github.com/xunit/visualstudio.xunit/commits)

---
updated-dependencies:
- dependency-name: xunit.runner.visualstudio
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Shouldly from 4.0.3 to 4.1.0 (#187)

Bumps [Shouldly](https://github.com/shouldly/shouldly) from 4.0.3 to 4.1.0.
- [Release notes](https://github.com/shouldly/shouldly/releases)
- [Changelog](https://github.com/shouldly/shouldly/blob/master/BREAKING%20CHANGES.txt)
- [Commits](shouldly/shouldly@v4.0.3...4.1.0)

---
updated-dependencies:
- dependency-name: Shouldly
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Forgind <[email protected]>

* Bump Microsoft.VisualStudio.Setup.Configuration.Interop (#186)

Bumps Microsoft.VisualStudio.Setup.Configuration.Interop from 1.16.30 to 3.3.2180.

---
updated-dependencies:
- dependency-name: Microsoft.VisualStudio.Setup.Configuration.Interop
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <[email protected]>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Microsoft.VisualStudio.SDK.EmbedInteropTypes

Bumps Microsoft.VisualStudio.SDK.EmbedInteropTypes from 15.0.21 to 15.0.36.

---
updated-dependencies:
- dependency-name: Microsoft.VisualStudio.SDK.EmbedInteropTypes
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>

* Enabling CodeQL (#190)

Enables CodeQL in build pipeline

* Update xml doc comments (#193)

Update xml doc comments

* Bump Shouldly from 4.1.0 to 4.2.1

Bumps [Shouldly](https://github.com/shouldly/shouldly) from 4.1.0 to 4.2.1.
- [Release notes](https://github.com/shouldly/shouldly/releases)
- [Changelog](https://github.com/shouldly/shouldly/blob/master/BREAKING%20CHANGES.txt)
- [Commits](shouldly/shouldly@4.1.0...4.2.1)

---
updated-dependencies:
- dependency-name: Shouldly
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bump Microsoft.VisualStudio.Setup.Configuration.Interop

Bumps Microsoft.VisualStudio.Setup.Configuration.Interop from 3.3.2180 to 3.6.2115.

---
updated-dependencies:
- dependency-name: Microsoft.VisualStudio.Setup.Configuration.Interop
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bump Nerdbank.GitVersioning from 3.5.107 to 3.6.133

Bumps [Nerdbank.GitVersioning](https://github.com/dotnet/Nerdbank.GitVersioning) from 3.5.107 to 3.6.133.
- [Release notes](https://github.com/dotnet/Nerdbank.GitVersioning/releases)
- [Commits](dotnet/Nerdbank.GitVersioning@v3.5.107...v3.6.133)

---
updated-dependencies:
- dependency-name: Nerdbank.GitVersioning
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Bump Microsoft.NET.Test.Sdk from 17.3.1 to 17.6.2

Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.3.1 to 17.6.2.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](microsoft/vstest@v17.3.1...v17.6.2)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>

* Update Releasing_MSBuildLocator.md

* Bump xunit.runner.visualstudio from 2.4.5 to 2.5.0 (#223)

Bumps [xunit.runner.visualstudio](https://github.com/xunit/visualstudio.xunit) from 2.4.5 to 2.5.0.
- [Release notes](https://github.com/xunit/visualstudio.xunit/releases)
- [Commits](xunit/visualstudio.xunit@v2.4.5...2.5.0)

---
updated-dependencies:
- dependency-name: xunit.runner.visualstudio
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Microsoft.NET.Test.Sdk from 17.6.2 to 17.6.3 (#221)

Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.6.2 to 17.6.3.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](microsoft/vstest@v17.6.2...v17.6.3)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump xunit from 2.4.2 to 2.5.0 (#222)

Bumps [xunit](https://github.com/xunit/xunit) from 2.4.2 to 2.5.0.
- [Commits](xunit/xunit@2.4.2...2.5.0)

---
updated-dependencies:
- dependency-name: xunit
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Make Unregister a no-op Fixes #77 (#204)

* Make Unregister a no-op

* Change comment

* Specify not to show Unregister

* Sort usings

---------

Co-authored-by: Forgind <[email protected]>

* Validate dotnet executable exists (#202)

* Validate dotnet executable exists

* PR comment

* Update src/MSBuildLocator/DotNetSdkLocationHelper.cs

Co-authored-by: Ladi Prosek <[email protected]>

* Simplify logic

Also avoids an unnecessary File.Exists check on Windows

---------

Co-authored-by: Forgind <[email protected]>
Co-authored-by: Ladi Prosek <[email protected]>

* Bump Microsoft.NET.Test.Sdk from 17.6.3 to 17.7.0 (#226)

Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.6.3 to 17.7.0.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](microsoft/vstest@v17.6.3...v17.7.0)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump Microsoft.VisualStudio.Setup.Configuration.Interop (#227)

Bumps Microsoft.VisualStudio.Setup.Configuration.Interop from 3.6.2115 to 3.7.2175.

---
updated-dependencies:
- dependency-name: Microsoft.VisualStudio.Setup.Configuration.Interop
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Respect DOTNET_ROOT (#225)

* Respect DOTNET_ROOT

* DOTNET_ROOT is a folder. Also, DOTNET_ROOT(x86)

* PR Feedback

---------

Co-authored-by: Forgind <[email protected]>

* Bump Microsoft.NET.Test.Sdk from 17.7.0 to 17.7.1 (#228)

Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 17.7.0 to 17.7.1.
- [Release notes](https://github.com/microsoft/vstest/releases)
- [Changelog](https://github.com/microsoft/vstest/blob/main/docs/releases.md)
- [Commits](microsoft/vstest@v17.7.0...v17.7.1)

---
updated-dependencies:
- dependency-name: Microsoft.NET.Test.Sdk
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* upgrade core version to net6.0  (#231)

* upgrade framework version + fix issue with path extraction from environment variable

* fix review comments

* Fix resolving hostfxr for Mac OS and Linux envs (#230)

* add setting of "DOTNET_HOST_PATH" env variable (#235)

* Fix hostfx resolving issue in some Mac machines (#236)

* Add diagnostic logging, and address issues in the code.

* Update the fix:

1, it turns out DotnetPath is the folder path. It just sets DOTNET_HOST_PATH incorrectly
  DOTNET_HOST_PATH is a file path, which is different than DOTNET_ROOT

 2, Fix DOTNET_HOST_PATH handling, which broke the application when it sets to a folder.

* use StringComparison.OrdinalIgnoreCase to compare file name

maybe should use platform dependent comparison, but it looks like the rest of code is doing that.

* do not create new instance on each call.

* Further hardern the logic inside HostFxrResolver

Adds more logging and handles empty folder.

* delete unnecessary logging.

* Additional logging.

* Throw errors instead of logging it.

---------

Co-authored-by: Lifeng Lu <[email protected]>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Forgind <[email protected]>
Co-authored-by: MichalPavlik <[email protected]>
Co-authored-by: Forgind <[email protected]>
Co-authored-by: Ladi Prosek <[email protected]>
Co-authored-by: Lifeng Lu <[email protected]>
Co-authored-by: Lifeng Lu <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants