3

Recently the scenario of editing project source code locally but building the project remotely on another machine (for example inside a Docker container or an Amazon Web Services host) is becoming more common. The setup typically involves starting a build from Emacs that first uses rsync to copy over the project directory to a remote host and then execute the build command there.

A problem arises however when there is a build error and the reported remote source code file paths do not match the paths on the local host. In this case Emacs' compilation mode is not able to automatically jump from the error to the local file (because it doesn't understand how to map the remote path to the local path). How can the Emacs compilation mode be configured to convert the remote paths to local paths?

Possibly the user would have to provide some basic remote path to local path mapping function. Or maybe some heuristic could be implemented which tries mapping only the part of the path relative to the project directory, which should be the same on the local and remote host.

  • 1
    Not quite what you're asking for I know, but if editing the code on the remote machine using Emacs' tramp mode is possible, then M-x compile will do the right thing and run the build on the remote machine and let you jump directly to the errors (also on the remote machine). Apologies if you knew that already. – stevoooo Mar 22 '17 at 22:01
  • I wrote a package, https://github.com/whatacold/ppcompile, to do the exact things, take a look if you're interested. – whatacold Jun 23 '19 at 02:46
  • @stevoooo But if you implement the solution below and go to next error in the compilation buffer, it takes you to a local file. If you recompile from that buffer, the compilation command is run locally and doesn't work. Is there a way to teach emacs to open the remote file from an error in compilation buffer? – Fred Schoen Jan 20 '21 at 10:43

1 Answers1

5

If I understand your question correctly, the simplest solution that I have found is to set the variable directory-abbrev-alist to map directories in the compilation buffer to the host system.

For example I run my phpunit tests in a docker container as a compile command. When there are errors or failures the filepaths in the compilation buffer show up as /var/www/html/... but on my host machine I am working at /home/user/MyApp

So in my emacs config (would work better as .dir-locals) I have:

(setq directory-abbrev-alist '(("^/var/www/html" . "/home/user/MyApp")))

Now when I click on or jump to paths in compilation buffer Emacs goes to the correct file on the host machine.

See the official Emacs docs for details on how directory-abbrev-alist works: https://ftp.gnu.org/old-gnu/Manuals/elisp-manual-20-2.5/html_node/elisp_380.html

Stefan
  • 26,154
  • 3
  • 46
  • 84
ghn
  • 53
  • 1
  • 4
  • I tried to use directory-local and file-local variables, both failed. But modifying in the emacs config works for me. Great time saver! – Fred Schoen Jan 20 '21 at 09:55
  • Forgot to mention my compile command to build in a local docker container. Invoke this from a local source file: docker exec CONTAINER_NAME sh -c "cd path/to/source && make" – Fred Schoen Jan 20 '21 at 10:40