Skip to content

Commit 1b3cc67

Browse files
fix(task): cwd of builtin commands (#69)
Co-authored-by: branchseer <dk4rest@gmail.com>
1 parent 3c6f51c commit 1b3cc67

18 files changed

Lines changed: 461 additions & 397 deletions

File tree

Cargo.lock

Lines changed: 296 additions & 269 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/vite_error/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ rusqlite = { workspace = true }
1717
serde_json = { workspace = true }
1818
serde_yml = { workspace = true }
1919
thiserror = { workspace = true }
20-
wax = { workspace = true }
21-
vite_str = { workspace = true }
2220
vite_path = { workspace = true }
21+
vite_str = { workspace = true }
22+
wax = { workspace = true }

crates/vite_package_manager/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use vite_str::Str;
1212
use wax::Glob;
1313

1414
use crate::package_manager::WorkspaceFile;
15-
pub use package_manager::{find_package_root, find_workspace_root};
15+
pub use package_manager::{WorkspaceRoot, find_package_root, find_workspace_root};
1616

1717
/// The workspace configuration for pnpm.
1818
#[derive(Debug, Deserialize)]

crates/vite_package_manager/src/package_manager.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use std::io::{BufReader, Seek, SeekFrom};
33
use std::path::Path;
44

55
use 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)]
1010
pub 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)]
5560
pub 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
}

crates/vite_path/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ rust-version.workspace = true
88

99
[dependencies]
1010
bincode = { workspace = true }
11+
diff-struct = { workspace = true }
1112
ref-cast = { workspace = true }
13+
serde = { workspace = true, features = ["derive", "rc"] }
1214
thiserror = { workspace = true }
1315
vite_str = { workspace = true }
14-
serde = { workspace = true, features = ["derive"] }
15-
diff-struct = { workspace = true }
1616

1717
[lints]
1818
workspace = true

crates/vite_path/src/absolute.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
sync::Arc,
77
};
88

9-
use crate::relative::{FromPathError, InvalidPathDataError, RelativePath, RelativePathBuf};
9+
use crate::relative::{FromPathError, InvalidPathDataError, RelativePathBuf};
1010

1111
/// A path that is guaranteed to be absolute
1212
#[derive(RefCastCustom, Debug, PartialEq, Eq)]
@@ -53,7 +53,7 @@ impl AbsolutePath {
5353
///
5454
/// If `base` is not a prefix of `self`, returns [`None`].
5555
///
56-
/// If the stripped path is not a valid [`RelativePath`]. Returns an error with the reason and the stripped path.
56+
/// If the stripped path is not a valid `RelativePath`. Returns an error with the reason and the stripped path.
5757
pub fn strip_prefix<P: AsRef<AbsolutePath>>(
5858
&self,
5959
base: P,

crates/vite_path/src/relative.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use std::{
1313
};
1414
use vite_str::Str;
1515

16-
use bincode::{Decode, Encode, de::Decoder, error::DecodeError, impl_borrow_decode};
17-
16+
use bincode::{Decode, Encode, de::Decoder, error::DecodeError};
1817
use ref_cast::{RefCastCustom, ref_cast_custom};
1918

2019
/// A relative path with additional guarantees to make it portable:
@@ -67,20 +66,8 @@ impl RelativePath {
6766

6867
/// A owned relative path buf with the same guarantees as `RelativePath`
6968
#[derive(
70-
Debug,
71-
Encode,
72-
PartialEq,
73-
Eq,
74-
PartialOrd,
75-
Ord,
76-
Hash,
77-
Clone,
78-
Serialize,
79-
Deserialize,
80-
Default,
81-
Diff,
69+
Debug, Encode, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Serialize, Deserialize, Default,
8270
)]
83-
#[diff(attr(#[derive(Debug)]))]
8471
pub struct RelativePathBuf(Str);
8572

8673
impl AsRef<Path> for RelativePathBuf {
@@ -106,7 +93,24 @@ impl PartialEq<&RelativePath> for RelativePathBuf {
10693
}
10794
}
10895

96+
impl Diff for RelativePathBuf {
97+
type Repr = Option<Str>;
98+
fn diff(&self, other: &Self) -> Self::Repr {
99+
self.0.diff(&other.0)
100+
}
101+
fn apply(&mut self, diff: &Self::Repr) {
102+
self.0.apply(diff);
103+
}
104+
fn identity() -> Self {
105+
Self(Str::identity())
106+
}
107+
}
108+
109109
impl RelativePathBuf {
110+
pub fn empty() -> Self {
111+
Self("".into())
112+
}
113+
110114
/// Extends `self` with `path`.
111115
///
112116
/// Unlike [`std::path::PathBuf::push`], `self` and `path` are both always relative,
@@ -171,7 +175,23 @@ impl<'a, Context> Decode<Context> for RelativePathBuf {
171175
.map_err(|err| DecodeError::OtherString(format!("{}: {}", err, path_str)))
172176
}
173177
}
174-
impl_borrow_decode!(RelativePathBuf);
178+
179+
bincode::impl_borrow_decode!(RelativePathBuf);
180+
181+
impl TryFrom<&Path> for RelativePathBuf {
182+
type Error = FromPathError;
183+
fn try_from(path: &Path) -> Result<Self, Self::Error> {
184+
Self::new(path)
185+
}
186+
}
187+
188+
impl TryFrom<&str> for RelativePathBuf {
189+
type Error = FromPathError;
190+
fn try_from(path: &str) -> Result<Self, Self::Error> {
191+
let path = Path::new(path);
192+
Self::try_from(path)
193+
}
194+
}
175195

176196
impl AsRef<RelativePath> for RelativePathBuf {
177197
fn as_ref(&self) -> &RelativePath {

0 commit comments

Comments
 (0)