Skip to content

Comments

Formatting for output of locate-project#8683

Closed
dtolnay wants to merge 6 commits intorust-lang:masterfrom
dtolnay-contrib:format
Closed

Formatting for output of locate-project#8683
dtolnay wants to merge 6 commits intorust-lang:masterfrom
dtolnay-contrib:format

Conversation

@dtolnay
Copy link
Member

@dtolnay dtolnay commented Sep 6, 2020

This PR brings cargo tree's existing --format flag to cargo locate-project.

$ cargo locate-project --format '{root}'
/git/cargo/Cargo.toml

$ PROJECT_ROOT=$(cargo locate-project -f{root})

This allows scripts to select a script-friendly output representation. All the logic for interpreting such format flags already exists and is used by cargo tree --format. This solution is straightforwardly forward-compatible with introducing additional entries into what locate-project can produce, unlike such alternatives as cargo locate-project --output-format plain.

I like jq (am the third biggest contributor to it) but in 2020 we can't count on it being installed on target systems.

Closes #8009.

@rust-highfive
Copy link

r? @Eh2406

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 6, 2020
@ehuss
Copy link
Contributor

ehuss commented Sep 9, 2020

Thanks for the PR! This definitely seems useful. Can you say more why you went with a free-form "format" option? We were thinking it might be better to use a fixed-format flag like --message-format which would be more consistent with other subcommands. It could default to json (which is a little inconsistent, but is what it is), and then support another option like maybe script?

@dtolnay
Copy link
Member Author

dtolnay commented Sep 9, 2020

I was hoping to propose a way to locate the workspace's Cargo.toml, something I needed in https://github.com/dtolnay/cargo-rm and realized the only way to get it from a script is jq or a heroic sed.

For just the current crate root a new --message-format with a plain path output would be good, possibly adding flags for other project locations that get supported by locate-project later.

@phil-opp
Copy link
Contributor

This PR (or some --message-format root-only option) would be also very useful for my locate_cargo_manifest crate, which currently parses the single line of JSON returned by cargo locate-project using the json crate. If there was some way to get only the path as output, we could make the crate completely dependency-free to further improve compile times.

(@dtolnay As an unrelated side note: The popular cargo-edit project also adds a cargo rm subcommand, so maybe you want to choose a different name for your project to avoid conflicts.)

@ehuss
Copy link
Contributor

ehuss commented Sep 15, 2020

For just the current crate root a new --message-format with a plain path output would be good, possibly adding flags for other project locations that get supported by locate-project later.

Yea, I think I would probably prefer that. Would you be ok with making that change?

Copy link
Member Author

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing in favor of #8707.

@dtolnay dtolnay closed this Sep 16, 2020
@dtolnay dtolnay deleted the format branch September 16, 2020 06:35
bors added a commit that referenced this pull request Sep 23, 2020
Add plain message format for locate-project

Supersedes #8683, as recommended in #8683 (comment). This PR adds a flag `--message-format` to `cargo locate-project` with possible values `json` (default) and `plain`.

```console
$ cargo locate-project --message-format json
{"root":"/git/cargo/Cargo.toml"}

$ cargo locate-project --message-format plain
/git/cargo/Cargo.toml
```

Closes #8009.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Command-line-friendly version of locate-project

5 participants