@@ -3,12 +3,13 @@ use std::io::{BufReader, Seek, SeekFrom};
33use std:: path:: Path ;
44
55use vite_error:: Error ;
6- use vite_path:: AbsolutePath ;
6+ use vite_path:: { AbsolutePath , RelativePathBuf } ;
77
88/// The package root directory and its package.json file.
99#[ derive( Debug ) ]
1010pub struct PackageRoot < ' a > {
1111 pub path : & ' a AbsolutePath ,
12+ pub cwd : RelativePathBuf ,
1213 pub package_json : File ,
1314}
1415
@@ -20,7 +21,11 @@ pub fn find_package_root<'a>(original_cwd: &'a AbsolutePath) -> Result<PackageRo
2021 loop {
2122 // Check for package.json
2223 if let Some ( file) = open_exists_file ( cwd. join ( "package.json" ) ) ? {
23- return Ok ( PackageRoot { path : cwd, package_json : file } ) ;
24+ return Ok ( PackageRoot {
25+ path : cwd,
26+ cwd : original_cwd. strip_prefix ( cwd) ?. expect ( "cwd must be within the package root" ) ,
27+ package_json : file,
28+ } ) ;
2429 }
2530
2631 if let Some ( parent) = cwd. parent ( ) {
@@ -54,6 +59,7 @@ pub enum WorkspaceFile {
5459#[ derive( Debug ) ]
5560pub struct WorkspaceRoot < ' a > {
5661 pub path : & ' a AbsolutePath ,
62+ pub cwd : RelativePathBuf ,
5763 pub workspace_file : WorkspaceFile ,
5864}
5965
@@ -70,6 +76,9 @@ pub fn find_workspace_root<'a>(original_cwd: &'a AbsolutePath) -> Result<Workspa
7076 if let Some ( file) = open_exists_file ( cwd. join ( "pnpm-workspace.yaml" ) ) ? {
7177 return Ok ( WorkspaceRoot {
7278 path : cwd,
79+ cwd : original_cwd
80+ . strip_prefix ( cwd) ?
81+ . expect ( "cwd must be within the pnpm workspace" ) ,
7382 workspace_file : WorkspaceFile :: PnpmWorkspaceYaml ( file) ,
7483 } ) ;
7584 }
@@ -83,6 +92,7 @@ pub fn find_workspace_root<'a>(original_cwd: &'a AbsolutePath) -> Result<Workspa
8392 file. seek ( SeekFrom :: Start ( 0 ) ) ?;
8493 return Ok ( WorkspaceRoot {
8594 path : cwd,
95+ cwd : original_cwd. strip_prefix ( cwd) ?. expect ( "cwd must be within the workspace" ) ,
8696 workspace_file : WorkspaceFile :: NpmWorkspaceJson ( file) ,
8797 } ) ;
8898 }
@@ -97,7 +107,11 @@ pub fn find_workspace_root<'a>(original_cwd: &'a AbsolutePath) -> Result<Workspa
97107 // We've reached the root, try to find the package root and return the non-workspace package.
98108 let package_root = find_package_root ( original_cwd) ?;
99109 let workspace_file = WorkspaceFile :: NonWorkspacePackage ( package_root. package_json ) ;
100- return Ok ( WorkspaceRoot { path : package_root. path , workspace_file } ) ;
110+ return Ok ( WorkspaceRoot {
111+ path : package_root. path ,
112+ cwd : package_root. cwd ,
113+ workspace_file,
114+ } ) ;
101115 }
102116 }
103117}
0 commit comments