Skip to content

Commit 6405ce0

Browse files
committed
internal/modpkgload: Make Package keep its set of files
Loading a package requires reading files and discovering which files are in that package. The Package type already provides access to a package's files' directories via the Locations method, but it does not hang on to the actual files themselves. This is unfortunate as re-reading the files may cause additional IO and may create race conditions. Add a files field and Files getter method to Package, and populate with the files that are included in a Package. Signed-off-by: Matthew Sackman <[email protected]> Change-Id: I94c8ae1e6d6b900f989d4408031ef05877ce0b00 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218034 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent c2f57e3 commit 6405ce0

File tree

10 files changed

+35
-2
lines changed

10 files changed

+35
-2
lines changed

internal/mod/modpkgload/pkgload.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ type Package struct {
109109
flags atomicLoadPkgFlags
110110

111111
// Populated by [loader.load].
112-
mod module.Version // module providing package
113-
modRoot module.SourceLoc // root location of module
112+
mod module.Version // module providing package
113+
modRoot module.SourceLoc // root location of module
114+
files []modimports.ModuleFile
114115
locs []module.SourceLoc // location of source code directories
115116
err error // error loading package
116117
imports []*Package // packages imported by this one
@@ -133,6 +134,10 @@ func (pkg *Package) Locations() []module.SourceLoc {
133134
return pkg.locs
134135
}
135136

137+
func (pkg *Package) Files() []modimports.ModuleFile {
138+
return pkg.files
139+
}
140+
136141
func (pkg *Package) Error() error {
137142
return pkg.err
138143
}
@@ -295,6 +300,7 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
295300
importsMap := make(map[string]bool)
296301
foundPackageFile := false
297302
excludedPackageFiles := 0
303+
var files []modimports.ModuleFile
298304
for _, loc := range pkg.locs {
299305
// Layer an iterator whose yield function keeps track of whether we have seen
300306
// a single valid CUE file in the package directory.
@@ -311,6 +317,7 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
311317
return true
312318
}
313319
foundPackageFile = true
320+
files = append(files, mf)
314321
return yield(mf, err)
315322
})
316323
}
@@ -331,6 +338,7 @@ func (pkgs *Packages) load(ctx context.Context, pkg *Package) {
331338
}
332339
return
333340
}
341+
pkg.files = files
334342
// Make the algorithm deterministic for tests.
335343
imports := slices.Sorted(maps.Keys(importsMap))
336344

