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 support broken #198

Closed
hohwille opened this issue Aug 20, 2019 · 17 comments
Closed

PowerShell support broken #198

hohwille opened this issue Aug 20, 2019 · 17 comments
Labels
bug Something isn't working help wanted Extra attention is needed powershell specific for Microsoft PowerShell scripts related to shell scripts (bash and CMD) windows specific for Microsoft Windows OS
Milestone

Comments

@hohwille
Copy link
Member

I am not sure if it is related to #178 but now PowerShell integration (Open Devon PowerShell here) stopped working.
When I click on it, I get this error message in a Popup:

This file does not have a program associated with it for performing this action. Please install a program or, if already installed, create an association in the Default Programs control panel.
@hohwille hohwille added bug Something isn't working powershell specific for Microsoft PowerShell scripts related to shell scripts (bash and CMD) windows specific for Microsoft Windows OS labels Aug 20, 2019
@hohwille hohwille added this to the release:3.0.0 milestone Aug 20, 2019
@hohwille
Copy link
Member Author

I fixed the path to devon.bat in the registry file. I already tested this on windows but it still fails with the same error.

@hohwille
Copy link
Member Author

hohwille commented Aug 20, 2019

OK. Seems as if the variable resolution does not work anymore. We should go for profile.ps1 file like we do for other shells.
However, devon.bat does not work properly in powershel while it works in CMD.
It is entirely wired. I hacked some debug code into it and did tweak this block:

...
:setup_env
set "WORKSPACE=main"
echo seting up devon ide
pushd %CD%
echo buh
:iterate_backwards
echo hi
if exist scripts\environment-project.bat (
  echo a
) else (
  echo b
)
echo here
if exist scripts\environment-project.bat (
  echo foo
  set DEVON_IDE_HOME=%CD%
  echo DEVON_IDE_HOME=%CD%
  call scripts\environment-project.bat
  echo devon-ide environment variables have been set for %CD% (workspace %WORKSPACE%)
  popd
  goto :eof
) else (
  echo mmmmmm
)
echo bla
...

When I call devon.bat in powershell, I get this output:

PS C:\projects\cool-project\system\windows\power-shell\Test> devon.bat
seting up devon ide
buh
hi
b
here
PS C:\projects\cool-project\system\windows\power-shell\Test>

Please note that if I update the working echo statements, I get the updated output so it is for sure I am editing the right file that is also called from powershell.
But how can the same if else block evaluate fine with only echos but terminate the entire processing in the second case? Why dont I get "mmmmmm" or "bla" as output?
I am totally puzzled. Windows sucks so hard...

@hohwille
Copy link
Member Author

I outcommented every single statement in the problematic if.
The error comes from the goto statement.
Is goto not working inside if-statements when I am inside power-shell?
OMG, Windows is such a pile of hacks.

@hohwille
Copy link
Member Author

Is somebody keen on implementing and maintaining a devon.ps1 script as well?
Or should we simply drop power-shell support?

hohwille added a commit that referenced this issue Aug 20, 2019
#178: also use reg import for uninstall avoiding admin rights
#191: fixed conflict with repo and shared software
#198: power-shell improvement
#199: also remove devon.bat on uninstall
#200: update installed devon[.bat] if newer
#202: fixed version compare and added tests
#203: workaround as cp -u is not working on MacOS
@hohwille
Copy link
Member Author

I found another bug and I assume this could never ever have worked before:
When powershell runs a command (e.g. devon.bat) it does not behave like CMD but like a real shell. That is, it creates a sub-shell where all the side-effects of that command take place but they do not interfere the parent shall. Therefore devon.bat can properly set the path but afterwards it exists and returns to the powershell that does not see any of these changes to environment variables.
That is why we are not executing the devon command in bash but source it. As CMD is based on the side effects we had this different approach for windows CMD so far.
https://stackoverflow.com/questions/49027851/setting-environment-variables-with-batch-file-lauched-by-powershell-script

@hohwille
Copy link
Member Author

Sourcing is supported by powershell:
https://superuser.com/questions/71446/equivalent-of-bashs-source-command-in-powershell

However, Sorry to say so, but powershell is broken. Maybe it is because windows 10 comes with powershell 1.0 while version 6 is already out. But this entirely sucks and I am giving up. It seems that some windows fan who is keen on powershell support would need to take over.

