2

I use KDiff3 (executable kdiff3.exe) in Git Bash as git difftool and I encountered a problem to use it under the Ubuntu subsystem.

The Ubuntu subsystem has already Git installed. So I copied my Git global configuration from Git Bash (changing the path accordingly):

[user]
    email = ...
    name = ...

[merge]
    tool = kdiff3

[mergetool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"

[diff]
    tool = kdiff3
    guitool = kdiff3

[difftool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"

[core]
    autocrlf = true

Then I encountered an error:

Opening of these files failed

- [filename].[extension] (A)

The program window opened correctly, but only the right side file (B) was displayed.

The error is repeatable every time. What can cause this error?

1
  • @PeterMortensen I wouldn't expect a response any time soon from the OP, since they haven't been seen on any Stack site in over a year, but I understand the intent, and I'll post a (very late) answer just to have it here. Might be tomorrow though. Sep 14, 2021 at 5:38

2 Answers 2

3

The problem is that Git uses temporary files during these operations and paths to temporary files might be absolute paths. You can see what paths are by changing the diff command to echo:

[difftool "kdiff3"]
    path = "/mnt/c/Program Files/KDiff3/kdiff3.exe"
    cmd = "echo $LOCAL $REMOTE"

The output will be something like:

/tmp/BPI1A2_Layout.js Layout.js

KDiff3 from Windows can access Layout.js because it is relative path. The path /tmp/BPI1A2_Layout.js doesn't exist on Windows, so KDiff3 can not find the file.

You could use the non-default TMPDIR for Git commands:

TMPDIR=".tmp" git difftool

Now the paths would be

.tmp/BPI1A2_Layout.js Layout.js

Both are now relative under the same directory structure which Windows is able to access and KDiff3 works as expected.

If it is OK to use a .tmp directory inside the project folder then a easy workaround is to create an alias:

alias git="TMPDIR=.tmp git"
1
  • Where do I run/put this command? TMPDIR=".tmp" git difftool
    – Jplus2
    May 23, 2021 at 23:39
0

An old question that got bumped today via an edit. The existing answer probably works, but I can think of two other possibilities as well.

To be clear, the OP was using Windows Subsystem for Linux (WSL) to run Ubuntu in Windows. They had previously been using Git for Windows with kdiff3.exe, but they wanted to transition the workflow to Linux Git in WSL/Ubuntu.

While you were on the right track with changing the path of the difftool from C:\... to /mnt/c/..., there was more to it than that. Since WSL does allow you to run Windows executables this way, it was actually attempting to run the kdiff3.exe. But since you were (then) using the Linux version of git, it was naturally using Linux paths for the files to compare.

To expand on what @oikku said in the other answer, since kdiff3.exe is a Windows executable, it only understands Windows path structures (a.k.a. C:\...). When Linux git passed it a Linux path, the Windows executable (rightly) choked, giving you the error Opening of these files failed.

For example:

# What Windows kdiff3 expected:
kdiff3.exe C:\file1.txt.rev1 C:\file1.txt.rev2
# What Windows kdiff3 got from Linux Git:
kdiff3.exe /mnt/c/file1.txt.rev1 /mnt/c/file1.txt.rev2

There are two additional possible solutions, at least today:

  • First, it should be possible to write a wrapper script around kdiff3.exe that would do the path translation of the arguments passed to it. You can use the wslpath command which is (at least now) installed by default in WSL/Ubuntu to do the directory translations. I can't think of any reason why this wouldn't work; you'd just need to make sure you passed through all of the possible arguments from your wrapper script to the command in the form it expects.

  • Second, you could install the Linux version of Kdiff3, but realize that it requires a GUI, which (at the time of this answer) WSL does not support directly. That means that you would need to install a Windows X server and the X libraries in WSL/Ubuntu. There are plenty of other questions/answers on this topic already across the Stack sites, so I won't repeat them here.

    Also note that Windows 11 will include WSLg, a feature that does allow you to use Linux GUI apps under WSL. So that will presumably makes this much easier when that becomes available.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .