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

fix(turborepo) Set package inference and single_package even without a repo state #5432

Merged
merged 2 commits into from
Jun 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
63 changes: 29 additions & 34 deletions crates/turborepo-lib/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,28 +596,36 @@ pub async fn run(
Command::Run(Box::new(run_args))
};

let mut pkg_inference_root = None;
// If this is a run command, and we know the actual invocation path, set the
// inference root, as long as the user hasn't overridden the cwd
if cli_args.cwd.is_none() && matches!(command, Command::Run(_)) {
if let Ok(invocation_dir) = env::var(INVOCATION_DIR_ENV_VAR) {
let invocation_path = Path::new(&invocation_dir);

// If repo state doesn't exist, we're either local turbo running at the root
// (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 = AbsoluteSystemPathBuf::cwd()?;
let repo_root = repo_state.as_ref().map_or(&this_dir, |r| &r.root);
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
debug!("pkg_inference_root set to \"{}\"", relative_path.display());
let utf8_path = relative_path
.to_str()
.ok_or_else(|| anyhow!("invalid utf8 path: {:?}", relative_path))?;
pkg_inference_root = Some(utf8_path.to_owned());
// Set some run flags if we have the data and are executing a Run
if let Command::Run(run_args) = &mut command {
// Don't overwrite the flag if it's already been set for whatever reason
run_args.single_package = run_args.single_package
|| repo_state
.as_ref()
.map(|repo_state| matches!(repo_state.mode, RepoMode::SinglePackage))
.unwrap_or(false);
// If this is a run command, and we know the actual invocation path, set the
// inference root, as long as the user hasn't overridden the cwd
if cli_args.cwd.is_none() {
if let Ok(invocation_dir) = env::var(INVOCATION_DIR_ENV_VAR) {
let invocation_path = Path::new(&invocation_dir);

// If repo state doesn't exist, we're either local turbo running at the root
// (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 = AbsoluteSystemPathBuf::cwd()?;
let repo_root = repo_state.as_ref().map_or(&this_dir, |r| &r.root);
if let Ok(relative_path) = invocation_path.strip_prefix(repo_root) {
debug!("pkg_inference_root set to \"{}\"", relative_path.display());
let utf8_path = relative_path
.to_str()
.ok_or_else(|| anyhow!("invalid utf8 path: {:?}", relative_path))?;
run_args.pkg_inference_root = Some(utf8_path.to_owned());
}
} else {
debug!("{} not set", INVOCATION_DIR_ENV_VAR);
}
} else {
debug!("{} not set", INVOCATION_DIR_ENV_VAR);
}
}

Expand All @@ -634,19 +642,6 @@ pub async fn run(

let version = get_version();

// Save all the mutation for the end. In the future, we should refactor this
// into a "parse don't validate" scheme where we construct a config struct
// that is contains all of the normalized, validated, and defaulted values.
if let Some(repo_state) = repo_state {
if let Command::Run(run_args) = &mut command {
if let Some(pkg_inference_root) = pkg_inference_root {
run_args.pkg_inference_root = Some(pkg_inference_root);
}
// Don't overwrite the flag if it's already been set for whatever reason
run_args.single_package =
run_args.single_package || matches!(repo_state.mode, RepoMode::SinglePackage);
}
}
cli_args.command = Some(command);
cli_args.cwd = Some(repo_root.as_path().to_owned());

Expand Down
20 changes: 20 additions & 0 deletions turborepo-tests/integration/tests/pkg-inference.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Setup
$ . ${TESTDIR}/../../helpers/setup.sh
$ . ${TESTDIR}/_helpers/setup_monorepo.sh $(pwd)

# Run as if called by global turbo
$ TURBO_INVOCATION_DIR=$(pwd)/packages/util ${TURBO} build --skip-infer
\xe2\x80\xa2 Packages in scope: util (esc)
\xe2\x80\xa2 Running build in 1 packages (esc)
\xe2\x80\xa2 Remote caching disabled (esc)
util:build: cache miss, executing 76ab904c7ecb2d51
util:build:
util:build: > build
util:build: > echo 'building'
util:build:
util:build: building

Tasks: 1 successful, 1 total
Cached: 0 cached, 1 total
Time:\s*[\.0-9]+m?s (re)