Using $ENV variables in using module statement
My interest is to see the ability for using an environment variable for the using module statement:
using module $env:MY_DIR\build\module.psm1
The variables already exist prior to running powershell, and I imagine they have to be loaded prior to modules, so I would assume that such would be possible, but I am not sure how the back end works on this subject.
The problem with this is that using module is a parse time concept. So that means:
using module already makes parsing really slow, but it's worth mentioningThe variables already exist prior to running powershell, and I imagine they have to be loaded prior to modules, so I would assume that such would be possible, but I am not sure how the back end works on this subject.
They might exist, but they're not constants. They'll still be pulled on every request because you can change them at any point.
Also it's worth noting that you can use using module .\path\relative\to\file.psm1 and the . will work like $PSScriptRoot. Everything else in PowerShell uses that to refer to the current working directory except using statements.
. will work like
$PSScriptRoot
I didn't know that - not relative to $PWD like most other directory access? Given that it would cause a rewrite in how the parser works, I'm not sure the feature request adds anything.
. will work like
$PSScriptRootI didn't know that - not relative to
$PWDlike most other directory access?
Yup 馃檪, except when using forward slashes due to a bug (#7424), then it's relative.
Super confusing, but tbh I'm not sure what else they could have done. Maybe treat PSScriptRoot and only PSScriptRoot as a constant, but 馃し鈥嶁檪 that'd be confusing too.
except when using forward slashes due to a bug (#7424), then it's relative
I don't even know what to say. I'm surprised that's not a higher priority bugfix for 7.0 GA. To stay on-topic: I think you make good points for why this isn't feasible as it would make for a miserable authoring experience moving forward (although I've seen mixed adoption of the using statement in general due to knowing the nuances this deep)
I understand.
I've learned you can also use ..\path to get a parent, etc
Its just when you are writing different runtime scripts, and importing modules often, especially in different points of the program, and potentially while being in different directories- It gets hard to keep track of it. It's more of a convenience suggestion, if it was possible.
@MaynardMiner If you need the dynamic behavior, I'd recommend sticking to Import-Module and importing your class definitions using Invoke-Expression to avoid the parse-time errors associated with PowerShell v5 classes.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Most helpful comment
I don't even know what to say. I'm surprised that's not a higher priority bugfix for 7.0 GA. To stay on-topic: I think you make good points for why this isn't feasible as it would make for a miserable authoring experience moving forward (although I've seen mixed adoption of the
usingstatement in general due to knowing the nuances this deep)