Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ on:

env:
RUST_BACKTRACE: full
SCCACHE_GHA_ENABLED: "true"
SCCACHE_GHA_ENABLED: "false"
RUSTC_WRAPPER: "sccache"
CARGO_INCREMENTAL: 0

Expand Down
35 changes: 34 additions & 1 deletion crates/cargo-lambda-metadata/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::{ArgAction, Args, ValueHint};
use env_file_reader::read_file;
use miette::Result;
use serde::{Deserialize, Serialize, ser::SerializeStruct};
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, env::VarError, path::PathBuf};

use crate::{cargo::deserialize_vec_or_map, error::MetadataError};

Expand Down Expand Up @@ -107,6 +107,39 @@ fn extract_var(line: &str) -> Result<(&str, &str), MetadataError> {
Ok((key, value))
}

pub trait EnvVarExtractor {
fn var(&self, name: &str) -> Result<String, VarError>;
}

pub struct SystemEnvExtractor;

impl EnvVarExtractor for SystemEnvExtractor {
fn var(&self, name: &str) -> Result<String, VarError> {
std::env::var(name)
}
}

pub struct HashMapEnvExtractor {
env: HashMap<String, String>,
}

impl From<Vec<(&str, &str)>> for HashMapEnvExtractor {
fn from(env: Vec<(&str, &str)>) -> Self {
Self {
env: env
.into_iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect(),
}
}
}

impl EnvVarExtractor for HashMapEnvExtractor {
fn var(&self, name: &str) -> Result<String, VarError> {
self.env.get(name).cloned().ok_or(VarError::NotPresent)
}
}

#[cfg(test)]
mod test {
use std::env::temp_dir;
Expand Down
3 changes: 2 additions & 1 deletion crates/cargo-lambda-watch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cargo_lambda_metadata::{
CargoMetadata, CargoPackage, filter_binary_targets_from_metadata, kind_bin_filter,
selected_bin_filter, watch::Watch,
},
env::SystemEnvExtractor,
lambda::Timeout,
};
use cargo_lambda_remote::tls::TlsOptions;
Expand Down Expand Up @@ -83,7 +84,7 @@ pub async fn run(
}

let base = dunce::canonicalize(".").into_diagnostic()?;
let ignore_files = watcher::ignore::discover_files(&base).await;
let ignore_files = watcher::ignore::discover_files(&base, SystemEnvExtractor).await;

let env = config.lambda_environment(base_env).into_diagnostic()?;

Expand Down
119 changes: 110 additions & 9 deletions crates/cargo-lambda-watch/src/watcher/ignore.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
use std::{collections::HashSet, path::Path, sync::Arc};
use std::{
collections::HashSet,
path::{Path, PathBuf},
sync::Arc,
};

