Skip to content

Commit

Permalink
Auto merge of #17110 - Veykril:cargo-script-mvp, r=Veykril
Browse files Browse the repository at this point in the history
Cargo script mvp

Based on #15456,

As the original PR stated, detached files are still horrendous to work with.
  • Loading branch information
bors committed Apr 19, 2024
2 parents 8621e79 + 0ce7179 commit c83d8cf
Show file tree
Hide file tree
Showing 10 changed files with 376 additions and 143 deletions.
2 changes: 1 addition & 1 deletion crates/load-cargo/src/lib.rs
Expand Up @@ -335,7 +335,7 @@ fn load_crate_graph(
) -> RootDatabase {
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
| ProjectWorkspace::Json { toolchain, target_layout, .. }
| ProjectWorkspace::DetachedFiles { toolchain, target_layout, .. }) = ws;
| ProjectWorkspace::DetachedFile { toolchain, target_layout, .. }) = ws;

let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
let mut db = RootDatabase::new(lru_cap);
Expand Down
24 changes: 21 additions & 3 deletions crates/project-model/src/cargo_workspace.rs
Expand Up @@ -305,6 +305,12 @@ impl CargoWorkspace {
.collect(),
);
}
// The manifest is a rust file, so this means its a script manifest
if cargo_toml.extension().is_some_and(|ext| ext == "rs") {
// Deliberately don't set up RUSTC_BOOTSTRAP or a nightly override here, the user should
// opt into it themselves.
other_options.push("-Zscript".to_owned());
}
meta.other_options(other_options);

// FIXME: Fetching metadata is a slow process, as it might require
Expand Down Expand Up @@ -373,11 +379,12 @@ impl CargoWorkspace {
let is_local = source.is_none();
let is_member = ws_members.contains(&id);

let manifest = AbsPathBuf::assert(manifest_path);
let pkg = packages.alloc(PackageData {
id: id.repr.clone(),
name,
version,
manifest: AbsPathBuf::assert(manifest_path).try_into().unwrap(),
manifest: manifest.clone().try_into().unwrap(),
targets: Vec::new(),
is_local,
is_member,
Expand All @@ -400,11 +407,22 @@ impl CargoWorkspace {
for meta_tgt in meta_targets {
let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
meta_tgt;
let kind = TargetKind::new(&kind);
let tgt = targets.alloc(TargetData {
package: pkg,
name,
root: AbsPathBuf::assert(src_path),
kind: TargetKind::new(&kind),
root: if kind == TargetKind::Bin
&& manifest.extension().is_some_and(|ext| ext == "rs")
{
// cargo strips the script part of a cargo script away and places the
// modified manifest file into a special target dir which is then used as
// the source path. We don't want that, we want the original here so map it
// back
manifest.clone()
} else {
AbsPathBuf::assert(src_path)
},
kind,
required_features,
});
pkg_data.targets.push(tgt);
Expand Down

0 comments on commit c83d8cf

Please sign in to comment.