Registry Mirror
Skip to content

After using #r on a non-existent .dll, creating that .dll requires document change to update diagnostics #1808

Closed

Description

@dsyme

F# files are not re-checked when changes happen to dependency files such as #load-ed scripts or referenced DLLs.

This greatly affects the usability of the Visual F# Tools as the user has no idea what state the type checking is in, and has to do artificial edits on their source code to clear errors

This is a regression to functionality that has been working since VS2008

For each F# source file (in or out of a project, whether script or otherwise), the compiler service provides a list of files in the DependencyFiles property. These are DLLs and source files. The IDE should install file watchers for these files, and if a change in a watched file is detected then a re-check of the file should be done, resulting in a refresh of the errors

For example,

  • if an error is corrected in a file referenced by a #load, and that file is saved to disk, then the red-squiggly on the #load should disappear

  • Assume one project references another (e.g. fsc references FSharp.Compiler). If the DLLs for the referenced did not exist on disk, then there will be red-squigglies in the project. When the referenced project gets built, DLLs are created by a build. These DLLs will be in the dependency files. When the files appear on disk, a re-check should occur, and the red squigglies should be cleared.

Repro steps

  1. There are many repros. For example, when scripting, create a script1.fsx with this content and save it to an empty directory.
#load "script2.fsx"

System.Console.WriteLine("Hello")

You will see a red-squiggly on script2.fsx

  1. Now create an empty script2.fsx using VS or any other tool.

    Expected: the red-squiggly should disappear in script1.fsx when you return to that file (without making any edits to script1.fsx)

  2. Now edit script2.fsx using so there is an error in the file. e.g. the contents

    let fail = 1 + 1.0
    

    Save the file and go back to the script1.fsx window.

    Expected: the red-squiggly should re-appear in script1.fsx when you return to that file (without making any edits to script1.fsx)

  3. Now edit script2.fsx to clear the error and save the file. Go back to the script1.fsx window.

    Expected: the red-squiggly should disappear from script1.fsx when you return to that file (without making any edits to script1.fsx)

Similarly for cross-project references to DLLs in project builds

Actual behavior

In all the cases above, the red-squigglies are not cleared in script1.fsx until you actually edit script1.fsx (which has the effect of forcing a re-check of the file)