use cargo_lambda_metadata::env::EnvVarExtractor;
use ignore::Match;
use ignore_files::{IgnoreFile, IgnoreFilter};
use tracing::{debug, trace, trace_span};
Expand All @@ -11,18 +16,78 @@ use watchexec::{

use crate::error::ServerError;

/// we discover ignore files from the `CARGO_LAMBDA_IGNORE_FILES` environment variable,
/// the current directory, and any parent directories that represent project roots
pub(crate) async fn discover_files(base: &Path) -> Vec<IgnoreFile> {
/// Collect ignore files from various sources:
/// - Files in the system using the keywords `CARGO_LAMBDA` and `cargo-lambda`:
/// - $HOME/.cargo-lambda/ignore
/// - $XDG_CONFIG_HOME/cargo-lambda/ignore
/// - $HOME/.CARGO_LAMBDA/ignore
/// - $XDG_CONFIG_HOME/CARGO_LAMBDA/ignore
///
/// - Origin-based ignore files (like `.gitignore`)
/// - Project-specific `.cargolambdaignore` file
/// - Custom ignore file specified via `CARGO_LAMBDA_IGNORE_FILE` environment variable
///
/// # Arguments
///
/// * `base` - The base path to start searching for ignore files from
///
/// # Returns
///
/// A vector of [`IgnoreFile`]s discovered from all sources
pub(crate) async fn discover_files(base: &Path, env: impl EnvVarExtractor) -> Vec<IgnoreFile> {
let mut ignore_files = HashSet::new();

let (env_ignore, env_ignore_errs) = ignore_files::from_environment(Some("CARGO_LAMBDA")).await;
trace!(ignore_files = ?env_ignore, errors = ?env_ignore_errs, "discovered ignore files from environment variable");
ignore_files.extend(env_ignore);
if !env_ignore.is_empty() {
trace!(ignore_files = ?env_ignore, errors = ?env_ignore_errs, "discovered ignore files from environment variable");
ignore_files.extend(env_ignore);
}

let (env_ignore, env_ignore_errs) = ignore_files::from_environment(Some("cargo-lambda")).await;
if !env_ignore.is_empty() {
trace!(ignore_files = ?env_ignore, errors = ?env_ignore_errs, "discovered ignore files from environment variable");
ignore_files.extend(env_ignore);
}

let (origin_ignore, origin_ignore_errs) = ignore_files::from_origin(base).await;
trace!(ignore_files = ?origin_ignore, errors = ?origin_ignore_errs, "discovered ignore files from origin");
ignore_files.extend(origin_ignore);
if !origin_ignore.is_empty() {
trace!(ignore_files = ?origin_ignore, errors = ?origin_ignore_errs, "discovered ignore files from origin");
ignore_files.extend(origin_ignore);
}

let mut ignore_files_vec = Vec::new();
let mut ignore_files_vec_errs = Vec::new();

let ignore_repo_rules_file = base.join(".cargolambdaignore");
if ignore_repo_rules_file.is_file() {
ignore_files::discover_file(
&mut ignore_files_vec,
&mut ignore_files_vec_errs,
None,
None,
ignore_repo_rules_file,
)
.await;
}

if let Ok(ignore_env_file) = env.var("CARGO_LAMBDA_IGNORE_FILE") {
let path = PathBuf::from(ignore_env_file);
if path.is_file() {
ignore_files::discover_file(
&mut ignore_files_vec,
&mut ignore_files_vec_errs,
None,
None,
path,
)
.await;
}
}

if !ignore_files_vec.is_empty() {
trace!(ignore_files = ?ignore_files_vec, errors = ?ignore_files_vec_errs, "discovered ignore files");
ignore_files.extend(ignore_files_vec);
}

let mut origins = HashSet::new();
let mut current = base;
Expand Down Expand Up @@ -153,8 +218,9 @@ impl Filterer for IgnoreFilterer {

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

use cargo_lambda_metadata::env::{HashMapEnvExtractor, SystemEnvExtractor};
use watchexec::event::Tag;

use super::*;
Expand Down Expand Up @@ -260,4 +326,39 @@ mod tests {
};
assert!(!filter.check_event(&event, Priority::Normal).unwrap());
}

#[tokio::test]
async fn test_discover_project_specific_files() {
let tempdir = tempfile::tempdir().unwrap();
let ignore_file = tempdir.path().join(".cargolambdaignore");
writeln!(File::create(&ignore_file).unwrap(), "*").unwrap();

let files = discover_files(tempdir.path(), SystemEnvExtractor).await;
assert_eq!(files.len(), 1);
assert_eq!(files[0].path, ignore_file);
}

#[tokio::test]
async fn test_discover_project_specific_files_with_env_var() {
let tempdir = tempfile::tempdir().unwrap();
let ignore_file = tempdir.path().join("clignore");
writeln!(File::create(&ignore_file).unwrap(), "*").unwrap();

let env = HashMapEnvExtractor::from(vec![(
"CARGO_LAMBDA_IGNORE_FILE",
ignore_file.to_str().unwrap(),
)]);

let files = discover_files(tempdir.path(), env).await;
assert_eq!(files.len(), 1);
assert_eq!(files[0].path, ignore_file);
}

#[tokio::test]
async fn test_discover_project_specific_files_with_env_var_not_found() {
let tempdir = tempfile::tempdir().unwrap();
let env = HashMapEnvExtractor::from(vec![]);
let files = discover_files(tempdir.path(), env).await;
assert_eq!(files.len(), 0);
}
}
31 changes: 31 additions & 0 deletions docs/commands/watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,34 @@ This configuration is applied to a function in a package. It will be merged with
[package.metadata.lambda.watch.router]
"/products" = "handle-products"
```

## Ignore files from hot reloading

Cargo Lambda supports ignore files and directories to avoid hot reloading when certain files are modified. This is useful to avoid unnecessary recompilations when the files are not relevant to the function.

The ignore files are discovered from the following sources:

- Git ignore rules (`.gitignore`)
- Files in the system using the keywords `CARGO_LAMBDA` and `cargo-lambda`:
- `$HOME/.cargo-lambda/ignore`
- `$XDG_CONFIG_HOME/cargo-lambda/ignore`
- `$APPDATA/cargo-lambda/ignore`
- `$USERPROFILE/.cargo-lambda/ignore`

- `$HOME/.CARGO_LAMBDA/ignore`
- `$XDG_CONFIG_HOME/CARGO_LAMBDA/ignore`
- `$APPDATA/CARGO_LAMBDA/ignore`
- `$USERPROFILE/.CARGO_LAMBDA/ignore`
- A file named `.cargolambdaignore` in the root of the project.
- A file specified in the `CARGO_LAMBDA_IGNORE_FILE` environment variable.

The ignore files are merged together and used to create glob patterns that are used to match the files that will be ignored.

The syntax of the ignore files is the same as the one used by [Git](https://git-scm.com/docs/gitignore).

```
*.rs
*.toml
*.lock
static/**
```