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

Install-ChocolateyShortcut - Don't create a folder if environment variable is used #1687

Closed
FranklinYu opened this issue Dec 5, 2018 · 6 comments

Comments

@FranklinYu
Copy link
Contributor

What You Are Seeing?

New folder %MyVar% appears in current directory when installing package with Install-ChocolateyShortcut.

What is Expected?

This folder does not appear, because in runtime the folder will be expanded with value of environment variable MyVar instead of verbatim %MyVar%.

How Did You Get This To Happen? (Steps to Reproduce)

  1. Create a package with choco new.

  2. In chocolateyInstall.ps1, add following statement:

    $args = @{
        shortcutFilePath = $shortcut
        targetPath = $exe
        WorkingDirectory = '%MyVar%'
    }
    Install-ChocolateyShortcut -ShortcutFilePath @args
  3. Pack the package with choco pack.

  4. Install the package.

Possible Cause

Install-ChocolateyShortcut automatically created the folder for working directory is the folder is missing. This is an (undocumented) feature.

When there is variable in it (such as %AppData%), Windows will not set the working directory to a folder named %AppData% in current directory. Instead, it will try to expand the variable, and set the result as working directory, at runtime. User can change the working directory by changing the environment variable, without changing the shortcut file itself. This is useful in multi-user environment, where each user has different %AppData%. Current helper creates the shortcut just well, so a small modification will be sufficient to support this pattern.

Output Log

(Omitted.)

@pauby
Copy link
Member

pauby commented Dec 5, 2018

I'm not really sure I understand this one so forgive me if I've missed something.

This folder does not appear, because in runtime the folder will be expanded with value of environment variable MyVar instead of verbatim %MyVar%.

This won't happen. %MyVar% is not a PowerShell variable. $env:MyVar is. So if you pass %MyVar% to the code it will interpret it literally as it's supposed to do.

Why can't you use $env:MyVar?

@FranklinYu
Copy link
Contributor Author

FranklinYu commented Dec 5, 2018

@pauby I exactly don't want it to understand. I want %MyVar% to be in the shortcut verbatim. So if you check the property of generated link, it shows exactly %MyVar% (yes, this 7 characters) in the Working Directory field.

And again, the shortcut is good. It is generated as I expected. I just want to correct the behavior of folder-creation.

FranklinYu added a commit to FranklinYu/choco that referenced this issue Dec 6, 2018
This will allow environment variables in shortcut working directory.
Environment variables will be kept in shortcut file, and be expanded by
Windows in runtime. This is useful when creating Start-menu items for
multi-user system.
@ferventcoder
Copy link
Member

ferventcoder commented Jan 18, 2019

@pauby This is what gets saved verbatim in the shortcut link, then Windows expands it when you access the .lnk file. It can't be a PowerShell variable because it uses cmd.exe to run.

@FranklinYu
Copy link
Contributor Author

@ferventcoder “Up For Grabs”? I thought we already have #1686?

ferventcoder pushed a commit that referenced this issue Mar 13, 2019
This will allow environment variables in shortcut working directory.
Environment variables will be kept in shortcut file, and be expanded by
Windows in runtime. This is useful when creating Start-menu items for
multi-user system.
ferventcoder added a commit that referenced this issue Mar 13, 2019
* pr1686:
  (GH-1687) Allow env var in shortcut working dir
ferventcoder added a commit that referenced this issue Mar 13, 2019
* stable:
  (GH-1687) Allow env var in shortcut working dir
@ferventcoder
Copy link
Member

This will be released with 0.10.12.

@ferventcoder
Copy link
Member

ferventcoder commented Mar 13, 2019

“Up For Grabs”? I thought we already have #1686?

@FranklinYu yes we do, the up for grabs label is what we add for community contributions whether or not there was an existing PR. Something you can ignore, it's not something we later remove. 👍

@ferventcoder ferventcoder changed the title Don't create the folder if variable detected Install-ChocolateyShortcut - Don't create a folder if environment variable is used Mar 13, 2019
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

4 participants