Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
217: Fixed toolchain version drift, improved error handling, config loading and refactoring. r=xFrednet a=nitkach ### This PR solves several problems ## Problem 1 After resolving this issue #188, this problem was revealed. I didn't create an issue, I just fixed it in this PR. When `cargo` of the new version tries to call `rustc` of the old version and pass it arguments that it does not support, the program breaks. This happens when executing a command in `cargo_metadata_command()`. Description of the situation in the diagram: ![toolchain-fiasco](https://github.com/nitkach/marker/assets/108859698/65be44f1-19f7-4881-8c1c-d8294db74c39) Inside `MetadataCommand::new()` creates `cargo metadata` which calls `rustc`. It, in turn, takes the toolchain from `PATH="~/.rustup/toolchains/1.65.0-*"` ## Problem 2 Also in repository with 1.65.0 in rust-toolchain file the following error occurs: ``` $ cargo marker Compiling Lints: error: the `-Z` flag is only accepted on the nightly channel of Cargo, but this is the `stable` channel See https://doc.rust-lang.org/book/appendix-07-nightly-rust.html for more information about Rust release channels. Error: LintCrateBuildFail ``` <!-- Reproduces if there is no `cargo_marker` in the current `Cargo.toml` dependencies. The `PATH` additionally specifies the path to `cargo-marker`. --> Diagram of command calls and environment variables: ![toolchain-unstable](https://github.com/nitkach/marker/assets/108859698/aeec07b2-c6a9-44c9-a0b6-46c8f0260f82) The command `cargo build ... -Z unstable-options ...` uses a stable toolchain, despite the specified `RUSTUP_TOOLCHAIN=nightly-2023-07-03`. Then `rustc` is called, the stable toolchain is taken from `PATH` and a command execution error occurs. Cannot use the arguments available on the nightly version of `cargo` in the stable channel. --- To fix this, I changed the `cargo_command()` of `Toolchain`. Now the ``` rustup run {toolchain} cargo ``` is being created. This makes `rustup` a proxy for `cargo`, guaranteed to call `cargo` with the specified `toolchain`. --- ### Cargo workspace manifest location Now the location of the workspace manifest file is obtained by executing the `cargo_locate_project` method on the `Cargo` type. The `rustup run {toolchain} cargo locate-project --workspace` command is executed. --- #### Error handling I have plan to refactor error handling in future PR. I have removed the error codes from `ExitStatus`, as they do not work as I assume. In the terminal, it always shows that `(exit code: 1)` is returned. https://github.com/nitkach/marker/blob/b95435d77065210bbe38e6995f35bd165f280612/cargo-marker/src/exit.rs#L81-L83 Adding the `Fatal` variant with a description and the source of the error makes creating errors easier. --- #### Parsing TOML as strongly typed data structures For parsing `toml`, I added the use of rust types, which deserializes the document into a structure with fields. --- #### `Display` error for toml file while parsing I also noticed that it is possible to output errors in more readable way `Debug`: ``` Can't parse config: Error { inner: Error { inner: TomlError { message: "data did not match any variant of untagged enum LintDependency", original: Some("[workspace]\nmembers = [\n \"cargo-marker\",\n \"marker_adapter\",\n \"marker_api\",\n \"marker_rustc_driver\",\n \"marker_utils\",\n \"marker_lints\",\n \"marker_uitest\",\n \"marker_uilints\",\n]\nresolver = \"2\"\n\n[workspace.metadata.marker.lints]\nmarker_lints = { foo = \"./marker_lints\" }\n"), keys: ["workspace", "metadata", "marker", "lints", "marker_lints"], span: Some(245..271) } } } ``` `Display`: ``` Can't parse config: TOML parse error at line 15, column 16 | 15 | marker_lints = { foo = "./marker_lints" } | ^^^^^^^^^^^^^^^^^^^^^^^^^^ data did not match any variant of untagged enum LintDependency ``` --- #### Some code changes I'm learning how to use iterators. Where the `mut` variable and the `for` loop were used, I replaced that with iterators. In some places, an immutable `Vec<T>` was created, which I changed to an `[T]`. Co-authored-by: Nikita <[email protected]>
- Loading branch information