public void CanDotSourcePath(string rawFileName) { string fullPath = Path.Combine(ScriptAssetPath, rawFileName); string quotedPath = PowerShellContext.QuoteEscapeString(fullPath); var psCommand = new System.Management.Automation.PSCommand().AddScript($". {quotedPath}"); using (var pwsh = System.Management.Automation.PowerShell.Create()) { pwsh.Commands = psCommand; pwsh.Invoke(); } }
public void CorrectlyQuoteEscapesPaths(string unquotedPath, string expectedQuotedPath) { string extensionQuotedPath = PowerShellContext.QuoteEscapeString(unquotedPath); Assert.Equal(expectedQuotedPath, extensionQuotedPath); }
protected async Task HandleLaunchRequest( LaunchRequestArguments launchParams, RequestContext <object> requestContext) { RegisterEventHandlers(); // Determine whether or not the working directory should be set in the PowerShellContext. if ((_editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Local) && !_editorSession.DebugService.IsDebuggerStopped) { // Get the working directory that was passed via the debug config // (either via launch.json or generated via no-config debug). string workingDir = launchParams.Cwd; // Assuming we have a non-empty/null working dir, unescape the path and verify // the path exists and is a directory. if (!string.IsNullOrEmpty(workingDir)) { try { if ((File.GetAttributes(workingDir) & FileAttributes.Directory) != FileAttributes.Directory) { workingDir = Path.GetDirectoryName(workingDir); } } catch (Exception ex) { workingDir = null; Logger.Write( LogLevel.Error, $"The specified 'cwd' path is invalid: '{launchParams.Cwd}'. Error: {ex.Message}"); } } // If we have no working dir by this point and we are running in a temp console, // pick some reasonable default. if (string.IsNullOrEmpty(workingDir) && launchParams.CreateTemporaryIntegratedConsole) { #if CoreCLR //TODO: RKH 2018-06-26 .NET standard 2.0 has added Environment.CurrentDirectory - let's use it. workingDir = AppContext.BaseDirectory; #else workingDir = Environment.CurrentDirectory; #endif } // At this point, we will either have a working dir that should be set to cwd in // the PowerShellContext or the user has requested (via an empty/null cwd) that // the working dir should not be changed. if (!string.IsNullOrEmpty(workingDir)) { await _editorSession.PowerShellContext.SetWorkingDirectory(workingDir, isPathAlreadyEscaped : false); } Logger.Write(LogLevel.Verbose, $"Working dir " + (string.IsNullOrEmpty(workingDir) ? "not set." : $"set to '{workingDir}'")); } // Prepare arguments to the script - if specified string arguments = null; if ((launchParams.Args != null) && (launchParams.Args.Length > 0)) { var sb = new StringBuilder(); for (int i = 0; i < launchParams.Args.Length; i++) { sb.Append(PowerShellContext.QuoteEscapeString(launchParams.Args[i])); if (i < launchParams.Args.Length - 1) { sb.Append(' '); } } arguments = sb.ToString(); Logger.Write(LogLevel.Verbose, "Script arguments are: " + arguments); } // Store the launch parameters so that they can be used later _noDebug = launchParams.NoDebug; _scriptToLaunch = launchParams.Script; _arguments = arguments; IsUsingTempIntegratedConsole = launchParams.CreateTemporaryIntegratedConsole; // If the current session is remote, map the script path to the remote // machine if necessary if (_scriptToLaunch != null && _editorSession.PowerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote) { _scriptToLaunch = _editorSession.RemoteFileManager.GetMappedPath( _scriptToLaunch, _editorSession.PowerShellContext.CurrentRunspace); } await requestContext.SendResult(null); // If no script is being launched, mark this as an interactive // debugging session _isInteractiveDebugSession = string.IsNullOrEmpty(_scriptToLaunch); // Send the InitializedEvent so that the debugger will continue // sending configuration requests await _messageSender.SendEvent( InitializedEvent.Type, null); }