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: Delimit envrionment variable names when setting #1255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

BlackHoleFox
Copy link

This is the simplest fix for the problem outlined in #1254: It just uses a brace pattern to delimit what the environment variable name key should be regardless of how key needs escaped. This lets it determine that the escape quotes are only for key instantiation.

With this patch cding in and out of my project directory works exactly like I'd expect and has no errors.

There might be another fix, like stripping unnecessary quoting, but this seems more robust and like it will work in more cases. I don't know how to write a test for this specific issue though since I'm unsure of how to minimize a Nix flake's output into direnv's test case definitions. If regression tests are wanted, help is appreciated please :)

cc @bamsammich as the original PowerShell support implementer

Closes #1254

Without this many exports, such as `$env:'depsBuildTargetPropagated'='';`,
are not valid Powershell and result in an error being thrown:
```
Invoke-Expression -Command $export;
     |        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Variable reference is not valid. ':' was not followed by a valid
     | variable name character. Consider using ${} to delimit the name
```

The quotes around escaped variable names cause Powershell to be confused
because it can't figure out what is part of the variable reference. So just
delimit the name to make it clear what's part of the name and what's part
of the value.
Copy link
Contributor

@bamsammich bamsammich left a comment

Choose a reason for hiding this comment

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

LGTM

@bamsammich
Copy link
Contributor

@BlackHoleFox I thought there was a PR check to run tests, but that doesn't appear to be true. Did you run the PowerShell test suite to confirm they still pass?

Copy link
Contributor

@bamsammich bamsammich left a comment

Choose a reason for hiding this comment

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

After testing this a bit, we didn't get the expected behavior with this change. The problem exists in escaping the key, meaning that this change will set ${env:'key'}=value literally in the environment, including quotes.

To fix this, you must comb through the escape cases to find the proper method for escaping environment variable names/keys.

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

Successfully merging this pull request may close these issues.

direnv with powershell fails to export Nix flake variables
2 participants