Activity

  1. added
    Impact-High
    on Nov 22, 2016
  2. cartermp commented on Nov 22, 2016

    @cartermp
    Contributor

    Thanks for the detailed bug, @dsyme. @brettfo and @Pilchie, I think this one should be high on our list of bugs to fix right now.

  3. dsyme commented on Nov 22, 2016

    @dsyme
    ContributorAuthor

    Here are some notes about the old implementation that has been lost

    • Assume file B has been checked and has DependencyFiles A1 and A2. (Note A1 and A2 might not yet exist)

    • In the old language service code, the DependencyFiles for B were consumed here after checking

    • This used the Visual Studio file watching facilities here to install watchers for A1 and A2. (We had to use the VIsual Studio file watching for technical reasons I never understood)

    • When a file change to A1 or A2 was detected by the VS file watching facilitieswe called NotifyFileTypeCheckStateIsDirty which stored the file away in dirtyForTypeCheckFiles

    • When a file creation to A1 or A2 was detected then we called InvalidateConfiguration to notify the F# compiler service that the whole project should be re-checked. This corresponds to creation of one of the source files of a project, or creation of one of the DLLs in a project.

    • IIRC we had to be careful not to listen to DLL deletions since that gives the very annoying behaviour of a "clean" build creating lots of red-squiggly errors

    • OnIdle processing then marked any matching files in the RunningDocumentTable as if they had been edited. THis would have the effect of marking file B as "edited" from the point of view of the language service, and it would be rechecked, and the errors cleared.

    These fixes are also crucial for correctly updating intellisense information in response to changes in dependent files.

    Separately

    • NotifyFileTypeCheckStateIsDIrty is also called when a BeforeBackgroundFileCheck event is raised by the background checker This indicates that refreshed background state is available ready to do an updated check of the file. This is crucial to ensure clearing of foreground errors and updates to intellisense information in response to progress in the background build.

    • It is also worth reviewing whether CheckProjectInBackground is ever being called in the new language service implementation In the old language service, this was be called each time you switched to a file in a project in Visual Studio (or equally to a script). This would have the effect of kicking off the background project build for the project, causing BeforeBackgroundFileCheck events to be raised file by file as the background build progressed through the files. Without calling CheckProjectInBackground you might not get proper updates in multi-project solutions.

  4. dsyme commented on Nov 22, 2016

    @dsyme
    ContributorAuthor

    Note the model described above is not ideal, especially for multi-project solutions (where you shouldn't have to even have referenced DLLs on-disk to check a project). Further, the implementation shouldn't need to do a "foreground" check to refresh errors - they should be pushed from the background build, which should utilize the active, unsaved content of buffers rather than files on disk. The F# Compiler service has various updates which make it possible to do a better job along these lines.

    But in VS2017 RC we have no updates in response to file events at all.... So let's first get back to parity and then progress things.

  5. cartermp commented on Nov 22, 2016

    @cartermp
    Contributor

    Agreed.

  6. dsyme commented on Nov 27, 2016

    @dsyme
    ContributorAuthor

    I'm having a hell of a hard time understanding how to implement this within the Roslyn framework.

    One thing we need is the equivalent of this

        member this.OnIdle() = 
            for file in dirtyForTypeCheckFiles do
                let rdt = this.ServiceProvider.RunningDocumentTable
                match this.ProjectSitesAndFiles.TryGetSourceOfFile(rdt, file) with
                | Some source -> source.RecordChangeToView()
                | None ->  ()
            dirtyForTypeCheckFiles <- Set.empty
    

    That is, when Idle we want to run through a set of documents and "act as if the document has changed" for the purposes of Roslyn semantic analysis.

    Another thing we need is a place where we can register and store VS file tracking handlers.

    I hacked about with this sort of thing:

            // Add a hook so that when the background builder refreshes the background semantic build for a file, we refresh
            // the editor.
            checker.BeforeBackgroundFileCheck.Add(fun (fileName, extraProjectInfo) ->  
               async {
                try 
                    match extraProjectInfo with 
                    | Some (:? Workspace as workspace) -> 
                        let solution = workspace.CurrentSolution
                        let documentIds = solution.GetDocumentIdsWithFilePath(fileName)
                        if not documentIds.IsEmpty then 
                            let documentId = documentIds |> Seq.head
                            let document = solution.GetDocument(documentId)
                            let! sourceText = document.GetTextAsync() |> Async.AwaitTask
                            let newSolution = solution.WithDocumentText(documentId, sourceText (* , PreservationMode.PreserveIdentity *))
                            workspace.TryApplyChanges(newSolution) |> ignore
                    | _ -> ()
                with ex -> 
                    Assert.Exception(ex)
                } |> Async.StartImmediate
            )
    
    

    I'll definitely need help for this

  7. added a commit that references this issue on Nov 27, 2016
  8. vladima commented on Nov 29, 2016

    @vladima
    Contributor

    you can request projects or documents to be re-checked by importing IDiagnosticsAnalyzerService and calling Reanalyze

  9. dsyme commented on Nov 29, 2016

    @dsyme
    ContributorAuthor

    @vladima Thanks!!

  10. dsyme commented on Nov 29, 2016

    @dsyme
    ContributorAuthor

    @vladima @OmarTawfik @brettfo In the old F# Language Service code (actually FSharp.LanguageService.Base, in C#) we implemented our own ViewFilter type that includes an implementation of IVsTextViewEvents including an action when the window got the focus:

            public virtual void OnSetFocus(IVsTextView view) {
                this.service.OnActiveViewChanged(view);
                ...
            }
    

    Do you know what the equivalent should be in the Roslynized code?

    The context is that we basically want to kick off a call to checker.CheckProjectInBackground() when a file in a project gets the focus. This will then make sure all errors eventually get updated.

    We don't explicitly implement IVsTextViewEvents in the new code.

    thanks
    don

  11. vladima commented on Nov 30, 2016

    @vladima
    Contributor

    I guess something like this might work (browser compiled solution, might contain peanuts errors)

    open System.ComponentModel.Composition
    open Microsoft.VisualStudio.Editor
    open Microsoft.VisualStudio.Utilities
    open Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem
    open Microsoft.CodeAnalysis.Diagnostics
    
    [<ContentType(FSharpCommonConstants.FSharpContentTypeName)>]
    [<Export(typeof<IVsTextViewCreationListener>)>]
    type FSharpTextViewCreationListener 
            [<ImportingConstructor>]
            (
                textViewAdapter: IVsEditorAdaptersFactoryService,
                workspace: VisualStudioWorkspaceImpl, 
                analyzerService: IDiagnosticAnalyzerService
            ) =
        interface IVsTextViewCreationListener with
            member this.VsTextViewCreated(textView) =
                let wpfTextView = textViewAdapter.GetWpfTextView(textView)
                match wpfTextView.TextBuffer.Properties.TryGetProperty<ITextDocument>(typeof<ITextDocument>) with
                | true, textDocument ->
                    let filePath = textDocument.FilePath
                    let onGotFocus = new EventHandler(fun _ _ ->
                        analyzerService.Reanalyze(workspace, projectIds = null, documentIds = workspace.CurrentSolution.GetDocumentIdsWithFilePath(filePath))
                    )
                    let rec onViewClosed = new EventHandler(fun _ _ -> 
                        wpfTextView.GotAggregateFocus.RemoveHandler(onGotFocus)
                        wpfTextView.Closed.RemoveHandler(onViewClosed)
                    )
                    wpfTextView.GotAggregateFocus.AddHandler(onGotFocus)
                    wpfTextView.Closed.AddHandler(onViewClosed)
                | _ -> ()
  12. 36 remaining items

  13. added this to the 16.2 milestone on Apr 23, 2019
  14. modified the milestones: 16.2, Backlog on Apr 30, 2019
  15. added
    Impact-Low
    and removed on Sep 1, 2020
  16. changed the title [-]After using #load on an unresolved .dll, resolving that .dll requires mouseover or document change to update diagnostics[/-] [+]After using #r on a non-existent .dll, creating that .dll requires document change to update diagnostics[/+] on Sep 1, 2020
  17. dsyme commented on Apr 5, 2022

    @dsyme
    ContributorAuthor

    I'm closing this very old issue - @TIHan implemented this correctly AFAIU

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-LangService-APIBugImpact-Low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions