Bug 429299 - launch Craft by running craftenv.ps1 falls on BadExpression error
Summary: launch Craft by running craftenv.ps1 falls on BadExpression error
Status: RESOLVED FIXED
Alias: None
Product: Craft
Classification: Developer tools
Component: Core (show other bugs)
Version: master
Platform: Other Microsoft Windows
: NOR normal
Target Milestone: ---
Assignee: Hannah von Reth
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-18 12:28 UTC by Gary Wang
Modified: 2020-11-19 10:30 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In: 2fd56e4fb2423e765f19823edc041063e2413809


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gary Wang 2020-11-18 12:28:10 UTC
SUMMARY

With clean install of craft, after it successful installed, and running craftenv.ps1 to get into the Craft shell, it will fall with PowerShell error.

STEPS TO REPRODUCE

1. Install Craft by running `iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/KDE/craft/master/setup/install_craft.ps1'))`
2. I choosed D:\CraftRoot\ as Craft Root, choosed MinGW as build system, and leaved other option as their default value.
3. Wait for installation finish.
4. Run `D:\CraftRoot\craft\craftenv.ps1` in the powershell window.
5. See the result.

OBSERVED RESULT

Craft shell won't work, and got the following error:


The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command name,
 a script block, or a CommandInfo object.
At D:\CraftRoot\craft\craftenv.ps1:130 char:8
+     (& $env:CRAFT_PYTHON ([IO.PATH]::COMBINE("$env:CraftRoot", "bin", ...
+        ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : BadExpression



EXPECTED RESULT

Able to enter a usable Craft shell.

SOFTWARE/OS VERSIONS
Windows: Windows 10 20H2, x64 installation.

ADDITIONAL INFORMATION

As mentioned in the description, the options used for installation is:

1. D:\CraftRoot\ as Craft Root
2. Use MinGW, not MSVC
3. Use colorful log (default)
4. Python is from system, 3.8 is used.

And also, I had tryed remove the CraftRoot folder and reinstall it but still falls on the same place. I'm not familiar with powershell at all so sorry I really don't know what's going on. Let me know if you need any additional information.

btw, an off-topic suggestion, it can be good if there is an offline installer for the first-time Craft setup, I have a bad internet and I actually tryed multiple time to re-install Craft but it sometimes falls on cloning the Craft repo from KDE invent website and all what I can do is delete the whole folder and start over again. Also if I can install it offline, I can setup a mirror to fetch binary faster, too.

Thanks.
Comment 1 Hannah von Reth 2020-11-18 12:33:27 UTC
Please follow the guide and update your powershell.
https://community.kde.org/Guidelines_and_HOWTOs/Build_from_source/Windows#Setting_up_a_powershell
Comment 2 Gary Wang 2020-11-19 01:58:29 UTC
(In reply to Hannah von Reth from comment #1)
> Please follow the guide and update your powershell.
> https://community.kde.org/Guidelines_and_HOWTOs/Build_from_source/
> Windows#Setting_up_a_powershell

I was using PowerShell 5.1 (according to the wiki page, seems >= 5.0 will be fine):

echo $PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
5      1      19041  610

After upgrade my PowerShell to 7.1.0 by downloading and installing PowerShell-7.1.0-win-x64.msi following the "In case you need a newer one, we recommend Powershell Core." link, I still got the same error:

InvalidOperation: D:\CraftRoot\craft\craftenv.ps1:130
Line |
 130 |      (& $env:CRAFT_PYTHON ([IO.PATH]::COMBINE("$env:CraftRoot", "bin", …
     |         ~~~~~~~~~~~~~~~~~
     | The expression after '&' in a pipeline element produced an object that was not valid. It must result
     | in a command name, a script block, or a CommandInfo object.


The "The latest version of the standard powershell can be found here." link seems outdated, it leads to a page says "Windows Management Framework 5.0 (Superceeded by WMF 5.1 RTM version: http://aka.ms/wmf5download)", also in the http://aka.ms/wmf5download page, after download and run W2K12-KB3191565-x64.msu, it says the update is not valid for my computer (not the original text. I'm not sure what's the original text since I'm not using English as the system language)


Let me know if you need any other information. Thanks!
Comment 3 Hannah von Reth 2020-11-19 08:23:33 UTC
Ok I guess $env:CRAFT_PYTHON is not properly defined.
Can you check your python install?
Can you  run "get-command py" "get-command python" "get-command python3" ?
What do they report.
Comment 4 Gary Wang 2020-11-19 09:00:03 UTC
(In reply to Hannah von Reth from comment #3)
> Ok I guess $env:CRAFT_PYTHON is not properly defined.
> Can you check your python install?
> Can you  run "get-command py" "get-command python" "get-command python3" ?
> What do they report.

get-command py

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     py.exe                                             3.8.515... C:\WINDOWS\py.exe


get-command python

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         0.0.0.0    C:\Users\Gary\AppData\Local\Microsoft\WindowsApps\python.exe


get-command python3

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python3.exe                                        0.0.0.0    C:\Users\Gary\AppData\Local\Microsoft\WindowsApps\python3.exe
Comment 5 Hannah von Reth 2020-11-19 09:04:50 UTC
Hmm that looks rather sane.

And you run D:\CraftRoot\craft\craftenv.ps1 in powershell core?
(You'll have a to start it explicitly it does not replace the system one)
Comment 6 Gary Wang 2020-11-19 09:42:01 UTC
(In reply to Hannah von Reth from comment #5)
> Hmm that looks rather sane.
> 
> And you run D:\CraftRoot\craft\craftenv.ps1 in powershell core?
> (You'll have a to start it explicitly it does not replace the system one)

Yes.

InvalidOperation: D:\CraftRoot\craft\craftenv.ps1:130
Line |
 130 |      (& $env:CRAFT_PYTHON ([IO.PATH]::COMBINE("$env:CraftRoot", "bin", …
     |         ~~~~~~~~~~~~~~~~~
     | The expression after '&' in a pipeline element produced an object that was not valid. It must result
     | in a command name, a script block, or a CommandInfo object.

PS D:\CraftRoot> echo $PSVersionTable.PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      1      0


The error message is actually prettier than the one I got from PowerShell 5.1

Is there any log file which I can get so you can get more useful data for troubleshooting? I know nothing about PowerShell so I don't know where I can look at for providing more useful information.
Comment 7 Hannah von Reth 2020-11-19 09:51:29 UTC
Hm not at that pint, you could add some print statements to https://invent.kde.org/packaging/craft/-/blob/master/craftenv.ps1#L24 

Like: write-host $py.Source $py.Version
Comment 8 Gary Wang 2020-11-19 10:10:39 UTC
(In reply to Hannah von Reth from comment #7)
> Hm not at that pint, you could add some print statements to
> https://invent.kde.org/packaging/craft/-/blob/master/craftenv.ps1#L24 
> 
> Like: write-host $py.Source $py.Version

Oh I got it! findPython("python3") and findPython("python") will always got version 0.0.0.0. Append a line findPython("py") after findPython("python") works and it can run under both PowerShell 5.1 and PowerShell 7.1 now! Here is the patch:



diff --git a/craftenv.ps1 b/craftenv.ps1
index 11ffe5221..b2bfb513a 100644
--- a/craftenv.ps1
+++ b/craftenv.ps1
@@ -79,6 +79,7 @@ findPython("python3.7")
 findPython("python3.6")
 findPython("python3")
 findPython("python")
+findPython("py")
 }

 function Global:craft()



Thanks a lot for helping me out!
Comment 9 Hannah von Reth 2020-11-19 10:30:05 UTC
You are right, thx https://invent.kde.org/packaging/craft/commit/2fd56e4fb2423e765f19823edc041063e2413809

The code there was a bit dated, from before we had the find function.