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

powershell_script returns 0 for scripts with syntax errors #2247

Closed
awithersdd opened this issue Oct 17, 2014 · 4 comments
Closed

powershell_script returns 0 for scripts with syntax errors #2247

awithersdd opened this issue Oct 17, 2014 · 4 comments
Labels
Platform: Windows Type: Bug Does not work as expected.

Comments

@awithersdd
Copy link

I submitted this PR: #2244 but it only adds a test showing the problem.

The following:

powershell_script "fail" do
  code <<-EOS
    if (Test-Path .) echo foo
  EOS
end

Will indicate that "fail" ran successfully. It did not, powershell reported a parse error. Basically if you make a syntax error in your powershell script the only evidence of failure will be that whatever it was supposed to do will not be done.

I understand there are two needs here, one is to have the actual value of the error code but I'd put syntax error reporting above error code matching (-Command vs -File). The only solutions I had for this are terrible.

@sersut
Copy link
Contributor

sersut commented Oct 17, 2014

/cc: @adamedx

@carpnick
Copy link

Fixed this by calling out to Powershell 2 times per script call in a private gem. Not the best implementation but works.

          def syntax_check 
            local_script_contents=("function testme(){ \n #{@originalScript.to_s} \n} \n")

            result = run_command(shell,flags,filename,file_extension, local_script_contents, @options, false)
            if(result.stderr.length >= 1 || result.exitstatus !=0)
              raise RuntimeError,"Did not syntactically pass the powershell check.  \n Standard out: #{result.stdout} \n Standard Error:#{result.stderr}"
            end
          end

@adamedx
Copy link
Contributor

adamedx commented Mar 11, 2015

That's not unreasonable @carpnick -- we could probably just include the block twice in the script in the provider to see if that triggers errors for non-wellformed methods.
In general, the problem is with the trap statement generated by the provider -- if we don't exit in the handler, everything is fine.
I do have another working solution that is a little simpler -- it relies on using a global to detect that the trap handler was triggered (not great), and double-checking with the return result of the script block (not terrible) which must be $null in that case.
I'll send a pr with the example if I can get it to pass specs.
cc @jdmundrawala , @btm.

@ksubrama
Copy link

The fix has been merged in #3080

@thommay thommay added Type: Bug Does not work as expected. and removed Bug labels Jan 25, 2017
@chef chef locked and limited conversation to collaborators Nov 16, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Platform: Windows Type: Bug Does not work as expected.
Projects
None yet
Development

No branches or pull requests

7 participants