internal: make it clearer where IO happens
This commit is contained in:
parent
398ae3e67f
commit
8f3335f5fb
@ -156,7 +156,7 @@ impl ChangeFixture {
|
|||||||
let crate_root = default_crate_root.unwrap();
|
let crate_root = default_crate_root.unwrap();
|
||||||
crate_graph.add_crate_root(
|
crate_graph.add_crate_root(
|
||||||
crate_root,
|
crate_root,
|
||||||
Edition::Edition2018,
|
Edition::CURRENT,
|
||||||
Some(CrateName::new("test").unwrap().into()),
|
Some(CrateName::new("test").unwrap().into()),
|
||||||
default_cfg.clone(),
|
default_cfg.clone(),
|
||||||
default_cfg,
|
default_cfg,
|
||||||
@ -227,10 +227,7 @@ impl From<Fixture> for FileMeta {
|
|||||||
krate: f.krate,
|
krate: f.krate,
|
||||||
deps: f.deps,
|
deps: f.deps,
|
||||||
cfg,
|
cfg,
|
||||||
edition: f
|
edition: f.edition.as_ref().map_or(Edition::CURRENT, |v| Edition::from_str(v).unwrap()),
|
||||||
.edition
|
|
||||||
.as_ref()
|
|
||||||
.map_or(Edition::Edition2018, |v| Edition::from_str(v).unwrap()),
|
|
||||||
env: f.env.into_iter().collect(),
|
env: f.env.into_iter().collect(),
|
||||||
introduce_new_source_root: f.introduce_new_source_root,
|
introduce_new_source_root: f.introduce_new_source_root,
|
||||||
}
|
}
|
||||||
|
@ -195,6 +195,10 @@ pub enum Edition {
|
|||||||
Edition2021,
|
Edition2021,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Edition {
|
||||||
|
pub const CURRENT: Edition = Edition::Edition2018;
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
#[derive(Default, Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct Env {
|
pub struct Env {
|
||||||
entries: FxHashMap<String, String>,
|
entries: FxHashMap<String, String>,
|
||||||
|
@ -217,7 +217,7 @@ impl Analysis {
|
|||||||
cfg_options.insert_atom("test".into());
|
cfg_options.insert_atom("test".into());
|
||||||
crate_graph.add_crate_root(
|
crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
Edition::Edition2018,
|
Edition::CURRENT,
|
||||||
None,
|
None,
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
cfg_options,
|
cfg_options,
|
||||||
|
@ -228,11 +228,11 @@ struct PackageMetadata {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl CargoWorkspace {
|
impl CargoWorkspace {
|
||||||
pub fn from_cargo_metadata(
|
pub fn fetch_metadata(
|
||||||
cargo_toml: &AbsPath,
|
cargo_toml: &AbsPath,
|
||||||
config: &CargoConfig,
|
config: &CargoConfig,
|
||||||
progress: &dyn Fn(String),
|
progress: &dyn Fn(String),
|
||||||
) -> Result<CargoWorkspace> {
|
) -> Result<cargo_metadata::Metadata> {
|
||||||
let mut meta = MetadataCommand::new();
|
let mut meta = MetadataCommand::new();
|
||||||
meta.cargo_path(toolchain::cargo());
|
meta.cargo_path(toolchain::cargo());
|
||||||
meta.manifest_path(cargo_toml.to_path_buf());
|
meta.manifest_path(cargo_toml.to_path_buf());
|
||||||
@ -262,10 +262,12 @@ impl CargoWorkspace {
|
|||||||
meta.other_options(vec![String::from("--filter-platform"), target]);
|
meta.other_options(vec![String::from("--filter-platform"), target]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Currently MetadataCommand is not based on parse_stream,
|
// FIXME: Fetching metadata is a slow process, as it might require
|
||||||
// So we just report it as a whole
|
// calling crates.io. We should be reporting progress here, but it's
|
||||||
|
// unclear whether cargo itself supports it.
|
||||||
progress("metadata".to_string());
|
progress("metadata".to_string());
|
||||||
let mut meta = meta.exec().with_context(|| {
|
|
||||||
|
let meta = meta.exec().with_context(|| {
|
||||||
let cwd: Option<AbsPathBuf> =
|
let cwd: Option<AbsPathBuf> =
|
||||||
std::env::current_dir().ok().and_then(|p| p.try_into().ok());
|
std::env::current_dir().ok().and_then(|p| p.try_into().ok());
|
||||||
|
|
||||||
@ -283,6 +285,14 @@ impl CargoWorkspace {
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
Ok(meta)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(
|
||||||
|
cargo_toml: &AbsPath,
|
||||||
|
config: &CargoConfig,
|
||||||
|
mut meta: cargo_metadata::Metadata,
|
||||||
|
) -> CargoWorkspace {
|
||||||
let mut pkg_by_id = FxHashMap::default();
|
let mut pkg_by_id = FxHashMap::default();
|
||||||
let mut packages = Arena::default();
|
let mut packages = Arena::default();
|
||||||
let mut targets = Arena::default();
|
let mut targets = Arena::default();
|
||||||
@ -296,9 +306,10 @@ impl CargoWorkspace {
|
|||||||
} = meta_pkg;
|
} = meta_pkg;
|
||||||
let meta = from_value::<PackageMetadata>(metadata.clone()).unwrap_or_default();
|
let meta = from_value::<PackageMetadata>(metadata.clone()).unwrap_or_default();
|
||||||
let is_member = ws_members.contains(id);
|
let is_member = ws_members.contains(id);
|
||||||
let edition = edition
|
let edition = edition.parse::<Edition>().unwrap_or_else(|err| {
|
||||||
.parse::<Edition>()
|
log::error!("Failed to parse edition {}", err);
|
||||||
.with_context(|| format!("Failed to parse edition {}", edition))?;
|
Edition::CURRENT
|
||||||
|
});
|
||||||
|
|
||||||
let pkg = packages.alloc(PackageData {
|
let pkg = packages.alloc(PackageData {
|
||||||
id: id.repr.clone(),
|
id: id.repr.clone(),
|
||||||
@ -366,7 +377,16 @@ impl CargoWorkspace {
|
|||||||
let build_data_config =
|
let build_data_config =
|
||||||
BuildDataConfig::new(cargo_toml.to_path_buf(), config.clone(), Arc::new(meta.packages));
|
BuildDataConfig::new(cargo_toml.to_path_buf(), config.clone(), Arc::new(meta.packages));
|
||||||
|
|
||||||
Ok(CargoWorkspace { packages, targets, workspace_root, build_data_config })
|
CargoWorkspace { packages, targets, workspace_root, build_data_config }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_cargo_metadata3(
|
||||||
|
cargo_toml: &AbsPath,
|
||||||
|
config: &CargoConfig,
|
||||||
|
progress: &dyn Fn(String),
|
||||||
|
) -> Result<CargoWorkspace> {
|
||||||
|
let meta = CargoWorkspace::fetch_metadata(cargo_toml, config, progress)?;
|
||||||
|
Ok(CargoWorkspace::new(cargo_toml, config, meta))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
|
pub fn packages<'a>(&'a self) -> impl Iterator<Item = Package> + ExactSizeIterator + 'a {
|
||||||
|
@ -126,7 +126,7 @@ impl ProjectWorkspace {
|
|||||||
cmd
|
cmd
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let cargo = CargoWorkspace::from_cargo_metadata(&cargo_toml, config, progress)
|
let meta = CargoWorkspace::fetch_metadata(&cargo_toml, config, progress)
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!(
|
format!(
|
||||||
"Failed to read Cargo metadata from Cargo.toml file {}, {}",
|
"Failed to read Cargo metadata from Cargo.toml file {}, {}",
|
||||||
@ -134,6 +134,7 @@ impl ProjectWorkspace {
|
|||||||
cargo_version
|
cargo_version
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
let cargo = CargoWorkspace::new(&cargo_toml, config, meta);
|
||||||
|
|
||||||
let sysroot = if config.no_sysroot {
|
let sysroot = if config.no_sysroot {
|
||||||
Sysroot::default()
|
Sysroot::default()
|
||||||
@ -156,15 +157,15 @@ impl ProjectWorkspace {
|
|||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let rustc = if let Some(rustc_dir) = rustc_dir {
|
let rustc = match rustc_dir {
|
||||||
Some(
|
Some(rustc_dir) => Some({
|
||||||
CargoWorkspace::from_cargo_metadata(&rustc_dir, config, progress)
|
let meta = CargoWorkspace::fetch_metadata(&rustc_dir, config, progress)
|
||||||
.with_context(|| {
|
.with_context(|| {
|
||||||
format!("Failed to read Cargo metadata for Rust sources")
|
format!("Failed to read Cargo metadata for Rust sources")
|
||||||
})?,
|
})?;
|
||||||
)
|
CargoWorkspace::new(&rustc_dir, config, meta)
|
||||||
} else {
|
}),
|
||||||
None
|
None => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref());
|
let rustc_cfg = rustc_cfg::get(Some(&cargo_toml), config.target.as_deref());
|
||||||
@ -595,7 +596,7 @@ fn detached_files_to_crate_graph(
|
|||||||
.map(|file_stem| CrateDisplayName::from_canonical_name(file_stem.to_string()));
|
.map(|file_stem| CrateDisplayName::from_canonical_name(file_stem.to_string()));
|
||||||
let detached_file_crate = crate_graph.add_crate_root(
|
let detached_file_crate = crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
Edition::Edition2018,
|
Edition::CURRENT,
|
||||||
display_name,
|
display_name,
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
@ -777,7 +778,7 @@ fn sysroot_to_crate_graph(
|
|||||||
let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
|
let display_name = CrateDisplayName::from_canonical_name(sysroot[krate].name.clone());
|
||||||
let crate_id = crate_graph.add_crate_root(
|
let crate_id = crate_graph.add_crate_root(
|
||||||
file_id,
|
file_id,
|
||||||
Edition::Edition2018,
|
Edition::CURRENT,
|
||||||
Some(display_name),
|
Some(display_name),
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
cfg_options.clone(),
|
cfg_options.clone(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user