Baran
Baran

Reputation: 394

Can't use 'cd' command in Dart CLI application

I am working on a Dart CLI application. I've clonned Flutter project from git into a directory, now I want to get in this directory and run flutter pub get in it. But I am having error. Btw I'm using dcli package.

Here is my method:

  void _runFlutterPubGet(String path) {
    'cd $path'.run;
    'flutter pub get'.run;
  }

Here is the error:

Unhandled exception:
cd C:\Users\baran\Software\Self\use_template\bin\trial_app 
exit: 2
reason: Could not find cd on the path.
wait_for_ex.dart : waitForEx : 21
runnable_process.dart : RunnableProcess._waitForStart : 300
runnable_process.dart : RunnableProcess.start : 278
runnable_process.dart : RunnableProcess.run : 167
run.dart : start : 249
string_as_process.dart : StringAsProcess.run : 80
use_template_base.dart : UseTemplateBase._runFlutterPubGet : 281
use_template_base.dart : UseTemplateBase.exec : 235
use_template.dart : main : 45
isolate_patch.dart : _delayEntrypointInvocation.<anonymous closure> : 295
isolate_patch.dart : _RawReceivePortImpl._handleMessage : 192

Upvotes: 0

Views: 650

Answers (3)

Brett Sutton
Brett Sutton

Reputation: 4584

Just a follow-up.

I'm the author of dcli.

You should never use cd in any program.

I've written an article about it. https://dcli.onepub.dev/dcli-api/the-evils-of-cd

Where required, dcli functions take a working directory (as you found) and ideally you should pass an absolute path.

Upvotes: 1

borealis-c
borealis-c

Reputation: 692

Just for information: cd is a internal command of the command shell interpreter cmd.exe. Therefore there is no cd.com/cd.exe, which you can call. When you start a shell like you do now (runInShell = true), you are able to submit the cd command as the shell interpreter takes over.

Upvotes: 1

Baran
Baran

Reputation: 394

I've changed my code to this:

  void _runFlutterPubGet(String path) {
    run('flutter pub get', workingDirectory: path, runInShell: true);
  }

As @julemand101 stated. Solved problem. Thanks for the answer.

Upvotes: 2

Related Questions