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

adding Invoke-Command "$(direnv hook pwsh)" to $PROFILE doesn't seem to work #1207

Closed
nicksettoon opened this issue Dec 4, 2023 · 2 comments
Labels

Comments

@nicksettoon
Copy link

nicksettoon commented Dec 4, 2023

Describe the bug

I'm not entirely sure if this is a bug or if it just me not understanding powershell .envrc syntax.

It occurs regardless of project directory, so i'm assuming it's just out of date powershell version at the moment.

Any help is appreciated. Also the powershell documentation could really use some love imho. But I'm willing to be humbled and told that I missed some obvious documentation file.

Furthermore I'm looking for advice on how to set up a project so that the .envrc sets up the env correctly on all OSes; linux, windows, macOS, and freeBSD.

Regardless, thank you all for your hard work. This project is awesome.

To Reproduce

install scoop, then install direnv via

scoop install direnv

This also happens when compiling from source, as I had to in order to get the pwsh hook to be available. (technically direnv v2.32.0 though)

add Invoke-Command "$(direnv hook pwsh)" to $PROFILE

re-open powershell or windows-terminal

Expected behavior

direnv doesn't produce an error when the shell starts, and a simple environment variable TEST_VAR to be loaded when reloading the project directory, set to a value of "hello world!".

Environment

  • OS: windows 10 22H2, 19045.3693
  • Shell:

windows-terminal: 1.18.3181.0

bash_path: C:\ProgramData\scoop\shims\bash.exe

powershell:

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  3693  
  • Direnv version: 2.33.0

Additional context

this happens when i open powershell or windows-terminal

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