internal/mod/modpkgload/pkgload_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ func TestLoadPackages(t *testing.T) {
8484
for _, loc := range pkg.Locations() {
8585
printf("\tlocation: %v\n", loc.Dir)
8686
}
87+
for _, file := range pkg.Files() {
88+
printf("\tfile: %v: %v\n", file.FilePath, file.Syntax.PackageName())
89+
}
8790
if imps := pkg.Imports(); len(imps) > 0 {
8891
printf("\timports:\n")
8992
for _, imp := range imps {

internal/mod/modpkgload/testdata/localpackage.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ main.test@v0:main
99
mod: main.test@v0
1010
external: false
1111
location: .
12+
file: main.cue: main
1213
imports:
1314
other.com/blah
1415
other.com/blah
1516
flags: inAll,isRoot,fromRoot,importsLoaded
1617
mod: local
1718
external: true
1819
location: cue.mod/gen/other.com/blah
20+
file: cue.mod/gen/other.com/blah/x.cue: blah
1921
-- main.cue --
2022
package main
2123
import "other.com/blah"

internal/mod/modpkgload/testdata/localpackageambiguous.txtar

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ main.test@v0:main
1717
mod: main.test@v0
1818
external: false
1919
location: .
20+
file: main.cue: main
2021
imports:
2122
example.com/blah
2223
example.com/blah
2324
flags: inAll,isRoot,fromRoot,importsLoaded
2425
mod: local
2526
external: true
2627
location: cue.mod/gen/example.com/blah
28+
file: cue.mod/gen/example.com/blah/x.cue: blah
2729
-- test1/initial-requirements --
2830
main.test@v0
2931
@@ -36,6 +38,7 @@ main.test@v0:main
3638
mod: main.test@v0
3739
external: false
3840
location: .
41+
file: main.cue: main
3942
imports:
4043
example.com/blah
4144
example.com/blah

internal/mod/modpkgload/testdata/localpackagemultipledirs.txtar

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ main.test@v0:main
99
mod: main.test@v0
1010
external: false
1111
location: .
12+
file: main.cue: main
1213
imports:
1314
other.com/blah
1415
other.com/blah
@@ -18,6 +19,9 @@ other.com/blah
1819
location: cue.mod/gen/other.com/blah
1920
location: cue.mod/usr/other.com/blah
2021
location: cue.mod/pkg/other.com/blah
22+
file: cue.mod/gen/other.com/blah/x.cue: blah
23+
file: cue.mod/usr/other.com/blah/x.cue: blah
24+
file: cue.mod/pkg/other.com/blah/x.cue: blah
2125
-- main.cue --
2226
package main
2327
import "other.com/blah"

internal/mod/modpkgload/testdata/multidir.txtar

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ main.test@v0:main
1010
mod: main.test@v0
1111
external: false
1212
location: .
13+
file: main.cue: main
1314
imports:
1415
example.com/blah@v0
1516
example.com/blah@v0
1617
flags: inAll,isRoot,fromRoot,importsLoaded
1718
1819
external: true
1920
location: _registry/example.com_v0.0.1/blah
21+
file: _registry/example.com_v0.0.1/blah/blah.cue: blah
22+
file: _registry/example.com_v0.0.1/x.cue: blah
2023
imports:
2124
foo.com/bar/hello/goodbye@v0
2225
foo.com/bar/hello/goodbye@v0

internal/mod/modpkgload/testdata/nocuefiles.txtar

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ main.test@v0:main
1010
mod: main.test@v0
1111
external: false
1212
location: .
13+
file: main.cue: main
1314
imports:
1415
example.com/blah@v0
1516
example.com/blah@v0

internal/mod/modpkgload/testdata/packagegranularity.txtar

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ main.test@v0:main
1212
mod: main.test@v0
1313
external: false
1414
location: .
15+
file: main.cue: main
1516
imports:
1617
example.com/blah@v0
1718
example.com/blah@v0
@@ -31,20 +32,23 @@ main.test@v0:main
3132
mod: main.test@v0
3233
external: false
3334
location: .
35+
file: main.cue: main
3436
imports:
3537
example.com/blah@v0
3638
example.com/blah@v0
3739
flags: inAll,isRoot,fromRoot,importsLoaded
3840
3941
external: true
4042
location: _registry/example.com_v0.0.1/blah
43+
file: _registry/example.com_v0.0.1/blah/blah.cue: blah
4144
imports:
4245
foo.com/bar/hello/goodbye@v0
4346
foo.com/bar/hello/goodbye@v0
4447
flags: inAll,isRoot,fromRoot,importsLoaded
4548
mod: foo.com/[email protected]
4649
external: true
4750
location: _registry/foo.com_bar_v0.0.1/hello/goodbye
51+
file: _registry/foo.com_bar_v0.0.1/hello/goodbye/hello.cue: goodbye
4852
-- main.cue --
4953
package main
5054
import "example.com/blah@v0"

internal/mod/modpkgload/testdata/simple.txtar

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ main.test@v0:main
99
mod: main.test@v0
1010
external: false
1111
location: .
12+
file: main.cue: main
1213
imports:
1314
example.com/blah@v0
1415
example.com/blah@v0
@@ -27,13 +28,15 @@ main.test@v0:main
2728
mod: main.test@v0
2829
external: false
2930
location: .
31+
file: main.cue: main
3032
imports:
3133
example.com/blah@v0
3234
example.com/blah@v0
3335
flags: inAll,isRoot,fromRoot,importsLoaded
3436
3537
external: true
3638
location: _registry/example.com_v0.0.1/blah
39+
file: _registry/example.com_v0.0.1/blah/blah.cue: blah
3740
imports:
3841
foo.com/bar/hello/goodbye@v0
3942
foo.com/bar/hello/goodbye@v0

internal/mod/modpkgload/testdata/withdefaultmajorversions.txtar

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ main.test@v0:main
1111
mod: main.test@v0
1212
external: false
1313
location: .
14+
file: main.cue: main
1415
imports:
1516
example.com/blah
1617
example.com/blah
1718
flags: inAll,isRoot,fromRoot,importsLoaded
1819
1920
external: true
2021
location: _registry/example.com_v0.0.1/blah
22+
file: _registry/example.com_v0.0.1/blah/blah.cue: blah
2123
imports:
2224
foo.com/bar/hello/goodbye@v0
2325
foo.com/bar/hello/goodbye@v0

0 commit comments

Comments
 (0)