Skip to content

Commit fe99216

Browse files
SarthakSingh31ojeda
authored andcommitted
rust: Support latest version of rust-analyzer
Sets the `sysroot` field in rust-project.json which is now needed in newer versions of rust-analyzer instead of the `sysroot_src` field. Till [1] `rust-analyzer` used to guess the `sysroot` based on the `sysroot_src` at [2]. Now `sysroot` is a required parameter for a `rust-project.json` file. It is required because `rust-analyzer` need it to find the proc-macro server [3]. In the current version of `rust-analyzer` the `sysroot_src` is only used to include the inbuilt library crates (std, core, alloc, etc) [4]. Since we already specify the core library to be included in the `rust-project.json` we don't need to define the `sysroot_src`. Code editors like VS Code try to use the latest version of rust-analyzer (which is updated every week) instead of the version of rust-analyzer that comes with the rustup toolchain (which is updated every six weeks along with the rust version). Without this change `rust-analyzer` is breaking for anyone using VS Code. As they are getting the latest version of `rust-analyzer` with the changes made in [1]. `rust-analyzer` will also start breaking for other developers as they update their rust version (assuming that also updates the rust-analyzer version on their system). This patch should work with every setup as there is no more guess work being done by `rust-analyzer`. [ Lukas, who leads the rust-analyzer team, says: `sysroot_src` is required now if you want to have the sysroot source libraries be loaded. I think we used to infer it as `{sysroot}/lib/rustlib/src/rust/library` before when only the `sysroot` field was given but that was since changed to make it possible in having a sysroot without the standard library sources (that is only have the binaries available). So if you want the library sources to be loaded by rust-analyzer you will have to set that field as well now. - Miguel ] Link: rust-lang/rust-analyzer#17287 [1] Link: https://github.com/rust-lang/rust-analyzer/blob/f372a8a1176ff8dd5f45ab2ddd45f3530db0374f/crates/project-model/src/workspace.rs#L367-L374 [2] Link: https://github.com/rust-lang/rust-analyzer/blob/eeb192b79aeac47b40add66347022af17a74fbaf/crates/project-model/src/sysroot.rs#L180-L192 [3] Link: https://github.com/search?q=repo%3AVeykril%2Frust-analyzer%20src_root()&type=code [4] Tested-by: Dirk Behme <[email protected]> Signed-off-by: Sarthak Singh <[email protected]> Link: https://rust-for-linux.zulipchat.com/#narrow/stream/291565-Help/topic/How.20to.20rust-analyzer.20correctly.20working Link: https://lore.kernel.org/r/[email protected] [ Formatted comment, fixed typo and removed spurious empty line. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 0ba521d commit fe99216

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

rust/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ rust-analyzer:
350350
$(Q)$(srctree)/scripts/generate_rust_analyzer.py \
351351
--cfgs='core=$(core-cfgs)' --cfgs='alloc=$(alloc-cfgs)' \
352352
$(realpath $(srctree)) $(realpath $(objtree)) \
353-
$(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
353+
$(rustc_sysroot) $(RUST_LIB_SRC) $(KBUILD_EXTMOD) > \
354354
$(if $(KBUILD_EXTMOD),$(extmod_prefix),$(objtree))/rust-project.json
355355

356356
redirect-intrinsics = \

scripts/generate_rust_analyzer.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def main():
145145
parser.add_argument('--cfgs', action='append', default=[])
146146
parser.add_argument("srctree", type=pathlib.Path)
147147
parser.add_argument("objtree", type=pathlib.Path)
148+
parser.add_argument("sysroot", type=pathlib.Path)
148149
parser.add_argument("sysroot_src", type=pathlib.Path)
149150
parser.add_argument("exttree", type=pathlib.Path, nargs="?")
150151
args = parser.parse_args()
@@ -154,9 +155,12 @@ def main():
154155
level=logging.INFO if args.verbose else logging.WARNING
155156
)
156157

158+
# Making sure that the `sysroot` and `sysroot_src` belong to the same toolchain.
159+
assert args.sysroot in args.sysroot_src.parents
160+
157161
rust_project = {
158162
"crates": generate_crates(args.srctree, args.objtree, args.sysroot_src, args.exttree, args.cfgs),
159-
"sysroot_src": str(args.sysroot_src),
163+
"sysroot": str(args.sysroot),
160164
}
161165

162166
json.dump(rust_project, sys.stdout, sort_keys=True, indent=4)

0 commit comments

Comments
 (0)