Skip to content
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

Command line option to print full file paths #5450

Open
emoon opened this issue May 1, 2018 · 8 comments
Open

Command line option to print full file paths #5450

emoon opened this issue May 1, 2018 · 8 comments
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.

Comments

@emoon
Copy link

emoon commented May 1, 2018

Sometimes it would be good to have full paths printed for filenames when building with Cargo. A typical example would be when building a large tree of code from an editor/IDE and when file paths only gets printed as src/filename.rs it makes it hard for the IDE to find it.

Cargo currently prints full names under certain conditions but there is no way to makes sure that always happen. Adding a command like option like --full-filename-paths or something better would allow editors/IDEs to easier navigate to compile errors/warnings etc.

See also

@alexcrichton alexcrichton added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label May 1, 2018
@dwijnand
Copy link
Member

This issue is similar to #5895 and I wonder: by "full file path" do you mean an absolute path, or simply a more consistent path, such as consistently a path relative to the root of the workspace?

@emoon
Copy link
Author

emoon commented Aug 18, 2018

Absolute path would be best as it would work in all cases regardless of how your structure looks like.

@librelois
Copy link

@dwijnand Any news about the implementation of this option?
It would really be very useful to me.

Thank :)

@emoon
Copy link
Author

emoon commented Nov 10, 2019

I took a look at this issue today and got it working with full paths. The diff looks like this.

diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 99fa98747..3e77dc871 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -633,8 +633,16 @@ fn path_args(bcx: &BuildContext<'_, '_>, unit: &Unit<'_>) -> (PathBuf, PathBuf)
     };
     assert!(src.is_absolute());
     if unit.pkg.package_id().source_id().is_path() {
-        if let Ok(path) = src.strip_prefix(ws_root) {
-            return (path.to_path_buf(), ws_root.to_path_buf());
+        // This setting allows us to print a full path to rustc as this is
+        // useful to print when getting errors of having the full filename printed
+        // as it makes it easier for editors/IDEs to find the file as the editor
+        // may not be have cwd relative to the file.
+        if true {
+            return (src, ws_root.to_path_buf());
+        } else {
+            if let Ok(path) = src.strip_prefix(ws_root) {
+                return (path.to_path_buf(), ws_root.to_path_buf());
+            }
         }
     }
     (src, unit.pkg.root().to_path_buf())

Now in order to have this fully working either:

  1. Always use absolute paths here instead of relative ones. There is a reasoning in the comments for this function why relative paths are used.
  2. Add a command line option (to replace the if true above) and pass it down to this code.

I'm not sure what the best option here would be.

@benma
Copy link

benma commented Mar 6, 2020

Bump. It would be great to get this feature, I also rely on absolute paths to jump to the error in my environment.

@catenacyber
Copy link

Until this gets in cargo, my workaround is something like

abspath=`cargo metadata | jq -r '.workspace_root'`
export RUSTFLAGS="$RUSTFLAGS --remap-path-prefix src=$abspath/src"

@MatrixDev
Copy link

This would be really nice to have. Currently it is very hard to navigate between errors in the Android Studio. It just doesn't highlight any file paths in the errors because they are all relative.

@zopsicle
Copy link

zopsicle commented Apr 2, 2024

I found a workaround which is to put the workspace root in a sibling directory of the package, instead of a parent directory. For example:

a/
    Cargo.toml  // package manifest, with `workspace = "../b"`
b/
    Cargo.toml  // workspace manifest, with `members = ["../a"]`

Now, cargo build --manifest-path b will print diagnostics with absolute paths. :3

@epage epage added the S-triage Status: This issue is waiting on initial triage. label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Error and warning messages generated by Cargo itself. C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants