dlang / dub

Package and build management system for D
MIT License
662 stars 228 forks source link

Proposal: Add --fullpath option to pass absolute file paths to the compiler, for tooling #2662

Open AndrejMitrovic opened 1 year ago

AndrejMitrovic commented 1 year ago

Currently the [dmd] compiler emits errors with either relative or absolute paths, based upon what was provided on the command-line.

For example:

int test() { return 1.0; }
$ dmd test.d
test.d(1): Error: cannot implicitly convert expression `1.0` of type `double` to `int`

$ dmd $PWD/test.d
/c/dev/d/test.d(1): Error: cannot implicitly convert expression `1.0` of type `double` to `int`

When using build tools like dub and code editors it can be hard to quickly jump to the file + location of a compile-time error when the errors show relative paths.

For example, I have a dub library project with some example code. I set up a shortcut in my editor to run dub build --root=examples/hello. And my actual directory structure is:

./examples/hello/src/hello/main.d

Here's a compiler error:

src\hello\main.d(18,5): Error: undefined identifier `Foo`

In the editor I have a hook for quickly jumping to the offending file + line by parsing the compiler's output. Easily done with a regex:

"file_regex": "^([^\\(]+)\\((\\d+,\\d+)\\): (.*)$",

But the problem is my editor doesn't know anything about dub's --root command, and so it doesn't know how to find the file src\hello\main.d(18,5), because the real path relative to the editor's current working directory is examples\hello\src\hello\main.d.


This could be worked around in various ways in the editor of choice, but it would be easier if we had a way of telling dub to pass absolute file and import paths to the compiler. Then jumping to the correct file is trivial.

The downside is dub could reach some platform limitations with this switch enabled. For example command prompt line string limitations, or some other syscall limitations.

Curious about your thoughts~

AndrejMitrovic commented 1 year ago

I also think this could be implemented in the compiler, but I'm really not sure if it belongs there or here..

AndrejMitrovic commented 1 year ago

I wrote a really silly script that helps me work around the issue: run_dub.d

Then I just run it as run_dub dub -q build --root=examples/hello and get nice full paths in the errors:

examples\hello\src\hello\main.d(18,5): Error: undefined identifier `Foo`
Error C:\Apps\DMD\dmd2\windows\bin\dmd.exe failed with exit code 1.

I'm sure It's overkill. 😄