intellij-powershell icon indicating copy to clipboard operation
intellij-powershell copied to clipboard

Run/Debug default working directory

Open b-amaral opened this issue 5 years ago • 7 comments

Hi, is it possible to make the default working directory be the directory where the .ps1 file is located, or the root of current intellij project directory?

I read the issue #3, the improve here is that we don't have to configure the working directory for every file.

Thanks in advance.

b-amaral avatar Apr 11 '19 12:04 b-amaral

You can configure default Run/Debug Configuration (for current project only), see Changing Run/Debug Configuration Templates. Does it help?

ant-druha avatar Apr 11 '19 12:04 ant-druha

Thanks for replying, it kind of help, but as i understand, still when i jump from one project to another i have to change the default on template, right? It's not a big trouble but would be nice if the plugin takes care of that

b-amaral avatar Apr 11 '19 12:04 b-amaral

There is a request in IDE platform for this feature, please feel free to vote for IDEA-65915.

ant-druha avatar Apr 11 '19 12:04 ant-druha

Cool thanks.

b-amaral avatar Apr 11 '19 12:04 b-amaral

Hi,

sorry for reviving this old issue, but I thought since this is still open and closely related to my question I hope its okay.

My question is that it seems that the working directory only accepts an absolute path. Is that right?

Would it be possible to support relative paths and/or predefined variables in the working directory input? This would make it a lot easier when sharing config files using VCS (since that feature is now added) with others using different directories. With respect to the root of relative paths I would guess it would be natural that it was relative to the directory of the current script file?

Best Regards Kristoffer

Krande avatar Jun 04 '21 10:06 Krande

@Krande actually, the IDE must automatically substitute the paths with the pre-defined in IDE path macros variables like $PROJECT_DIR$ in Run Configuration paths. It would be nice if you open a separate request and show what exactly is not working as you would like to in run configurations. Thanks. E.g. here is the example of a run configuration IDE creates for me (notice the scriptUrl and workingDirectory paths):

<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="test-ps-version.ps1" type="PowerShellRunType" factoryName="PowerShell" scriptUrl="$PROJECT_DIR$/set_content_remote/test-ps-version.ps1" workingDirectory="$PROJECT_DIR$">
    <method />
  </configuration>
</component>

ant-druha avatar Jun 04 '21 12:06 ant-druha

Let's start from the questions y'all asked during the discussion, since I believe there's still some information we are missing here.

@b-amaral,

is it possible to make the default working directory be the directory where the .ps1 file is located

Not yet (but see below!).

is it possible to make the default working directory be […] the root of current intellij project directory?

Yes, it is supposed to work if you override your parameters in the run configuration template. But apparently it doesn't. I've filed an issue on that: #138.

@b-amaral,

as i understand, still when i jump from one project to another i have to change the default on template, right?

The matter is kinda complicated, but this is not entirely correct. There's an additional layer of indirection available: the run configuration templates for new projects (File → New Projects Setup → Run Configuration Templates…). It is possible to set up a template that will be copied to each new project you create or open[^1] in an IntelliJ-based IDE, and you even can write some interesting things there: for example, you can set the working directory as $PROJECT_DIR$, and it will always be resolved on template instantiation to the current project directory.

I think this is also broken due to the same reason highlighted in #138. We'll fix that.

@Krande,

My question is that it seems that the working directory only accepts an absolute path. Is that right?

No, this is not right (but essentially you are correct since everything other than absolute paths is not very useful). It accepts any kind of path, but certain paths are useless to enter, and I'll explain why.

  1. If no path is entered, then the default will be used. "The default" is custom-defined by the plugin to be $env:HOME or user.home Java system property or user.dir Java system property (essentially this is a fancy way of saying we use the user profile dir by default).
  2. If an absolute path is entered, you get it.
  3. If a relative path is entered, then it's resolved normally according to the operating system rules. The downside is that the operating system rules define it as a path relative to the working directory of the current process, in our case the IDE process. In different versions and different operating systems, the actual IDE working directory may be pretty wild, and you've apparently observed a variant of that: the bin directory from the IDE installation. It is mostly useless to refer to "just a relative path" there currently, since it depends on the way you start the IDE, and it's unstable: for example, it is not guaranteed it will always be the project directory.
  4. An important note: the IDE tries to internally resolve most paths relatively to the project root and store them in a relative way (try setting different paths and see what's getting stored in .idea/workspace.xml after you close the IDE).

@Krande,

Would it be possible to support relative paths and/or predefined variables in the working directory input?

It should work with the IDE path variables already, but we'll figure out more about that after I get to the issue #63 you reported.

@Krande,

I would guess it would be natural that it was relative to the directory of the current script file?

I am still a bit conflicted about this. For my own projects, I more often see the need of running the scripts from the project root than from their own directory. But I think I've invented a solution that should be good enough.

The Solution

I totally agree with you folks that the current default behavior is kinda not helpful: barely anybody wants to run their scripts from the home directory and not the project root. But note that it's also our responsibility to keep compatibility with the previous versions of behavior and not break anything on update.

And so, I believe we should do this.

  1. The configurations saved by the previous versions of the plugin, if there was a convention of if (workDir == null) workDir = $HOME, should still work the way they did before. This is for me to investigate if it was possible, or if the working directory was always materialized on save.
  2. Of course, we'll fix #138, but there should be less need to use it in the future.
  3. For new configurations created by the plugin, we should
    • by default, set the working directory to the project directory
    • we should track how the user changes the script file path in realtime, and if the previous value of the path field was corresponding to the default we've calculated, we should update it with the path change as well
      • I.e. if you don't change the working dir manually, it tries to automatically track the parent path of whatever you've entered. But if you do change it manually, then it stays what you've entered.
        • Perhaps we could also add a "Reset" button there to quickly jump to the calculated default in case you've accidentally typed something?
    • we use a similar editing approach in Rider run configs, so I'll check how that works
  4. The people who want the configuration to run in the project root may use the browse dialog that has an option to quickly reset to the project root.
  5. The people who want to run their script in the home dir could also use the folder browser features to more or less quickly set it up to what they want.

[^1]: Provided you never opened it in an IDE before, i.e. there's no .idea folder.

ForNeVeR avatar Sep 17 '23 22:09 ForNeVeR