getPaths() Issues


#1

I’ve been having some trouble with the getPaths() method from atom.project. My original github issue: https://github.com/atom/atom/issues/13860#event-977163759. I hope that link provides what I need, so I won’t re-type everything here. Thanks for looking.
~Samwise


#2

Right, it get’s weirder. If I reset the path variable every time a command is run, the path variable is logged correctly, as it should be. However, the path that is being run by xterm still does not work. Does anyone have any insight on this?


#3

I hope I am understanding your question but:
When you set paths here Atom’s project manager is still in an uninitialized state.

Explanation

When you open an editor window, Atom follows these steps:

  1. Load all packages
  2. Deserialize the window (this also includes initializing atom.project)
  3. Activate all packages
  4. Execute your init script

The line paths = atom.project.getPaths().shift() gets executed in step 1 but atom.project.getPaths() returns an empty array until step 2 (and [].shift() is undefined). Your init script works because it’s loaded in step 4 when everything is already set up.

In your case you should probably just rewrite the entire file.
Don’t cache atom.project.getPaths(), there is nothing to gain from doing that.


#4

Would what I did for this PR work? https://github.com/ysgard/d-struct/pull/10


#5

Just resetting the variable won’t work because the commands object is still set in load-time (step 1). Remove paths altogether and generate the command string by calling a function. Something like this:

commands =
    build:
        win32: -> "start cmd /k \"cd #{atom.project.getPaths().shift()} && dub run\""
        darwin: -> "osascript -e \'tell application \"Terminal\" to do script \"cd #{atom.project.getPaths().shift()}; dub run\"\'"
        linux: -> "xterm -e \"bash -ic \'cd #{atom.project.getPaths().shift()} && dub run && read -s -n 1 line\'\""
    run:
        win32: -> "start cmd /k \"cd \"#{atom.project.getPaths().shift()}\" && dub build\""
        darwin: -> "osascript -e \'tell application \"Terminal\" to do script \"cd #{atom.project.getPaths().shift()}; dub build\"\'"
        linux: -> "xterm -e \"bash -ic \'cd #{atom.project.getPaths().shift()} && dub build && read -s -n 1 line\'\""

# Open a terminal and run dub
dub_build = ->
  # Run the terminal app
  popen commands.build[platform()]() if platform() of commands.build

# Open a terminal and run the app with dub
dub_run = ->
  # Run the terminal app
popen commands.run[platform()]() if platform() of commands.run

#6

See, the thing is, even if I don’t cache getPaths(), it still doesn’t work. Inserting getPaths() instead of my path variable into the instances where it is used produces the same result. I’ll try it, but I don’t expect anything different.


#7

In the run and build functions, I’ve got it logging the result of getPaths() to the console. The logging works. XTerm does not. It’s as if commands is not updated when it is used, but set up in stage one. What do I have to do to reset that?


#8

Nevermind all that I just said. I didn’t catch some of the syntax subtleties that coffee has in your code there, and it (more or less) works now. I’ve still got to go back to struggling with bash to make that command work, but I’ve got the API down. At this point, I even understand why your solution works. Thanks for the help.