I find there are some differences that I can't figure out which nor how when executing a binary withpub run bin_executable.dart args rather than dart bin/bin_executable.dart args.
Execution is notably slow in the first form, and the output is worst, sometimes erroneous...
I searched here but didn't find anything useful. Any help?
pub run bin_executable.dart runs pub, and pub starts an new process that runs dart bin_executable.dart
There was a recent change that allows pub to run bin_executable.dart in a new Dart isolate instead of a new process. That might make it a little faster but I think the main purpose is to allow to attach a debugger to debug bin_executable.dart when run with pub (especially for pub run test)
I assume the main part or the slower execution is the start time for pub
Related
I have a web listener written in dart. I want to start it quicker, thats how I found https://dart.dev/tools/dart-compile#jit-snapshot.
However, when I execute dart compile jit-snapshot server.dart it runs the code which will run forever and so I never get the server.jit file. When I close the web-listener after a couple seconds it works fine but thats not what i want to compile.
Dart SDK version: 2.19.0-424.0.dev (dev) (Wed Nov 23 07:45:20 2022 -0800) on "linux_arm64"
dart compile jit-snapshot server.dart
should: generate server.jit
does: run forever. When I cancel it with Ctrl+c the .jit file is not generated...
For best performance, use AOT compilation
dart compile aot-snapshot server.dart
or build an executable
dart compile exe server.dart
As a newbie in dart dev , I starting by setting up my VS Code for Dart/Flutter .
When I try to run a simple Dart program ( just to check everything works fine printing sum of 2 int 4+6 )
PS B:\Flutter> dart .\Sum_Course_Exemple.dart
'git' is not recognized as an internal or external command,
operable program or batch file.
Building flutter tool...
Running pub upgrade...
4+6=10
These lines takes too long to show the output .
Am I doing something wrong ? why it's taking 3min to show that output for a simple code ?
Either Flutter or pub is trying to install or upgrade dependencies, but this relies on Git, which you do not have installed.
As a result, the Flutter SDK tries to perform some first-run initialisation every time you run your program, which is why it takes so long each time.
Git can be obtained from its website.
I couldn't find in the docs what are the differences (and which is preferred) between these 2 commands.
I can use both:
pub run
pub run myscript.dart
or
dart
dart myscript.dart
What are the differences between them?
I'm aware of this question: What does 'pub run' differs from normal execution?, but it's old and doesn't answer the question.
I have a simple dart project that has one executable in bin folder (test.dart). I have activated it with dart global activate and now I can run it directly with just typing the name of that executable file.
Inside that dart file I would like to know the path of that script. Basically for now I'm just printing something like this:
print('1: ' + Platform.script.toString());
print('2: ' + Platform.script.path);
print('3: ' + Platform.executable);
print('4: ' + Platform.packageRoot);
print('5: ' + Platform.resolvedExecutable);
When I run it directly:
test
or with pub:
pub global run test
or even with package name:
pub global run test:test
I always get the same result:
1: http://localhost:53783/test.dart
2: /test.dart
3: E:\apps\dart-sdk\bin\dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
The issue here is that I can't get the absolute path for test.dart file.
When I run it like this:
dart /path/to/project/bin/test.dart
I get what I need:
1: file:///E:/projects/dart/test/bin/test.dart
2: /E:/projects/dart/test/bin/test.dart
3: dart
4:
5: E:\apps\dart-sdk\bin\dart.exe
Is there a way how to get absolute path for a script that is currently running, regardless of a way how it was executed?
tl;dr: There's not a great way of doing what you want, but it's in the works.
The notion of a "path for a script that is currently running" is more complicated than it might sound at first blush. There are a number of ways that the Dart ecosystem invokes main(). Off the top of my head, here are a few:
Manually running the file with dart.
Running the file in an isolate.
Compiling the file to a snapshot and either manually running it or running it in an isolate.
Automatically compiling the file to a cached executable and running that either in a subprocess or in an isolate.
Adding a wrapper script that imports the file and invokes main(), and either running that in a subprocess or in an isolate.
Serving the file over HTTP, and running it either in a subprocess or in an isolate.
In some of these cases, the "script that is running" is actually a wrapper, not the original file you authored. In others, it's a snapshot that may have no inherent knowledge of the file from which it was created. In others, the file has been modified by transformers and the actual Dart code that's running isn't on disk at all.
I suspect what you're actually looking for isn't the executable URL itself, but the location of your package's files. We're working on a collection of APIs that will make this possible, as well as a resource package that will provide a nice API for dealing with your packages' resources.
Until that lands, there is a dart:mirrors hack you can use. If you give one of your library files an explicit library tag, say library my.cool.package, you can write:
var libPath = currentMirrorSystem().findLibrary(#my.cool.package).uri.path;
This will give you the path to your library, which you can use to figure out where your package's lib/ files live.
If you want a reliable way to access the current file while running in pub you'll need to use mirrors.
Here's a sample usage with dartdoc tests - https://github.com/dart-lang/dartdoc/blob/41a5e4d3f6e0084a9bc2af80546da331789f410d/test/compare_output_test.dart#L17
import 'dart:mirrors';
Uri get _currentFileUri =>
(reflect(main) as ClosureMirror).function.location.sourceUri;
void main() { ... }
Not particularly pretty, but it's easy to just put into a util file.
I'm using pub serve to run my page. I noticed that there are tools like lite-server for npm, that make sure that after file contents are changed, the web page is automatically refreshed.
Anyone got something like that working for dart using pub serve? I thought maybe some grinder script with the watcher package could make that work?
Pub should handle monitoring and autodeployment of changes itself when run in debug or release mode.
You can verify this works correctly by issuing from your project root
pub serve
then make a change to a html or dart file and verify that the project is automatically rebuilt.
If this is not working, you might try experimenting with the --force-poll option when running pub serve. Per documentation:
--[no-]force-poll Force the use of a polling filesystem watcher.