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

Add autosync-before-run, --sync and --no-sync arguments to rye test and rye run (OneOffLock sync mode) #1006

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ __pycache__
.idea
token.txt
dist
**/*.rs.pending-snap
5 changes: 5 additions & 0 deletions docs/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ use-uv = true
# to `true` when uv is enabled and `false` otherwise.
autosync = true

# Enable or disable automatic `sync` ahead of `run` and `test`. This defaults
# to `true` when uv is enabled and `false` otherwise. Note that autosync invoked
# before `run` and `test` will never update your lockfiles.
autosync-before-run = true

# Marks the managed .venv in a way that cloud-based synchronization systems
# like Dropbox and iCloud Files will not upload it. This defaults to `true`
# as a .venv in cloud storage typically does not make sense. Set this to
Expand Down
3 changes: 2 additions & 1 deletion rye/src/cli/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::consts::VENV_BIN;
use crate::lock::KeyringProvider;
use crate::pyproject::{BuildSystem, DependencyKind, ExpandedSources, PyProject};
use crate::sources::py::PythonVersion;
use crate::sync::{autosync, sync, SyncOptions};
use crate::sync::{autosync, sync, SyncOptions, SyncMode};
use crate::utils::{format_requirement, get_venv_python_bin, set_proxy_variables, CommandOutput};
use crate::uv::UvBuilder;

Expand Down Expand Up @@ -322,6 +322,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
autosync(
&pyproject_toml,
output,
SyncMode::Regular,
cmd.pre,
cmd.with_sources,
cmd.generate_hashes,
Expand Down
3 changes: 2 additions & 1 deletion rye/src/cli/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use pep508_rs::Requirement;
use crate::config::Config;
use crate::lock::KeyringProvider;
use crate::pyproject::{DependencyKind, PyProject};
use crate::sync::autosync;
use crate::sync::{autosync, SyncMode};
use crate::utils::{format_requirement, CommandOutput};

/// Removes a package from this project.
Expand Down Expand Up @@ -82,6 +82,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
autosync(
&pyproject_toml,
output,
SyncMode::Regular,
cmd.pre,
cmd.with_sources,
cmd.generate_hashes,
Expand Down
28 changes: 23 additions & 5 deletions rye/src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use anyhow::{bail, Context, Error};
use clap::Parser;
use console::style;

use crate::config::Config;
use crate::pyproject::{PyProject, Script};
use crate::sync::{sync, SyncOptions};
use crate::sync::{autosync, sync, SyncOptions, SyncMode};
use crate::tui::redirect_to_stderr;
use crate::utils::{exec_spawn, get_venv_python_bin, success_status, IoPathContext};
use crate::utils::{exec_spawn, get_venv_python_bin, success_status, CommandOutput, IoPathContext};

/// Runs a command installed into this package.
#[derive(Parser, Debug)]
Expand All @@ -26,6 +27,18 @@ pub struct Args {
/// Use this pyproject.toml file
#[arg(long, value_name = "PYPROJECT_TOML")]
pyproject: Option<PathBuf>,
/// Runs `sync` even if auto-sync is disabled.
#[arg(long)]
sync: bool,
/// Does not run `sync` even if auto-sync is enabled.
#[arg(long, conflicts_with = "sync")]
no_sync: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
/// Turns off all output.
#[arg(short, long, conflicts_with = "verbose")]
quiet: bool,
}

#[derive(Parser, Debug)]
Expand All @@ -36,11 +49,16 @@ enum Cmd {

pub fn execute(cmd: Args) -> Result<(), Error> {
let _guard = redirect_to_stderr(true);
let output = CommandOutput::from_quiet_and_verbose(cmd.quiet, cmd.verbose);
let pyproject = PyProject::load_or_discover(cmd.pyproject.as_deref())?;

// make sure we have the minimal virtualenv.
sync(SyncOptions::python_only().pyproject(cmd.pyproject))
.context("failed to sync ahead of run")?;
if (Config::current().autosync_before_run() && !cmd.no_sync) || cmd.sync {
autosync(&pyproject, output, SyncMode::OneOffLock)?;
} else {
// make sure we have the minimal virtualenv.
sync(SyncOptions::python_only().pyproject(cmd.pyproject))
.context("failed to sync ahead of run")?;
}

if cmd.list || cmd.cmd.is_none() {
return list_scripts(&pyproject);
Expand Down
9 changes: 8 additions & 1 deletion rye/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::config::Config;
use crate::consts::VENV_BIN;
use crate::lock::KeyringProvider;
use crate::pyproject::{locate_projects, normalize_package_name, DependencyKind, PyProject};
use crate::sync::autosync;
use crate::sync::{autosync, SyncMode};
use crate::utils::{CommandOutput, QuietExit};

/// Run the tests on the project.
Expand All @@ -32,6 +32,12 @@ pub struct Args {
/// Disable test output capture to stdout.
#[arg(long = "no-capture", short = 's')]
no_capture: bool,
/// Runs `sync` even if auto-sync is disabled.
#[arg(long)]
sync: bool,
/// Does not run `sync` even if auto-sync is enabled.
#[arg(long, conflicts_with = "sync")]
no_sync: bool,
/// Enables verbose diagnostics.
#[arg(short, long)]
verbose: bool,
Expand Down Expand Up @@ -90,6 +96,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
autosync(
&projects[0],
output,
SyncMode::OneOffLock,
cmd.pre,
cmd.with_sources,
cmd.generate_hashes,
Expand Down
11 changes: 10 additions & 1 deletion rye/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,23 @@ impl Config {
Ok(rv)
}

/// Enable autosync.
/// Enable autosync for `add` and `remove`.
pub fn autosync(&self) -> bool {
self.doc
.get("behavior")
.and_then(|x| x.get("autosync"))
.and_then(|x| x.as_bool())
.unwrap_or_else(|| self.use_uv())
}

/// Enable autosync for `run` and `test`.
pub fn autosync_before_run(&self) -> bool {
self.doc
.get("behavior")
.and_then(|x| x.get("autosync-before-run"))
.and_then(|x| x.as_bool())
.unwrap_or_else(|| self.use_uv())
}

/// Indicates if uv should be used instead of pip-tools.
pub fn use_uv(&self) -> bool {
Expand Down
14 changes: 10 additions & 4 deletions rye/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum SyncMode {
Regular,
/// recreate everything
Full,
/// Recreate if no lock file present, otherwise install without updating
OneOffLock,
}

/// Updates the virtualenv based on the pyproject.toml
Expand Down Expand Up @@ -179,16 +181,19 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
// hack to make this work for now. We basically sym-link pip itself
// into a folder all by itself and place a second file in there which we
// can pass to pip-sync to install the local package.
let target_lockfile = if cmd.dev { &dev_lockfile } else { &lockfile };
let has_lock = target_lockfile.is_file();
if recreate || cmd.mode != SyncMode::PythonOnly {
let sources = ExpandedSources::from_sources(&pyproject.sources()?)?;
if cmd.no_lock {
let lockfile = if cmd.dev { &dev_lockfile } else { &lockfile };
if !lockfile.is_file() {
if !has_lock {
bail!(
"Locking is disabled but lockfile '{}' does not exist",
lockfile.display()
target_lockfile.display()
);
}
} else if cmd.mode == SyncMode::OneOffLock && has_lock {
// do nothing
} else if let Some(workspace) = pyproject.workspace() {
// make sure we have an up-to-date lockfile
update_workspace_lockfile(
Expand Down Expand Up @@ -319,6 +324,7 @@ pub fn sync(mut cmd: SyncOptions) -> Result<(), Error> {
pub fn autosync(
pyproject: &PyProject,
output: CommandOutput,
sync_mode: SyncMode,
pre: bool,
with_sources: bool,
generate_hashes: bool,
Expand All @@ -327,7 +333,7 @@ pub fn autosync(
sync(SyncOptions {
output,
dev: true,
mode: SyncMode::Regular,
mode: sync_mode,
force: false,
no_lock: false,
lock_options: LockOptions {
Expand Down
4 changes: 4 additions & 0 deletions rye/tests/test_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,9 @@ fn test_dotenv() {
42 23

----- stderr -----
Reusing already existing virtualenv
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);
}
16 changes: 16 additions & 0 deletions rye/tests/test_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ fn test_init_lib() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

assert!(
Expand Down Expand Up @@ -91,6 +95,10 @@ fn test_init_default() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

assert!(
Expand Down Expand Up @@ -141,6 +149,10 @@ fn test_init_script() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);

rye_cmd_snapshot!(space.rye_cmd().arg("run").arg("python").arg("-mmy_project"), @r###"
Expand All @@ -150,6 +162,10 @@ fn test_init_script() {
Hello from my-project!

----- stderr -----
Reusing already existing virtualenv
Installing dependencies
Audited 1 package in [EXECUTION_TIME]
Done!
"###);
}

Expand Down
Loading