Sublime Forum

Is it possible to specify working directory as build argument in a build system?

#1

Is it possible to specify working directory as build argument in a build system?

"shell_cmd": "${working_dir}"

Also,

$folder : The path to the first folder opened in the current project.

What is the meaning of ‘opened’?

I read this as the first folder listed in my *.sublime-project file, in

"folders": []

but ‘opened’ could also be the first folder expanded in the sidebar or something.

how does the following example work:

"working_dir": "${project_path:${folder:${file_path}}}"

is it an if, else condition with precedence from right to left?

wouldn’t it be nice if ${folder} would be the current folder I am working in? That is, the path to app1, app2 or app3 depending on which file I am currently editing.

{
  "build_systems":
  [
    {
      "working_dir": "${folder}/build"
    }
  ],
  "folders":
  [
    {
      "path": "app1"
    },
    {
      "path": "app2"
    },
    {
      "path": "app3"
    }
  ]
}
0 Likes

#2

Not directly like in your example; there’s no variable that expands to what the working directory would be. If you were using working_dir in the build to set a working directory, you could use the same variables here.

When you don’t specify an explicit working_dir, Sublime tries to infer one from the path of the current file. In that case you could use ${file_path} to do the same thing.

When there’s no working_dir set and the current file has no path (no file or the file has not been saved yet) then whatever the last working directory was is what would be used. To capture that you would need a custom build target to create your own variable.

It means the folder that’s currently at the top of the side bar. That corresponds to your idea of being the first folder in the sublime_project file, with the added notion that even without a project, if you open a folder, this variable will still expand to that path.

You could consider it an if-else, yes. Variables are of the form ${variable:default_value}, where the :default_value part is optional. If a variable would expand out to be an empty string, then the default_value is used instead.

So here $working_dir is set to the path where the currently loaded sublime_project file is stored. If there isn’t a project loaded, that value is empty and so the default of ${folder:${file_path}} is used instead. That would be the currently open folder unless there isn’t one, in which case it’s the path of the currently open file. If there is also not a currently open file, then the working directory is just empty.

You could do that with a custom build target if you wanted that sort of functionality. However as far as I know it’s not possible by default (unless there is no folder structure under app in your example, in which case just use $file_path).

2 Likes