Skip to content

Commit c83d8cf

Browse files
committed
Auto merge of rust-lang#17110 - Veykril:cargo-script-mvp, r=Veykril
Cargo script mvp Based on rust-lang/rust-analyzer#15456, As the original PR stated, detached files are still horrendous to work with.
2 parents 8621e79 + 0ce7179 commit c83d8cf

File tree

10 files changed

+376
-143
lines changed

10 files changed

+376
-143
lines changed

crates/load-cargo/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ fn load_crate_graph(
335335
) -> RootDatabase {
336336
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
337337
| ProjectWorkspace::Json { toolchain, target_layout, .. }
338-
| ProjectWorkspace::DetachedFiles { toolchain, target_layout, .. }) = ws;
338+
| ProjectWorkspace::DetachedFile { toolchain, target_layout, .. }) = ws;
339339

340340
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
341341
let mut db = RootDatabase::new(lru_cap);

crates/project-model/src/cargo_workspace.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@ impl CargoWorkspace {
305305
.collect(),
306306
);
307307
}
308+
// The manifest is a rust file, so this means its a script manifest
309+
if cargo_toml.extension().is_some_and(|ext| ext == "rs") {
310+
// Deliberately don't set up RUSTC_BOOTSTRAP or a nightly override here, the user should
311+
// opt into it themselves.
312+
other_options.push("-Zscript".to_owned());
313+
}
308314
meta.other_options(other_options);
309315

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

382+
let manifest = AbsPathBuf::assert(manifest_path);
376383
let pkg = packages.alloc(PackageData {
377384
id: id.repr.clone(),
378385
name,
379386
version,
380-
manifest: AbsPathBuf::assert(manifest_path).try_into().unwrap(),
387+
manifest: manifest.clone().try_into().unwrap(),
381388
targets: Vec::new(),
382389
is_local,
383390
is_member,
@@ -400,11 +407,22 @@ impl CargoWorkspace {
400407
for meta_tgt in meta_targets {
401408
let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
402409
meta_tgt;
410+
let kind = TargetKind::new(&kind);
403411
let tgt = targets.alloc(TargetData {
404412
package: pkg,
405413
name,
406-
root: AbsPathBuf::assert(src_path),
407-
kind: TargetKind::new(&kind),
414+
root: if kind == TargetKind::Bin
415+
&& manifest.extension().is_some_and(|ext| ext == "rs")
416+
{
417+
// cargo strips the script part of a cargo script away and places the
418+
// modified manifest file into a special target dir which is then used as
419+
// the source path. We don't want that, we want the original here so map it
420+
// back
421+
manifest.clone()
422+
} else {
423+
AbsPathBuf::assert(src_path)
424+
},
425+
kind,
408426
required_features,
409427
});
410428
pkg_data.targets.push(tgt);

0 commit comments

Comments
 (0)