2018-11-05 12:48:08 -07:00
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2018-10-19 14:03:29 -06:00
package lsp
import (
2018-12-05 15:00:36 -07:00
"context"
2020-07-28 16:00:54 -06:00
"crypto/sha256"
2020-05-28 19:21:29 -06:00
"fmt"
2020-07-16 08:45:30 -06:00
"path/filepath"
2019-05-17 11:45:50 -06:00
"strings"
2020-01-31 15:27:08 -07:00
"sync"
2018-11-13 09:13:53 -07:00
2020-04-17 07:32:56 -06:00
"golang.org/x/tools/internal/event"
2020-03-10 21:09:39 -06:00
"golang.org/x/tools/internal/lsp/debug/tag"
2019-12-17 14:13:33 -07:00
"golang.org/x/tools/internal/lsp/mod"
2018-10-19 14:03:29 -06:00
"golang.org/x/tools/internal/lsp/protocol"
2018-11-02 14:15:31 -06:00
"golang.org/x/tools/internal/lsp/source"
2020-07-16 08:45:30 -06:00
"golang.org/x/tools/internal/span"
2020-01-11 21:59:57 -07:00
"golang.org/x/tools/internal/xcontext"
2020-07-13 23:50:57 -06:00
"golang.org/x/xerrors"
2018-10-19 14:03:29 -06:00
)
2020-07-21 00:05:22 -06:00
// idWithAnalysis is used to track if the diagnostics for a given file were
// computed with analyses.
type idWithAnalysis struct {
2020-07-26 16:01:39 -06:00
id source . VersionedFileIdentity
2020-01-31 15:27:08 -07:00
withAnalysis bool
}
2020-01-11 21:59:57 -07:00
func ( s * Server ) diagnoseDetached ( snapshot source . Snapshot ) {
ctx := snapshot . View ( ) . BackgroundContext ( )
ctx = xcontext . Detach ( ctx )
2020-04-29 11:33:43 -06:00
reports , shows := s . diagnose ( ctx , snapshot , false )
if shows != nil {
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
// If a view has been created or the configuration changed, warn the user.
2020-04-29 11:33:43 -06:00
s . client . ShowMessage ( ctx , shows )
}
2020-01-31 15:27:08 -07:00
s . publishReports ( ctx , snapshot , reports )
2019-11-15 12:47:29 -07:00
}
2020-01-11 21:59:57 -07:00
func ( s * Server ) diagnoseSnapshot ( snapshot source . Snapshot ) {
2019-11-20 12:26:02 -07:00
ctx := snapshot . View ( ) . BackgroundContext ( )
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
// Ignore possible workspace configuration warnings in the normal flow.
2020-04-29 11:33:43 -06:00
reports , _ := s . diagnose ( ctx , snapshot , false )
2020-01-31 15:27:08 -07:00
s . publishReports ( ctx , snapshot , reports )
2019-11-20 12:26:02 -07:00
}
2019-03-14 15:19:01 -06:00
2020-01-11 21:59:57 -07:00
// diagnose is a helper function for running diagnostics with a given context.
// Do not call it directly.
2020-07-21 00:05:22 -06:00
func ( s * Server ) diagnose ( ctx context . Context , snapshot source . Snapshot , alwaysAnalyze bool ) ( map [ idWithAnalysis ] map [ string ] * source . Diagnostic , * protocol . ShowMessageParams ) {
2020-04-20 10:14:12 -06:00
ctx , done := event . Start ( ctx , "lsp:background-worker" )
2019-12-17 14:13:33 -07:00
defer done ( )
2020-02-18 14:44:12 -07:00
// Wait for a free diagnostics slot.
select {
case <- ctx . Done ( ) :
2020-04-29 11:33:43 -06:00
return nil , nil
2020-02-18 14:44:12 -07:00
case s . diagnosticsSema <- struct { } { } :
}
defer func ( ) { <- s . diagnosticsSema } ( )
2020-01-31 15:27:08 -07:00
var reportsMu sync . Mutex
2020-07-21 00:05:22 -06:00
reports := map [ idWithAnalysis ] map [ string ] * source . Diagnostic { }
2020-01-31 15:27:08 -07:00
2020-07-21 00:05:22 -06:00
// First, diagnose the go.mod file.
modReports , modErr := mod . Diagnostics ( ctx , snapshot )
2020-03-30 10:45:15 -06:00
if ctx . Err ( ) != nil {
2020-04-29 11:33:43 -06:00
return nil , nil
2020-03-30 10:45:15 -06:00
}
2020-07-13 23:50:57 -06:00
if modErr != nil {
event . Error ( ctx , "warning: diagnose go.mod" , modErr , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) . Filename ( ) ) )
}
2020-07-21 00:05:22 -06:00
for id , diags := range modReports {
2020-06-19 14:31:22 -06:00
if id . URI == "" {
event . Error ( ctx , "missing URI for module diagnostics" , fmt . Errorf ( "empty URI" ) , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) . Filename ( ) ) )
continue
}
2020-07-21 00:05:22 -06:00
key := idWithAnalysis {
2020-07-14 22:19:10 -06:00
id : id ,
withAnalysis : true , // treat go.mod diagnostics like analyses
2020-01-31 15:27:08 -07:00
}
2020-07-21 00:05:22 -06:00
if _ , ok := reports [ key ] ; ! ok {
reports [ key ] = map [ string ] * source . Diagnostic { }
}
for _ , d := range diags {
reports [ key ] [ diagnosticKey ( d ) ] = d
}
2020-01-24 08:14:25 -07:00
}
2020-01-11 21:59:57 -07:00
// Diagnose all of the packages in the workspace.
2020-07-22 09:32:32 -06:00
wsPkgs , err := snapshot . WorkspacePackages ( ctx )
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
if err != nil {
2020-07-13 23:50:57 -06:00
// Try constructing a more helpful error message out of this error.
if s . handleFatalErrors ( ctx , snapshot , modErr , err ) {
return nil , nil
}
msg := ` The code in the workspace failed to compile ( see the error message below ) .
If you believe this is a mistake , please file an issue : https : //github.com/golang/go/issues/new.`
event . Error ( ctx , msg , err , tag . Snapshot . Of ( snapshot . ID ( ) ) , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) ) )
if err := s . client . ShowMessage ( ctx , & protocol . ShowMessageParams {
Type : protocol . Error ,
Message : fmt . Sprintf ( "%s\n%v" , msg , err ) ,
} ) ; err != nil {
2020-07-20 23:34:22 -06:00
event . Error ( ctx , "ShowMessage failed" , err , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) . Filename ( ) ) )
2020-07-13 23:50:57 -06:00
}
2020-04-29 11:33:43 -06:00
return nil , nil
2020-01-31 15:27:08 -07:00
}
2020-07-21 00:05:22 -06:00
var (
showMsg * protocol . ShowMessageParams
wg sync . WaitGroup
)
2020-07-22 09:32:32 -06:00
for _ , pkg := range wsPkgs {
2020-01-31 15:27:08 -07:00
wg . Add ( 1 )
2020-07-22 09:32:32 -06:00
go func ( pkg source . Package ) {
2020-01-31 15:27:08 -07:00
defer wg . Done ( )
2020-07-21 00:05:22 -06:00
2020-07-28 18:05:06 -06:00
withAnalysis := alwaysAnalyze // only run analyses for packages with open files
var gcDetailsDir span . URI // find the package's optimization details, if available
2020-07-21 13:15:06 -06:00
for _ , pgf := range pkg . CompiledGoFiles ( ) {
if snapshot . IsOpen ( pgf . URI ) {
2020-07-21 00:05:22 -06:00
withAnalysis = true
2020-07-16 08:45:30 -06:00
}
2020-07-28 18:05:06 -06:00
if gcDetailsDir == "" {
dirURI := span . URIFromPath ( filepath . Dir ( pgf . URI . Filename ( ) ) )
s . gcOptimizationDetailsMu . Lock ( )
_ , ok := s . gcOptimizatonDetails [ dirURI ]
s . gcOptimizationDetailsMu . Unlock ( )
if ok {
gcDetailsDir = dirURI
2020-07-16 08:45:30 -06:00
}
2020-01-11 21:59:57 -07:00
}
2020-01-31 15:27:08 -07:00
}
2020-07-21 00:05:22 -06:00
2020-07-15 15:15:09 -06:00
pkgReports , warn , err := source . Diagnostics ( ctx , snapshot , pkg , withAnalysis )
2020-07-21 00:05:22 -06:00
2020-01-31 15:27:08 -07:00
// Check if might want to warn the user about their build configuration.
2020-04-29 11:33:43 -06:00
// Our caller decides whether to send the message.
2020-01-31 15:27:08 -07:00
if warn && ! snapshot . View ( ) . ValidBuildConfiguration ( ) {
2020-07-21 00:05:22 -06:00
showMsg = & protocol . ShowMessageParams {
2020-03-08 09:38:04 -06:00
Type : protocol . Warning ,
2020-07-20 23:34:22 -06:00
Message : ` You are neither in a module nor in your GOPATH. If you are using modules, please open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new. ` ,
2020-04-29 11:33:43 -06:00
}
2020-01-31 15:27:08 -07:00
}
if err != nil {
2020-07-22 09:32:32 -06:00
event . Error ( ctx , "warning: diagnose package" , err , tag . Snapshot . Of ( snapshot . ID ( ) ) , tag . Package . Of ( pkg . ID ( ) ) )
2020-01-31 15:27:08 -07:00
return
}
2020-07-21 00:05:22 -06:00
// Add all reports to the global map, checking for duplciates.
2020-01-31 15:27:08 -07:00
reportsMu . Lock ( )
2020-07-21 00:05:22 -06:00
for id , diags := range pkgReports {
key := idWithAnalysis {
2020-01-31 15:27:08 -07:00
id : id ,
2020-07-21 00:05:22 -06:00
withAnalysis : withAnalysis ,
}
if _ , ok := reports [ key ] ; ! ok {
reports [ key ] = map [ string ] * source . Diagnostic { }
}
for _ , d := range diags {
reports [ key ] [ diagnosticKey ( d ) ] = d
2020-01-11 21:59:57 -07:00
}
2020-01-31 15:27:08 -07:00
}
2020-07-28 18:05:06 -06:00
// If gc optimization details are available, add them to the
// diagnostic reports.
if gcDetailsDir != "" {
gcReports , err := source . GCOptimizationDetails ( ctx , snapshot , gcDetailsDir )
if err != nil {
event . Error ( ctx , "warning: gc details" , err , tag . Snapshot . Of ( snapshot . ID ( ) ) )
}
for id , diags := range gcReports {
key := idWithAnalysis {
id : id ,
withAnalysis : withAnalysis ,
}
if _ , ok := reports [ key ] ; ! ok {
reports [ key ] = map [ string ] * source . Diagnostic { }
}
for _ , d := range diags {
reports [ key ] [ diagnosticKey ( d ) ] = d
}
}
}
2020-01-31 15:27:08 -07:00
reportsMu . Unlock ( )
2020-07-22 09:32:32 -06:00
} ( pkg )
2020-01-31 15:27:08 -07:00
}
wg . Wait ( )
2020-07-21 00:05:22 -06:00
return reports , showMsg
}
// diagnosticKey creates a unique identifier for a given diagnostic, since we
// cannot use source.Diagnostics as map keys. This is used to de-duplicate
// diagnostics.
func diagnosticKey ( d * source . Diagnostic ) string {
var tags , related string
for _ , t := range d . Tags {
tags += fmt . Sprintf ( "%s" , t )
}
for _ , r := range d . Related {
related += fmt . Sprintf ( "%s%s%s" , r . URI , r . Message , r . Range )
}
key := fmt . Sprintf ( "%s%s%s%s%s%s" , d . Message , d . Range , d . Severity , d . Source , tags , related )
2020-07-28 16:00:54 -06:00
return fmt . Sprintf ( "%x" , sha256 . Sum256 ( [ ] byte ( key ) ) )
2019-12-17 14:13:33 -07:00
}
2020-07-21 00:05:22 -06:00
func ( s * Server ) publishReports ( ctx context . Context , snapshot source . Snapshot , reports map [ idWithAnalysis ] map [ string ] * source . Diagnostic ) {
2019-12-05 14:37:47 -07:00
// Check for context cancellation before publishing diagnostics.
if ctx . Err ( ) != nil {
return
}
2019-11-20 23:24:43 -07:00
s . deliveredMu . Lock ( )
defer s . deliveredMu . Unlock ( )
2020-07-21 00:05:22 -06:00
for key , diagnosticsMap := range reports {
2019-12-05 14:37:47 -07:00
// Don't deliver diagnostics if the context has already been canceled.
if ctx . Err ( ) != nil {
break
}
2019-11-20 23:24:43 -07:00
// Pre-sort diagnostics to avoid extra work when we compare them.
2020-07-21 00:05:22 -06:00
var diagnostics [ ] * source . Diagnostic
for _ , d := range diagnosticsMap {
diagnostics = append ( diagnostics , d )
}
2019-11-20 23:24:43 -07:00
source . SortDiagnostics ( diagnostics )
toSend := sentDiagnostics {
2020-07-26 16:01:39 -06:00
id : key . id ,
2020-01-13 18:41:03 -07:00
sorted : diagnostics ,
2020-01-31 15:27:08 -07:00
withAnalysis : key . withAnalysis ,
2020-01-13 18:41:03 -07:00
snapshotID : snapshot . ID ( ) ,
2019-11-20 23:24:43 -07:00
}
2020-01-17 11:12:00 -07:00
2020-01-10 10:29:37 -07:00
// We use the zero values if this is an unknown file.
2020-01-31 15:27:08 -07:00
delivered := s . delivered [ key . id . URI ]
2020-01-13 18:41:03 -07:00
2020-01-17 11:12:00 -07:00
// Snapshot IDs are always increasing, so we use them instead of file
// versions to create the correct order for diagnostics.
// If we've already delivered diagnostics for a future snapshot for this file,
// do not deliver them.
if delivered . snapshotID > toSend . snapshotID {
// Do not update the delivered map since it already contains newer diagnostics.
continue
}
// Check if we should reuse the cached diagnostics.
if equalDiagnostics ( delivered . sorted , diagnostics ) {
// Make sure to update the delivered map.
2020-01-31 15:27:08 -07:00
s . delivered [ key . id . URI ] = toSend
2020-01-08 11:53:50 -07:00
continue
}
2020-01-17 11:12:00 -07:00
// If we've already delivered diagnostics for this file, at this
// snapshot, with analyses, do not send diagnostics without analyses.
2020-07-26 16:01:39 -06:00
if delivered . snapshotID == toSend . snapshotID && delivered . id == toSend . id &&
2020-01-10 10:29:37 -07:00
delivered . withAnalysis && ! toSend . withAnalysis {
2020-01-17 11:12:00 -07:00
// Do not update the delivered map since it already contains better diagnostics.
2020-01-10 10:29:37 -07:00
continue
2019-11-20 23:24:43 -07:00
}
2019-12-05 14:37:47 -07:00
if err := s . client . PublishDiagnostics ( ctx , & protocol . PublishDiagnosticsParams {
2020-01-10 10:29:37 -07:00
Diagnostics : toProtocolDiagnostics ( diagnostics ) ,
2020-02-12 14:36:46 -07:00
URI : protocol . URIFromSpanURI ( key . id . URI ) ,
2020-01-31 15:27:08 -07:00
Version : key . id . Version ,
2019-12-05 14:37:47 -07:00
} ) ; err != nil {
2020-03-30 10:45:15 -06:00
event . Error ( ctx , "publishReports: failed to deliver diagnostic" , err , tag . URI . Of ( key . id . URI ) )
2019-05-01 20:46:07 -06:00
continue
2019-03-14 15:19:01 -06:00
}
2019-11-20 23:24:43 -07:00
// Update the delivered map.
2020-01-31 15:27:08 -07:00
s . delivered [ key . id . URI ] = toSend
2019-11-20 23:24:43 -07:00
}
}
// equalDiagnostics returns true if the 2 lists of diagnostics are equal.
// It assumes that both a and b are already sorted.
2020-03-31 21:53:42 -06:00
func equalDiagnostics ( a , b [ ] * source . Diagnostic ) bool {
2019-11-20 23:24:43 -07:00
if len ( a ) != len ( b ) {
return false
}
for i := 0 ; i < len ( a ) ; i ++ {
if source . CompareDiagnostic ( a [ i ] , b [ i ] ) != 0 {
return false
}
2019-05-01 20:46:07 -06:00
}
2019-11-20 23:24:43 -07:00
return true
2018-12-18 14:18:03 -07:00
}
2020-03-31 21:53:42 -06:00
func toProtocolDiagnostics ( diagnostics [ ] * source . Diagnostic ) [ ] protocol . Diagnostic {
2018-11-12 12:15:47 -07:00
reports := [ ] protocol . Diagnostic { }
for _ , diag := range diagnostics {
2019-10-11 03:39:09 -06:00
related := make ( [ ] protocol . DiagnosticRelatedInformation , 0 , len ( diag . Related ) )
for _ , rel := range diag . Related {
related = append ( related , protocol . DiagnosticRelatedInformation {
Location : protocol . Location {
2020-02-12 14:36:46 -07:00
URI : protocol . URIFromSpanURI ( rel . URI ) ,
2019-10-11 03:39:09 -06:00
Range : rel . Range ,
} ,
Message : rel . Message ,
} )
}
2019-09-24 22:46:57 -06:00
reports = append ( reports , protocol . Diagnostic {
2019-10-11 03:39:09 -06:00
Message : strings . TrimSpace ( diag . Message ) , // go list returns errors prefixed by newline
Range : diag . Range ,
Severity : diag . Severity ,
Source : diag . Source ,
Tags : diag . Tags ,
RelatedInformation : related ,
2019-09-24 22:46:57 -06:00
} )
2018-11-12 12:15:47 -07:00
}
2019-09-24 14:28:59 -06:00
return reports
2018-10-29 16:12:41 -06:00
}
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
2020-07-13 23:50:57 -06:00
func ( s * Server ) handleFatalErrors ( ctx context . Context , snapshot source . Snapshot , modErr , loadErr error ) bool {
modURI := snapshot . View ( ) . ModFile ( )
// We currently only have workarounds for errors associated with modules.
if modURI == "" {
return false
}
switch loadErr {
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
case source . InconsistentVendoring :
item , err := s . client . ShowMessageRequest ( ctx , & protocol . ShowMessageRequestParams {
Type : protocol . Error ,
Message : ` Inconsistent vendoring detected . Please re - run "go mod vendor" .
See https : //github.com/golang/go/issues/39164 for more detail on this issue.`,
Actions : [ ] protocol . MessageActionItem {
{ Title : "go mod vendor" } ,
} ,
} )
2020-07-13 23:50:57 -06:00
// If the user closes the pop-up, don't show them further errors.
if item == nil {
return true
}
if err != nil {
2020-07-20 23:34:22 -06:00
event . Error ( ctx , "go mod vendor ShowMessageRequest failed" , err , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) . Filename ( ) ) )
2020-07-13 23:50:57 -06:00
return true
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
}
if err := s . directGoModCommand ( ctx , protocol . URIFromSpanURI ( modURI ) , "mod" , [ ] string { "vendor" } ... ) ; err != nil {
if err := s . client . ShowMessage ( ctx , & protocol . ShowMessageParams {
Type : protocol . Error ,
Message : fmt . Sprintf ( ` "go mod vendor" failed with %v ` , err ) ,
} ) ; err != nil {
2020-07-13 23:50:57 -06:00
if err != nil {
2020-07-20 23:34:22 -06:00
event . Error ( ctx , "go mod vendor ShowMessage failed" , err , tag . Directory . Of ( snapshot . View ( ) . Folder ( ) . Filename ( ) ) )
2020-07-13 23:50:57 -06:00
}
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
}
}
2020-07-13 23:50:57 -06:00
return true
}
// If there is a go.mod-related error, as well as a workspace load error,
// there is likely an issue with the go.mod file. Try to parse the error
// message and create a diagnostic.
if modErr == nil {
return false
}
if xerrors . Is ( loadErr , source . PackagesLoadError ) {
fh , err := snapshot . GetFile ( ctx , modURI )
if err != nil {
return false
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
}
2020-07-13 23:50:57 -06:00
diag , err := mod . ExtractGoCommandError ( ctx , snapshot , fh , loadErr )
if err != nil {
return false
}
2020-07-21 00:05:22 -06:00
s . publishReports ( ctx , snapshot , map [ idWithAnalysis ] map [ string ] * source . Diagnostic {
2020-07-26 16:01:39 -06:00
{ id : fh . VersionedFileIdentity ( ) } : { diagnosticKey ( diag ) : diag } ,
2020-07-13 23:50:57 -06:00
} )
return true
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
}
2020-07-13 23:50:57 -06:00
return false
internal/lsp: handle unknown revision in go.mod file
This change ensures that, when the initial workspace load fails, we
re-run it if the go.mod file changes. Previously, if a user opened a
workspace with a corrupt go.mod file, we never recovered.
To reinitialize the workspace on-demand, we use the initializeOnce field
as an indicator of whether or not we should reinitialize. Every call to
awaitInitialized (which is called by all functions that need the IWL),
passes through the initialization code. If a retry isn't necessary,
this is a no-op, but if it is, we will call the initialization logic.
Only the first attempt uses a detached context; subsequent attempts can
be canceled by their contexts.
To indicate that we should reinitialize, we call maybeReinitialize.
Right now, we only call this when the go.mod file changes. In the
future, we may need it in other cases.
Fixes golang/go#38232
Change-Id: I77eefebb0ac38fbd0fe2c7da09c864eba45b075f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2020-07-11 23:52:22 -06:00
}