Skip to content

Commit

Permalink
Add --locked flag for several subcommands (#133)
Browse files Browse the repository at this point in the history
* Add `--locked` flag for `build` and `check` subcommands

* Update `Projects.load` to replace boolean arg with enum for clarity

* Update `project` module to use an enum instead of a bool for project locking

* Update `Projects` to take `ProjectLocking` arg when loading a project

* Add `--locked` flag to `install` and `run` subcommands too
  • Loading branch information
kylewlacy authored Sep 29, 2024
1 parent 8236cf1 commit 441ff4c
Show file tree
Hide file tree
Showing 12 changed files with 443 additions and 203 deletions.
346 changes: 193 additions & 153 deletions crates/brioche-core/src/project.rs

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions crates/brioche-core/src/script/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
blob::BlobHash,
project::{
analyze::{GitRefOptions, StaticInclude, StaticOutput, StaticOutputKind, StaticQuery},
ProjectHash, Projects,
ProjectHash, ProjectLocking, ProjectValidation, Projects,
},
recipe::{Artifact, DownloadRecipe, Recipe, WithMeta},
Brioche,
Expand Down Expand Up @@ -41,8 +41,14 @@ impl RuntimeBridge {
tokio::spawn(async move {
match message {
RuntimeBridgeMessage::LoadProjectFromModulePath { path, result_tx } => {
let result =
projects.load_from_module_path(&brioche, &path, false).await;
let result = projects
.load_from_module_path(
&brioche,
&path,
ProjectValidation::Minimal,
ProjectLocking::Unlocked,
)
.await;
let _ = result_tx.send(result);
}
RuntimeBridgeMessage::LoadSpecifierContents {
Expand Down Expand Up @@ -124,7 +130,12 @@ impl RuntimeBridge {
}

let result = projects
.load_from_module_path(&brioche, &path, false)
.load_from_module_path(
&brioche,
&path,
ProjectValidation::Minimal,
ProjectLocking::Unlocked,
)
.await
.map(|_| true);
let _ = result_tx.send(result);
Expand Down
18 changes: 13 additions & 5 deletions crates/brioche-core/src/script/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use tower_lsp::lsp_types::request::GotoTypeDefinitionResponse;
use tower_lsp::lsp_types::*;
use tower_lsp::{Client, LanguageServer};

use crate::project::Projects;
use crate::project::{ProjectLocking, ProjectValidation, Projects};
use crate::script::compiler_host::{brioche_compiler_host, BriocheCompilerHost};
use crate::script::format::format_code;
use crate::{Brioche, BriocheBuilder};
Expand Down Expand Up @@ -501,10 +501,18 @@ async fn try_update_lockfile_for_module(
.context("failed to build `Brioche` instance")?;
let projects = Projects::default();

tokio::time::timeout(load_timeout, projects.load(&brioche, &project_path, false))
.await
.context("timed out trying to load project")?
.context("failed to load project")?;
tokio::time::timeout(
load_timeout,
projects.load(
&brioche,
&project_path,
ProjectValidation::Minimal,
ProjectLocking::Unlocked,
),
)
.await
.context("timed out trying to load project")?
.context("failed to load project")?;

let updated = projects
.commit_dirty_lockfile_for_project_path(&project_path)
Expand Down
15 changes: 13 additions & 2 deletions crates/brioche-core/tests/project_load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use assert_matches::assert_matches;
use brioche_core::project::{ProjectLocking, ProjectValidation};

#[tokio::test]
async fn test_project_load_simple() -> anyhow::Result<()> {
Expand Down Expand Up @@ -792,11 +793,21 @@ async fn test_project_load_with_remote_workspace_registry_dep() -> anyhow::Resul

let projects = brioche_core::project::Projects::default();
let bar_hash = projects
.load(&brioche, &bar_dir, true)
.load(
&brioche,
&bar_dir,
ProjectValidation::Standard,
ProjectLocking::Unlocked,
)
.await
.expect("failed to load bar project");
let foo_hash = projects
.load(&brioche, &foo_dir, true)
.load(
&brioche,
&foo_dir,
ProjectValidation::Standard,
ProjectLocking::Unlocked,
)
.await
.expect("failed to load foo project");

Expand Down
27 changes: 23 additions & 4 deletions crates/brioche-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{

use brioche_core::{
blob::{BlobHash, SaveBlobOptions},
project::{self, ProjectHash, Projects},
project::{self, ProjectHash, ProjectLocking, ProjectValidation, Projects},
recipe::{
CreateDirectory, Directory, File, ProcessRecipe, ProcessTemplate, ProcessTemplateComponent,
Recipe, WithMeta,
Expand Down Expand Up @@ -60,7 +60,14 @@ pub async fn load_project(
path: &Path,
) -> anyhow::Result<(Projects, ProjectHash)> {
let projects = Projects::default();
let project_hash = projects.load(brioche, path, true).await?;
let project_hash = projects
.load(
brioche,
path,
ProjectValidation::Standard,
ProjectLocking::Unlocked,
)
.await?;

Ok((projects, project_hash))
}
Expand All @@ -70,7 +77,14 @@ pub async fn load_project_no_validate(
path: &Path,
) -> anyhow::Result<(Projects, ProjectHash)> {
let projects = Projects::default();
let project_hash = projects.load(brioche, path, false).await?;
let project_hash = projects
.load(
brioche,
path,
ProjectValidation::Minimal,
ProjectLocking::Unlocked,
)
.await?;

Ok((projects, project_hash))
}
Expand Down Expand Up @@ -402,7 +416,12 @@ impl TestContext {

let projects = Projects::default();
let project_hash = projects
.load(&self.brioche, &temp_project_path, true)
.load(
&self.brioche,
&temp_project_path,
ProjectValidation::Standard,
ProjectLocking::Unlocked,
)
.await
.expect("failed to load temp project");
projects.commit_dirty_lockfiles().await.unwrap();
Expand Down
28 changes: 22 additions & 6 deletions crates/brioche/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{path::PathBuf, process::ExitCode};

use anyhow::Context as _;
use brioche_core::{fs_utils, reporter::ConsoleReporterKind};
use brioche_core::{fs_utils, project::ProjectLocking, reporter::ConsoleReporterKind};
use clap::Parser;
use human_repr::HumanDuration;
use tracing::Instrument;
Expand All @@ -24,6 +24,10 @@ pub struct BuildArgs {
#[arg(long)]
check: bool,

/// Validate that the lockfile is up-to-date
#[arg(long)]
locked: bool,

/// Replace the output path if it already exists
#[arg(long)]
replace: bool,
Expand Down Expand Up @@ -53,12 +57,24 @@ pub async fn build(args: BuildArgs) -> anyhow::Result<ExitCode> {
.await?;
let projects = brioche_core::project::Projects::default();

let build_future = async {
let project_hash = super::load_project(&brioche, &projects, &args.project).await?;
let locking = if args.locked {
ProjectLocking::Locked
} else {
ProjectLocking::Unlocked
};

let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?;
if num_lockfiles_updated > 0 {
tracing::info!(num_lockfiles_updated, "updated lockfiles");
let build_future = async {
let project_hash = super::load_project(&brioche, &projects, &args.project, locking).await?;

// If the `--locked` flag is used, validate that all lockfiles are
// up-to-date. Otherwise, write any out-of-date lockfiles
if args.locked {
projects.validate_no_dirty_lockfiles()?;
} else {
let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?;
if num_lockfiles_updated > 0 {
tracing::info!(num_lockfiles_updated, "updated lockfiles");
}
}

if args.check {
Expand Down
63 changes: 55 additions & 8 deletions crates/brioche/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::path::PathBuf;
use std::process::ExitCode;

use brioche_core::project::ProjectHash;
use brioche_core::project::ProjectLocking;
use brioche_core::project::ProjectValidation;
use brioche_core::project::Projects;
use brioche_core::reporter::ConsoleReporterKind;
use brioche_core::reporter::Reporter;
Expand All @@ -13,6 +15,10 @@ use crate::consolidate_result;

#[derive(Debug, Parser)]
pub struct CheckArgs {
/// Validate that the lockfile is up-to-date
#[arg(long)]
locked: bool,

#[command(flatten)]
project: super::MultipleProjectArgs,
}
Expand All @@ -26,6 +32,14 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<ExitCode> {
.await?;
let projects = brioche_core::project::Projects::default();

let check_options = CheckOptions {
locked: args.locked,
};
let locking = if args.locked {
ProjectLocking::Locked
} else {
ProjectLocking::Unlocked
};
let mut error_result = Option::None;

// Handle the case where no projects and no registries are specified
Expand All @@ -40,10 +54,25 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<ExitCode> {
for project_path in projects_path {
let project_name = format!("project '{name}'", name = project_path.display());

match projects.load(&brioche, &project_path, true).await {
match projects
.load(
&brioche,
&project_path,
ProjectValidation::Standard,
locking,
)
.await
{
Ok(project_hash) => {
let result =
run_check(&reporter, &brioche, &projects, project_hash, &project_name).await;
let result = run_check(
&reporter,
&brioche,
&projects,
project_hash,
&project_name,
&check_options,
)
.await;
consolidate_result(&reporter, &project_name, result, &mut error_result);
}
Err(e) => {
Expand All @@ -65,8 +94,15 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<ExitCode> {
.await
{
Ok(project_hash) => {
let result =
run_check(&reporter, &brioche, &projects, project_hash, &project_name).await;
let result = run_check(
&reporter,
&brioche,
&projects,
project_hash,
&project_name,
&check_options,
)
.await;
consolidate_result(&reporter, &project_name, result, &mut error_result);
}
Err(e) => {
Expand All @@ -86,17 +122,28 @@ pub async fn check(args: CheckArgs) -> anyhow::Result<ExitCode> {
Ok(exit_code)
}

struct CheckOptions {
locked: bool,
}

async fn run_check(
reporter: &Reporter,
brioche: &Brioche,
projects: &Projects,
project_hash: ProjectHash,
project_name: &String,
options: &CheckOptions,
) -> Result<bool, anyhow::Error> {
let result = async {
let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?;
if num_lockfiles_updated > 0 {
tracing::info!(num_lockfiles_updated, "updated lockfiles");
// If the `--locked` flag is used, validate that all lockfiles are
// up-to-date. Otherwise, write any out-of-date lockfiles
if options.locked {
projects.validate_no_dirty_lockfiles()?;
} else {
let num_lockfiles_updated = projects.commit_dirty_lockfiles().await?;
if num_lockfiles_updated > 0 {
tracing::info!(num_lockfiles_updated, "updated lockfiles");
}
}

brioche_core::script::check::check(brioche, projects, project_hash).await
Expand Down
12 changes: 10 additions & 2 deletions crates/brioche/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{path::PathBuf, process::ExitCode};

use brioche_core::{
project::{ProjectHash, Projects},
project::{ProjectHash, ProjectLocking, ProjectValidation, Projects},
reporter::{ConsoleReporterKind, Reporter},
};
use clap::Parser;
Expand Down Expand Up @@ -35,7 +35,15 @@ pub async fn format(args: FormatArgs) -> anyhow::Result<ExitCode> {
for project_path in args.project {
let project_name = format!("project '{name}'", name = project_path.display());

match projects.load(&brioche, &project_path, true).await {
match projects
.load(
&brioche,
&project_path,
ProjectValidation::Standard,
ProjectLocking::Unlocked,
)
.await
{
Ok(project_hash) => {
let result = run_format(
&reporter,
Expand Down
Loading

0 comments on commit 441ff4c

Please sign in to comment.