From b74750b82ef88bcec13a5f20c8094ac44e948a40 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 13 Feb 2018 13:07:01 +0300 Subject: [PATCH] Use cargo-metadata command to learn about workspace root Closes https://github.com/kwrooijen/cargo.el/issues/51 --- cargo-process.el | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cargo-process.el b/cargo-process.el index 46f1b63..51663de 100644 --- a/cargo-process.el +++ b/cargo-process.el @@ -177,9 +177,19 @@ (buffer-substring-no-properties (line-beginning-position) (line-end-position)))) +(defun cargo-process--workspace-root () + "Find the worksapce root using `cargo metadata`." + (let* ((metadata-text (shell-command-to-string + "cargo metadata --format-version 1 --no-deps")) + (metadata-json (json-read-from-string metadata-text)) + (workspace-root (alist-get 'workspace_root metadata-json))) + workspace-root)) + (defun cargo-process--project-root () "Find the root of the current Cargo project." - (let ((root (locate-dominating-file (or buffer-file-name default-directory) "Cargo.toml"))) + (let* ((guess-root (locate-dominating-file (or buffer-file-name default-directory) "Cargo.toml")) + (workspace-root (cargo-process--workspace-root)) + (root (or workspace-root guess-root))) (and root (file-truename root)))) (define-derived-mode cargo-process-mode compilation-mode "Cargo-Process."