Unable to find type [LocationChangedEventArgs].
At line:1 char:81
+ ... .Automation;  $hook = [EventHandler[LocationChangedEventArgs]] {   pa ...
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (LocationChangedEventArgs:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

The property 'LocationChangedAction' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:586
+ ...  } else {   $ExecutionContext.SessionState.InvokeCommand.LocationChan ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : PropertyAssignmentException

Loading personal and system profiles took 693ms.
PS C:\> harness # alias to take me to the project directory

PS C:\users\myUser\path\to\project> cat .envrc

#! bash
export TEST_VAR="hello world!"

PS C:\users\myUser\path\to\project> direnv allow .

PS C:\users\myUser\path\to\project> cat C:\Users\myUser\.local\share\direnv\allow\*

C:\users\myUser\path\to\project\.envrc

PS C:\users\myUser\path\to\project> direnv reload
PS C:\users\myUser\path\to\project> echo $env:TEST_VAR

PS C:\users\myUser\path\to\project> echo $TEST_VAR

PS C:\users\myUser\path\to\project> direnv status

direnv exec path C:/Users/myUser/scoop/apps/direnv/current/direnv.exe
DIRENV_CONFIG C:\Users\myUser\.config\direnv
bash_path C:\ProgramData\scoop\shims\bash.exe
disable_stdin false
warn_timeout 5s
whitelist.prefix []
whitelist.exact map[]
No .envrc or .env loaded
Found RC path C:\users\myUser\path\to\project\.envrc
Found watch: ".envrc" - 2023-12-04T16:23:25-06:00
Found watch: "..\\..\\..\\..\\.local\\share\\direnv\\allow\\a2312b02a018c8b945157901e65891646b1ab732e4253b091fa6fbffbea0530d" - 1969-12-31T18:00:00-06:00
Found watch: "..\\..\\..\\..\\.local\\share\\direnv\\deny\\6e00c7a05a7914b651321ec3c996cc3f394e866e58ee4d586db3b635282fab51" - 1969-12-31T18:00:00-06:00
Found RC allowed 1
Found RC allowPath C:\Users\myUser\.local\share\direnv\allow\a2312b02a018c8b945157901e65891646b1ab732e4253b091fa6fbffbea0530d
@nicksettoon nicksettoon added the Bug label Dec 4, 2023
@patrickhuber
Copy link

patrickhuber commented Dec 5, 2023

I'm currently messing around with powershell as well. If I had to guess, that LocationChangedEventArgs only works in powershell version 7.2 and above https://learn.microsoft.com/en-us/dotnet/api/system.management.automation.locationchangedeventargs?view=powershellsdk-7.3.0

When you are using windows terminal, you have to manually set powershell 7.x to the default otherwise it drops down to 5.x. https://www.tenforums.com/tutorials/177955-how-change-default-profile-windows-terminal-app-windows-10-a.html

The DIRENV_CONFIG env var isn't registered for me, so I guess I have to insert that into the $PROFILE as well. Did scoop install that for you?

Update, turns out the XDG directories are required because the $env:HOME variable is not set on windows, the $HOME variable is set, but they are not the same thing. Code for finding default directories is here https://github.com/direnv/direnv/blob/master/xdg/xdg.go . I would assume something like os.UserHomeDir would be a better default.

Using this $PROFILE script I was able to get direnv to recognize the .envrc. I noticed it has to be a .envrc, I need to check why .env isn't working. Edit: this appears to be due to the missing direnv.toml https://direnv.net/man/direnv.toml.1.html#codeloaddotenvcode

$env:XDG_CACHE_HOME=(Join-Path $HOME .cache)
$env:XDG_DATA_HOME=(Join-Path $HOME .local share)
$env:XDG_CONFIG_HOME=(Join-Path $HOME .config)
if(-not(Test-Path $env:XDG_CACHE_HOME)){
	mkdir $env:XDG_CACHE_HOME
}
if(-not(Test-Path $env:XDG_DATA_HOME)){
	mkdir $env:XDG_DATA_HOME
}
if(-not(Test-Path $env:XDG_CONFIG_HOME)){
	mkdir $env:XDG_DATA_HOME
}
Invoke-Expression "$(direnv hook pwsh)"

Here is the output from $PSVersionTable

$PSVERSIONTABLE

Name                           Value
----                           -----
PSVersion                      7.4.0
PSEdition                      Core
GitCommitId                    7.4.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Now I'm getting an error (with redaction)

.envrc

export FOO=foo
cd direnvtest
/bin/bash: "{{REDACTED}}/scoop/apps/direnv/current/direnv.exe": No such file or directory
/bin/bash: __main__: command not found
direnv: error exit status 127

I did

scoop bucket add main
scoop install main/direnv

This appears to be direnv calling through bash and using a windows path in WSL for the executable?

@nicksettoon
Copy link
Author

nicksettoon commented Dec 5, 2023

perfect, that was it @patrickhuber

getting this now, but i'll have to assess later. this can be closed.

here's a really garbage scoop manifest for powershell 7.4.0

{
    "##": "This file is a template. Fill the blanks and add or remove fields as necessary.",
    "version": "7.4.0",
    "description": "Ideally, the latest version of Windows Powershell",
    "homepage": "https://github.com/PowerShell/PowerShell",
    "license": "MIT",
    "notes": "https://github.com/PowerShell/PowerShell/releases/tag/v7.4.0",
    "architecture": {
        "64bit": {
            "url": "https://github.com/PowerShell/PowerShell/releases/download/v7.4.0/PowerShell-7.4.0-win-x64.zip",
            "hash": "62151DB1D98A8B56AEB249CC8A3CE17948F1C83B4062DAC8D0C4302DE71CBD75"
        }
    },
    "bin": "pwsh.exe"
}

you might have to run the powershell that comes with your OS and run scoop reset * to get the scoop apps and other stuffs to work.

also the $PROFILE location is different for this version compared to mine.
$env:USERPROFILE\Documents\Powershell\Microsoft.PowerShell_profile.ps1

PowerShell 7.4.0
PS C:\users\myUser> harness
direnv: loading ~/path/to/project/nrkl/.envrc
direnv: export +COMMONPROGRAMFILES +COMSPEC +EXEPATH +MSYSTEM +PATH +PLINK_PROTOCOL +PROGRAMFILES +SYSTEMDRIVE +SYSTEMROOT +TERM +TEST_VAR +WINDIR -ComSpec -CommonProgramFiles -Path -ProgramFiles -SystemDrive -SystemRoot -windir
Remove-Item: A positional parameter cannot be found that accepts argument 'Path'.
Remove-Item: A positional parameter cannot be found that accepts argument 'ComSpec'.
Remove-Item: A positional parameter cannot be found that accepts argument 'windir'.
Remove-Item: A positional parameter cannot be found that accepts argument 'CommonProgramFiles'.
Remove-Item: A positional parameter cannot be found that accepts argument 'SystemDrive'.
Remove-Item: A positional parameter cannot be found that accepts argument 'ProgramFiles'.
Remove-Item: A positional parameter cannot be found that accepts argument 'SystemRoot'.

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

No branches or pull requests

2 participants