2

I have run into an ambiguous reference error when using a component from a package, where a primary project also refers to a shared dependency of that component. I have outlined an example project and a dependency project which illustrates the issue.

My aim is to have a base library of UI components, and a separate project that may subclass those components. I am currently unable to subclass those components, because as soon as I do, there ends up being two compiled versions of it - leading to the ambiguous reference error.

Dart SDK 0.6.5.0 r25017


My Package

This is the package that I created and imported into my current project. It defines a single Web Component and has a dependency on Web UI.

MyPackageComponent.html

<!DOCTYPE html>
<html><body>
    <element name="my-package-component" constructor="MyPackageComponent" extends="span">
        <template>My Package Component</template>
        <script type="application/dart" src="MyPackageComponent.dart"></script>
    </element>
</body></html>

MyPackageComponent.dart

import "package:web_ui/web_ui.dart";
class MyPackageComponent extends WebComponent
{
    MyPackageComponent();
}

My Project

This is the primary project. It defines a single derived Web Component and has dependencies on My Package and Web UI.

MyLocalComponent.html

<!DOCTYPE html>
<html><body>
    <element name="my-local-component" constructor="MyLocalComponent" extends="span">
        <template>My Local Component</template>
        <script type="application/dart" src="MyLocalComponent.dart"></script>
    </element>
</body></html>

MyLocalComponent.dart

Note that MyLocalComponent is subclassed from MyPackageComponent.

import "package:MyPackage/MyPackageComponent.dart";
class MyLocalComponent extends MyPackageComponent
{
    MyLocalComponent();
}

Index.html

<!DOCTYPE html>
<html><head>
    <link rel="import" href="package:MyPackage/MyPackageComponent.html"/>
    <link rel="import" href="MyLocalComponent.html"/>
</head><body>
    <my-local-component></my-local-component>     <!-- REF 1 -->
    <my-package-component></my-package-component> <!-- REF 2 -->
    <!-- Dart scripts etc. -->
</body>

Result

Everything runs fine if I use only the local component in Index.html (see REF 1). When I use the packaged component (see REF 2), I run into the following error:

Internal error: 'file:[omitted]/MyProject/src/out/Index.html.dart': Error: line [omitted] pos [omitted]: ambiguous reference: 'MyPackageComponent' is defined in library 'file:[omitted]/MyProject/src/out/_from_packages/MyPackage/MyPackageComponent.html.dart' and also in 'package:MyPackage/MyPackageComponent.dart'
  __t.component(new MyPackageComponent()..host = __e0);
                    ^ [omitted]/MyProject/src/out/index.html_bootstrap.dart:0

I am not sure if I am doing something wrong or if this is a bug. It seems highly unlikely that I can not use resources from other packages.

Investigation

There is an auto-generated folder in the out directory named _from_packages that seems to be the source of the ambiguous reference. MyPackageComponent is defined in there as well as in the packages directory.

Seems like the Web UI compiler is generating a class for the imported MyPackageComponent.dart (used specifically by the imported component) in addition to the original in packages/MyPackage/MyPackageComponent.dart which is referred to elsewhere in My Project.

4

0 回答 0