Skip to content

Commit eac9728

Browse files
committed
gopls/cache: ignore imports of stdlib
When a module loads a package, we care about imports of other packages *within* that module. We use the FromExternalModule method to detect loaded packages which are external to the current module. Unfortunately, this is insufficient: FromExternalModule returns false for imports of stdlib packages. Additionally, normalizeImportPath would panic when faced with a stdlib package. This is also fixed, as normalizeImportPath is used when processing the imports of a package. Signed-off-by: Matthew Sackman <[email protected]> Change-Id: If7e861a007f6a12945014752bd7caac850b13bc6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218472 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 1789dd4 commit eac9728

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

cmd/cue/cmd/integration/workspace/editing_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ language: version: "v0.11.0"
1717
-- a/a.cue --
1818
package a
1919
20+
import "strings"
21+
2022
v1: int
2123
2224
-- a/d/d.cue --
@@ -46,11 +48,14 @@ v3: a.v1
4648
)
4749
env.OpenFile("a/a.cue")
4850
env.Await(
51+
env.DoneWithOpen(),
4952
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=unknown Created", rootURI),
5053
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Reloaded", rootURI),
5154
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/a/a.cue found [Package dir=%v/a importPath=mod.example/x/a@v0]", rootURI, rootURI, rootURI),
5255
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/a@v0]", rootURI),
5356
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/a importPath=mod.example/x/a@v0", rootURI, rootURI),
57+
// We do not load stdlib packages
58+
NoLogExactf(protocol.Debug, "module=mod.example/x@v0 Loaded Package dir= importPath=strings"),
5459
)
5560
})
5661
})
@@ -63,6 +68,7 @@ v3: a.v1
6368
)
6469
env.OpenFile("b/c/c.cue")
6570
env.Await(
71+
env.DoneWithOpen(),
6672
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=unknown Created", rootURI),
6773
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Reloaded", rootURI),
6874
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/b/c/c.cue found [Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b]", rootURI, rootURI, rootURI),
@@ -82,6 +88,7 @@ v3: a.v1
8288
// disk, but has not been opened in the editor.
8389
env.WriteWorkspaceFile("b/b.cue", "package b\n\nv2: int\n")
8490
env.Await(
91+
env.DoneWithChangeWatchedFiles(),
8592
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/b/c@v0:b]", rootURI),
8693
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b", rootURI, rootURI),
8794
// We still do not load the parent/same package
@@ -98,6 +105,7 @@ v3: a.v1
98105
)
99106
env.OpenFile("b/c/c.cue")
100107
env.Await(
108+
env.DoneWithOpen(),
101109
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=unknown Created", rootURI),
102110
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Reloaded", rootURI),
103111
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/b/c/c.cue found [Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b]", rootURI, rootURI, rootURI),
@@ -111,6 +119,7 @@ v3: a.v1
111119
// a.cue, we should see a reload of x/a and x/b/c:b
112120
env.WriteWorkspaceFile("a/a.cue", "package a\n\nv1: string\n")
113121
env.Await(
122+
env.DoneWithChangeWatchedFiles(),
114123
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/a@v0 mod.example/x/b/c@v0:b]", rootURI),
115124
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/a importPath=mod.example/x/a@v0", rootURI, rootURI),
116125
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b", rootURI, rootURI),
@@ -123,6 +132,7 @@ v3: a.v1
123132
rootURI := env.Sandbox.Workdir.RootURI()
124133
env.OpenFile("b/b.cue")
125134
env.Await(
135+
env.DoneWithOpen(),
126136
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/b/b.cue found [Package dir=%v/b importPath=mod.example/x/b@v0]", rootURI, rootURI, rootURI),
127137
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/b@v0]", rootURI),
128138
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/b importPath=mod.example/x/b@v0", rootURI, rootURI),
@@ -132,6 +142,7 @@ v3: a.v1
132142
// Now open the child/same package
133143
env.OpenFile("b/c/c.cue")
134144
env.Await(
145+
env.DoneWithOpen(),
135146
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/b/c/c.cue found [Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b]", rootURI, rootURI, rootURI),
136147
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/b/c@v0:b]", rootURI),
137148
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/b/c importPath=mod.example/x/b/c@v0:b", rootURI, rootURI),
@@ -140,6 +151,7 @@ v3: a.v1
140151
// package from "b" to "bz"
141152
env.EditBuffer("b/b.cue", fake.NewEdit(0, 9, 0, 9, "z"))
142153
env.Await(
154+
env.DoneWithChange(),
143155
// We should see a single reload of both existing packages:
144156
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/b/c@v0:b mod.example/x/b@v0]", rootURI),
145157
// The load of mod.example/x/b@v0 will have failed, so we should see a new pkg search for b.cue:
@@ -151,6 +163,7 @@ v3: a.v1
151163
// A further edit of b/b.cue should now cause package x/b:bz to be reloaded, but x/b/c:b does not get reloaded:
152164
env.EditBuffer("b/b.cue", fake.NewEdit(2, 0, 2, 0, "w"))
153165
env.Await(
166+
env.DoneWithChange(),
154167
// Now 2 loads of x/b:bz
155168
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/b@v0:bz]", rootURI),
156169
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/b importPath=mod.example/x/b@v0:bz", rootURI, rootURI),
@@ -165,12 +178,14 @@ v3: a.v1
165178
rootURI := env.Sandbox.Workdir.RootURI()
166179
env.OpenFile("a/a.cue")
167180
env.Await(
181+
env.DoneWithOpen(),
168182
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/a/a.cue found [Package dir=%v/a importPath=mod.example/x/a@v0]", rootURI, rootURI, rootURI),
169183
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/a@v0]", rootURI),
170184
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/a importPath=mod.example/x/a@v0", rootURI, rootURI),
171185
)
172186
env.OpenFile("a/d/d.cue")
173187
env.Await(
188+
env.DoneWithOpen(),
174189
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 For file %v/a/d/d.cue found [Package dir=%v/a/d importPath=mod.example/x/a/d@v0:e]", rootURI, rootURI, rootURI),
175190
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/a/d@v0:e]", rootURI),
176191
LogExactf(protocol.Debug, 1, false, "Module dir=%v module=mod.example/x@v0 Loaded Package dir=%v/a/d importPath=mod.example/x/a/d@v0:e", rootURI, rootURI),
@@ -179,6 +194,7 @@ v3: a.v1
179194
// become the same package as a/d/d.cue
180195
env.EditBuffer("a/a.cue", fake.NewEdit(0, 8, 0, 9, "e"))
181196
env.Await(
197+
env.DoneWithChange(),
182198
// We should first see a reload of x/a, which will fail.
183199
LogExactf(protocol.Debug, 2, false, "Module dir=%v module=mod.example/x@v0 Loading packages [mod.example/x/a@v0]", rootURI),
184200
// There'll then be a new search and it should find both packages.

internal/golangorgx/gopls/cache/module.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func (m *Module) ReloadPackages() error {
314314
// within this module, we should track all of them.
315315
pkgsImportsWorklist := make(map[*Package]*modpkgload.Package)
316316
for _, loadedPkg := range loadedPkgs.All() {
317-
if loadedPkg.FromExternalModule() {
317+
if loadedPkg.FromExternalModule() || modpkgload.IsStdlibPackage(loadedPkg.ImportPath()) {
318318
// Because we don't currently support "replace" in module.cue
319319
// files, we cannot have one local module importing another
320320
// local module. Therefore, there's no need to attempt to
@@ -491,7 +491,7 @@ func (m *Module) normalizeImportPath(importPath string) ast.ImportPath {
491491
}
492492

493493
ip := ast.ParseImportPath(importPath).Canonical()
494-
if ip.Version != "" {
494+
if ip.Version != "" || modpkgload.IsStdlibPackage(importPath) {
495495
return ip
496496
}
497497

0 commit comments

Comments
 (0)