Skip to content

Conversation

heaths
Copy link
Contributor

@heaths heaths commented Oct 30, 2024

Avoid ambiguities with any user-defined tracing modules by globally qualifying types used in the attribute-generated code e.g., ::tracing::Level.

Fixes: #3119 for the v0.2.x crate versions.

Motivation

We need to define helper functions for tracing within our own crates, and tracing is too good a name to pass up. Our helpers will mostly wrap and otherwise "hide" the tracing crate, but when using #[instrument] the generated code conflicts with our tracing module.

Solution

Globally qualify any token paths e.g., ::tracing to avoid ambiguities.

@heaths heaths requested review from hawkw, davidbarsky and a team as code owners October 30, 2024 06:09
@heaths
Copy link
Contributor Author

heaths commented Oct 30, 2024

Note: the CONTRIBUTING.md file recommends updating the CHANGELOG.md but the last entry is very old - over 4 years old; however, the CHANGELOG.md file in the v0.1.x branch is up to date. I plan to make a PR against that branch as well and will update that CHANGELOG.md. I'm happy to update master's as well if that is appropriate.

Copy link
Contributor

@kaffarell kaffarell left a comment

Choose a reason for hiding this comment

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

LGTM – gave it a quick spin and it works alright. Just FYI, usually PR's are merged on master and then backported by us to v0.1.x.

@heaths
Copy link
Contributor Author

heaths commented Oct 30, 2024

Thanks. Should I close the other PR then or will maintainers?

@kaffarell
Copy link
Contributor

No, this PR looks fine, the maintainers are gonna backport everything!

@heaths
Copy link
Contributor Author

heaths commented Jan 10, 2025

@davidbarsky @hawkw is this good? Want me to rebase on another branch or anything? We'd love to get this fix to unblock some of our scenarios.

Avoid ambiguities with any user-defined `tracing` modules by globally qualifying types used in the attribute-generated code e.g., `::tracing::Level`.

Fixes: tokio-rs#3119
@jplatte jplatte enabled auto-merge (squash) May 28, 2025 07:33
@jplatte jplatte merged commit a148289 into tokio-rs:master May 28, 2025
56 checks passed
@hds hds mentioned this pull request May 28, 2025
25 tasks
@heaths heaths deleted the issue3119-v0.2 branch May 28, 2025 14:05
hds pushed a commit that referenced this pull request May 29, 2025
Avoid ambiguities with any user-defined `tracing` modules by globally qualifying types used in the attribute-generated code e.g., `::tracing::Level`.
hds pushed a commit that referenced this pull request Jun 3, 2025
Avoid ambiguities with any user-defined `tracing` modules by globally qualifying types used in the attribute-generated code e.g., `::tracing::Level`.
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 10, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths (#3126)

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 10, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths ([#3126])

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 10, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths ([#3126])

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126

Signed-off-by: Hayden Stainsby <hds@caffeineconcepts.com>
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 10, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths ([#3126])

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126

Signed-off-by: Hayden Stainsby <hds@caffeineconcepts.com>
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 6, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths ([#3126])

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126
hds added a commit that referenced this pull request Jun 6, 2025
# 0.1.29 (June 6, 2025)

### Changed

- Bump MSRV to 1.65 ([#3033])

### Fixed

- Let `dead_code` lint work on `#[instrument]`ed functions ([#3108])
- Globally qualify attribute paths ([#3126])

[#3033]: #3033
[#3108]: #3108
[#3126]: #3126
@ilya-zlobintsev
Copy link

Hey, just a heads-up that this is a breaking change in some situations, as global paths don't work with crate re-exports.

In our code base we have a shared library across multiple repositories, and this library re-exports commonly used crates (including tracing) to make version management easier. Previously we could do use lib::tracing::{self, instrument}; to use the instrument macro and have it access tracing, but now each service will have to explicitly specify tracing as a direct dependency.

@jplatte
Copy link
Member

jplatte commented Jun 9, 2025

Makes sense. However, what you were doing before was never meant to be supported, to my understanding.

@jplatte
Copy link
Member

jplatte commented Jun 9, 2025

If you need a new hack, you can add

use lib::tracing::*;
extern crate self as tracing;

to your crate roots. I wouldn't recommend it over the explicit dependency, just pointing out it's possible 😅

@hds
Copy link
Contributor

hds commented Jun 9, 2025

I think the problem here is that we can either support users having some module called tracing in the current scope that isn't the real tracing or we can support users only importing tracing via a re-export of the whole crate which brings the real tracing into scope as a module.

However, I don't believe that we can support both cases at the same time. If you know a way, please let me know.

I appreciate that this is a breaking change for you, and I don't want to downplay that, but we're really considering the previous behavior of not using an absolute path as a defect that should be fixed and how the macro should have been implemented all along.

@ilya-zlobintsev
Copy link

we're really considering the previous behavior of not using an absolute path as a defect that should be fixed and how the macro should have been implemented all along

Yes, I agree, this is an understandable change, and I'm not asking for it to be reverted. Just writing a heads-up in case anyone else encounters this (and perhaps this could also be mentioned in the changelog).

@hds
Copy link
Contributor

hds commented Jun 9, 2025

@ilya-zlobintsev Thank you for the heads up and for the understanding. This was missed from the tracing-attributes changelog, but due to another issue (#3306), we'll be releasing a new version soon, so we'll put it in there.

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.

Generated #[instrument] code is not qualified to the tracing crate
5 participants