Open mindplay-dk opened 4 years ago
Okay, a bit of progress - I was able to reference this library for a way to get the correct URL.
It looks like this:
import 'dart:developer' as dev;
import 'package:path/path.dart' as path;
main(List<String> args) async {
if (HotReloader.isHotReloadable) {
var info = await dev.Service.getInfo();
var uri = info.serverUri;
uri = uri.replace(path: path.join(uri.path, 'ws'));
if (uri.scheme == 'https') {
uri = uri.replace(scheme: 'wss');
} else {
uri = uri.replace(scheme: 'ws');
}
print('Hot reloading enabled');
final reloader = HotReloader(vmServiceUrl: uri.toString());
await reloader.addPackagePath(Uri(scheme: 'package', path: 'server_test/'));
await reloader.go();
}
// ...
}
Something similar should probably replace the constant URL in the library?
(Let me know if you'd like a PR for that change.)
This gets me connected and running - I now see messages like these:
Paths C:\workspace\dart\server-test\lib\src\images.dart changed!
Reloading the application...
So far, so good.
It doesn't work though - changes made to that script don't have any effect.
I'm using shelf
, and the thing I was hoping to reload is a Middleware
inserted into a Pipeline
at start-up. So this is effectively a function instance that persists in an object property somewhere.
I don't know the limitations or the precise workings of hot reloading, but would I need to do something else to get this working? (e.g. tear down and rebuild my HttpServer
instance with new instances of the middleware and router etc.?)
I've also tried the basic example that ships with the package, and this doesn't work either.
For example, changing the count
value, or changing the increment expression to count += 2
doesn't have any effect other than seeing the "paths changed" and "reloading" messages on the console.
For the record, someone on the Dart Gitter channel gave it a shot on Linux - he also had to copy the code I posted (to get the websocket path) to get it working, so that bit needs a PR either way.
He then confirmed that the example did work for him.
So the issue is likely Windows-specific.
We also found that the vm_service_lib package has been superseded by the vm_service package and should be upgraded - I tried switching that dependency, which was easy enough, just needs a simple update of the import
paths, but it didn't address this issue.
Please submit a PR with the fix.
I will open a separate issue to track the Windows problem.
Thank you so much @mindplay-dk, your snippet worked for me on a macOS setup where I was having this issue as well.
I've got a straight forward setup like this:
When I launch my
server.dart
script, I get this output:But the moment I change one of the files, it crashes with this stack trace:
I'm on 64-bit Windows 10 with the following Dart version:
I noticed a mismatch in socket URLs between the startup message:
And the error message:
The websocket URL displayed at startup contains a random key that changes with every launch, so a constant is probably not enough to make this work.
Likely related to #5, though the error message in that issue is different. (?)
Let me know if there's anything I can do to help resolve the issue.