@@ -32,10 +32,11 @@ use crate::utils::fs::{
3232 read_regular_to_string_sync,
3333} ;
3434use crate :: utils:: pnpm_workspace:: governing_workspace_file;
35- use crate :: utils:: workspace_globs:: { workspaces_include , workspaces_include_as, WorkspacesDialect } ;
35+ use crate :: utils:: workspace_globs:: { workspaces_include_as, WorkspacesDialect } ;
3636use crate :: vendor:: cargo:: NOT_WORKSPACE_ROOT ;
3737use crate :: vendor:: cargo_manifest;
3838use crate :: vendor:: lock_inventory:: ProjectView ;
39+ use crate :: vendor:: vlt_lock_text:: { self , DepIdKind , LockSniff } ;
3940use crate :: vendor:: { bun_lock_text, bun_lockb} ;
4041
4142use super :: engine:: { Candidate , Refusal } ;
@@ -592,6 +593,9 @@ async fn package_json_workspace_root(root: &Path) -> Option<(PathBuf, Vec<&'stat
592593/// is older Bun's; 1.3.0/1.3.1 never list an excluded one. A list that
593594/// cannot be read keeps the wider pre-1.3 reading.
594595async fn lock_lists_member ( root : & Path , lock : & str , patterns : & [ String ] , rel : & [ String ] ) -> bool {
596+ if lock == VLT_LOCK {
597+ return vlt_lists_member ( root, & [ patterns. to_vec ( ) ] , rel) . await ;
598+ }
595599 let dialect = lock_dialect ( root, lock) . await ;
596600 if !workspaces_include_as ( patterns, rel, dialect) {
597601 return false ;
@@ -754,7 +758,7 @@ async fn vlt_workspace_root(root: &Path) -> Option<PathBuf> {
754758 let Ok ( text) = read_regular_to_string ( & ancestor. join ( VLT_JSON ) ) . await else {
755759 continue ;
756760 } ;
757- let Some ( patterns ) = vlt_workspace_patterns ( & text) else {
761+ let Some ( groups ) = vlt_workspace_patterns ( & text) else {
758762 continue ;
759763 } ;
760764 let Ok ( rel) = canonical. strip_prefix ( ancestor) else {
@@ -764,17 +768,88 @@ async fn vlt_workspace_root(root: &Path) -> Option<PathBuf> {
764768 . components ( )
765769 . map ( |c| c. as_os_str ( ) . to_string_lossy ( ) . into_owned ( ) )
766770 . collect ( ) ;
767- if !workspaces_include ( & patterns, & rel) {
771+ let any_vlt_lists = [ WorkspacesDialect :: VLT , WorkspacesDialect :: VLT_PRE_1_3 ]
772+ . iter ( )
773+ . any ( |& dialect| {
774+ groups
775+ . iter ( )
776+ . any ( |g| workspaces_include_as ( g, & rel, dialect) )
777+ } ) ;
778+ if !any_vlt_lists {
768779 continue ;
769780 }
770781 if !ancestor. join ( VLT_LOCK ) . is_file ( ) {
771782 return None ;
772783 }
784+ if !vlt_lists_member ( ancestor, & groups, & rel) . await {
785+ continue ;
786+ }
773787 return Some ( ancestor. to_path_buf ( ) ) ;
774788 }
775789 None
776790}
777791
792+ /// Whether vlt lists the member `rel` under the `workspaces` `groups` of
793+ /// the root `root`, which holds `vlt-lock.json` (#1542). vlt globs each
794+ /// group on its own. vlt 1.3 and older vlt read a `!` pattern differently
795+ /// ([`WorkspacesDialect::VLT`], [`WorkspacesDialect::VLT_PRE_1_3`]) but
796+ /// write the same lock, so for a directory only one of them lists, the
797+ /// lock decides: vlt records an edge from every member it installed that
798+ /// has a dependency, so a lock with an edge from the member, or from any
799+ /// other directory only older vlt lists, is older vlt's. A lock that
800+ /// cannot be read keeps the wider reading.
801+ async fn vlt_lists_member ( root : & Path , groups : & [ Vec < String > ] , rel : & [ String ] ) -> bool {
802+ let lists = |dir : & [ String ] , dialect| {
803+ groups
804+ . iter ( )
805+ . any ( |group| workspaces_include_as ( group, dir, dialect) )
806+ } ;
807+ let current = lists ( rel, WorkspacesDialect :: VLT ) ;
808+ let older = lists ( rel, WorkspacesDialect :: VLT_PRE_1_3 ) ;
809+ if current == older {
810+ return current;
811+ }
812+ let Some ( importers) = vlt_lock_workspace_dirs ( root) . await else {
813+ return true ;
814+ } ;
815+ let older_lock = importers. iter ( ) . any ( |dir| {
816+ lists ( dir, WorkspacesDialect :: VLT_PRE_1_3 ) && !lists ( dir, WorkspacesDialect :: VLT )
817+ } ) ;
818+ // A directory only vlt 1.3 lists (`!!x`) is a member unless the lock
819+ // shows older vlt; one only older vlt lists is a member only then.
820+ if current {
821+ !older_lock || importers. iter ( ) . any ( |dir| dir == rel)
822+ } else {
823+ older_lock
824+ }
825+ }
826+
827+ /// The workspace directories `root`'s `vlt-lock.json` holds edges from, as
828+ /// `/`-separated components. `None` when the lock cannot be read.
829+ async fn vlt_lock_workspace_dirs ( root : & Path ) -> Option < Vec < Vec < String > > > {
830+ let text = read_regular_to_string ( & root. join ( VLT_LOCK ) ) . await . ok ( ) ?;
831+ let LockSniff :: Readable ( lock) = vlt_lock_text:: sniff_lock ( & text) else {
832+ return None ;
833+ } ;
834+ let Some ( edges) = lock. edges ( ) else {
835+ return Some ( Vec :: new ( ) ) ;
836+ } ;
837+ Some (
838+ edges
839+ . keys ( )
840+ . filter_map ( |key| vlt_lock_text:: split_dep_id ( key. split_once ( ' ' ) ?. 0 ) )
841+ . filter ( |id| id. kind == DepIdKind :: Workspace )
842+ . map ( |id| {
843+ id. first
844+ . split ( [ '/' , '\\' ] )
845+ . filter ( |part| !part. is_empty ( ) && * part != "." )
846+ . map ( str:: to_string)
847+ . collect ( )
848+ } )
849+ . collect ( ) ,
850+ )
851+ }
852+
778853/// Whether `<dir>/vlt.json` has a `workspaces` field. vlt keys its
779854/// precedence on the field: with it, `package.json` `workspaces` is
780855/// ignored; without it (or without the file), vlt reads `package.json`.
@@ -802,10 +877,10 @@ fn nearer_root(
802877 }
803878}
804879
805- /// The `workspaces` patterns of a `vlt.json`: a string, an array, or an
806- /// object of named groups whose values are a string or an array. `None`
807- /// when the field is absent or the file does not parse.
808- fn vlt_workspace_patterns ( vlt_json : & str ) -> Option < Vec < String > > {
880+ /// The `workspaces` pattern groups of a `vlt.json`: a string or an array
881+ /// (one group), or an object of named groups whose values are a string or
882+ /// an array. `None` when the field is absent or the file does not parse.
883+ fn vlt_workspace_patterns ( vlt_json : & str ) -> Option < Vec < Vec < String > > > {
809884 fn strings ( value : & serde_json:: Value , out : & mut Vec < String > ) {
810885 match value {
811886 serde_json:: Value :: String ( s) => out. push ( s. clone ( ) ) ,
@@ -818,12 +893,15 @@ fn vlt_workspace_patterns(vlt_json: &str) -> Option<Vec<String>> {
818893 let text = strip_bom ( vlt_json) ;
819894 let doc: serde_json:: Value = serde_json:: from_str ( text) . ok ( ) ?;
820895 let field = doc. get ( "workspaces" ) ?;
821- let mut out = Vec :: new ( ) ;
822- match field {
823- serde_json:: Value :: Object ( groups) => groups. values ( ) . for_each ( |v| strings ( v, & mut out) ) ,
824- other => strings ( other, & mut out) ,
825- }
826- Some ( out)
896+ let group = |value| {
897+ let mut out = Vec :: new ( ) ;
898+ strings ( value, & mut out) ;
899+ out
900+ } ;
901+ Some ( match field {
902+ serde_json:: Value :: Object ( groups) => groups. values ( ) . map ( group) . collect ( ) ,
903+ other => vec ! [ group( other) ] ,
904+ } )
827905}
828906
829907/// The `workspaces` patterns of a `package.json`: the array form (npm,
@@ -2012,7 +2090,10 @@ mod tests {
20122090 let json = r#"{"workspaces":["a/*"]}"# ;
20132091 for bom in [ "" , "\u{feff} " ] {
20142092 let text = format ! ( "{bom}{json}" ) ;
2015- assert_eq ! ( vlt_workspace_patterns( & text) , Some ( vec![ "a/*" . to_string( ) ] ) ) ;
2093+ assert_eq ! (
2094+ vlt_workspace_patterns( & text) ,
2095+ Some ( vec![ vec![ "a/*" . to_string( ) ] ] )
2096+ ) ;
20162097 assert_eq ! ( workspace_patterns( & text) , Some ( vec![ "a/*" . to_string( ) ] ) ) ;
20172098 }
20182099 let text = format ! ( "\u{feff} \u{feff} {json}" ) ;
@@ -2026,18 +2107,17 @@ mod tests {
20262107 fn vlt_workspace_patterns_read_every_shape ( ) {
20272108 assert_eq ! (
20282109 vlt_workspace_patterns( r#"{"workspaces":"packages/*"}"# ) ,
2029- Some ( vec![ "packages/*" . to_string( ) ] )
2110+ Some ( vec![ vec! [ "packages/*" . to_string( ) ] ] )
20302111 ) ;
20312112 assert_eq ! (
20322113 vlt_workspace_patterns( "\u{feff} {\" workspaces\" :[\" a/*\" ,\" b\" ]}" ) ,
2033- Some ( vec![ "a/*" . to_string( ) , "b" . to_string( ) ] )
2114+ Some ( vec![ vec! [ "a/*" . to_string( ) , "b" . to_string( ) ] ] )
20342115 ) ;
20352116 assert_eq ! (
20362117 vlt_workspace_patterns( r#"{"workspaces":{"apps":"apps/*","libs":["libs/*","x"]}}"# ) ,
20372118 Some ( vec![
2038- "apps/*" . to_string( ) ,
2039- "libs/*" . to_string( ) ,
2040- "x" . to_string( )
2119+ vec![ "apps/*" . to_string( ) ] ,
2120+ vec![ "libs/*" . to_string( ) , "x" . to_string( ) ]
20412121 ] )
20422122 ) ;
20432123 assert_eq ! ( vlt_workspace_patterns( r#"{"registries":{}}"# ) , None ) ;
@@ -2164,7 +2244,11 @@ mod tests {
21642244 write ( tmp. path ( ) , "vlt.json" , r#"{"registries":{}}"# ) ;
21652245 } else {
21662246 write ( tmp. path ( ) , "package.json" , r#"{"private":true}"# ) ;
2167- write ( tmp. path ( ) , "vlt.json" , & format ! ( r#"{{"workspaces":{list}}}"# ) ) ;
2247+ write (
2248+ tmp. path ( ) ,
2249+ "vlt.json" ,
2250+ & format ! ( r#"{{"workspaces":{list}}}"# ) ,
2251+ ) ;
21682252 }
21692253 write ( tmp. path ( ) , VLT_LOCK , "{}" ) ;
21702254 write ( tmp. path ( ) , "packages/a/package.json" , "{}" ) ;
@@ -2182,11 +2266,7 @@ mod tests {
21822266 Some ( WORKSPACE_LOCKFILE_ELSEWHERE ) ,
21832267 "{case}"
21842268 ) ;
2185- assert_eq ! (
2186- member_stray_lock( & member) . await . is_some( ) ,
2187- stray,
2188- "{case}"
2189- ) ;
2269+ assert_eq ! ( member_stray_lock( & member) . await . is_some( ) , stray, "{case}" ) ;
21902270 // `packages/c` is not listed: its own lock governs it.
21912271 let other = tmp. path ( ) . join ( "packages/c" ) ;
21922272 assert_eq ! ( code( & other, "npm" ) . await , None , "{case}" ) ;
@@ -2226,7 +2306,11 @@ mod tests {
22262306 false ,
22272307 ) ,
22282308 // A later pattern whose text the `!` matches lists it again.
2229- ( r#"["packages/*","!packages/a","packages/a"]"# , lock ( & [ ] ) , true ) ,
2309+ (
2310+ r#"["packages/*","!packages/a","packages/a"]"# ,
2311+ lock ( & [ ] ) ,
2312+ true ,
2313+ ) ,
22302314 ( r#"["!packages/*","packages/a"]"# , lock ( & [ ] ) , true ) ,
22312315 // vlt 1.2 installed the excluded member (or another one).
22322316 (
@@ -2240,7 +2324,11 @@ mod tests {
22402324 true ,
22412325 ) ,
22422326 // An unreadable lock keeps the wider reading.
2243- ( r#"["!packages/a","packages/*"]"# , "not json" . to_string ( ) , true ) ,
2327+ (
2328+ r#"["!packages/a","packages/*"]"# ,
2329+ "not json" . to_string ( ) ,
2330+ true ,
2331+ ) ,
22442332 ] {
22452333 let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
22462334 write ( tmp. path ( ) , "package.json" , r#"{"private":true}"# ) ;
@@ -2254,12 +2342,12 @@ mod tests {
22542342 write ( tmp. path ( ) , "packages/a/vlt-lock.json" , "{}" ) ;
22552343 let member = tmp. path ( ) . join ( "packages/a" ) ;
22562344 let case = format ! ( "{workspaces} with {root_lock:?}" ) ;
2345+ assert_eq ! ( code( & member, "npm" ) . await . is_some( ) , refused, "{case}" ) ;
22572346 assert_eq ! (
2258- code ( & member, "npm" ) . await . is_some( ) ,
2347+ member_stray_lock ( & member) . await . is_some( ) ,
22592348 refused,
22602349 "{case}"
22612350 ) ;
2262- assert_eq ! ( member_stray_lock( & member) . await . is_some( ) , refused, "{case}" ) ;
22632351 }
22642352 }
22652353
0 commit comments