Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(turborepo): Move some shim paths over to AbsoluteSystemPath #5108

Merged
merged 4 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 6 additions & 14 deletions crates/turborepo-lib/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::{
env::{self, current_dir},
io, mem,
env, io, mem,
path::{Path, PathBuf},
process,
};

use anyhow::{anyhow, Result};
use clap::{ArgAction, CommandFactory, Parser, Subcommand, ValueEnum};
use clap_complete::{generate, Shell};
use dunce::canonicalize as fs_canonicalize;
use serde::Serialize;
use tracing::{debug, error};
use turbopath::AbsoluteSystemPathBuf;
Expand Down Expand Up @@ -559,10 +557,10 @@ pub async fn run(
let invocation_path = Path::new(&invocation_dir);

// If repo state doesn't exist, we're either local turbo running at the root
// (current_dir), or inference failed If repo state does exist,
// (cwd), or inference failed If repo state does exist,
// we're global turbo, and want to calculate package inference based on the repo
// root
let this_dir = current_dir()?;
let this_dir = AbsoluteSystemPathBuf::cwd()?;
let repo_root = repo_state.as_ref().map(|r| &r.root).unwrap_or(&this_dir);
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
debug!("pkg_inference_root set to \"{}\"", relative_path.display());
Expand All @@ -582,21 +580,15 @@ pub async fn run(
if let Some(Command::Run(run_args)) = &mut cli_args.command {
run_args.single_package = matches!(repo_state.mode, RepoMode::SinglePackage);
}
cli_args.cwd = Some(repo_state.root);
cli_args.cwd = Some(repo_state.root.as_path().to_owned());
}

let repo_root = if let Some(cwd) = &cli_args.cwd {
let canonical_cwd = fs_canonicalize(cwd)?;
// Update on clap_args so that Go gets a canonical path.
cli_args.cwd = Some(canonical_cwd.clone());
canonical_cwd
AbsoluteSystemPathBuf::from_cwd(cwd)?
} else {
current_dir()?
AbsoluteSystemPathBuf::cwd()?
};

// a non-absolute repo root is a bug
let repo_root = AbsoluteSystemPathBuf::new(repo_root).expect("repo_root is not absolute");

let version = get_version();

match cli_args.command.as_ref().unwrap() {
Expand Down
23 changes: 16 additions & 7 deletions crates/turborepo-lib/src/package_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use anyhow::{anyhow, Result};
use itertools::Itertools;
use regex::Regex;
use serde::{Deserialize, Serialize};
use turbopath::AbsoluteSystemPath;

use crate::{
commands::CommandBase,
Expand Down Expand Up @@ -85,7 +86,7 @@ pub struct Globs {
}

impl Globs {
pub fn test(&self, root: PathBuf, target: PathBuf) -> Result<bool> {
pub fn test(&self, root: &Path, target: PathBuf) -> Result<bool> {
let search_value = target
.strip_prefix(root)?
.to_str()
Expand Down Expand Up @@ -121,10 +122,11 @@ impl PackageManager {
///
/// ```
/// ```
pub fn get_workspace_globs(&self, root_path: &Path) -> Result<Option<Globs>> {
pub fn get_workspace_globs(&self, root_path: &AbsoluteSystemPath) -> Result<Option<Globs>> {
let globs = match self {
PackageManager::Pnpm | PackageManager::Pnpm6 => {
let workspace_yaml = fs::read_to_string(root_path.join("pnpm-workspace.yaml"))?;
let workspace_yaml =
fs::read_to_string(root_path.join_component("pnpm-workspace.yaml"))?;
let pnpm_workspace: PnpmWorkspace = serde_yaml::from_str(&workspace_yaml)?;
if pnpm_workspace.packages.is_empty() {
return Ok(None);
Expand All @@ -133,7 +135,8 @@ impl PackageManager {
}
}
PackageManager::Berry | PackageManager::Npm | PackageManager::Yarn => {
let package_json_text = fs::read_to_string(root_path.join("package.json"))?;
let package_json_text =
fs::read_to_string(root_path.join_component("package.json"))?;
let package_json: PackageJsonWorkspaces = serde_json::from_str(&package_json_text)?;

if package_json.workspaces.as_ref().is_empty() {
Expand Down Expand Up @@ -237,7 +240,7 @@ impl PackageManager {

#[cfg(test)]
mod tests {
use std::{fs::File, path::Path};
use std::fs::File;

use tempfile::tempdir;
use turbopath::AbsoluteSystemPathBuf;
Expand Down Expand Up @@ -393,9 +396,15 @@ mod tests {

#[test]
fn test_get_workspace_globs() {
let cwd = AbsoluteSystemPathBuf::cwd().unwrap();
let repo_root = cwd
.ancestors()
.find(|path| path.join_component(".git").exists())
.unwrap();
let with_yarn = repo_root.join_components(&["examples", "with-yarn"]);
let package_manager = PackageManager::Npm;
let globs = package_manager
.get_workspace_globs(Path::new("../../examples/with-yarn"))
.get_workspace_globs(with_yarn.as_absolute_path())
.unwrap()
.unwrap();

Expand All @@ -422,7 +431,7 @@ mod tests {
}];

for test in tests {
match test.globs.test(test.root, test.target) {
match test.globs.test(&test.root, test.target) {
Ok(value) => assert_eq!(value, test.output.unwrap()),
Err(value) => assert_eq!(value.to_string(), test.output.unwrap_err().to_string()),
};
Expand Down
Loading