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

If the package uses $packageParameters instead of $env:PackageParameters, quotes are removed #406

Closed
dhilgarth opened this issue Sep 11, 2015 · 23 comments
Assignees
Milestone

Comments

@dhilgarth
Copy link

I am trying to pass quoted values to the -params switch, but it looks like it is simply not working.

In a test package, I am simply outputting $packageParameters to see what I get.

Here is what I tried:

  • -params '/licenseName:""Daniel Hilgarth""'
  • -params '"/licenseName:""Daniel Hilgarth"""'
  • -params '"/licenseName:"Daniel Hilgarth""'
  • -params '/licenseName:"Daniel Hilgarth"'
  • -params "/licenseName:""Daniel Hilgarth"""

Both of them resulted in a $packageParameters value fof /licenseName:Daniel Hilgarth

Additionally, I tried these:

  • -params '"/licenseName:\"Daniel Hilgarth\""' -> Resulted in the message that choco wants to install two packages: <package-name>;Hilgarth
  • -params '"/licenseName:"Daniel Hilgarth""' -> Result: /licenseName:Daniel Hilgarth``

I have the feeling that this is a bug in chocolatey's parameters parsing

@ferventcoder
Copy link
Member

Did you read the wiki/choco --help on how to pass options with quotes?

@ferventcoder
Copy link
Member

It's very specific on how to do this:

  • Pass quotes in arguments: When you need to pass quoted values to
    to something like a native installer, you are in for a world of fun. In
    cmd.exe you must pass it like this: -ia "/yo=""Spaces spaces""". In
    PowerShell.exe, you must pass it like this: -ia '/yo=""Spaces spaces""'.
    No other combination will work. In PowerShell.exe if you are on version
    v3+, you can try --% before -ia to just pass the args through as is,
    which means it should not require any special workarounds.

@ferventcoder
Copy link
Member

The above applies to any option, not just install args.

@dhilgarth
Copy link
Author

Yes, I did read this. Did you check the samples I posted? :-) Both of them I have tested and they did not work.

@ferventcoder
Copy link
Member

I did, I believe you are in PowerShell and you need -params '/licenseName:""Daniel Hilgarth""'

I didn't see it in the sample set.

@dhilgarth
Copy link
Author

It's the very first entry in the list of things I tried :)

@ferventcoder
Copy link
Member

What version of choco?

@dhilgarth
Copy link
Author

v0.9.9.8

@ferventcoder
Copy link
Member

Can you post a gist of the log file (relevant bits)?

@ferventcoder
Copy link
Member

I'm guessing a bug :)

@ferventcoder
Copy link
Member

Apologies, I've gotten this question about how to pass arguments quite a few times, I default to passing back the wiki article

@dhilgarth
Copy link
Author

Don't worry, I understand that you have to process issues very quickly.

Here is the gist:
https://gist.github.com/dhilgarth/77ce8e452d17375a3bc5

It contains the complete log for the test with -params '/licenseName:""Daniel Hilgarth""'

In line 129 is the output from my Write-Host $packageParameters (without double quotes) and in line 28 chocolatey reports what got passed in (with double quotes). So somewhere inbetween, those quotes are stripped out.

@ferventcoder
Copy link
Member

You know what - I'm guessing that something in the way this gets passed back to powershell may be doing something weird - look at line 128 https://gist.github.com/dhilgarth/77ce8e452d17375a3bc5#file-chocolatey-log-L128 (it's still passed with quotes).

@dhilgarth
Copy link
Author

Yes, that is the reason. I built a batch script to automatically pull down my boxstarter configuration etc. from a private git repository and I went nuts figuring out the quote escaping of the -Command parameter. Most likely, you need to escape the quotes. Depending on how you actually call the command, either by \" or by ``"`.

Here is my batch script as an example:

PowerShell -ExecutionPolicy Unrestricted -NoExit -Command $cloneScriptFolder = (join-path (gi $env:temp).FullName \"boxstarter\"); $cloneScript = (join-path $cloneScriptFolder \"clone.ps1\"); md $cloneScriptFolder -Force; wget http://downloads.fire-development.com/clone.ps1 -OutFile $cloneScript; $quotedCloneScript = '\"{0}\"' -f $cloneScript; Start-Process PowerShell -Verb RunAs -ArgumentList \"-ExecutionPolicy Unrestricted -NoExit -File $quotedCloneScript\"

@ferventcoder
Copy link
Member

It looks like the issue is right here - https://github.com/chocolatey/choco/blob/master/src/chocolatey/infrastructure.app/services/PowershellService.cs#L116-L117.

InstallArgs doesn't really suffer because it is also passed as an environment variable and is used later by chocolatey itself.

The package itself should probably be using $env:ChocolateyPackageParameters instead of $packageParameters. We should still fix this though.

@ferventcoder
Copy link
Member

@dhilgarth we commented at almost the same time. :)

@dhilgarth
Copy link
Author

I noticed :) I agree, that is where the quote escaping should happen.

@ferventcoder ferventcoder changed the title Package parameters: Can't quote values If the package uses $packageParameters instead of $env:PackageParameters, quotes are removed Sep 15, 2015
@ferventcoder
Copy link
Member

@dhilgarth if you feel comfortable providing a PR, I'd accept escaping quotes as ``"`.

@dhilgarth
Copy link
Author

Not sure that is correct. Would need to test.

@ferventcoder
Copy link
Member

But I dropped the priority since the package isn't following the recommendations of what to use. https://github.com/chocolatey/choco/blob/master/src/chocolatey/infrastructure.app/templates/ChocolateyReadMeTemplate.cs#L92

@dhilgarth
Copy link
Author

Indeed. I missed that - I just copied the code from somewhere, but I forgot from where. Maybe I simply missed that line that assigned the environment variable to the local variable.

@ferventcoder
Copy link
Member

Looks like the escape needed to be \"

ferventcoder added a commit that referenced this issue Sep 17, 2015
When using powershell arguments ( `$packageParameters`) instead of the
environment variables (`$env:chocolateyPackageParameters`), be sure
that the arguments are able to retain any quotes that are passed in so
that the two variables match. Otherwise you get strange behavior.

Folks should not really be using the passed in arguments as they are
not part of the public API, but that doesn't mean they won't use them.
ferventcoder added a commit that referenced this issue Sep 17, 2015
Rename function to something more readable.
ferventcoder added a commit that referenced this issue Sep 17, 2015
* stable:
  (GH-338) Specs
  (GH-338) Remove incompatible attributes in files
  (GH-338) IFilesService.ensure_compatible_file_attributes
  (GH-338) IFileSystem enhancements
  (GH-338) Fix ensure_file_attribute_set
  (maint) remove debug message
  (GH-406) rename function
  (GH-406) Passed powershell args retain quotes
@ferventcoder ferventcoder self-assigned this Sep 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants