Use preLaunchTasks in flutter launch config - flutter

I am trying to run a preLaunchTask to define environment vars. The problem is that the tasks runs in a separate terminal while the flutter launch runs in the Debug Console.
Is there a way to launch the tasks in the debug console?
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "LoadEnvVars",
"command": "pwsh.exe",
"args": [
"-ExecutionPolicy",
"Bypass",
"-File",
"${workspaceFolder}/load_env_vars.ps1"
],
},
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "makon_front (pwsh)",
"request": "launch",
"type": "dart",
"args": ["--dart-define",
"FIREBASE_WEB_CONFIG=${env:FIREBASE_CONFIG_WEB}",
"--dart-define",
"USE_FB_EMULATOR=${env:USE_FB_EMULATOR}"],
"preLaunchTask": "LoadEnvVars"
},
{
"name": "makon_front (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "makon_front (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
],
}
I have tested the actual string in dart pad, and it works fine. I have also ran const bool.hasEnvironment in the Debug Console, it returns a false. Clearly the env vars are not loading in the debug console before dart launch. How do I do that?

Related

Preload module in VSCode Launch debug

I'm trying to configure my VSCode launch.json for my node app. My node command for starting the app uses the -r flag to preload a module (dotenv/config). How do I configure that in my launch.json? I can't figure it out.
My run command:
node -r dotenv/config server.js
My launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "My Launch",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceRoot}\\myapp\\src\\server.js",
"env": {
"MY_CONFIG_PATH": ".env"
}
}
]
}
I am also struggling with adding the preLoadedModules in my launch.json, but if you are just wanting to load your environment file you can add "envFile":"${workspaceFolder}/.env" to your launch.json and your app should run.
Example below:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\dist\\index.js",
"preLaunchTask": "tsc: build - tsconfig.json",
"envFile": "${workspaceFolder}/.env",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
]
}
]
}

Debug current open Flutter file in VSCode

I am writing a Flutter tutorial project in VS Code with multiple main() functions.
For example:
main.dart //Contains a main() function
step1.dart //Contains a main() function
step2.dart //Contains a main() function
If I have step1.dart open, then pressing F5 runs main.dart and not step1.dart in debug mode.
I can hover the mouse over the main() function in step1.dart, and then select 'debug' from the context menu. This works as expected and runs step1.dart in debug. However, there is no associated shortcut.
What can I press to run the active open file in debug mode, not main.dart?
As long as I have experience of having multiple flutter projects on a workspace (not too good feedback at all), the key is to set the pre-build process configuration file .vscode/launch.json, you can get more info on debugging#_launch-configurations.
When you create the file, it will look something like:
{
"version": "0.2.0",
"configurations": [
{
"name": "Flutter",
"request": "launch",
"type": "dart",
}
]
}
Then add "program": "lib/your-entry-point.dart":
{
"version": "0.2.0",
"configurations": [
{
"name": "main",
"request": "launch",
"type": "dart",
"program": "lib/main.dart"
},{
"name": "step1",
"request": "launch",
"type": "dart",
"program": "lib/step1.dart"
},{
"name": "step2",
"request": "launch",
"type": "dart",
"program": "lib/step2.dart"
}
]
}
This will then create the following launch options.
I made this launch.json file in the .vscode folder.
Worked like a charm.
{
"version": "0.2.0",
"configurations": [
{
"name": "Dart: Current File",
"type": "dart",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}"
},
]
}
Here are the variables you can use in launch.json.
https://code.visualstudio.com/docs/editor/variables-reference

Blazor client WASM launch configuration for VSCode

Where can I find an example of the .NET Core Launch (Blazor Standalone) launch configuration? And before you refer me to this https://learn.microsoft.com/en-us/aspnet/core/blazor/debug?tabs=visual-studio-code&view=aspnetcore-3.1#vscode I have already been there. No actual example of the configuration file is present.
I struggled with this too; Just debug with ".Net Core" should be first choice; should auto generate:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (Blazor Standalone)",
"type": "coreclr",
"request": "launch",
"program": "dotnet",
"args": [
"run"
],
"cwd": "${workspaceFolder}/src/Project.UI",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
{
"name": ".NET Core Debug Blazor Web Assembly in Chrome",
"type": "pwa-chrome",
"request": "launch",
"timeout": 30000,
"url": "https://localhost:5001",
"webRoot": "${workspaceFolder}/src/Project.UI",
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}"
}
]
}
You need to create a build task (task.json) first for the blazor WebAssembly
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/BlazorSVgTest.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
]
}
And then create a launch task (launch.json) with debug enabled
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/BlazorSVgTest.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
]
}
Okay, after revisiting this I now understand. in the launch.json I needed to add a line for "url": "https://localhost:{PORT}" and use the first port mentioned in Properties/launchSettings.json.
To get the browser to open I added "browser": "edge" but it does not automatically open the page so I do still need to manually navigate to the url but I can live with that. If anyone figured out how to get the working feel free to share. My launch.json looks like the following:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch and Debug Standalone Blazor WebAssembly App",
"type": "blazorwasm",
"request": "launch",
"url": "https://localhost:7051",
"browser": "edge"
}
]
}

