Search code examples
fluttervisual-studio-codevscode-tasks

Problem of using args in VS Code custom tasks.json


So, Inside of the tasks array in the .vscode/tasks.json, I have this one task:

{
      "label": "buildReleaseIos",
      "type": "shell",
      "command": "flutter build ios -t lib/main_prod.dart --dart-define=DART_DEFINE_APP_NAME=App Name --dart-define=DART_DEFINE_APP_SUFFIX=.myapp"
}

This task is valid and I'm able to execute it properly.

The problem is, if I move the --dart-define from command to the args array (which I think is the better practice), like this:

{
      "label": "buildReleaseIos",
      "type": "shell",
      "command": "flutter build ios -t lib/main_prod.dart",
      "args": [
        "--dart-define=BCP_DART_DEFINE_APP_NAME=App Name",
        "--dart-define=BCP_DART_DEFINE_APP_SUFFIX=.myapp",
      ]
}

There's this error:

> Executing task: 'flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp <

/bin/bash: flutter build ios -t lib/main_prod.dart: No such file or directory
The terminal process "/bin/bash '-c', ''flutter build ios -t lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' --dart-define=DART_DEFINE_APP_SUFFIX=.myapp'" terminated with exit code: 127.

But, I also have similar args usage in the launch.json which works fine:

{
      "name": "Flutter Debug Prod",
      "request": "launch",
      "type": "dart",
      "program": "lib/main_prod.dart",
      "args": [
        "--dart-define=DART_DEFINE_APP_NAME=App Name",
      ],
}

Maybe I'm mistaken on how args in tasks.json works? Can you show me how to properly use args?

Thanks


Solution

  • That translation in bash should look like

    "/bin/bash '-c', 'flutter' 'build' 'ios' '-t' 'lib/main_prod.dart' '--dart-define=DART_DEFINE_APP_NAME=App Name' '--dart-define=DART_DEFINE_APP_SUFFIX=.myapp'
    

    which means you need flutter in program and everything else as separate elements of args, or you can just put everything in program, but then whitespace quoting can be a bit wonky.