Skip to content

Commit 44abc2a

Browse files
committed
internal/lsp: only load by view when there are no go.mod files
In debugging the metadata CLs, I noticed that when a view's only go.mod file became unparseable, we would fall back into reloading the entire view. In such cases, we should just not reinitialize. Change-Id: I1b552158da8855bf80e9ded8b29c346c67564239 Reviewed-on: https://go-review.googlesource.com/c/tools/+/300674 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 11e8f6b commit 44abc2a

File tree

2 files changed

+52
-20
lines changed

2 files changed

+52
-20
lines changed

gopls/internal/regtest/diagnostics/diagnostics_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,3 +1862,26 @@ package main
18621862
)
18631863
})
18641864
}
1865+
1866+
func TestInitialization(t *testing.T) {
1867+
const files = `
1868+
-- go.mod --
1869+
module mod.com
1870+
1871+
go 1.16
1872+
-- main.go --
1873+
package main
1874+
`
1875+
Run(t, files, func(t *testing.T, env *Env) {
1876+
env.OpenFile("go.mod")
1877+
env.Await(env.DoneWithOpen())
1878+
env.RegexpReplace("go.mod", "module", "modul")
1879+
env.SaveBufferWithoutActions("go.mod")
1880+
env.Await(
1881+
OnceMet(
1882+
env.DoneWithSave(),
1883+
NoLogMatching(protocol.Error, "initial workspace load failed"),
1884+
),
1885+
)
1886+
})
1887+
}

internal/lsp/cache/view.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -546,28 +546,32 @@ func (s *snapshot) initialize(ctx context.Context, firstAttempt bool) {
546546
Message: err.Error(),
547547
})
548548
}
549-
for modURI := range s.workspace.getActiveModFiles() {
550-
fh, err := s.GetFile(ctx, modURI)
551-
if err != nil {
552-
addError(modURI, err)
553-
continue
554-
}
555-
parsed, err := s.ParseMod(ctx, fh)
556-
if err != nil {
557-
addError(modURI, err)
558-
continue
559-
}
560-
if parsed.File == nil || parsed.File.Module == nil {
561-
addError(modURI, fmt.Errorf("no module path for %s", modURI))
562-
continue
549+
if len(s.workspace.getActiveModFiles()) > 0 {
550+
for modURI := range s.workspace.getActiveModFiles() {
551+
fh, err := s.GetFile(ctx, modURI)
552+
if err != nil {
553+
addError(modURI, err)
554+
continue
555+
}
556+
parsed, err := s.ParseMod(ctx, fh)
557+
if err != nil {
558+
addError(modURI, err)
559+
continue
560+
}
561+
if parsed.File == nil || parsed.File.Module == nil {
562+
addError(modURI, fmt.Errorf("no module path for %s", modURI))
563+
continue
564+
}
565+
path := parsed.File.Module.Mod.Path
566+
scopes = append(scopes, moduleLoadScope(path))
563567
}
564-
path := parsed.File.Module.Mod.Path
565-
scopes = append(scopes, moduleLoadScope(path))
566-
}
567-
if len(scopes) == 0 {
568+
} else {
568569
scopes = append(scopes, viewLoadScope("LOAD_VIEW"))
569570
}
570-
err := s.load(ctx, firstAttempt, append(scopes, packagePath("builtin"))...)
571+
var err error
572+
if len(scopes) > 0 {
573+
err = s.load(ctx, firstAttempt, append(scopes, packagePath("builtin"))...)
574+
}
571575
if ctx.Err() != nil {
572576
return
573577
}
@@ -578,7 +582,12 @@ func (s *snapshot) initialize(ctx context.Context, firstAttempt bool) {
578582
MainError: err,
579583
DiagList: append(modDiagnostics, extractedDiags...),
580584
}
581-
} else if len(modDiagnostics) != 0 {
585+
} else if len(modDiagnostics) == 1 {
586+
s.initializedErr = &source.CriticalError{
587+
MainError: fmt.Errorf(modDiagnostics[0].Message),
588+
DiagList: modDiagnostics,
589+
}
590+
} else if len(modDiagnostics) > 1 {
582591
s.initializedErr = &source.CriticalError{
583592
MainError: fmt.Errorf("error loading module names"),
584593
DiagList: modDiagnostics,

0 commit comments

Comments
 (0)