Tuesday, December 15, 2015

Atom editor and Perforce integration on Linux (Mac)

Atom for editing, Perforce for version control

Atom is a really cool text editor developed by GitHub. It is modern, lightweight, fast, provides it's own package manager and tons of packages/plugins as well. Though, if you are using Perforce for version control, things may get a bit bitter.

This short post provides an easy and elegant fix for integrating Atom with Perforce on a Linux (Mac) machine. It solves the problem when the atom-perforce package is unable to pick the right p4 client.


Permissions are the key, or are they?

I am using Atom and Perforce on an Ubuntu machine. The atom-perforce package was integrating well until the middle of November 2015, i.e. version number ranging about 1.6.1 - 1.6.4.
Since then, I was getting the following error message when trying to save my edits:

Unable to save file: Permission denied ...
So I opened a bug report. Developers of atom-perforce package are pretty fast, but my problem persisted even after new releases.

Perforce (both p4 and p4v) was working, Atom was working. Only the integration wasn't. I ended up manually marking each file for edit using p4 edit from the command line. Not a nice experience.

The issue really was that atom-perforce did not recognize my p4 client, even though p4 info and p4 client were returning correct values. It turns out that it can be fixed by three lines of code in two files.

Environment variables and config files to the rescue!

You need to do two things:

  • set the P4CONFIG environment variable
  • set the P4CLIENT as an environment variable and a setting in the .p4config file

Lets assume that our p4 client is called myclient. Therefore, add these lines to your .bashrc file (commonly located in ~/.bashrc):

export P4CLIENT=myclient
export P4CONFIG=~/.p4config
and then this line to ~/.p4config:
P4CLIENT=myclient

Now either restart your terminal or reload .bashrc (with source /.bashrc) and run Atom from terminal simply by typing atom. Integration should now work as expected.

There is one limitation, though - it works only if you launch atom from the command line.

Let's not forget

A big thank you goes to Bert who pointed out this method. Keep up the good work!