@@ -24,6 +24,19 @@ pub trait PackageChangeMapper {
24
24
fn detect_package ( & self , file : & AnchoredSystemPath ) -> PackageMapping ;
25
25
}
26
26
27
+ impl < L , R > PackageChangeMapper for either:: Either < L , R >
28
+ where
29
+ L : PackageChangeMapper ,
30
+ R : PackageChangeMapper ,
31
+ {
32
+ fn detect_package ( & self , file : & AnchoredSystemPath ) -> PackageMapping {
33
+ match self {
34
+ either:: Either :: Left ( l) => l. detect_package ( file) ,
35
+ either:: Either :: Right ( r) => r. detect_package ( file) ,
36
+ }
37
+ }
38
+ }
39
+
27
40
/// Detects package by checking if the file is inside the package.
28
41
///
29
42
/// Does *not* use the `globalDependencies` in turbo.json.
@@ -73,6 +86,43 @@ impl PackageChangeMapper for DefaultPackageChangeMapper<'_> {
73
86
}
74
87
}
75
88
89
+ pub struct DefaultPackageChangeMapperWithLockfile < ' a > {
90
+ base : DefaultPackageChangeMapper < ' a > ,
91
+ }
92
+
93
+ impl < ' a > DefaultPackageChangeMapperWithLockfile < ' a > {
94
+ pub fn new ( pkg_dep_graph : & ' a PackageGraph ) -> Self {
95
+ Self {
96
+ base : DefaultPackageChangeMapper :: new ( pkg_dep_graph) ,
97
+ }
98
+ }
99
+ }
100
+
101
+ impl PackageChangeMapper for DefaultPackageChangeMapperWithLockfile < ' _ > {
102
+ fn detect_package ( & self , path : & AnchoredSystemPath ) -> PackageMapping {
103
+ // If we have a lockfile change, we consider this as a root package change,
104
+ // since there's a chance that the root package uses a workspace package
105
+ // dependency (this is cursed behavior but sadly possible). There's a chance
106
+ // that we can make this more accurate by checking which package
107
+ // manager, since not all package managers may permit root pulling from
108
+ // workspace package dependencies
109
+ if PackageManager :: supported_managers ( )
110
+ . iter ( )
111
+ . any ( |pm| pm. lockfile_name ( ) == path. as_str ( ) )
112
+ {
113
+ PackageMapping :: Package ( (
114
+ WorkspacePackage {
115
+ name : PackageName :: Root ,
116
+ path : AnchoredSystemPathBuf :: from_raw ( "" ) . unwrap ( ) ,
117
+ } ,
118
+ PackageInclusionReason :: ConservativeRootLockfileChanged ,
119
+ ) )
120
+ } else {
121
+ self . base . detect_package ( path)
122
+ }
123
+ }
124
+ }
125
+
76
126
#[ derive( Error , Debug ) ]
77
127
pub enum Error {
78
128
#[ error( transparent) ]
@@ -88,7 +138,7 @@ pub enum Error {
88
138
/// changes all packages. Since we have a list of global deps,
89
139
/// we can check against that and avoid invalidating in unnecessary cases.
90
140
pub struct GlobalDepsPackageChangeMapper < ' a > {
91
- pkg_dep_graph : & ' a PackageGraph ,
141
+ base : DefaultPackageChangeMapperWithLockfile < ' a > ,
92
142
global_deps_matcher : wax:: Any < ' a > ,
93
143
}
94
144
@@ -97,36 +147,19 @@ impl<'a> GlobalDepsPackageChangeMapper<'a> {
97
147
pkg_dep_graph : & ' a PackageGraph ,
98
148
global_deps : I ,
99
149
) -> Result < Self , Error > {
150
+ let base = DefaultPackageChangeMapperWithLockfile :: new ( pkg_dep_graph) ;
100
151
let global_deps_matcher = wax:: any ( global_deps) ?;
101
152
102
153
Ok ( Self {
103
- pkg_dep_graph ,
154
+ base ,
104
155
global_deps_matcher,
105
156
} )
106
157
}
107
158
}
108
159
109
160
impl PackageChangeMapper for GlobalDepsPackageChangeMapper < ' _ > {
110
161
fn detect_package ( & self , path : & AnchoredSystemPath ) -> PackageMapping {
111
- // If we have a lockfile change, we consider this as a root package change,
112
- // since there's a chance that the root package uses a workspace package
113
- // dependency (this is cursed behavior but sadly possible). There's a chance
114
- // that we can make this more accurate by checking which package
115
- // manager, since not all package managers may permit root pulling from
116
- // workspace package dependencies
117
- if PackageManager :: supported_managers ( )
118
- . iter ( )
119
- . any ( |pm| pm. lockfile_name ( ) == path. as_str ( ) )
120
- {
121
- return PackageMapping :: Package ( (
122
- WorkspacePackage {
123
- name : PackageName :: Root ,
124
- path : AnchoredSystemPathBuf :: from_raw ( "" ) . unwrap ( ) ,
125
- } ,
126
- PackageInclusionReason :: ConservativeRootLockfileChanged ,
127
- ) ) ;
128
- }
129
- match DefaultPackageChangeMapper :: new ( self . pkg_dep_graph ) . detect_package ( path) {
162
+ match self . base . detect_package ( path) {
130
163
// Since `DefaultPackageChangeMapper` is overly conservative, we can check here if
131
164
// the path is actually in globalDeps and if not, return it as
132
165
// PackageDetection::Package(WorkspacePackage::root()).
@@ -160,7 +193,8 @@ mod tests {
160
193
use super :: { DefaultPackageChangeMapper , GlobalDepsPackageChangeMapper } ;
161
194
use crate :: {
162
195
change_mapper:: {
163
- AllPackageChangeReason , ChangeMapper , PackageChanges , PackageInclusionReason ,
196
+ AllPackageChangeReason , ChangeMapper , LockfileContents , PackageChanges ,
197
+ PackageInclusionReason ,
164
198
} ,
165
199
discovery:: { self , PackageDiscovery } ,
166
200
package_graph:: { PackageGraphBuilder , WorkspacePackage } ,
@@ -208,7 +242,7 @@ mod tests {
208
242
[ AnchoredSystemPathBuf :: from_raw ( "README.md" ) ?]
209
243
. into_iter ( )
210
244
. collect ( ) ,
211
- None ,
245
+ LockfileContents :: Unchanged ,
212
246
) ?;
213
247
214
248
// We should return All because we don't have global deps and
@@ -228,7 +262,7 @@ mod tests {
228
262
[ AnchoredSystemPathBuf :: from_raw ( "README.md" ) ?]
229
263
. into_iter ( )
230
264
. collect ( ) ,
231
- None ,
265
+ LockfileContents :: Unchanged ,
232
266
) ?;
233
267
234
268
// We only get a root workspace change since we have global deps specified and
0 commit comments