-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a macro to track a file #73921
Comments
IMO, the proper solution is to add proc_macro APIs that indicate "interest" in a file, rather than further expanding the set of macros. |
Isn't this already supported using |
The user of the proc-macro would need to add it to their I've seen it mentioned in several places where there is desire to register change detection for various things (including environment variables mentioned here). Grepping through crates.io, I found about 10 proc-macros that use the include_str/include_bytes trick to force a dependency check. |
cc #74690, where the ability to register the usage of an environment variable was added. I would suspect adding support for tracking files could use a similar mechanism. I'm not sure if this specifically needs to use a macro, or if a function call would be sufficient ( |
The bare minimum would be ability to add file paths to dependencies. However, it would be significantly more useful to also have ability to load the file into a token stream and create a source map for it, so all the spans are assigned properly. |
This is #55904 |
add `track_path::path` fn for usage in `proc_macro`s Adds a way to declare a dependency on external files without including them, to either re-trigger the build of a file as well as covering the use case of including dependencies within the `rustc` invocation, such that tools like `sccache`/`cachepot` are able to handle references to external files which are not included. Ref rust-lang#73921
The documentation for
However, I just tested it in the process of fixing launchbadge/sqlx#681 and it seems to work for directories too. Is this intentional? Because if it's not, I would love for it to continue working as-is because it'll let us finally fix that bug. And if it is intentional or it's decided that working for directories is a feature and not a bug, it should be mentioned in the documentation. |
It's actually kind of an issue for us that On that note, the function should probably document how it treats relative paths, if at all. Is it relative to the workspace root? If that's the case then we don't even need to canonicalize. |
The specified file path gets into the depinfo file written by rustc. IIRC, cargo recently supported directories |
+1, it should probably accept a |
Yea, this will be dependent on the build system. I think for Cargo it is safe to rely on that behavior, as it is documented in the link given. Taking a |
This is a sensible position, and is exactly why @sunshowers created Taking a |
yeah, depinfo is an instance of what I call the "makefile problem". Given that rustc uses these files, should it support non-utf8 paths at all? cargo already doesn't. I guess you don't need cross-platform support for depinfo files, so the case is a bit less strong for rustc than for cargo. |
There's a couple orthogonal concerns here: The API really needs to document what it does with relative paths. If it's purely up to whatever is consuming the depinfo then that should be documented as well, though it would also be very helpful to describe how Cargo treats it in that case. SQLx's proc macros are already written with the assumption that Cargo is being used, although that has also drawn a few complaints. If we don't want to specify the behavior with relative paths and just tell the user to always canonicalize the path, that's okay. We just need to say one way or the other. As for non-UTF-8 paths, I'm torn. In SQLx's case the paths are taken from string literals in the macro input so they should always be UTF-8. If I knew I didn't need to manipulate the paths at all, I'd be perfectly fine with the function taking If the treatment of relative dirs was specified but differed from my expectation, say for example, I'm expecting the paths to be relative to the crate root whereas Cargo interprets the paths relative to the workspace root, then I would still need to do path manipulation and For sanity if we take Do we have anything against having two different functions? Say, |
Tangential question: What's the path forward to stabilize this? I don't see any other PRs which are tracking this feature. |
@pksunkara the PR #87173 is probably the next step. @abonander Adding two versions seems excessive compared to the feature provided, #87173 (comment) resolves this by a steering meeting discussion outcome, to use a I am not aware of any further issues at this point. |
@drahnr please see my concerns above about specifying the semantics regarding relative paths and directories, that doesn't appear to have been discussed at all in the linked PR. |
@abonander indeed, I'll document the behaviour on relative paths and which base will be used in detail. |
This is now tracked in #99515, together with the |
I recently released
doc-comment
0.4 which converted declarative macros to proc-macros. I encountered a "funny" issue though: I needed the compiler to track files that were included using my proc-macros. In order to do so, I generated anonymous constants which looked like this:However, I have no use for those constants and they make the source code heavier for just keeping track of a file. Therefore, I propose to add a
track!("file")
macro which would allow to track files likeinclude_str!
without requiring the usage of anonymous constants.I can make the implementation if the rust teams agree to add this feature. :)
The text was updated successfully, but these errors were encountered: