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 dependency resolution for path dependencies #3398

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@

### Bug Fixes

- Fixes a bug with path dependencies. If a package was added to a path dependency,
it would not be added to the dependant's `manifest.toml`, causing the build to fail.
([Juraj Petráš](https://github.com/Hackder))

## v1.3.2 - 2024-07-11

### Language Server
Expand Down
86 changes: 73 additions & 13 deletions compiler-cli/src/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use flate2::read::GzDecoder;
use futures::future;
use gleam_core::{
build::{Mode, Target, Telemetry},
config::PackageConfig,
config::{Dependencies, PackageConfig},
dependency,
error::{FileIoAction, FileKind, StandardIoAction},
hex::{self, HEXPM_PUBLIC_KEY},
Expand Down Expand Up @@ -626,13 +626,11 @@ fn get_manifest<Telem: Telemetry>(

let manifest = read_manifest_from_disc(paths)?;

let all_dependencies = recursively_collect_dependencies_from_path_deps(config, paths)?;

// If the config has unchanged since the manifest was written then it is up
// to date so we can return it unmodified.
if is_same_requirements(
&manifest.requirements,
&config.all_dependencies()?,
paths.root(),
)? {
if is_same_requirements(&manifest.requirements, &all_dependencies, paths.root())? {
tracing::debug!("manifest_up_to_date");
Ok((false, manifest))
} else {
Expand All @@ -642,6 +640,69 @@ fn get_manifest<Telem: Telemetry>(
}
}

fn recursively_collect_dependencies_from_path_deps(
config: &PackageConfig,
paths: &ProjectPaths,
) -> Result<Dependencies> {
let mut acc = HashMap::new();
let mut current_path = vec![(config.name.clone(), paths.root().into())];

do_recursively_collect_dependencies_from_path_deps(
&config.dependencies,
paths.root(),
&mut current_path,
&mut acc,
)?;

Ok(acc)
}

fn do_recursively_collect_dependencies_from_path_deps(
deps: &Dependencies,
root_path: &Utf8Path,
current_path: &mut Vec<(EcoString, Utf8PathBuf)>,
acc: &mut Dependencies,
) -> Result<()> {
acc.reserve(deps.len());
for (name, requirement) in deps.iter() {
match requirement {
Requirement::Path { path } => {
let new_path = fs::canonicalise(&root_path.join(path))?;
let paths = ProjectPaths::new(new_path);
let config = crate::config::read(paths.root_config())?;

let _ = acc.insert(name.clone(), Requirement::Path { path: path.clone() });

// If we have already seen this package in the current recursive
// path, then we have a cycle
if current_path.iter().any(|(_, p)| p == paths.root()) {
tracing::debug!("found_cycle_in_path_dep_collecting");
return Err(Error::PackageCycle {
packages: current_path.iter().map(|(n, _)| n.clone()).collect(),
});
}

current_path.push((config.name.clone(), paths.root().into()));

tracing::debug!(package=%config.name, path=%path, "recursively_collecting_dependency");
do_recursively_collect_dependencies_from_path_deps(
&config.dependencies,
&paths.root(),
current_path,
acc,
)?;

let _ = current_path.pop().expect("must have at least one element");
}
_ => {
let _ = acc.insert(name.clone(), requirement.clone());
}
}
}

Ok(())
}

fn is_same_requirements(
requirements1: &HashMap<EcoString, Requirement>,
requirements2: &HashMap<EcoString, Requirement>,
Expand All @@ -665,17 +726,16 @@ fn same_requirements(
requirement2: Option<&Requirement>,
root_path: &Utf8Path,
) -> Result<bool> {
let (left, right) = match (requirement1, requirement2) {
match (requirement1, requirement2) {
(Requirement::Path { path: path1 }, Some(Requirement::Path { path: path2 })) => {
let left = fs::canonicalise(&root_path.join(path1))?;
let right = fs::canonicalise(&root_path.join(path2))?;
(left, right)
}
(_, Some(requirement2)) => return Ok(requirement1 == requirement2),
(_, None) => return Ok(false),
};

Ok(left == right)
Ok(left == right)
}
(_, Some(requirement2)) => Ok(requirement1 == requirement2),
(_, None) => Ok(false),
}
}

#[derive(Clone, Eq, PartialEq, Debug)]
Expand Down