Intellisense/Caching problem, loading assembly

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 year and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
HelmiSokrates
Posts: 12
Last visit: Thu Feb 29, 2024 5:11 am

Intellisense/Caching problem, loading assembly

Post by HelmiSokrates »

Product, version and build: PS Studio 5.8.217
Operating system: Win10 Enterprise
PowerShell version(s): 5.1

Hello,

I've problems with the caching functionality of PowerShell Studio (or understanding how it works) and loading an .NET assembly.

I'm working on a PS project that i just transferred from one large ps1-file to multiple files in PS Studio and now have the following structure:

Project root
- Assembly
-- MyLibrary.dll [Build Order 1]
- Classes folder
-- ClassA.ps1 [Build Order 3]
-- ClassB.ps1 [Build Order 4]
-- ClassC.ps1 [Build Order 5]
-- ClassD.ps1 [Build Order 6]
- Enums folder
-- Enum1.ps1 [Build Order 2]
-- Enum2.ps1 [Build Order 3]
Startup.pss [Build Order 0]
MainClass.ps1 [Build Order 7]

Enum1 is referenced by ClassA
Enum2 is referenced by ClassB
ClassA is referenced by ClassB
ClassB is referenced by ClassC
MainClass references to ClassC and ClassD

"Shared"-Property of classes and enums is True

The "Functions" pane shows the following structure:
ClassA.ps1
- Classes
-- ClassA
ClassB.ps1
- Classes
-- ClassB
-- ClassB
ClassC.ps1
- Classes
-- ClassC
ClassD.ps1
- Classes
-- ClassD
Enum1.ps1
- Classes
-- Enum1
-- Enum1
Enum2.ps1
- Classes
-- Enum2

* I don't know why ClassB and Enum1 are displayed twice. They are definitively only declared once and just shared by the "Shared"-property (so no explitig referencing in other project files)
* Although the enums are displayed in the functions pane, the Intellisense does not know them
* Although the Intellisense knows ClassB and ClassD, the Editor marks them with an error "Type [..] not found"
* Before transferring the project I've included the assembly with Add-Type. Now i tried the same in Startup.pss and/or adding it as module, but whatever I do, it results in "Type [..] not found"
* ScriptAnalyzer shows multiple TypeNotFound errors for the Assembly, but not for the other Classes

I tried rebuilding and refreshing the cache multiple times, also restarting PS Studio.
I'm really frustrated. I hope there's an easy way to handle this.

Btw. is there an option in PS Studio to suppress ScriptAnalyzer notifications like "...is not available by default in PowerShell versions 3,4" - I don't care about v3 and v4?

Regards
Florian
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Intellisense/Caching problem, loading assembly

Post by brittneyr »

For your first issue, without seeing the project, I'm not sure exactly what is happening. If you can, please upload it here:
https://www.sapien.com/support/upload

Are your project files' Build property set to Include or Content? This could be causing the behavior you are seeing. When a project is being built, it combines files marked as Include into one file when running or exporting the project. If files have the Build setting to Content, then the file is simply copied over. If you are doing this, you will need to make sure the file is dot sourced if you are referencing it.

You need to add the Add-Type reference with your assembly. Please make sure that the assembly can be found by the final script. If you project files' Build setting value is Content, then you will need to Add-Type reference in the files that are referencing that assembly.

Cache is for modules, not class or assembly information. This is to make sure PowerShell Studio is able to provide PrimalSense for modules installed on your machine.

You may find the following articles helpful:
https://www.sapien.com/blog/2022/03/15/ ... roduction/
https://www.sapien.com/blog/2022/08/03/ ... -projects/

As for suppressing ScriptAnalyzer notifications about v3 and v4, we'll look into this and get back to you.
Brittney
SAPIEN Technologies, Inc.
HelmiSokrates
Posts: 12
Last visit: Thu Feb 29, 2024 5:11 am

Re: Intellisense/Caching problem, loading assembly

