Skip to content

Commit d69653a

Browse files
committed
fix: error when decoding invalid JSON from cargo metadata.
1 parent 27d7cb3 commit d69653a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
66
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
77
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

9+
## [Unreleased]
10+
11+
### Fixed
12+
13+
- Error when decoding invalid JSON or blank string from cargo metadata.
14+
915
## [4.24.0] - 2024-05-30
1016

1117
### Added

lua/rustaceanvim/cargo.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function cargo.get_root_dir(file_name)
5959
upward = true,
6060
path = path,
6161
})[1])
62+
---@type string | nil
6263
local cargo_workspace_dir = nil
6364
if vim.fn.executable('cargo') == 1 then
6465
local cmd = { 'cargo', 'metadata', '--no-deps', '--format-version', '1' }
@@ -80,8 +81,10 @@ function cargo.get_root_dir(file_name)
8081
cm = -1
8182
end
8283
if cm == 0 then
83-
cargo_workspace_dir = vim.fn.json_decode(cargo_metadata)['workspace_root']
84-
---@cast cargo_workspace_dir string
84+
local ok, cargo_metadata_json = pcall(vim.fn.json_decode, cargo_metadata)
85+
if ok then
86+
cargo_workspace_dir = cargo_metadata_json['workspace_root']
87+
end
8588
end
8689
end
8790
return cargo_workspace_dir

0 commit comments

Comments
 (0)