Skip to content

Conversation

@tronical
Copy link
Contributor

@tronical tronical commented Jan 5, 2025

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.

src/lib.rs Outdated

fn workspace_manifest_path(cargo_toml_manifest: &Path) -> Result<Option<PathBuf>, Error> {
// Skip invoking Cargo if we're building a crate packaged with cargo.
if !cargo_toml_manifest.with_extension(".toml.orig").exists() {
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if !cargo_toml_manifest.with_extension(".toml.orig").exists() {
if cargo_toml_manifest.with_extension(".toml.orig").exists() {

Isn't that the correct way?

Copy link
Owner

Choose a reason for hiding this comment

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

Because this file exists if the package was packaged by cargo, but also generally a little bit fragile because anything could create this file.

Maybe to fix your issue, just returning None when CARGO env variable wasn't found?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ooops, patch tested and submitted differ :( my bad, sorry.

Yes, I’d be happy to change the PR to return None of cargo isn’t set. I guess the corresponding error type can also go away then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To clarify: intended was this:

if cargo_toml_manifest.with_extension("toml.orig").exists() {

(note the removed leading dot)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed the version that I intended and that I verified that it works as intended.

I think it's a win-win kind of, as it also speeds up the regular builds. But if you prefer the None on unset Cargo env, then I can also do that.

Copy link
Owner

Choose a reason for hiding this comment

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

I'm a little bit afraid it breaks something else :D Also in a non obvious way. I think for now I would opt-in to check for CARGO being unchecked. But we could open an issue to speed up regular builds.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright, sounds good. Force-push coming up to correct this :)

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.
@tronical tronical changed the title Fix use with packaged crates and Bazel Fix use with Bazel Jan 5, 2025
NotFound(PathBuf),
CargoManifestDirNotSet,
CargoEnvVariableNotSet,
FailedGettingWorkspaceManifestPath,
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
FailedGettingWorkspaceManifestPath,
#[deprecated]
CargoEnvVariableNotSet,
FailedGettingWorkspaceManifestPath,

path.display(),
),
Error::CargoEnvVariableNotSet => f.write_str("`CARGO` env variable not set."),
Error::FailedGettingWorkspaceManifestPath =>
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
Error::FailedGettingWorkspaceManifestPath =>
#[allow(deprecated)]
Error::CargoEnvVariableNotSet => f.write_str("`CARGO` env variable not set."),
Error::FailedGettingWorkspaceManifestPath =>

@bkchr bkchr merged commit e2c5dbe into bkchr:master Jan 5, 2025
3 checks passed
@bkchr
Copy link
Owner

bkchr commented Jan 5, 2025

Ty!

rossdylan added a commit to rossdylan/rstest that referenced this pull request Nov 3, 2025
rstest currently fails to build under bazel with
```
error: custom attribute panicked
    |
250 |     #[rstest]
    |     ^^^^^^^^^
    |
    = help: message: rstest is present in `Cargo.toml` qed: Could not find `Cargo.toml` in manifest dir:
```

I've tracked this back to proc-macro-crate, which fixed it in bkchr/proc-macro-crate#55
rossdylan added a commit to rossdylan/rstest that referenced this pull request Nov 11, 2025
rstest currently fails to build under bazel with
```
error: custom attribute panicked
    |
250 |     #[rstest]
    |     ^^^^^^^^^
    |
    = help: message: rstest is present in `Cargo.toml` qed: Could not find `Cargo.toml` in manifest dir:
```

I've tracked this back to proc-macro-crate, which fixed it in bkchr/proc-macro-crate#55
rossdylan added a commit to rossdylan/rstest that referenced this pull request Nov 13, 2025
rstest currently fails to build under bazel with
```
error: custom attribute panicked
    |
250 |     #[rstest]
    |     ^^^^^^^^^
    |
    = help: message: rstest is present in `Cargo.toml` qed: Could not find `Cargo.toml` in manifest dir:
```

I've tracked this back to proc-macro-crate, which fixed it in bkchr/proc-macro-crate#55
rossdylan added a commit to rossdylan/rstest that referenced this pull request Nov 13, 2025
rstest currently fails to build under bazel with
```
error: custom attribute panicked
    |
250 |     #[rstest]
    |     ^^^^^^^^^
    |
    = help: message: rstest is present in `Cargo.toml` qed: Could not find `Cargo.toml` in manifest dir:
```

I've tracked this back to proc-macro-crate, which fixed it in bkchr/proc-macro-crate#55
la10736 pushed a commit to la10736/rstest that referenced this pull request Nov 14, 2025
#329)

* Upgrade proc-macro-crate to 3.4.0 in order fix compilation under bazel

rstest currently fails to build under bazel with
```
error: custom attribute panicked
    |
250 |     #[rstest]
    |     ^^^^^^^^^
    |
    = help: message: rstest is present in `Cargo.toml` qed: Could not find `Cargo.toml` in manifest dir:
```

I've tracked this back to proc-macro-crate, which fixed it in bkchr/proc-macro-crate#55

* fix typo in changelog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants