Skip to content

Commit 0e574eb

Browse files
committed
Patch cargo script root files back to manifest
1 parent c0f1738 commit 0e574eb

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

crates/project-model/src/cargo_workspace.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,12 @@ impl CargoWorkspace {
354354
let is_local = source.is_none();
355355
let is_member = ws_members.contains(&id);
356356

357+
let manifest = AbsPathBuf::assert(manifest_path);
357358
let pkg = packages.alloc(PackageData {
358359
id: id.repr.clone(),
359360
name,
360361
version,
361-
manifest: AbsPathBuf::assert(manifest_path).try_into().unwrap(),
362+
manifest: manifest.clone().try_into().unwrap(),
362363
targets: Vec::new(),
363364
is_local,
364365
is_member,
@@ -374,11 +375,22 @@ impl CargoWorkspace {
374375
for meta_tgt in meta_targets {
375376
let cargo_metadata::Target { name, kind, required_features, src_path, .. } =
376377
meta_tgt;
378+
let kind = TargetKind::new(&kind);
377379
let tgt = targets.alloc(TargetData {
378380
package: pkg,
379381
name,
380-
root: AbsPathBuf::assert(src_path),
381-
kind: TargetKind::new(&kind),
382+
root: if kind == TargetKind::Bin
383+
&& manifest.extension().is_some_and(|ext| ext == "rs")
384+
{
385+
// cargo strips the script part of a cargo script away and places the
386+
// modified manifest file into a special target dir which is then used as
387+
// the source path. We don't want that, we want the original here so map it
388+
// back
389+
manifest.clone()
390+
} else {
391+
AbsPathBuf::assert(src_path)
392+
},
393+
kind,
382394
required_features,
383395
});
384396
pkg_data.targets.push(tgt);

crates/project-model/src/workspace.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl ProjectWorkspace {
425425
detached_files: Vec<AbsPathBuf>,
426426
config: &CargoConfig,
427427
) -> Vec<anyhow::Result<ProjectWorkspace>> {
428-
dbg!(detached_files
428+
detached_files
429429
.into_iter()
430430
.map(|detached_file| {
431431
let dir = detached_file
@@ -495,7 +495,7 @@ impl ProjectWorkspace {
495495
cargo_script,
496496
})
497497
})
498-
.collect())
498+
.collect()
499499
}
500500

501501
/// Runs the build scripts for this [`ProjectWorkspace`].
@@ -805,7 +805,7 @@ impl ProjectWorkspace {
805805
} => (
806806
if let Some(cargo) = cargo_script {
807807
cargo_to_crate_graph(
808-
load,
808+
&mut |path| load(path),
809809
None,
810810
cargo,
811811
sysroot.as_ref().ok(),

crates/rust-analyzer/src/reload.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -717,9 +717,7 @@ pub fn ws_to_crate_graph(
717717
let mut toolchains = Vec::default();
718718
let e = Err(Arc::from("missing layout"));
719719
for ws in workspaces {
720-
dbg!(ws);
721720
let (other, mut crate_proc_macros) = ws.to_crate_graph(&mut load, extra_env);
722-
dbg!(&other);
723721
let num_layouts = layouts.len();
724722
let num_toolchains = toolchains.len();
725723
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
@@ -758,7 +756,6 @@ pub fn ws_to_crate_graph(
758756
}
759757
crate_graph.shrink_to_fit();
760758
proc_macro_paths.shrink_to_fit();
761-
dbg!(crate_graph);
762759
(crate_graph, proc_macro_paths, layouts, toolchains)
763760
}
764761

crates/rust-analyzer/tests/slow-tests/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,10 @@ version = "0.1.0"
139139
pub struct SpecialHashMap2;
140140
//- /src/lib.rs
141141
#!/usr/bin/env -S cargo +nightly -Zscript
142-
//! ---cargo
143-
//! [dependencies]
144-
//! dependency = { path = "../dependency" }
145-
//! ---
142+
---cargo
143+
[dependencies]
144+
dependency = { path = "../dependency" }
145+
---
146146
use dependency::Spam;
147147
use dependency2::Spam;
148148
"#,
@@ -178,10 +178,10 @@ use dependency2::Spam;
178178
server.write_file_and_save(
179179
"src/lib.rs",
180180
r#"#!/usr/bin/env -S cargo +nightly -Zscript
181-
//! ---cargo
182-
//! [dependencies]
183-
//! dependency2 = { path = "../dependency2" }
184-
//! ---
181+
---cargo
182+
[dependencies]
183+
dependency2 = { path = "../dependency2" }
184+
---
185185
use dependency::Spam;
186186
use dependency2::Spam;
187187
"#

0 commit comments

Comments
 (0)