Skip to content

allow ps1 files to be executed without pwsh/powershell -c file.ps1 - #14379

Merged
WindSoilder merged 5 commits into
nushell:mainfrom
fdncred:run_ps1_files
Nov 20, 2024
Merged

WindSoilder merged 5 commits into
nushell:mainfrom
fdncred:run_ps1_files

Conversation

@fdncred

@fdncred fdncred commented Nov 18, 2024

Copy link
Copy Markdown
Contributor

Description

This PR allows nushell to run powershell scripts easier. You can already do powershell -c script.ps1 but this PR takes it a step further by doing the powershell -c part for you. So, if you have script.ps1 you can execute it by running it in the command position of the repl.
image

or once it's in json, just consume it with nushell.
image

User-Facing Changes

Easier to run powershell scripts. It should work on Windows with powershell.exe and mac/linux if you have pwsh.exe installed. More testing is needed.

Tests + Formatting

After Submitting

@NotTheDr01ds

NotTheDr01ds commented Nov 18, 2024 •

Copy link
Copy Markdown
Contributor

I'd love to see this also require the script to (1) be on the search path or (2) require an absolute or relative path (e.g. ./test.ps1). This would mimic the Linux equivalent functionality, right?

@fdncred

fdncred commented Nov 18, 2024

Copy link
Copy Markdown
Contributor Author

The implementation works similar to how the nushell script functionality works on Windows. I'd prefer to leave it like it is for now. However, it should still work like ./test1.ps1 or .\test1.ps1. It's just not required.

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I like the idea!
Do you think it's possible to add a windows test for it?

@fdncred

fdncred commented Nov 19, 2024

Copy link
Copy Markdown
Contributor Author

Do you think it's possible to add a windows test for it?

@WindSoilder I'm not familiar with these type of tests and can't get it to work. Maybe you'll have some insight.

I added this test below to nushell\crates\nu-command\tests\commands\run_external.rs

#[cfg(windows)]
#[test]
fn can_run_ps1_files() {
    use nu_test_support::fs::Stub::FileWithContent;
    Playground::setup("run a Windows ps file", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContent(
            "foo.ps1",
            r#"
                Write-Host Hello World
            "#,
        )]);

        let actual = nu!(cwd: dirs.test(), pipeline("foo.ps1"));
        assert!(actual.out.contains("Hello World"));
    });
}

Then it gives this error, which is a powershell error that shows up when it can't find a file. I'm not sure why it's not finding my foo.ps1 file because I the test does the same as the foo.bat and foo.cmd.

running 1 test
test commands::run_external::can_run_ps1_files ... FAILED

successes:

successes:

failures:

---- commands::run_external::can_run_ps1_files stdout ----
=== stderr
foo.ps1 : The term 'foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, 
verify that the path is correct and try again.
At line:1 char:1
+ foo.ps1
+ ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (foo.ps1:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

@fdncred

fdncred commented Nov 19, 2024

Copy link
Copy Markdown
Contributor Author

I think I may have figured it out. For some reason, I can't have a path with spaces in it and i have to provide the full path to the ps1 file in the test. I don't understand why but it seems to be passing now.

@fdncred
fdncred marked this pull request as draft November 19, 2024 19:05
@fdncred
fdncred marked this pull request as ready for review November 19, 2024 19:19
@fdncred

fdncred commented Nov 19, 2024

Copy link
Copy Markdown
Contributor Author

I tried to make it work on non-windows where pwsh existed, but it seems like too much trouble for now. I'll settle for Windows only atm.

@WindSoilder WindSoilder left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM

@WindSoilder
WindSoilder merged commit 42d2adc into nushell:main Nov 20, 2024
@WindSoilder WindSoilder added the notes:mention Noted in "Hall of Fame" section label Nov 20, 2024
@github-actions github-actions Bot added this to the v0.101.0 milestone Nov 20, 2024
@fdncred
fdncred deleted the run_ps1_files branch November 20, 2024 14:59
@pfmoore

pfmoore commented Dec 28, 2024

Copy link
Copy Markdown

Is it possible to configure this to use pwsh rather than powershell? The two have different behaviours (powershell is missing a lot of the newer pwsh features) and many of my scripts don't work with powershell.

@fdncred

fdncred commented Dec 28, 2024

Copy link
Copy Markdown
Contributor Author

I chose to make it powershell since it is installed on every modern Windows installation, pwsh is not. There could be a config point written/added to allow this but there currently is not.

@pfmoore

pfmoore commented Dec 28, 2024

Copy link
Copy Markdown

Yes, I think powershell is the correct default, but I would like it to be configurable as pwsh is basically just better (when it's installed!)

I don't know the codebase, so I'm not sure if I could write a PR myself to make this configurable, unfortunately :-(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:mention Noted in "Hall of Fame" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants