Scripty icon indicating copy to clipboard operation
Scripty copied to clipboard

InvalidOperationException when loading build project

Open ghost opened this issue 9 years ago • 7 comments

I'm running the custom tool version of Scripty and I have a problem when accessing the Context.Project.Build property. The first time this is working well but when I run the script a second time I get the following exception:

System.InvalidOperationException: An equivalent project (a project with the same global properties and tools version) is already present in the project collection, with the path %PATH%. To load an equivalent into this project collection, unload this project first.
   at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args)
   at Microsoft.Build.Evaluation.ProjectCollection.LoadedProjectCollection.AddProject(Project project)
   at Microsoft.Build.Evaluation.ProjectCollection.OnAfterRenameLoadedProject(String oldFullPathIfAny, Project project)
   at Microsoft.Build.Evaluation.Project.<Initialize>b__152_0(String oldFullPath)
   at Microsoft.Build.Evaluation.Project.Initialize(IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectLoadSettings loadSettings)
   at Microsoft.Build.Evaluation.Project..ctor(String projectFile, IDictionary`2 globalProperties, String toolsVersion, String subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings loadSettings)
   at Scripty.Core.ProjectTree.ProjectRoot.get_Build()
   at Scripty.Core.ProjectTree.ProjectRoot.get_Children()
   at Submission#0.Utils.FindTemplates(ScriptContext context)
   at Submission#0.ProcessTemplates()
   at Submission#0.<<Initialize>>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.<RunSubmissionsAsync>d__9`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.Script`1.<RunSubmissionsAsync>d__21.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.ScriptStateTaskExtensions.<GetEvaluationResultAsync>d__1`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Scripty.Core.ScriptEngine.<Evaluate>d__5.MoveNext()

I think this is because the running process is still the same when running a second time and the project is never unloaded from it.

I worked around this by loading the project by myself and calling

ProjectCollection.GlobalProjectCollection.UnloadProject(project);

when I'm done with it. Alternativly loading the project via

ProjectCollection.GlobalProjectCollection.GetLoadedProjects(context.ProjectFilePath)

is also working well but I don't think it will outside of VS.

P.S.: T4 is dead, long live Scripty. Thanks for a really nice tool!

ghost avatar Sep 14 '16 11:09 ghost

Thanks for the praise, it keeps me going!

I haven't had much time this week, but hopefully I'll get a chance to dig into this a bit more next week. Thanks for the detailed bug report, and especially for the workaround. That should help narrow down what's going on. I'll keep you updated.

daveaglick avatar Sep 16 '16 19:09 daveaglick

No updates for this issue?

iDarush avatar Nov 29 '16 17:11 iDarush

@iDarush Not yet - been swamped the last couple months. Hoping to get back to Scripty over the holidays when things settle for a bit.

daveaglick avatar Nov 29 '16 18:11 daveaglick

I'm getting the same "equivalent project" exception @0x084E reported, when using the VS2015 extension + "Custom Tool" approach to consuming Scripty. Once the project has loaded for the first time, I can make exactly one change to a .csx file. Every other time I try to save the .csx file, I get the error.

@0x084E got me on the right path for working around the issue; however, I'm new enough that I didn't understand where the value of project came from, when calling the UnloadProject() method. Here's the .csx code that allows me to make multiple changes without having to restart Visual Studio:

using Microsoft.Build.Evaluation;

ProjectCollection.GlobalProjectCollection.UnloadProject(Project.Build);

ghost avatar Nov 30 '16 18:11 ghost

I was fighting some blockers on getting a VS 2017 version out. Now that I've gotten past those I can start working on the issue backlog. First thing to check is if the new 0.7.x version helps with this issue. Can any of you try this scenario out with the new version and report back? Thanks!

daveaglick avatar Mar 28 '17 23:03 daveaglick

Hello @daveaglick,

Thanks for making this great tool. I just tried it out in VS 2017 and I can confirm that this bug still exists in 0.7.0.0. Thankfully calling UnloadProject as mentioned above still works to get around the problem.

Thanks!

jadennis avatar Apr 10 '17 16:04 jadennis

Might this be this problem: https://github.com/dotnet/roslyn/wiki/FAQ#how-can-i-use-roslyn-in-an-msbuild-task-and-avoid-metadata-fetching-and-re-entrancy-conflicts

Sebazzz avatar Nov 27 '17 08:11 Sebazzz