Is it possible to launch a vscode extension with a set of custom settings?

I'm developing a vscode extension, and while testing it out I've hit a bit of a snag. Some functionality depends on custom settings that I define in the configuration of package.json. I would like to be able to launch a new VSCode window with the settings set to particular values, but I can't find how to do that.
What I have in launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceRoot}"
],
"ns.customSetting": "customValue"
},
{
"name": "Extension with another value",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceRoot}"
],
"ns.customSetting": "anotherValue"
}
]
}
That hasn't been working, but hopefully the idea is clear. I want that new window to open with the custom setting already set to the value I want. How do I do that?

How to run a command in Visual Studio Code with launch.json

Is there a way to execute an ssh command when debugging a project with .vscode/launch.json?
For example: ssh -i xxxxx.
Or is it possible to create a command that you can run from the F1 command palette pop-up? Something like RunCustomCommandxx.
You can define a task in your tasks.json file and specify that as the preLaunchTask in your launch.json and the task will be executed before debugging begins.
Example:
In tasks.json:
For version 0.1.0:
{
"version": "0.1.0",
"tasks": [{
"taskName": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"isShellCommand": true
}]
}
For version 2.0.0 (newer and recommended):
{
"version": "2.0.0",
"tasks": [{
"label": "echotest",
"command": "echo", // Could be any other shell command
"args": ["test"],
"type": "shell"
}]
}
In launch.json:
{
"configurations": [
{
// ...
"preLaunchTask": "echotest", // The name of the task defined above
// ...
}
]
}
Tasks documentation: https://code.visualstudio.com/docs/editor/tasks
Launch configuration: https://code.visualstudio.com/docs/editor/debugging#_launch-configurations
The format changed. Visual Studio Code can create the tasks.json file for you. Inside your launch.json file and inside any configurations object, just define preLaunchTask to force auto-creation of the tasks.json template file:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "launch program",
"skipFiles": [ "<node_internals>/**"],
"preLaunchTask": "myShellCommand",
"program": "${workspaceFolder}/test.js"
}
]
}
If you do not have file tasks.json configured:
launch the debugger. Visual Studio Code will inform you: "could not find the task
myShellCommand"
select Configure Task → Create tasks.json file
from template → Others
Your tasks.json template file will then be created for you. Add your command to command and the name you put in preLaunchTask to label:
{
"version": "2.0.0",
"tasks": [
{
"label": "myShellCommand",
"type": "shell",
"command": "echo goodfood"
}
]
}
For me, I just needed an environment variable, which is different. You don't want a task for this because (at least for me) it doesn't work when the launcher is run.
Thanks to here, I got it working like this, inside my launcher (launch.json) entry:
"environment": [{
"name": "ENVIRONMENT_VARIABLE_NAME",
"value": "${workspaceFolder}/lib" //Set to whatever value you want.
}],
My version of the configuration allows to just run a defined task and carried on (in my case, the task is to run the currently open Groovy file):
"configurations": [
{
"name": "Launch groovy",
"request": "launch",
"type": "coreclr",
"preLaunchTask": "groovy",
"program": "cmd.exe",
"args": ["/c"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
}
]
And the task:
"tasks": [
{
"label": "groovy",
"type": "shell",
"command": "groovy ${file}"
}
]
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch app",
"program": "lib/main.dart",
"request": "launch",
"type": "dart"
},
{
"name": "Build an APK Release",
"command": "flutter build apk --release",
"request": "launch",
"type": "node-terminal"
},
{
"name": "Install an APK on a device",
"command": "flutter install",
"request": "launch",
"type": "node-terminal"
}
]
}
for flutter developers who is searching how to run flutter build commands.
in tasks.json add
"tasks": [
{
"type": "flutter",
"command": "flutter",
"args": [
"pub",
"run",
"build_runner",
"build",
"--delete-conflicting-outputs"
],
"problemMatcher": [
"$dart-build_runner"
],
"group": "build",
"label": "flutter: flutter pub run build_runner build --delete-conflicting-outputs"
},
]
then from vscode you can try "run task" it will show you flutter: flutter pub run build_runner build --delete-conflicting-outputs
this way you don't need to memorize and type to terminal source code generation/build commands