PS C:\projects\cool-project\system\windows\power-shell> . $env:USERPROFILE/scripts/devon.bat
devon-ide environment variables have been set for C:\projects\cool-project in workspace main
PS C:\projects\cool-project\system\windows\power-shell> mvn -v
mvn : The term 'mvn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ mvn -v
+ ~~~
    + CategoryInfo          : ObjectNotFound: (mvn:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS C:\projects\cool-project\system\windows\power-shell> echo $env:PATH
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Git\cmd;C:\Users\hohwille\AppData\Local\Microsoft\WindowsApps;C:\Users\hohwille\scripts;
PS C:\projects\cool-project\system\windows\power-shell> devon mvn -v
Running: /c/projects/cool-project/software/maven/bin/mvn -v
Apache Maven 3.6.1 (d66c9c0b3152b2e69ee9bac180bb8fcc8e6af555; 2019-04-04T21:00:29+02:00)
Maven home: C:\projects\cool-project\software\maven
Java version: 11.0.4, vendor: AdoptOpenJDK, runtime: C:\projects\cool-project\software\java
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
devon-ide environment variables have been set for C:\projects\cool-project in workspace main
PS C:\projects\cool-project\system\windows\power-shell>

@hohwille
Copy link
Member Author

Set-Alias : A positional parameter cannot be found that accepts argument 'C:\Users\hohwille\scripts\devon.bat'.
At \\vmware-host\Shared Folders\Documents\WindowsPowerShell\profile.ps1:1 char:1
+ Set-Alias -Name devon -Value . $env:USERPROFILE\scripts\devon.bat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-Alias], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SetAliasCommand

So all tricky and nothing working as expected with powershell.
Even

echo Welcome to PowerShell

Is giving this output:

Welcome
to
PowerShell

There are really nice concepts in PowerShell but I am facing too many drawbacks here.

I think it is a good move that Microsoft deeply integrates bash, ssh and the entire linux subsystem as well as docker into windows. I will therefore focus on bash and leave powershell support open to some drivers who really want to have this feature. I will do a minimum fix now and are done.

BTW: profile.ps1 is not resolved from $env:USERPROFILE\Documents\WindowsPowerShell\profile.ps1. On my machine Documents are mounted from VMWareFusion to a totally different location:

$profile | Format-List * -force

AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    : \\vmware-host\Shared Folders\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : \\vmware-host\Shared Folders\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 89

So if someone wants to take over, he would have to figure out via which variables one can figure out where to put or find profile.ps1.

@hohwille hohwille modified the milestones: release:3.0.0, nice-to-have Aug 21, 2019
@hohwille
Copy link
Member Author

So with PR #208 I created a minimum fix so powershell is working in general. It will be included in bat16 release.
However, it does not support to update environment variables by just calling devon and also the look and feel of the powershell is taken from CMD while powershell typically has this blue apprearance with other fonts and layout that does not show up.

Feel free to create a profile.ps1 that solves all the issues and contribute a PR that creates this profile during windows setup.bat (and ideally reverts these changes in devon ide uninstall commandlet). So I am leaving the field for heros that can create such a PR.

@hohwille hohwille added the help wanted Extra attention is needed label Aug 21, 2019
@markusschuh
Copy link
Member

For the moment just a few additional hints:

  1. Environment variables are expanded in registry values only (at least for windows own purposes) if type REG_EXPAND_SZ is used. This type can't be set for new Default values with help of regedit AFAIK, but reg can do that. (see below)

  2. I have removed the quoted braces after _pushd. Windows CMD delegates argument resolving to each single command and for pushd space escape is not necessary, (Tested on Win7 and Win10) In combination with the /s of cmd the escape quote even seem to cause trouble here. (at least for Win10)

  3. I have introduced start here. The started process inherits the cmd environment. So when the "Prompt here" was used on a devon instance base the powershell will keep the environment changed by devon.bat.

In summary this results in a command like this
reg add HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\devon_ps\command /ve /t REG_EXPAND_SZ /d "cmd.exe /s /c pushd %l & %USERPROFILE%\\scripts\\devon.bat & start powershell.exe -NoExit -Command Set-Location -LiteralPath '%l'"

Some topics are still open though:

  1. This will need to be run for all four set keys of the current reg file.

  2. It will probably be necessary to add an /f in order to forcibly overwrite whatever exist.

  3. I have not yet tested if this reg automatically adds missing registry path elements above.

