Skip to content

Commit

Permalink
Patch cargo script root files back to manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Apr 19, 2024
1 parent 0b24599 commit 46f0554
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
18 changes: 15 additions & 3 deletions crates/project-model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,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 @@ -406,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
6 changes: 3 additions & 3 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ impl ProjectWorkspace {
detached_files: Vec<AbsPathBuf>,
config: &CargoConfig,
) -> Vec<anyhow::Result<ProjectWorkspace>> {
dbg!(detached_files
detached_files
.into_iter()
.map(|detached_file| {
let dir = detached_file
Expand Down Expand Up @@ -508,7 +508,7 @@ impl ProjectWorkspace {
cargo_script,
})
})
.collect())
.collect()
}

/// Runs the build scripts for this [`ProjectWorkspace`].
Expand Down Expand Up @@ -822,7 +822,7 @@ impl ProjectWorkspace {
} => (
if let Some(cargo) = cargo_script {
cargo_to_crate_graph(
load,
&mut |path| load(path),
None,
cargo,
sysroot.as_ref().ok(),
Expand Down
2 changes: 0 additions & 2 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,7 @@ pub fn ws_to_crate_graph(
let mut toolchains = Vec::default();
let e = Err(Arc::from("missing layout"));
for ws in workspaces {
dbg!(ws);
let (other, mut crate_proc_macros) = ws.to_crate_graph(&mut load, extra_env);
dbg!(&other);
let num_layouts = layouts.len();
let num_toolchains = toolchains.len();
let (ProjectWorkspace::Cargo { toolchain, target_layout, .. }
Expand Down
16 changes: 8 additions & 8 deletions crates/rust-analyzer/tests/slow-tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ version = "0.1.0"
pub struct SpecialHashMap2;
//- /src/lib.rs
#!/usr/bin/env -S cargo +nightly -Zscript
//! ---cargo
//! [dependencies]
//! dependency = { path = "../dependency" }
//! ---
---cargo
[dependencies]
dependency = { path = "../dependency" }
---
use dependency::Spam;
use dependency2::Spam;
"#,
Expand Down Expand Up @@ -178,10 +178,10 @@ use dependency2::Spam;
server.write_file_and_save(
"src/lib.rs",
r#"#!/usr/bin/env -S cargo +nightly -Zscript
//! ---cargo
//! [dependencies]
//! dependency2 = { path = "../dependency2" }
//! ---
---cargo
[dependencies]
dependency2 = { path = "../dependency2" }
---
use dependency::Spam;
use dependency2::Spam;
"#
Expand Down

0 comments on commit 46f0554

Please sign in to comment.