Skip to content

Commit

Permalink
fix: error when decoding invalid JSON from cargo metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 5, 2024
1 parent 27d7cb3 commit d69653a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- Error when decoding invalid JSON or blank string from cargo metadata.

## [4.24.0] - 2024-05-30

### Added
Expand Down
7 changes: 5 additions & 2 deletions lua/rustaceanvim/cargo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function cargo.get_root_dir(file_name)
upward = true,
path = path,
})[1])
---@type string | nil
local cargo_workspace_dir = nil
if vim.fn.executable('cargo') == 1 then
local cmd = { 'cargo', 'metadata', '--no-deps', '--format-version', '1' }
Expand All @@ -80,8 +81,10 @@ function cargo.get_root_dir(file_name)
cm = -1
end
if cm == 0 then
cargo_workspace_dir = vim.fn.json_decode(cargo_metadata)['workspace_root']
---@cast cargo_workspace_dir string
local ok, cargo_metadata_json = pcall(vim.fn.json_decode, cargo_metadata)
if ok then
cargo_workspace_dir = cargo_metadata_json['workspace_root']
end
end
end
return cargo_workspace_dir
Expand Down

0 comments on commit d69653a

Please sign in to comment.