You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Inline Rename and Find all References fail across projects #3033
It looks like what's happening is that you're adding a project reference to a project which has itself not been added to the VisualStudioProjectTracker yet. That causes this code to get null back out here, which we just pass along until we dereference it to get the Idhere. Not sure why we don't have the other project in the workspace yet though.
@Pilchie Ugh, yes, all we've done is create a project ID. Likely we should do this:
let rec setup (site: IProjectSite) =
....
for referencedSite in referencedProjectSites do
setup referencedSite
let referencedProjectFileName = referencedSite.ProjectFileName()
let referencedProjectDisplayName = projectDisplayNameOf referencedProjectFileName
let referencedProjectId = workspace.ProjectTracker.GetOrCreateProjectIdForPath(referencedProjectFileName, referencedProjectDisplayName)
project.AddProjectReference(ProjectReference referencedProjectId)
if not (workspace.ProjectTracker.ContainsProject(project)) then
workspace.ProjectTracker.AddProject(project)
rather than
let rec setup (site: IProjectSite) =
....
for referencedSite in referencedProjectSites do
let referencedProjectFileName = referencedSite.ProjectFileName()
let referencedProjectDisplayName = projectDisplayNameOf referencedProjectFileName
let referencedProjectId = workspace.ProjectTracker.GetOrCreateProjectIdForPath(referencedProjectFileName, referencedProjectDisplayName)
project.AddProjectReference(ProjectReference referencedProjectId)
if not (workspace.ProjectTracker.ContainsProject(project)) then
workspace.ProjectTracker.AddProject(project)
for referencedSite in referencedProjectSites do
setup referencedSite
@cartermp I can confirm this is not fixed in latest nightly for the case where you create a project and library and then rename. I can confirm it does work for the case where you restart VS, i.e. the solution and project are already created.
The underlying bug feels like it must either be
the project options/references are not being kept up-to-date in the face of add/remove references between projects, or
something is cached using the previous options/references
I suppose the problem is likely to be that, in the incremental case, project.AddProjectReference is never being called, in LanguageService.Fs. That's only being called in the initial setup code.
That's also partially confirmed by the fact the unload/reload project is now a workaround
Here's my analysis - @KevinRansom, @brettfo, @Pilchie please read. I will submit the content below as documentation, but I really need you guys to fully understand how this code works and fix the bugs in it, to make sure we don't have any further massive snafus :)
In LanguageService.fs we are trying to keep several versions of project information in sync. Here's how I believe this works:
For the old F# project system (Which is what we are dealing with here), the authoritative view of the project information is maintained by Project.fs. This information is conveyed to the rest of the implementation via an IProjectSite interface (an unfortunate name given that Site has other meanings in VS?).
For the new F# project system my limited understanding is that it manages the project information using a Roslyn project in the workspace. This means a lot of the stuff below is irrelevant in that case.
For most purposes, an IProjectSite has to provide three main things: the source files, the compilation options, and the assembly references. Project.fs collects the first two from MSBuild. For the third - assembly references - it looks through the nodes of the hierarchy for the F# project, e.g. here. This looks for AssemblyReferenceNode nodes in the hierarchy and extracts out their referenced path. (There seems to be an essentially duplicated version of this code in ProjectSitesAndFiles which walks over the IProjectSite using cruddy VS APIs to again collect referenced project nodes as further IProjectSite references. We can probably get rid of this duplication easily enough.)
In our LanguageService.fs, we also use this IProjectSite information to maintain a corresponding Roslyn project in the workspace. This is done by SetupProjectFile and SyncProject by making various calls to add/remove methods on both project: AbstractProject and projectContext: IWorkspaceProjectContext. (These are actually the same object but are two interfaces to it, something that's not entirely clear to me).
The calls we currently make in SyncProject and SetupProjectFile are
On incremental project sync SyncProject we variously call projectContext.AddSourceFile, projectContext.RemoveSourceFile, projectContext.AddMetadataReference, projectContext.RemoveMetadataReference based on a diff to the list.
On initial project setup, we call SyncProject then also call project.AddProjectReference(..) for each referenced project (after setting up each referenced project). This is only done on initial setup, which is the bug. Note that during initial setup we call bothproject.AddProjectReference and projectContext.AddMetadataReference. I don't think the AbstractProject implementation knows that these are related, though I'm not sure it matters
So one fix might be to call project.AddProjectReference and project.RemoveProjectReference in SyncProject. However Roslyn only makes project.AddProjectReference available - project.RemoveProjectReference is protected. So that approach won't work without a Roslyn change
However, looking at the Roslyn CPS project code, it seems to me that Roslyn's CPSProject/AbstractProject implementation is probably meant to convert the metadata references managed by AddMetadataReference and RemoveMetadataReference to be project references, using Roslyn's "ProjectReferenceConversion" internal feature. That is, from what I see here this should be happening. However I haven't checked under the debugger. I expect it is not happening correctly, since otherwise we wouldn't have this bug.
I think it's worth checking to see what happens in the project conversion - it is tied to the output path of the projects, and only does the conversion if the incoming metadata reference path matches a project's output path. This is doubly complicated by the fact that most projects actually have two output paths. The compiler is generally told to build to the obj path, and then MSBuild copies the resulting binary to the bin directory. It's this bin directory that is normally passed to other projects as a reference. My hunch is that the paths aren't lining up quite right.
For reference: the split between AbstractProject and IWorkspaceProjectContext is basically between what grew up organically to support the old C# project system, and what we'd like to expose publicly for CPS based projects going forward. We should over time be working toward making it so that F# doesn't need to deal with AbstractProject at all if possible.
This is now fixed as of 15.6 Preview 1.1. I suspect that the work @KevinRansom did in the relevant files (LanguageService.fs and ProjectSitesAndFiles.fs) to clean things up and enable .NET Standard and .NET Core support is what has fixed this.
Activity
vasily-kirichenko commentedon May 12, 2017
Were all documents saved to disk? If not, it's a known bug - FCS reads all files from disk, except current one.
vasily-kirichenko commentedon May 12, 2017
I can reproduce it. However, if you close the solution and reopen it, renaming works OK:
vasily-kirichenko commentedon May 12, 2017
I've tested it on #3025 and found that it does not fix this bug.
vasily-kirichenko commentedon May 12, 2017
I debugged it seems that
this.Solution.GetProjectDependencyGraph().GetProjectsThatDirectlyDependOnThisProjectdoes not return proper results.cartermp commentedon May 13, 2017
Thanks for the investigating work @vasily-kirichenko
cartermp commentedon Jul 25, 2017
From @vasily-kirichenko:
vasily-kirichenko commentedon Jul 25, 2017
Adding project reference raises NRE:
Stack trace:
vasily-kirichenko commentedon Jul 25, 2017
After that
GetDependentProjectsreturns empty list, hence the features do not work.dsyme commentedon Jul 25, 2017
@Pilchie could you take a look at https://github.com/Microsoft/visualfsharp/blob/master/vsintegration/src/FSharp.Editor/LanguageService/LanguageService.fs#L420 for us? What are we doing wrong here to get a stack like the above?
Particularly:
Pilchie commentedon Jul 25, 2017
It looks like what's happening is that you're adding a project reference to a project which has itself not been added to the
VisualStudioProjectTrackeryet. That causes this code to getnullback out here, which we just pass along until we dereference it to get theIdhere. Not sure why we don't have the other project in the workspace yet though.@jasonmalinowski any thoughts?
dsyme commentedon Jul 25, 2017
@Pilchie Ugh, yes, all we've done is create a project ID. Likely we should do this:
rather than
dsyme commentedon Jul 25, 2017
@Pilchie @cartermp @vasily-kirichenko Here's that potential fix if you get to give it a go on the repro and other cases #3387
vasily-kirichenko commentedon Jul 26, 2017
#3387 seems to fix "Find all references", but not Inline Rename.
vasily-kirichenko commentedon Jul 26, 2017
But loading VFT solution raises this assert (twice):
Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
Could not retrive referenced project guidproject file
at Microsoft.VisualStudio.FSharp.ProjectSystem.ProjectReferenceNode..ctor(ProjectNode root, ProjectElement element)
at Microsoft.VisualStudio.FSharp.ProjectSystem.FSharpProjectReferenceNode..ctor(ProjectNode root, ProjectElement element)
at Microsoft.VisualStudio.FSharp.ProjectSystem.FSharpReferenceContainerNode.CreateProjectReferenceNode(ProjectElement element)
at Microsoft.VisualStudio.FSharp.ProjectSystem.ReferenceContainerNode.CreateReferenceNode(String referenceType, ProjectElement element, BuildResult buildResult)
at Microsoft.VisualStudio.FSharp.ProjectSystem.ReferenceContainerNode.LoadReferencesFromBuildProject(Project buildProject)
at Microsoft.VisualStudio.FSharp.ProjectSystem.ProjectNode.ProcessReferences()
at Microsoft.VisualStudio.FSharp.ProjectSystem.ProjectNode.Reload()
at Microsoft.VisualStudio.FSharp.ProjectSystem.FSharpProjectNode.Reload()
at Microsoft.VisualStudio.FSharp.ProjectSystem.ProjectNode.......
--------------------------- Abort Retry Ignore ---------------------------dsyme commentedon Aug 17, 2017
@cartermp I can confirm this is not fixed in latest nightly for the case where you create a project and library and then rename. I can confirm it does work for the case where you restart VS, i.e. the solution and project are already created.
The underlying bug feels like it must either be
I suppose the problem is likely to be that, in the incremental case,
project.AddProjectReferenceis never being called, in LanguageService.Fs. That's only being called in the initial setup code.That's also partially confirmed by the fact the unload/reload project is now a workaround
dsyme commentedon Aug 17, 2017
Here's my analysis - @KevinRansom, @brettfo, @Pilchie please read. I will submit the content below as documentation, but I really need you guys to fully understand how this code works and fix the bugs in it, to make sure we don't have any further massive snafus :)
In LanguageService.fs we are trying to keep several versions of project information in sync. Here's how I believe this works:
For the old F# project system (Which is what we are dealing with here), the authoritative view of the project information is maintained by Project.fs. This information is conveyed to the rest of the implementation via an
IProjectSiteinterface (an unfortunate name given thatSitehas other meanings in VS?).For the new F# project system my limited understanding is that it manages the project information using a Roslyn project in the workspace. This means a lot of the stuff below is irrelevant in that case.
For most purposes, an
IProjectSitehas to provide three main things: the source files, the compilation options, and the assembly references. Project.fs collects the first two from MSBuild. For the third - assembly references - it looks through the nodes of the hierarchy for the F# project, e.g. here. This looks forAssemblyReferenceNodenodes in the hierarchy and extracts out their referenced path. (There seems to be an essentially duplicated version of this code in ProjectSitesAndFiles which walks over the IProjectSite using cruddy VS APIs to again collect referenced project nodes as further IProjectSite references. We can probably get rid of this duplication easily enough.)In our LanguageService.fs, we use this IProjectSite information to incrementally maintain a corresponding F# CompilerService
FSharpProjectOptionsvalue. I think that is working correctly at the moment, which is why features like file checking, autocomplete and goto-definition are working.In our LanguageService.fs, we also use this IProjectSite information to maintain a corresponding Roslyn project in the workspace. This is done by
SetupProjectFileandSyncProjectby making various calls to add/remove methods on bothproject: AbstractProjectandprojectContext: IWorkspaceProjectContext. (These are actually the same object but are two interfaces to it, something that's not entirely clear to me).The calls we currently make in
SyncProjectandSetupProjectFileareOn incremental project sync
SyncProjectwe variously callprojectContext.AddSourceFile,projectContext.RemoveSourceFile,projectContext.AddMetadataReference,projectContext.RemoveMetadataReferencebased on a diff to the list.On initial project setup, we call
SyncProjectthen also callproject.AddProjectReference(..)for each referenced project (after setting up each referenced project). This is only done on initial setup, which is the bug. Note that during initial setup we call bothproject.AddProjectReferenceandprojectContext.AddMetadataReference. I don't think theAbstractProjectimplementation knows that these are related, though I'm not sure it mattersSo one fix might be to call
project.AddProjectReferenceandproject.RemoveProjectReferenceinSyncProject. However Roslyn only makesproject.AddProjectReferenceavailable -project.RemoveProjectReferenceis protected. So that approach won't work without a Roslyn changeHowever, looking at the Roslyn CPS project code, it seems to me that Roslyn's CPSProject/AbstractProject implementation is probably meant to convert the metadata references managed by
AddMetadataReferenceandRemoveMetadataReferenceto be project references, using Roslyn's "ProjectReferenceConversion" internal feature. That is, from what I see here this should be happening. However I haven't checked under the debugger. I expect it is not happening correctly, since otherwise we wouldn't have this bug.Pilchie commentedon Aug 17, 2017
Good analysis @dsyme.
I think it's worth checking to see what happens in the project conversion - it is tied to the output path of the projects, and only does the conversion if the incoming metadata reference path matches a project's output path. This is doubly complicated by the fact that most projects actually have two output paths. The compiler is generally told to build to the
objpath, and then MSBuild copies the resulting binary to thebindirectory. It's thisbindirectory that is normally passed to other projects as a reference. My hunch is that the paths aren't lining up quite right.For reference: the split between
AbstractProjectandIWorkspaceProjectContextis basically between what grew up organically to support the old C# project system, and what we'd like to expose publicly for CPS based projects going forward. We should over time be working toward making it so that F# doesn't need to deal withAbstractProjectat all if possible.Also tagging @jasonmalinowski.
dsyme commentedon Aug 17, 2017
@Pilchie Thanks, great. Yes, it's possible they aren't. We need to debug into the roslyn code to be sure
cartermp commentedon Jan 7, 2018
This is now fixed as of 15.6 Preview 1.1. I suspect that the work @KevinRansom did in the relevant files (
LanguageService.fsandProjectSitesAndFiles.fs) to clean things up and enable .NET Standard and .NET Core support is what has fixed this.