Tuesday, December 17, 2013

Pub Serve Polymer Elements

OK. I know I'm doing something incredibly stupid, but... I still cannot get pub serve, the simple web server built into Dart's Pub package manager, to serve up Polymer Element resources.

I do not believe that I am doing any crazy though. I have a very simple, web-only application. The pubspec.yaml contains only the latest versions of Polymer and Polymer Elements:
name: flex
  polymer: any
  polymer_elements: any
  unittest: any
I see no errors upon starting pub serve (though it does take a good 10 seconds to spin up). If I curl for similar resources, then everything works OK:
➜  dart git:(master) ✗ curl http://localhost:8080/packages/polymer_expressions/expression.dart
// Copyright (c) 2013, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
But if I curl for Polymer Elements resources, I get nothing back:
➜  dart git:(master) ✗ curl http://localhost:8080/packages/polymer_elements/polymer_elements.dart
Nothing. The connection just hangs.

Pub's server can be run in verbose mode with pub serve -v (there are verbosity levels, but I just ask for everything here). With that, I find that the regular, working requests look like:
FINE: BarbackServer GET /packages/polymer_expressions/expression.dart
    | Loading polymer_expressions|lib/expression.dart
MSG : GET /packages/polymer_expressions/expression.dart → polymer_expressions|lib/expression.dart
FINE: BarbackServer GET /packages/polymer_expressions/expression.dart
    | Served polymer_expressions|lib/expression.dart
Whereas the failing request look like:
FINE: BarbackServer GET /packages/polymer_elements/polymer_elements.dart
    | Loading polymer_elements|lib/polymer_elements.dart
So the request is coming into the Pub server. It seems to parse it properly—the requests is for the lib/polymer_elements.dart file in the polymer_elements package. But then... nothing.

So I try using the GitHub repository for Polymer Elements instead. Perhaps it hold some insights into my woes:
name: flex
  polymer: any
    git: https://github.com/ErikGrimes/polymer_elements
  unittest: any
But trying a pub upgrade with that yields:
➜  dart git:(master) ✗ pub upgrade
Resolving dependencies.........................
Incompatible dependencies on 'polymer_elements':
- 'flex' depends on it from source git
- 'polymer_ui_elements' depends on it from source hosted
Why on earth is Polymer Elements trying to pull in Polymer UI elements anyway.

I do the usual GitHub upstream dance and use my repository instead:
name: flex
  polymer: any
    git: https://github.com/eee-c/polymer_elements
  unittest: any
And before I realize my mistake, I pub upgrade and pub serve and find that I can now curl for Polymer Element resources:
➜  dart git:(master) ✗ curl http://localhost:8080/packages/polymer_elements/polymer_elements.dart
// needed as entry point for dartdoc generation
 * Polymer Elements
library polymer_elements;
// ...
My mistake is pointing my pubspec.yaml dependencies at my fork of Polymer Elements as it exists on GitHub, not as it exists locally. But at least I know a change in the code since then has affected things. A git diff --stat <SHA1> where <SHA1> points to a0717db12 (my last commit) reveals that there have been a lot of code changes in the meantime. I tend to doubt code changes are my problem here. Instead, I think the problem is in the changes to the project's pubspec.yaml:
➜  polymer_elements git:(master) gd a0717db12 pubspec.yaml
diff --git a/pubspec.yaml b/pubspec.yaml
index 398337d..7df61fd 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
 name: polymer_elements
-version: 0.0.13
+version: 0.1.2
 - Erik Grimes 
 - Günter Zöchbauer 
@@ -11,8 +11,11 @@ documentation: http://erikgrimes.github.io/polymer_elements/docs/index.html
   sdk: ">=1.0.0 <2.0.0"
-  polymer: ">=0.9.0 <0.10.0"
-#  polymer_ui_elements: 0.0.13
+  polymer: ">=0.9.2+3 <0.10.0"
+  polymer_ui_elements: ">=0.1.1 <0.2.0"
+#  polymer_ui_elements: 
+#    path: ../polymer_ui_elements  
   unittest: ">=0.9.0 <0.10.0"
And indeed, if I remove the Polymer UI Elements dependencies from my local fork of Polymer Elements, then I can pub upgrade without error and, more importantly, successfully curl after starting pub serve. In fact, I can now serve my flex-layout web page:
<!DOCTYPE html>
<html lang="en">
    <title>Flex Test</title>
    <!-- Load component(s) -->
    <link rel="import" href="/packages/polymer_elements/polymer_flex_layout/polymer_flex_layout.html">
    <script type="application/dart">export 'package:polymer/init.dart';</script>
    <script src="packages/browser/dart.js"></script>
     <polymer-flex-layout isContainer>
       <div align=center flex>Main</div>
And see the results in Dartium:

I suspect that my problems have to do with circular dependencies in Polymer Elements and Polymer UI Elements. I am still not certain that either needs to depend on the other. Regardless, pub serve probably ought to deal with this situation better. PRs and bug reports to come…

Day #968


  1. Hi Chris, this is interesting. I also just saw your PR. Thanks for your effort. We are going to remove the dependency as you suggested at least temporary. We were not happy with this circular dependency from the beginning but some examples in polymer_elements depend on polymer_ui_elements.

    1. Ah, thanks for the explanation. I was wondering if that was intentional or leftover from the initial split of the two. The circular dependency does not seem like an ideal solution, but also seems understandable in this case. It'll be interesting to see how the two bugs in Pub are resolved.

      FWIW when I did a bower install of polymer-elements the other day, the polymer-ui-elements were not installed. So even if removing the polymer_ui_elements dependency is not an ideal solution, it may be the most consistent solution :-\