@@ -36,9 +36,22 @@ impl UserPatchIndex {
3636 /// Inspect the project, declared workspace members and ancestor workspace
3737 /// settings. Errors in a potentially governing file protect its ecosystem;
3838 /// unreadable metadata must not silently disable the consent check.
39+ ///
40+ /// A workspace run: nothing outside the git repository root is read
41+ /// (see [`Self::discover_scoped`]).
3942 pub async fn discover ( root : & Path ) -> Self {
43+ Self :: discover_scoped ( root, ReadScope :: Workspace ) . await
44+ }
45+
46+ /// [`Self::discover`] in the read scope the run's mode fixes. A
47+ /// [`ReadScope::Workspace`] run never reads a project file outside the
48+ /// git repository root: a workspace member or `--patch-dir` beyond it
49+ /// protects every npm package instead. A [`ReadScope::Global`] run reads
50+ /// the evidence wherever it lives, with no repository bound and no
51+ /// such fallback.
52+ pub async fn discover_scoped ( root : & Path , reads : ReadScope ) -> Self {
4053 let root = std:: path:: absolute ( root) . unwrap_or_else ( |_| root. to_path_buf ( ) ) ;
41- Self :: walk ( & ProjectView :: Disk ( & root) , Some ( & root) , Scope :: Gate ) . await
54+ Self :: walk ( & ProjectView :: Disk ( & root) , Some ( & root) , Scope :: Gate , reads ) . await
4255 }
4356
4457 /// The patches a project's pnpm, yarn or patch-package setup applies
@@ -61,7 +74,8 @@ impl UserPatchIndex {
6174 /// a vlt project patches a dependency only through a postinstall tool
6275 /// such as patch-package. Sources are reported root-relative.
6376 pub ( crate ) async fn npm_family_in ( view : & ProjectView < ' _ > , locks : & [ ( & str , & str ) ] ) -> Self {
64- let mut index = Self :: walk ( view, None , Scope :: NpmFamily ) . await ;
77+ // Discovery and the hosted engine only ever run on a workspace.
78+ let mut index = Self :: walk ( view, None , Scope :: NpmFamily , ReadScope :: Workspace ) . await ;
6579 for ( rel, text) in locks {
6680 let path = Path :: new ( rel) ;
6781 match rel. rsplit ( '/' ) . next ( ) {
@@ -86,13 +100,20 @@ impl UserPatchIndex {
86100 /// anchored: the absolute project root for the gate, `None` for
87101 /// root-relative sources.
88102 ///
89- /// Nothing outside the git repository root is read: the ancestor walk
90- /// stops at the directory holding `.git`, and a workspace member or
91- /// patch-package directory beyond it protects every npm package
92- /// instead of being read. Without a `.git` the gate's container rule
93- /// holds (the walk never reaches the home directory or above it). An
94- /// in-memory project is its own repository root.
95- async fn walk ( view : & ProjectView < ' _ > , shown_root : Option < & Path > , scope : Scope ) -> Self {
103+ /// The ancestor walk stops at the directory holding `.git`, and without
104+ /// a `.git` the gate's container rule holds (the walk never reaches the
105+ /// home directory or above it). In a [`ReadScope::Workspace`] run
106+ /// nothing outside that git repository root is read: a workspace member
107+ /// or patch-package directory beyond it protects every npm package
108+ /// instead of being read. An in-memory project is its own repository
109+ /// root. A [`ReadScope::Global`] run reads members and patch-package
110+ /// directories wherever they are.
111+ async fn walk (
112+ view : & ProjectView < ' _ > ,
113+ shown_root : Option < & Path > ,
114+ scope : Scope ,
115+ reads : ReadScope ,
116+ ) -> Self {
96117 let mut index = Self :: default ( ) ;
97118 let shown_base = shown_root. unwrap_or ( Path :: new ( "" ) ) ;
98119 let at = |rel : & Path | Dir {
@@ -157,6 +178,12 @@ impl UserPatchIndex {
157178 } else {
158179 repo_depth = Some ( 0 ) ;
159180 }
181+ // The read bound: the repository root in a workspace run, none in a
182+ // global one.
183+ let repo_depth = match reads {
184+ ReadScope :: Workspace => repo_depth,
185+ ReadScope :: Global => None ,
186+ } ;
160187 let mut seen = BTreeSet :: new ( ) ;
161188 while let Some ( dir) = pending. pop_front ( ) {
162189 if !seen. insert ( dir. rel . clone ( ) ) {
@@ -950,6 +977,31 @@ impl UserPatchIndex {
950977 }
951978}
952979
980+ /// Which project files a run may read, chosen once from the CLI mode
981+ /// (never inferred from the filesystem). The same shape as #2025's
982+ /// `utils::repo_root::ReadScope`, which replaces it when the two meet.
983+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
984+ pub enum ReadScope {
985+ /// A project run (the default `--cwd` project): project files are read
986+ /// only inside the git repository root.
987+ Workspace ,
988+ /// A global run (`--global` / `--global-prefix`): files are read
989+ /// wherever they live on the system.
990+ Global ,
991+ }
992+
993+ impl ReadScope {
994+ /// The scope of a run whose global flag (`--global` or
995+ /// `--global-prefix`) is `global`.
996+ pub fn of_global ( global : bool ) -> Self {
997+ if global {
998+ ReadScope :: Global
999+ } else {
1000+ ReadScope :: Workspace
1001+ }
1002+ }
1003+ }
1004+
9531005/// What a [`UserPatchIndex::walk`] reads.
9541006#[ derive( Clone , Copy , PartialEq , Eq ) ]
9551007enum Scope {
@@ -1596,6 +1648,76 @@ mod tests {
15961648 }
15971649 }
15981650
1651+ /// The repository-root bound is a workspace-run rule only. A workspace
1652+ /// run reads no workspace member or `--patch-dir` outside the git
1653+ /// repository: each protects every npm package instead. A global run
1654+ /// reads both wherever they are and protects exactly the packages they
1655+ /// patch.
1656+ #[ tokio:: test]
1657+ async fn the_repository_bound_holds_for_workspace_runs_only ( ) {
1658+ let outer = tempfile:: tempdir ( ) . unwrap ( ) ;
1659+ let repo = outer. path ( ) . join ( "repo" ) ;
1660+ write ( & repo, ".git/HEAD" , "ref: refs/heads/main\n " ) ;
1661+ write (
1662+ & repo,
1663+ "package.json" ,
1664+ r#"{"workspaces":["../shared/*"],"scripts":{"postinstall":"patch-package --patch-dir ../fixes"}}"# ,
1665+ ) ;
1666+ write ( outer. path ( ) , "fixes/left-pad+1.3.0.patch" , "diff\n " ) ;
1667+ write ( outer. path ( ) , "shared/a/package.json" , "{}" ) ;
1668+ write (
1669+ outer. path ( ) ,
1670+ "shared/a/patches/is-number+7.0.0.patch" ,
1671+ "diff\n " ,
1672+ ) ;
1673+
1674+ let workspace = UserPatchIndex :: discover_scoped ( & repo, ReadScope :: Workspace ) . await ;
1675+ let sources = workspace. conflicts ( "pkg:npm/other@1.0.0" ) ;
1676+ for what in [
1677+ "workspace outside the git repository" ,
1678+ "patch-package directory outside" ,
1679+ ] {
1680+ assert ! (
1681+ sources. iter( ) . any( |s| s. contains( what) ) ,
1682+ "{what}: {sources:?}"
1683+ ) ;
1684+ }
1685+ assert ! (
1686+ !sources. iter( ) . any( |s| s. ends_with( ".patch" ) ) ,
1687+ "{sources:?}"
1688+ ) ;
1689+ assert_eq ! (
1690+ UserPatchIndex :: discover( & repo)
1691+ . await
1692+ . conflicts( "pkg:npm/other@1.0.0" ) ,
1693+ sources,
1694+ "discover() is the workspace scope"
1695+ ) ;
1696+
1697+ let global = UserPatchIndex :: discover_scoped ( & repo, ReadScope :: Global ) . await ;
1698+ assert ! (
1699+ global. conflicts( "pkg:npm/other@1.0.0" ) . is_empty( ) ,
1700+ "{global:?}"
1701+ ) ;
1702+ let left_pad = global. conflicts ( "pkg:npm/left-pad@1.3.0" ) ;
1703+ assert ! (
1704+ left_pad
1705+ . iter( )
1706+ . any( |s| s. ends_with( "fixes/left-pad+1.3.0.patch" ) ) ,
1707+ "{left_pad:?}"
1708+ ) ;
1709+ let is_number = global. conflicts ( "pkg:npm/is-number@7.0.0" ) ;
1710+ assert ! (
1711+ is_number
1712+ . iter( )
1713+ . any( |s| s. ends_with( "shared/a/patches/is-number+7.0.0.patch" ) ) ,
1714+ "{is_number:?}"
1715+ ) ;
1716+ assert ! ( global. conflicts( "pkg:npm/left-pad@1.2.0" ) . is_empty( ) ) ;
1717+ assert_eq ! ( ReadScope :: of_global( true ) , ReadScope :: Global ) ;
1718+ assert_eq ! ( ReadScope :: of_global( false ) , ReadScope :: Workspace ) ;
1719+ }
1720+
15991721 /// The view-based index the hosted engine and `vex` discovery read
16001722 /// ([`UserPatchIndex::npm_family_in`]) protects exactly what the CLI's
16011723 /// consent gate ([`UserPatchIndex::discover`]) protects for a root
0 commit comments