dot sourcing and debugging

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 month and 1 week 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.
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

dot sourcing and debugging

Post by Lembasts »

Going on one of my previous posts:
viewtopic.php?p=85822#p85822

I have decided to dotsource my functions to share them between my PS Studio apps and other automation scripts.

What I have noticed is that everything works if I run the app normally but when I go into debug mode there appears to be a scoping issue.
On my main form, I assign stuff to $global:PSDefaultParameterValues. This appears to be lost when in a dotsourced function in debug mode.
Also, it appears you cannot set breakpoints in the dotsourced functions.
Don't know if there are ways of dealing with this?
Thanks
David
by brittneyr » Wed Aug 28, 2024 10:46 am
For debugging with dot sourced files, please try Debugging with Multiple Files (Ctrl+M):
SPS_RibbonDebugMultipleFiles.png
This will open a dialog with a list of files currently open in the editor to include while debugging:
SPS_SelectSecondaryFilesToDebug.png
Go to full post
User avatar
brittneyr
Site Admin
Posts: 1807
Last visit: Wed Nov 13, 2024 7:37 am
Answers: 44
Been upvoted: 34 times

Re: dot sourcing and debugging

Post by brittneyr »

For debugging with dot sourced files, please try Debugging with Multiple Files (Ctrl+M):
SPS_RibbonDebugMultipleFiles.png
SPS_RibbonDebugMultipleFiles.png (60.77 KiB) Viewed 1086 times
This will open a dialog with a list of files currently open in the editor to include while debugging:
SPS_SelectSecondaryFilesToDebug.png
SPS_SelectSecondaryFilesToDebug.png (12.5 KiB) Viewed 1086 times
Brittney
SAPIEN Technologies, Inc.
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

Re: dot sourcing and debugging

Post by Lembasts »

Hmm. Still have two issues.
I tried the 'multiple files' thing and breakpoints set in dotsourced functions arent actually working.
Also, global variables are not available in the dotsourced functions.
User avatar
brittneyr
Site Admin
Posts: 1807
Last visit: Wed Nov 13, 2024 7:37 am
Answers: 44
Been upvoted: 34 times

Re: dot sourcing and debugging

Post by brittneyr »

When debugging with the multiple files option, are the breakpoints from the dot sourced files written to the Tools Output pane?
Brittney
SAPIEN Technologies, Inc.
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

Re: dot sourcing and debugging

Post by Lembasts »

Yes it does say 'Line breakpoint on...' in the relevant ps1 file but it doesnt stop when getting to the debug point.
User avatar
brittneyr
Site Admin
Posts: 1807
Last visit: Wed Nov 13, 2024 7:37 am
Answers: 44
Been upvoted: 34 times

Re: dot sourcing and debugging

Post by brittneyr »

With my tests, I haven't found any issues with debugging with multiple files with the latest build of PowerShell Studio. I also have not found any issues with global variables and debugging.

Please answer the following:
What build of PowerShell Studio are you using?
Is it just this one dot sourced file or is it all dot sourced files having this issue?
Is the line you where you have the breakpoint set being reached?
Are you using Windows PowerShell or PowerShell 7?
If you have multiple copies/version of the same file that is dot sourced, do you have the correct file open in the editor?
Brittney
SAPIEN Technologies, Inc.
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

Re: dot sourcing and debugging

Post by Lembasts »

What build of PowerShell Studio are you using?
247
Is it just this one dot sourced file or is it all dot sourced files having this issue?
All
Is the line you where you have the breakpoint set being reached?
Yes -absolutely!
Are you using Windows PowerShell or PowerShell 7?
V5.1
If you have multiple copies/version of the same file that is dot sourced, do you have the correct file open in the editor?
Only one copy of each function file exists

Just to further describe the environment.
The project includes a file called commonfunctions.ps1. The 'Shared' property of this file is set to True.
When the full function code was included for all functions, everything worked fine.
I removed the full function code from all functions in Commonfunctions.ps1 and placed them on a server.
Commonfunctions.ps1 now has multiple lines in the format:

Code: Select all

. \\<server>\<share>\<filename>
When running as normal (without debug) everything works fine regarding globally scoped variables.
User avatar
brittneyr
Site Admin
Posts: 1807
Last visit: Wed Nov 13, 2024 7:37 am
Answers: 44
Been upvoted: 34 times

Re: dot sourcing and debugging

Post by brittneyr »

Are the functions from the dot sourced files recognized by the editor? If you hover over them, do you see the file information?

Please try the following:
-Set a breakpoint at a function call in your project from one of your dot sourced files.
-Verify that this dot sourced file is open in the editor with same exact path. This can be verified by hovering over the file tab.
-Start debugging with the Debug with Multiple files option with the dot sourced file checked.
-When it hits the breakpoint, step into the function (F11).
-When you stepped into the function, did it switch to that dot sourced file open in the editor? Or does it open a new tab?
Brittney
SAPIEN Technologies, Inc.
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

Re: dot sourcing and debugging

Post by Lembasts »

"same exact path" was the clue.
The dotsourced function was \\server\share1\filename
but the file open in the editor was \\server\share2\folder\folder\filename

It was the same file but not the "same exact path"
So when I opened it using the same path it worked!
Thanks
David
User avatar
Lembasts
Posts: 433
Last visit: Wed Nov 13, 2024 2:27 pm
Has voted: 3 times
Been upvoted: 1 time

Re: dot sourcing and debugging

Post by Lembasts »

So I had a ps1 file with multiple lines dotsourcing my functions like:
. \\ps\functions$\add-thingy.ps1

...and that all worked fine.
I thought it was more efficient to not specify each line so I changed that ps1 file to remove all the individual dotsourcing lines and run this code:

Code: Select all

$functionpath = '\\ps\functions$\'
$functions = Get-ChildItem -path "$functionpath\*.ps1" -name | where { $_ -notlike '*tempPoint*' }
foreach ($function in $functions) {
    . ($functionpath + $function)
}
When I run this in the PS studio console all is fine. But when I build an exe package it doesnt work.
Any ideas?
Thanks
David
This topic is 1 month and 1 week 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.