-
Notifications
You must be signed in to change notification settings - Fork 8.1k
shebang without .ps1 extension ends up in recursive loop calling powershell #3963
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -635,7 +635,41 @@ private CommandInfo GetInfoFromPath(string path) | |
| break; | ||
| } | ||
|
|
||
| if (String.Equals(extension, StringLiterals.PowerShellScriptFileExtension, StringComparison.OrdinalIgnoreCase)) | ||
| // handle case where we are called by the OS to handle a shebang script using us as the interpreter | ||
| bool isShebangScript = false; | ||
| try | ||
| { | ||
| using(FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) | ||
| { | ||
| byte[] bytes = new byte[2]; | ||
| int bytesRead = fileStream.Read(bytes, 0, 2); // read the first two bytes to determine if shebang | ||
| if (bytesRead == 2) | ||
| { | ||
| if (bytes[0] == '#' && bytes[1] == '!') | ||
| { | ||
| // see if we are supposed to be the interpreter | ||
| using(StreamReader file = new StreamReader(fileStream)) | ||
| { | ||
| string interpreter = file.ReadLine(); | ||
| System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should have an internal flag
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should certainly cache this since this if using shebang with PowerShell becomes popular. I have to explicitly check if the interpreter is exactly the same as the running PowerShell otherwise I let the OS handle it as we would want to support scripts that target specific versions of PowerShell instead of just the system default one. Actually this reminds me that the current code won't work with
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I open PR #3969 to cache the reflection - we can use
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // this returns path to powershell.dll | ||
| string powershellPath = assembly.Location.Replace(".dll",""); | ||
| // need to handle both powershell and powershell.exe | ||
| if (interpreter.Split(' ',2)[0].Replace(".exe","") == powershellPath) | ||
| { | ||
| isShebangScript = true; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| catch(Exception) | ||
| { | ||
| // If we can't read the file, safe to assume it's not a script | ||
| } | ||
|
|
||
| if (isShebangScript || String.Equals(extension, StringLiterals.PowerShellScriptFileExtension, StringComparison.OrdinalIgnoreCase) || isShebangScript) | ||
| { | ||
| if ((_commandTypes & CommandTypes.ExternalScript) != 0) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a lot of code! Can we just read the line (with length < Path.Max) and check Shebang with
RegEx.IsMatch()? And it seems we're going to have less trouble with BOM.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to limit how much gets read (first two bytes) since every file will get analyzed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In either case, the entire file will be read by OS for native processing or PowerShell processing. The question is only in this local buffer. I guess 64K is not a problem.