Skip to content

Commit db99213

Browse files
committed
Fix use with Bazel
When the application is built with Bazel as build system, environment variables like CARGO_MANIFEST_DIR, etc. are set for compatibility, but CARGO itself isn't, because Bazel is the tool of choice. Therefore any attempt to invoke Cargo to locate the workspace manifest path fails. As a fallback, a lack of the CARGO environment variable now just means no workspace support.
1 parent a12fb32 commit db99213

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/lib.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ use toml_edit::{DocumentMut, Item, Table, TomlError};
100100
pub enum Error {
101101
NotFound(PathBuf),
102102
CargoManifestDirNotSet,
103-
CargoEnvVariableNotSet,
104103
FailedGettingWorkspaceManifestPath,
105104
CouldNotRead { path: PathBuf, source: io::Error },
106105
InvalidToml { source: TomlError },
@@ -138,7 +137,6 @@ impl fmt::Display for Error {
138137
crate_name,
139138
path.display(),
140139
),
141-
Error::CargoEnvVariableNotSet => f.write_str("`CARGO` env variable not set."),
142140
Error::FailedGettingWorkspaceManifestPath =>
143141
f.write_str("Failed to get the path of the workspace manifest path."),
144142
}
@@ -241,7 +239,11 @@ pub fn crate_name(orig_name: &str) -> Result<FoundCrate, Error> {
241239
}
242240

243241
fn workspace_manifest_path(cargo_toml_manifest: &Path) -> Result<Option<PathBuf>, Error> {
244-
let stdout = Command::new(env::var("CARGO").map_err(|_| Error::CargoEnvVariableNotSet)?)
242+
let Ok(cargo) = env::var("CARGO") else {
243+
return Ok(None);
244+
};
245+
246+
let stdout = Command::new(cargo)
245247
.arg("locate-project")
246248
.args(&["--workspace", "--message-format=plain"])
247249
.arg(format!("--manifest-path={}", cargo_toml_manifest.display()))

0 commit comments

Comments
 (0)