Post by HelmiSokrates »

Thanks for your reply!

I just uploaded the project.

The ps-files are included, the assemblies are defined as Content.

I thought, when setting the file assemblies for Startup.pss via menu they are configured for use by PowerShell Studio. But also when I (additionally) try to Add-Type them in Startup.pss, same errors occur/intellisense does not know them.

Thank you for your explanation and the blog references!
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Intellisense/Caching problem, loading assembly

Post by brittneyr »

After looking over your project, I see a couple things. As your script files build setting are set to 'Include', all of these scripts will be combined into one script. You should not need to dot source them in you Startup.pss file. To demonstrate what I'm talking about, you can export the project from the ribbon to a script (Export->Export to File). I recommend opening the resulting export.ps1 file to see how your final script is being generated.

As for your Add-Type assembly references, please try moving them to the top of the file, out of the Main function.
Brittney
SAPIEN Technologies, Inc.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Intellisense/Caching problem, loading assembly

Post by brittneyr »

As per the issue discussed in your other post (viewtopic.php?p=84249#p84249),
please remove the references in the assemblies dialog and move your Add-Type references to the top of your Startup.pss file.
Brittney
SAPIEN Technologies, Inc.
HelmiSokrates
Posts: 12
Last visit: Thu Feb 29, 2024 5:11 am

Re: Intellisense/Caching problem, loading assembly

Post by HelmiSokrates »

Thank you for your reply and sorry for my late reply!

I now deleted the project and started an empty one and I'm afraid the intellisense problems still exist (for now I excluded the assemblies and just tested with the enums).

No assemblies are added to the list.

Following code is abridged where marked:

Globals.ps1 (Build order 0, Shared true)
  1. function Get-ScriptDirectory
  2. {
  3.     # ABRIDGED
  4.     return <directory>
  5. }
  6.  
  7. [string]$ScriptDirectory = Get-ScriptDirectory
Startup.pss (Build order 1)
  1. function Main
  2. {
  3.     # ABRIDGED - defult code only
  4. }
Enums\EnumA.ps1 (Build order 2, Shared true)
  1. enum EnumA
  2. {
  3.     Unknown
  4.     Easy
  5.     Medium
  6.     Hard
  7. }
Classes\ClassZ.ps1 (Build order 3, Shared true)
  1. class ClassZ
  2. {
  3.     ClassZ()
  4.     {
  5.  
  6.     }
  7.    
  8.     [EnumA]$AnyValue
  9. }
Line "[EnumA]$AnyValue" is marked with: Unable to find type [EnumA].

Thanks for the hint with exporting the script!
Running the script (PSStudio, PowerShell) works fine.
Creating an instance of ClassZ in Startup.pss also works and intellisense knows ClassZ
When putting the enum code directly above the class code in ClassZ.ps1, EnumA is recognized.

So with this basic project it seems the problem is that intellisense won't recognize enums declared in other files.
For now I do as described above and just move the code directly to the class files.
But, as already written in my other post, if this is a bug it wouldn't bother me, if this will be fixed in the future :-)
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 3:14 pm
Answers: 39
Been upvoted: 30 times

Re: Intellisense/Caching problem, loading assembly

Post by brittneyr »

Are your files ClassZ.ps1 and EnumA.ps1 Build setting set to 'Include' or 'Content'?

From your previous upload, I'm not able to reproduce this behavior when the project files Build setting is set to 'Include':
SPS_BuildSetting.png
SPS_BuildSetting.png (16.12 KiB) Viewed 4616 times
Brittney
SAPIEN Technologies, Inc.
HelmiSokrates
Posts: 12
Last visit: Thu Feb 29, 2024 5:11 am

Re: Intellisense/Caching problem, loading assembly

Post by HelmiSokrates »

I apologize for my late reply.

The Builod setting is/was set to Include.

But after the last update of PSStudio everything works as expected. I had no more issues with caching.

Thank you very much for your support!
This topic is 1 year and 2 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.