Skip to content

fix: Fix proc-macro-srv search paths for Arch Linux #13639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions crates/ide-diagnostics/src/handlers/macro_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use crate::{Diagnostic, DiagnosticsContext};
// This diagnostic is shown for macro expansion errors.
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
// Use more accurate position if available.
let display_range = d
.precise_location
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);

let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
Diagnostic::new("macro-error", d.message.clone(), display_range).experimental()
}

Expand Down
5 changes: 1 addition & 4 deletions crates/ide-diagnostics/src/handlers/unresolved_macro_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ pub(crate) fn unresolved_macro_call(
d: &hir::UnresolvedMacroCall,
) -> Diagnostic {
// Use more accurate position if available.
let display_range = d
.precise_location
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.macro_call.clone()).range);

let display_range = ctx.resolve_precise_location(&d.macro_call, d.precise_location);
let bang = if d.is_bang { "!" } else { "" };
Diagnostic::new(
"unresolved-macro-call",
Expand Down
12 changes: 1 addition & 11 deletions crates/ide-diagnostics/src/handlers/unresolved_proc_macro.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use hir::db::DefDatabase;
use syntax::NodeOrToken;

use crate::{Diagnostic, DiagnosticsContext, Severity};

Expand All @@ -19,16 +18,7 @@ pub(crate) fn unresolved_proc_macro(
proc_attr_macros_enabled: bool,
) -> Diagnostic {
// Use more accurate position if available.
let display_range = (|| {
let precise_location = d.precise_location?;
let root = ctx.sema.parse_or_expand(d.node.file_id)?;
match root.covering_element(precise_location) {
NodeOrToken::Node(it) => Some(ctx.sema.original_range(&it)),
NodeOrToken::Token(it) => d.node.with_value(it).original_file_range_opt(ctx.sema.db),
}
})()
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()))
.range;
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);

let config_enabled = match d.kind {
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,
Expand Down
22 changes: 22 additions & 0 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ struct DiagnosticsContext<'a> {
resolve: &'a AssistResolveStrategy,
}

impl<'a> DiagnosticsContext<'a> {
fn resolve_precise_location(
&self,
node: &InFile<SyntaxNodePtr>,
precise_location: Option<TextRange>,
) -> TextRange {
let sema = &self.sema;
(|| {
let precise_location = precise_location?;
let root = sema.parse_or_expand(node.file_id)?;
match root.covering_element(precise_location) {
syntax::NodeOrToken::Node(it) => Some(sema.original_range(&it)),
syntax::NodeOrToken::Token(it) => {
node.with_value(it).original_file_range_opt(sema.db)
}
}
})()
.unwrap_or_else(|| sema.diagnostics_display_range(node.clone()))
.range
}
}

pub fn diagnostics(
db: &RootDatabase,
config: &DiagnosticsConfig,
Expand Down
15 changes: 15 additions & 0 deletions crates/project-model/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,21 @@ impl ProjectWorkspace {
}
}

pub fn find_sysroot_proc_macro_srv(&self) -> Option<AbsPathBuf> {
match self {
ProjectWorkspace::Cargo { sysroot: Some(sysroot), .. }
| ProjectWorkspace::Json { sysroot: Some(sysroot), .. } => {
let standalone_server_name =
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
["libexec", "lib"]
.into_iter()
.map(|segment| sysroot.root().join(segment).join(&standalone_server_name))
.find(|server_path| std::fs::metadata(&server_path).is_ok())
}
_ => None,
}
}

/// Returns the roots for the current `ProjectWorkspace`
/// The return type contains the path and whether or not
/// the root is a member of the current workspace
Expand Down
22 changes: 5 additions & 17 deletions crates/rust-analyzer/src/cli/load_cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,12 @@ pub fn load_workspace(
};

let proc_macro_client = if load_config.with_proc_macro {
let mut path = AbsPathBuf::assert(std::env::current_exe()?);
let mut args = vec!["proc-macro"];

if let ProjectWorkspace::Cargo { sysroot, .. } | ProjectWorkspace::Json { sysroot, .. } =
&ws
{
if let Some(sysroot) = sysroot.as_ref() {
let standalone_server_name =
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
let server_path = sysroot.root().join("libexec").join(&standalone_server_name);
if std::fs::metadata(&server_path).is_ok() {
path = server_path;
args = vec![];
}
}
}
let (server_path, args): (_, &[_]) = match ws.find_sysroot_proc_macro_srv() {
Some(server_path) => (server_path, &[]),
None => (AbsPathBuf::assert(std::env::current_exe()?), &["proc-macro"]),
};

ProcMacroServer::spawn(path.clone(), args.clone()).map_err(|e| e.to_string())
ProcMacroServer::spawn(server_path, args).map_err(|e| e.to_string())
} else {
Err("proc macro server disabled".to_owned())
};
Expand Down
36 changes: 5 additions & 31 deletions crates/rust-analyzer/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,50 +305,24 @@ impl GlobalState {
let files_config = self.config.files();
let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);

let standalone_server_name =
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);

if self.proc_macro_clients.is_empty() {
if let Some((path, path_manually_set)) = self.config.proc_macro_srv() {
tracing::info!("Spawning proc-macro servers");
self.proc_macro_clients = self
.workspaces
.iter()
.map(|ws| {
let (path, args) = if path_manually_set {
let (path, args): (_, &[_]) = if path_manually_set {
tracing::debug!(
"Pro-macro server path explicitly set: {}",
path.display()
);
(path.clone(), vec![])
(path.clone(), &[])
} else {
let mut sysroot_server = None;
if let ProjectWorkspace::Cargo { sysroot, .. }
| ProjectWorkspace::Json { sysroot, .. } = ws
{
if let Some(sysroot) = sysroot.as_ref() {
let server_path = sysroot
.root()
.join("libexec")
.join(&standalone_server_name);
if std::fs::metadata(&server_path).is_ok() {
tracing::debug!(
"Sysroot proc-macro server exists at {}",
server_path.display()
);
sysroot_server = Some(server_path);
} else {
tracing::debug!(
"Sysroot proc-macro server does not exist at {}",
server_path.display()
);
}
}
match ws.find_sysroot_proc_macro_srv() {
Some(server_path) => (server_path, &[]),
None => (path.clone(), &["proc-macro"]),
}
sysroot_server.map_or_else(
|| (path.clone(), vec!["proc-macro".to_owned()]),
|path| (path, vec![]),
)
};

tracing::info!(?args, "Using proc-macro server at {}", path.display(),);
Expand Down