0

I have a simple dart project, it has file structure like:

darttest  tree
.
├── packages
│   ├── browser -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/browser-0.5.20/lib
│   ├── meta -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/meta-0.5.20/lib
│   ├── start -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/start-0.0.8/lib
│   └── unittest -> /Users/freewind/.pub-cache/hosted/pub.dartlang.org/unittest-0.5.20/lib
├── pubspec.lock
├── pubspec.yaml
└── src
    └── server.dart

It uses the library start, and is very simple.

The pubspec.yaml:

name: darttest
dependencies:
  start: any

The src/server.dart:

import 'package:start/start.dart';

void main() {
  start(public: 'web', port: 3000).then((Server app) {
    app.get('/').listen((request) {
      request.response
        .header('Content-Type', 'text/html; charset=UTF-8')
        .send('Hello, dart');
    });
  });
}

When I on darttest dir and run:

➜  darttest  dart src/server.dart
Unable to open file:
/Users/freewind/dev/workspace/darttest/src/packages/start/start.dart
'file:///Users/freewind/dev/workspace/darttest/src/server.dart': Error: line 1 pos 1: 
library handler failed
import 'package:start/start.dart';
^

It tries to find the start from darttest/src/packages/start/start.dart, but there is no such file, actually, the start in inside darttest/packages.

Do I have to put the server.dart inside the root of project? Is there any rule for this? Or how to fix this error?

Charles
  • 50,943
  • 13
  • 104
  • 142
Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Do you use the Dart Editor? You a missing the directory links to the /package/ folder. You can create them by running "Pub Install" on the pubspec.yaml file (context menu). – Fox32 Jul 08 '13 at 13:50

1 Answers1

2

EDIT
pub install was renamed to pub get (pub install is still available as an undocumented alias for pub get)
EDIT

pub install solves this issue by creating additional packages directories that link to the main packages directory at the root of your package. It assumes your package is laid out according to the package layout guide, and creates a linked packages directory in bin/, test/, and example/, as well as their subdirectories

Dart Editor

If you are using the Dart Editor it can automaticly create a directory link to the main package folder.

You need to choose "Pub Install" from the context menu on the pubspec.yaml file in the Files view.

Command Line

If you want to create the directory from the command line you can use the pub install command. You need to run it in the directory of your pubspec.yaml file.

PS: Take a look at the Package layout conventions for Dart packages. They recommend to use the bin folder for standalone Dart applications.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Fox32
  • 13,126
  • 9
  • 50
  • 71
  • Thank you, I'm not using Dart Editor, instead, I use command line with Dart SDK. Do I have to create the link inside `src` manually? Can I run some pub command to do this? – Freewind Jul 08 '13 at 14:00
  • The command line equivalent is `pub install`, see http://pub.dartlang.org/doc/#installing-dependencies and http://pub.dartlang.org/doc/pub-install.html – Fox32 Jul 08 '13 at 14:04
  • I'm not sure if it's a good idea to put the `server.dart` to `bin`, because it has lots of logic. And it's not a library, it's not proper to put it inside `lib`. I don't know where to put it now :( – Freewind Jul 08 '13 at 14:10
  • If it contains a main and is not a tool, an unit test, an example or a web application it should be in bin. – Fox32 Jul 08 '13 at 14:13
  • It's the server-side of a restful api provider, where should I put it? – Freewind Jul 08 '13 at 14:16
  • For me it has a main and should be in bin. But that is only a convention, you are free organise it like you need it. – Fox32 Jul 08 '13 at 14:20
  • @Freewind: Move logic that not related to `parsing cmd-line arguments` to separate file. Best place for this are directories `'lib'` (for `stubs`) and `'lib/src'` (for `implementation`). Organize this as project but not as `all-in-one-file`. This is the preffered way to programming in `Dart`. – mezoni Jul 08 '13 at 14:34