Project folder path of opened file



We can add as many project folders. I am trying to find the project folder root path of the opened file.

Eg :


  • Nested
  • onefile.js
  • anotherfile.js

Assume that Hello and World are added as two project folders via right click + Add Project Folder . And the opened file is onefile.js .

We can get the directories eg : Hello , World via


I am looking for how we can get the path of the project folder. ie Hello in this case.
I am not actually looking something as






Is it possible ?

Thank you


Once you have a Directory use Directory::getPath().


Have you tried atom.project.getPaths()?


How do you find which path matches which project?


There’s only one project per window. If you mean which path matches which project root, I believe they’re returned in the same order as displayed in.


Now I’m confused. You can have multiple projects in the tree all in one window, right?


I’m using the term “project” to refer to the object referred to be the atom.project global. There is always one and only one Project. It may have multiple “project roots” (which are the directories that are shown in the tree view) that are directories on disk that each may contain a software development project.

Yes, it does get very confusing.


I thought atom.project was nothing other than an atom software class/object used to provide methods to manage projects. And a real project is a disk directory, usually with a git sub-dir.

There is nothing that ties different root dirs together, is there? Other IDEs often allow a project to include multiple directories but Atom doesn’t. Atom just allows you to open multiple projects in one window.


The Project instance at atom.project does. It keeps track of all the different “root dirs” which we call “project roots”.


That is just on object instance in memory, right? There is no atom command to open up a master project that contains multiple root directories. A window setup is persisted but only temporarily until some other setup is saved.


Atom persists the Project state on exit whether that is single-root or multi-root. It is true that Atom doesn’t have a very obvious way of launching that same state but it does exist. I haven’t verified it lately, but if you launch Atom with atom dir1 dir2 dir3 do some stuff and close it … then launch later with the same command, it should open that same saved state.

See this Issue for a summary on Atom launch modes:


So using the same command line will restore the entire window state with the same open-tabs, etc? So the actual name of the “project” (as you think of it), is the list of dirs, like dir1 dir2 dir3. This is extremely non-intuitive. It should not be documented until a reasonable scheme is implemented. It would be embarrassing (grin).


Hi all,

Thank you for all the comments. Let me clarify the actual problem, so you guys know what I am trying to do.

I was trying to build a hyperclick provider for php . In that I need to pass the root folder so that it can detect some other files from the root.

The code that does is . So via atom api you can get the directory, and iterate through all the directories and path. As @leedohm mentioned you can use atom.project.getPaths() , thank you I wasn’t aware of it.

ref = atom.project.getDirectories();
currentFile = path.dirname(textEditor.getPath());
projectPath = '';
for (i = 0, len = ref.length; i < len; i++) {
  directory = ref[i];
  if (currentFile.indexOf(directory.path) > -1) {
      projectPath = directory.path;

This was what I was doing, but the projectPath will be different when we have same directory for project paths.

Lets see the projects added and file opened :

Projects :


and the opened file is /var/www/projects/project2/src/Model/Table/ArticleTable.php , the code if (currentFile.indexOf(directory.path) > -1) { some how gave false positive. I may want to look more why so then.

Thank you .


To get the current applicable project root for a given file, I did this in Coffeescript:

# If there is no file, we can't find a project root from it.
if not @buffer?.file?.path

# Get the project root and the relative path to the project.
[projectPath, relativePath] = atom.project.relativizePath(@buffer.file.path)

From what I’m understanding, this may be what you are looking for. The key function is relativizePath which takes a full path and finds the correct root.



This was what I was trying to get.

Thank you.


A Directory object also has a contain method you could use.

filePath = atom.workspace.getActivePaneItem().buffer.file.path;
let projectPath = "";
	if (dir.contains(filePath)) {
		projectPath = dir.path;


New method:


Return selected project folder.


This is great, however the getActivePackage method is not available at startup. How does one wait for the tree-view package to load before this request?