but I think that this is behaving differently as of 1.26, as I'm getting a different output based on the current directory name. If the directory name matches the package name, the package name isn't separated from the current directory, e.g ${cwd}#${version}. If they mismatch, then there is an additional separator, e.g. ${cwd}#${package}:${version}.
I filed an issue with Cargo here but I have a feeling that it may have always worked this way but the recommendation of the above was inaccurate.
What is the current best way to get the binary name that Cargo will produce from src/main.rs?
It's probably easier, or at least more accurate, to use cargo metadata and a json parser. You want "packages" -> "targets" selected where "kind" is "bin".
Or, for all packages, for all targets, for any target having a kind array containing an element bin, for any of those having a src_path that does not contain .cargo/registry, get me the name.
@colindean could you expand a little bit why you are looking for package or binary name?
"Name of the binary" is actually a pretty complex business, and the question itself is under specified.
If you have a particular crate whose name is important to you, I suggest just hard-coding it.
If you want to write a generic automation which handles any crate, here are some bullet points to consider:
In general, Cargo project is a workspace of many packages, and each package can have its own binary. To learn the set of packages inside a worksapce, look at the members field of cargo metadata
The name of the binary, even for src/main.rs, is not necessary derived from the name of the package. To learn the name, take a look at name field of the corresponding target
A package could have several binaries, and neither of them is inherently "primary"
The name of the binary depends on the platform: foo vs foo.exe.
A binary is not necessary a single file. On windows and macs, Cargo also produces debug information in a separate file.
Besides cargo metadata, there's also cargo build --message-format=json, which will print full paths to artifacts as they are produced
There's also (unstable for now) cargo build --output-dir=out option which instructs Cargo to put final artifacts to the specified dir.
I want the package name and version because I want to have a single source for the name and version of the package: the Cargo.toml. I use that name and version in the documentation generation tasks:
If I were you, I'd just grepped Cargo.toml for name/version: it should be simpler than wrestling with cargo metadata and should cover this isolated use-case. Doing this "properly" is going to be hard, because Cargo's project model is pretty involved. RELEASE_BINARY := target/release/$(ARTIFACT) already places a lot of assumptions about project structure.