Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Run with PowerShell doesn't use scripts location as working directory #16811

Open
Jack251970 opened this issue Feb 14, 2025 · 13 comments · May be fixed by #16837
Open

Bug: Run with PowerShell doesn't use scripts location as working directory #16811

Jack251970 opened this issue Feb 14, 2025 · 13 comments · May be fixed by #16837

Comments

@Jack251970
Copy link
Contributor

Jack251970 commented Feb 14, 2025

Description

When I use system Explorer to run this powershell file with Run with Powershell Context Menu Item, it works well.

Image

But when I use Files to run powershell file with Run with Powershell Context Menu Item, I encounter this issue.

Image

These are the contents of my ps1 file and I just want to run one dotnet run task.

dotnet run --project build/Build.csproj
exit $LASTEXITCODE;

Related file tree is like that:

  • build.ps1
    • build/Build,csproj

Steps To Reproduce

  1. Create one VS project.
  2. Create ps1 file for it.
  3. Run this ps1 file.

Files Version

3.9.1.0

Windows Version

10.0.26100.3194

User ID

a2edf6d2-76d7-4004-bceb-5cd7ffa27d13

Log File

debug.log

@Josh65-2201
Copy link
Member

Thanks for the report, can you screenshot the full PowerShell window

@Josh65-2201 Josh65-2201 added the needs - additional info Needs more information from the reporter label Feb 14, 2025
@yaira2
Copy link
Member

yaira2 commented Feb 14, 2025

Are you using the action on the toolbar, or the shell extension in the context menu?

@Jack251970
Copy link
Contributor Author

Thanks for the report, can you screenshot the full PowerShell window

Sure! This is the full screenshot.

Image

@Jack251970
Copy link
Contributor Author

Are you using the action on the toolbar, or the shell extension in the context menu?

@yaira2 I find that neither of them can work. They both give me the issue as above.

Image

@Josh65-2201
Copy link
Member

Are you able to add the working directory path to the script and send the output

@Jack251970
Copy link
Contributor Author

@Josh65-2201 Hello, thanks for you reply! I think I have found the reason. If I want to execute ps1 file in Files, I need to add one more line: Set-Location -Path $PSScriptRoot to set working directory to the script's location.

However, I am very curious that why files set C:\WINDOWS\System32 by default. (For Explorer, you do not need to add this.)

Image

@Josh65-2201
Copy link
Member

Josh65-2201 commented Feb 16, 2025

It's not suppose to, you might be able to create a shortcut to the script and leave the start in target in properties blank to get the correct path.

@Josh65-2201 Josh65-2201 changed the title Bug: Dotnet Run PS File Failed to Run with Run with Powershell Context Menu Item Bug: Run with PowerShell doesn't use scripts location as working directory Feb 16, 2025
@Josh65-2201 Josh65-2201 removed the needs - additional info Needs more information from the reporter label Feb 16, 2025
@Jack251970
Copy link
Contributor Author

It's not suppose to, you might be able to create a shortcut to the script and leave the start in target in properties blank to get the correct path.

@Josh65-2201 Sorry I know really little about this. Could you provide me more details?

And if you do not think this is a issue, feel free to close it.

@Josh65-2201
Copy link
Member

It is an issue, what I suggest is a workaround until it fixed. You can right click the script file > Create shortcut > Right click the shortcut > Open properties > Go to Shortcut > Check that Start in text box is empty > Run the shortcut and see if it get the correct path

@ferrariofilippo
Copy link
Contributor

We might add an optional workingDirectory param to the function declaration and then assign it to Process.StartInfo.WorkingDirectory

private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options)
{
Process process = new();
process.StartInfo.FileName = "powershell.exe";

@yaira2
Copy link
Member

yaira2 commented Feb 19, 2025

Modfying CreatePowershellProcess can cause unexpected behavior elsewhere, but perhaps we can create a separate method that uses the working dirctory.

@ferrariofilippo
Copy link
Contributor

Modfying CreatePowershellProcess can cause unexpected behavior elsewhere, but perhaps we can create a separate method that uses the working dirctory.

Something like this shouldn't affect existing code

                private static Process CreatePowershellProcess(string command, PowerShellExecutionOptions options, string? wd = null)
                {
                    ...
                    if (wd is not null)
                        process.StartInfo.WorkingDirectory = wd;

@yaira2
Copy link
Member

yaira2 commented Feb 20, 2025

Let's try it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

Successfully merging a pull request may close this issue.

4 participants