Cargo: Command line option to print full file paths

Created on 1 May 2018  路  6Comments  路  Source: rust-lang/cargo

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.

C-feature-request

Most helpful comment

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

Thank :)

All 6 comments

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?

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

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

Thank :)

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.

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

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"
Was this page helpful?
0 / 5 - 0 ratings