And besides the missing topics all this will of course not solve the fundamental issue, that the original concept of devonfw-ide - Run a command inside the current whatever shell to initialize path and other environment so it matches with the current devon instance - will not work without some native powershell scripting. This were IMHO far too much work. It would be easier, if there were introduced another subcommand like "devon init´powershell" or alike, that would start another powershell process window in such a case with the potentially modified environment. The devonfw approach to use a pure - argument free - "devon" for the same is unusual - compared to other sdk CLI commands - and is IMHO the root cause for the need of different hacks here.

@hohwille
Copy link
Member Author

@markusschuh Thanks for your experts input that is always extremely helpful. 👍

The advantage of the *.reg files is IMHO that special users can omit the setup and manually decide which of the registry tweaks to install.
If I got your explanation (esp. 1.) right, we can not use environment variables in a *.ref file but in such case need to use reg add to directly write the value.
However, we could simply omit the variable by replacing %USERPROFILE%\\scripts\\devon.bat with just devon.bat as this has been added to our PATH by setup just before.
Also I noticed that with reg add I can end up in interactive mode:

Value  exists, overwrite(Yes/No)? yes

So what you are saying with your second 2. I also need to add /f switch.

However, first I will try without absolute path and variable via reg file.

What is great about your fix is that then power-shell comes back up shiny in the used layout so people really notice the difference between power-shell and CMD.

@hohwille
Copy link
Member Author

So for Directory\shell it is working as proposed. However, for Background\shell it is not working properly:

The filename, directory name, or volume label syntax is incorrect.

Power-shell then works but like before looks like CMD.

@hohwille
Copy link
Member Author

Ah, I see. The look & feel just comes from the start command in front that I forgot for background.
Then my question is: Why do we need this pushd at all?
All I would need to do is adding the start to the current reg file and are done for what we reached so far.

@hohwille
Copy link
Member Author

OK, fixed.
The remaining issue is the environment modification of a pure devon command invocation that is not even working with the sourcing feature of powershell:

PS C:\projects> cd .\cool-project\
PS C:\projects\cool-project> . .\scripts\devon
devon-ide environment variables have been set for C:\projects\cool-project in workspace main
PS C:\projects\cool-project> mvn -v
mvn : The term 'mvn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ mvn -v
+ ~~~
    + CategoryInfo          : ObjectNotFound: (mvn:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I think I am now getting why:
The devon.bat is sourced by it does not change the environment variables actually. It calls another script environment-project.bat that is setting the variables. This is invoked via call:
https://github.com/devonfw/devon-ide/blob/cad2ceef89f56736cf5a6e876303533d4038ae80/scripts/src/main/resources/scripts/devon.bat#L111

So IMHO we would need to figure out if we are running in power-shell at this code-line and if so then do not use call but again use source instead. Right?

@hohwille
Copy link
Member Author

hohwille commented Aug 23, 2019

source seems to be an internal function of power-shell that can not be used from bat file:

'source' is not recognized as an internal or external command,
operable program or batch file.

So in other words we could only re-implement devon.bat and environment-project.bat for Powershell and maintain it redundantly. I am not going to do this now.
Volunteers are welcome but after Windows 10 still comes with power-shell 1.0 I do not see the huge benefit of full power-shell support.

@hohwille
Copy link
Member Author

My last try way:

PS C:\projects\cool-project> $env:DEVON_IDE_HOME="C:\projects\cool-project"
PS C:\projects\cool-project> . .\scripts\environment-project.bat

C:\projects\cool-project>rem This batch is not supposed to be called manually
PS C:\projects\cool-project> mvn -v
mvn : The term 'mvn' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ mvn -v
+ ~~~
    + CategoryInfo          : ObjectNotFound: (mvn:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

So I am giving up.

hohwille added a commit that referenced this issue Aug 23, 2019
#198: fixed power-shell look&feel issue
#213: fixed cygwin setup
#214: fixed usage documentation
#216: no symlinks for cygwin
#219 fixed undefined variables
#220: use mklink on windows
#5: reduced export
in Eclipse show also IDE folder name in titlebar
@hohwille
Copy link
Member Author

Improved with PR #221
I am closing this issue now.
If someone expects the devon command to update environments properly in powershell he needs to file a new issue and most probably also provide a pull-request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed powershell specific for Microsoft PowerShell scripts related to shell scripts (bash and CMD) windows specific for Microsoft Windows OS
Projects
None yet
Development

No branches or pull requests

2 participants