Skip to content

Commit

Permalink
fix: correct impl WorldProvider::entry for CompileOnceArgs (#1230)
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin authored Jan 29, 2025
1 parent 0451a10 commit 8d588c1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
43 changes: 25 additions & 18 deletions crates/tinymist-project/src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,40 +76,47 @@ impl WorldProvider for CompileOnceArgs {
}

fn entry(&self) -> Result<EntryOpts> {
let input = self.input.as_ref().context("entry file must be provided")?;
let input = Path::new(&input);
let entry = if input.is_absolute() {
input.to_owned()
} else {
std::env::current_dir().unwrap().join(input)
let mut cwd = None;
let mut cwd = move || {
cwd.get_or_insert_with(|| {
std::env::current_dir().context("failed to get current directory")
})
.clone()
};

let main = {
let input = self.input.as_ref().context("entry file must be provided")?;
let input = Path::new(&input);
if input.is_absolute() {
input.to_owned()
} else {
cwd()?.join(input)
}
};

let root = if let Some(root) = &self.root {
if root.is_absolute() {
root.clone()
} else {
std::env::current_dir().unwrap().join(root)
cwd()?.join(root)
}
} else {
std::env::current_dir().unwrap()
main.parent()
.context("entry file don't have a valid parent as root")?
.to_owned()
};

if !entry.starts_with(&root) {
log::error!("entry file must be in the root directory");
std::process::exit(1);
}

let relative_entry = match entry.strip_prefix(&root) {
Ok(relative_entry) => relative_entry,
let relative_main = match main.strip_prefix(&root) {
Ok(relative_main) => relative_main,
Err(_) => {
log::error!("entry path must be inside the root: {}", entry.display());
std::process::exit(1);
log::error!("entry file must be inside the root, file: {main:?}, root: {root:?}");
bail!("entry file must be inside the root, file: {main:?}, root: {root:?}");
}
};

Ok(EntryOpts::new_rooted(
root.clone(),
Some(relative_entry.to_owned()),
Some(relative_main.to_owned()),
))
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/tinymist-world/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub enum EntryOpts {
/// The world forbids direct access to files outside this directory.
root: PathBuf,
/// Relative path to the main file in the workspace.
entry: Option<PathBuf>,
main: Option<PathBuf>,
},
RootByParent {
/// Path to the entry file of compilation.
Expand All @@ -171,11 +171,11 @@ impl EntryOpts {
}

pub fn new_workspace(root: PathBuf) -> Self {
Self::Workspace { root, entry: None }
Self::Workspace { root, main: None }
}

pub fn new_rooted(root: PathBuf, entry: Option<PathBuf>) -> Self {
Self::Workspace { root, entry }
pub fn new_rooted(root: PathBuf, main: Option<PathBuf>) -> Self {
Self::Workspace { root, main }
}

pub fn new_rootless(entry: PathBuf) -> Option<Self> {
Expand All @@ -194,7 +194,7 @@ impl TryFrom<EntryOpts> for EntryState {

fn try_from(value: EntryOpts) -> Result<Self, Self::Error> {
match value {
EntryOpts::Workspace { root, entry } => Ok(EntryState::new_rooted(
EntryOpts::Workspace { root, main: entry } => Ok(EntryState::new_rooted(
root.as_path().into(),
entry.map(VirtualPath::new),
)),
Expand Down

0 comments on commit 8d588c1

Please sign in to comment.