WPF Packaging Error when script includes event handler with dot sourcing.

Issue #118 new
Chris Kizziar created an issue

I’ve run into an interesting issue when trying to bundle my WPF project as an executable. It’s a (at least by my standards) fairly complex WPF project, and one of the buttons' event handlers dot sources a script file containing variables.

$WPFButtonOne.add_Click({
    IF ($WPFInstallIdSwitch.IsChecked) {
        IF ($WPFInstallId.Text.Length -eq 0) {
            [MahApps.Metro.Controls.Dialogs.DialogManager]::ShowModalMessageExternal($Window,"Error","Please enter a valid installation ID or select to proceed without one. ",$okOnlyDialog)
        }
        ELSEIF ($WPFInstallId.Text -match '^[0-9A-Z]{5}(-[0-9A-Z]{5}){3}$') {
            $InstallId = $WPFInstallId.Text
            Import-Config $InstallId
            . "$env:temp\$InstallId.cfg.ps1"
            $WPFTabTwo.Focus()
        }
        ELSE {
            [MahApps.Metro.Controls.Dialogs.DialogManager]::ShowModalMessageExternal($Window,"Error","The product key is not a valid format, please check your key. ",$okOnlyDialog)
        }
    }

The problem is that the ps1 file it references doesn’t exist in the project and hasn’t even been defined yet. Earlier in the application the user provides input, and based on this input I have a function that downloads the file from an API, so the entry in the event handler references a variable that doesn’t get set until the user provides an ID. This works beautifully in VS Debug, but when I try to build the package into an executable, it throws this error:

Severity    Code    Description Project File    Line    Suppression State
Error       The "PowerShellPackageTask" task failed unexpectedly.
System.NullReferenceException: Object reference not set to an instance of an object.
   at PowerShellToolsPro.Packager.Bundler.ResolveDotSourcedFileName(String root, String fileName, String& resolvedFileName)
   at PowerShellToolsPro.Packager.Bundler.FindReplacements(String root, IEnumerable`1 bareWordDotSourced, BundleResult bundleResult, Dictionary`2 replacements)
   at PowerShellToolsPro.Packager.Bundler.Bundle(String root)
   at PowerShellToolsPro.Packager.BundleStage.Execute(PackageProcess process, StageResult previousStage)
   at PowerShellToolsPro.Packager.PackageProcess.Execute()
   at PowerShellToolsPro.MsBuild.PowerShellPackageTask.Execute()
   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext() WPFTestProject  C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\PowerShell Tools for Visual Studio\PowerShellProTools.targets    5   

If I comment out the line with the dot source, it builds successfully. Is there a way for me to tell it to ignore a particular reference, or a better way to import the ps1 file so that the variables contained are available to the app? Thanks in advance for any help or guidance on this.

Environment:
Visual Studio Enterprise 2017 (15.9.11)
PowerShell Pro Tools 4.4.0
PSVersionTable:
Name                           Value
----                           -----
PSVersion                      5.1.14393.2879
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.2879
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Comments (2)

  1. Adam Driscoll

    Can you please validate with the latest release? I’m no longer seeing this behavior. The bundler should ignore paths it cannot resolve.

  2. Log in to comment