I want to create a Builder that would create Env class on the fly. That class should be available for import anywhere in my project. It is as container for environment constants defined in build.yml
Builder itself works fine, it generates desired output, however when I run
pub run build_runner build
build fails with following message
[SEVERE] build_web_compilers:entrypoint on web/main.dart: Unable to
find modules for some sources, this is usually the result of either a
bad import, a missing dependency in a package (or possibly a
dev_dependency needs to move to a real dependency), or a build failure
(if importing a generated file).
Please check the following imports:
import
'../.dart_tool/build/generated/buildertest/lib/environment.g.dart';
from buildertest|web/main.dart at 2:1
[INFO] Running build completed, took 4.1s [INFO] Caching finalized
dependency graph completed, took 450ms [SEVERE] Failed after 4.6s
import by itself is ok, as it works fine when I run following snippet
import '../.dart_tool/build/generated/buildertest/lib/environment.g.dart';
void main() {
print("Name:${Environment.name}");
print("Endpoint:${Environment.endpoint}");
}
with
dart web/main.dart
What should be done to make this import valid??
Here is build.yaml
targets:
$default:
builders:
angular_components|scss_builder:
enabled: True
env|builder:
options:
name: "default"
endpoint: "http://example.org"
enabled: true
builders:
env|builder:
import: "env/EnvBuilder.dart"
builder_factories: ["builderFactory"]
is_optional: false
build_extensions:
"$lib$": ["environment.g.dart"]
auto_apply: root_package
runs_before: ["build_test:test_bootstrap","build_modules:module_library","build_modules:dartdevc","build_web_compilers:ddc","build_modules:vm","build_modules:dart2js","build_web_compilers:entrypoint"]
Related
I am trying to replace python with dart as a scripting language for my tools. I can create a python file anywhere, import global packages and run it but I can't seem to get this working with dart. Do I always need a pubspec.yaml file to run a dart script?
This is the script I am trying to run:
import 'package:http/http.dart' as http;
main(List<String> args) async{
var res = await http.get('https://example.com');
print(res.headers);
}
This is the error I am getting:
Error: Could not resolve the package 'http' in 'package:http/http.dart'.
test2.dart:1:8: Error: Not found: 'package:http/http.dart'
import 'package:http/http.dart' as http;
^
test2.dart:4:19: Error: Method not found: 'get'.
var res = await http.get('https://example.com');
No, you don't need a pubspec.yaml file to run a program, but it does need to somehow be able to resolve all the imports.
The pubspec.yaml file is used for obtaining the packages (from pub.dev, a git repository, etc.) but not for finding the packages at runtime. The pub program reads the pubspec.yaml file and downloads the packages mentioned in it, and maintains a packages specification file indicating where each package resolves to. By default the packages specification is in a file called .packages in the same directory as the pubspec.yaml file. The Dart runtime normally finds the packages by looking at the .packages package specification file, but there are other ways.
Here are some options:
Put a .packages file in the same directory as the Dart program, or in an ancestor directory.
Use the --packages option to specify the package specification file to use:
dart --packages=/home/username/stuff/mypackagespecfile myprogram.dart
The Dart runtime also has a --package-root option. But I haven't figured out how to make it work.
The import statements use URIs, so import 'file://...'; can also work in some cases.
Use dart2native to compile the program into an executable.
Note: Dart scripts can also start with a hash-bang line:
#!/usr/bin/env dart
import 'package:http/http.dart' as http;
main(List<String> args) async{
var res = await http.get('https://example.com');
print(res.headers);
}
Then you can make the program executable and run it without needing to type in the Dart runtime:
$ chmod a+x myprogram.dart
$ ./myprogram.dart
I try to run in VSCode a simple dart program with
import 'dart:html';
clause.
import 'dart:html';
// import 'package:html/dom.dart';
// import 'package:html/dom_parsing.dart';
// import 'package:html/parser.dart';
void main() async{
var myTable = new TableElement()
..setAttribute('border','1');
// ..setAttribute(name, value);
...
In Run mode (I use VSCode extension "Code Runner 0.9.9") and in Debug appeared the same error:
Error: Not found: 'dart:html'
import 'dart:html';
I have installed Dart SDK 2.3.1 at Windows10 and not installed Flutter at all.
PATH pointed to Dart SDK bin directory
PATH =D:\Dart\dart-sdk\bin;
*) At project directory I try to add additional directive at pubspec.yaml
dependencies:
----
name: main
description: Test App sample22
dependencies:
html:
---
After "pub get" command I'll see that html present but error still persist.
pub get
Resolving dependencies...
+ charcode 1.1.2
+ csslib 0.16.0
+ html 0.14.0+2
+ path 1.6.2
+ source_span 1.5.5
+ term_glyph 1.1.0
Changed 6 dependencies!
*) My next step was to import html parts via "package:html/" (marked as comments in code sample). It is not helped and required class TableElement still unrecognizable.
"main.dart:8:19: Error: Method not found: 'TableElement'."
*) I try to change "launch.json" string from
default
"program": "bin/main.dart",
to
"program": "D:/Dart/WRK03t/main.dart",
And rename my code file to "main.dart"
*) Also I try to remove Dart extension from VSCode, restart PC and install it again. it's not helped.
But let me say that when I compile main.dart to js
"dart2js -m -o tst.js main.dart"
Resulted tst.js run correctly within the html page.
Almost the same problem in Request Dart Installation doesnt find dart:html
dart:html is only available in the browser. This is the error you get if you try to run code that uses it on the VM (instead of the browser). This is expected.
If you need to run your code outside of the browser (eg. in the VM as a CLI app or via Fluter) you cannot use dart:html. If you only want to use it in the browser but VS Code is trying to run your code in the VM, you'll need to set up some VS Code tasks/launch configs to run build_runner, similar to the Dart DevTools project:
https://github.com/flutter/devtools/tree/abe811f66e1bd36612b76bbe28250bc669a6ce08/.vscode
Following the sample on the Dartson documentation page,
import 'package:dartson/dartson.dart';
import 'package:some_dependency/some_class.dart';
import 'my_class.dart';
#Serializer(
entities: [
MyClass,
SomeClass,
],
)
final Dartson<Map<String, dynamic>> serializer = _serializer$dartson;
I'm getting undefined name _serializer$dartson.useCodec(json) and also wondering where _serializer is referenced from.
Yes, I have added the required dependencies to my pubspec.yaml.
dependencies:
dartson: ^1.0.0-alpha+2
dev_dependencies:
build_runner: ^0.10.0
The required dependency on build_runner indicates that a build step is needed. Depending on how Dartson define's its builders it may be enough to run pub run build_runner build (or for flutter, flutter packages pub run build_runner build.
You might want to file an issue on Dartson to make the build instructions more clear.
I'm a newbie. I create an angular application using the IDEA template and insert the code from the example:
<p *ngIf="true">
Expression is true and ngIf is true.
This paragraph is in the DOM.
</p>
An error occurs:
[INFO]
------------------------------------------------------------------------ [INFO] Starting Build [INFO] Updating asset graph completed, took 16ms
[SEVERE] angular on lib/app_component.dart: Template parse errors:
line 6, column 4 of AppComponent: ParseErrorLevel.FATAL: Can't bind to
'ngIf' since it isn't an input of any bound directive. Please check
that the spelling is correct, and that the intended directive is
included in the host component's list of directives.
^^^^^^^^^^^^ [INFO] Running build completed, took 650ms [INFO] Caching
finalized dependency graph completed, took 853ms [SEVERE]
build_web_compilers|entrypoint on web/main.dart (cached): Unable to
find modules for some sources, this is usually the result of either a
bad import, a missing dependency in a package (or possibly a
dev_dependency needs to move to a real dependency), or a build failure
(if importing a generated file).
Please check the following imports:
import 'package:untitled1/app_component.template.dart' as ng; from
untitled1|web/main.dart at 2:1
[SEVERE] build_web_compilers|entrypoint on test/app_test.dart
(cached): Unable to find modules for some sources, this is usually the
result of either a bad import, a missing dependency in a package (or
possibly a dev_dependency needs to move to a real dependency), or a
build failure (if importing a generated file).
Please check the following imports:
import 'package:untitled1/app_component.template.dart' as ng; from
untitled1|test/app_test.dart at 5:1
[SEVERE] build_web_compilers|entrypoint on
test/app_test.dart.browser_test.dart (cached): Unable to find modules
for some sources, this is usually the result of either a bad import, a
missing dependency in a package (or possibly a dev_dependency needs to
move to a real dependency), or a build failure (if importing a
generated file).
Please check the following imports:
import 'package:untitled1/app_component.template.dart' as ng; from
untitled1|test/app_test.dart at 5:1
[SEVERE] Failed after 1.6s
How to fix?
You need to list directives you use in the template in the #Component() annotation
import 'package:angular/angular.dart'
...
#Component(
selector: 'foo-bar',
templateUrl: 'foo_bar.html',
directives: [coreDirectives /* or NgIf */],
)
I am trying to build my WebGL dart application with "pub build".
I am able to run "pub serve" without problems, but "pub build" gives me 2 erros:
Directive not allowed here
part of MyProject
^^^^^^^^^^^^^^^^
Could not find main [I think this happends because of unfinished compile]
I only use browser:any as dependency.
Why could this be?
Thank you very much!
EDIT:
My pubspec.yaml
name: LD29
description: My LD29 game
author: snip
homepage: snip
dependencies:
browser: any
The includes:
library LD29;
import 'dart:html';
import 'dart:async';
import 'dart:math';
import 'dart:typed_data';
import 'dart:web_gl';
part 'Scene.dart';
part 'MainScene.dart';
part 'Entities.dart';
part 'Quadgrid.dart';
part 'Vecd.dart';
part 'Quad.dart';
part 'TexQuad.dart';
part 'ShaderProgram.dart';
part 'ShaderSource.dart';
part 'TextureGL.dart';
part 'FontRendering.dart';
The problematic "part of"
part of LD29;
class Quadgrid {
const static int size = 20;
const MAX_GRID_W = DISPLAY_WIDTH / size;
const MAX_GRID_H = DISPLAY_HEIGHT / size;
...
}
It seems no diffrent from my other files.
OK, after coming back to it a few months later, I gave debugging this problem another go. For my application, we moved the main() method to a different file than the one that had the 'library coUclient' command at the top. This meant that 'pub build' would fail:
master#dellaptop ~/Dropbox/Dart/coUclient $ pub build
Loading source assets...
Building coUclient...
[Info from Dart2JS]:
Compiling coUclient|web/dart/engine/initialize.dart...
[Error from Dart2JS]:
web/dart/engine/initialize.dart:1:1:
Directive not allowed here.
part of coUclient;
^^^^^^^^^^^^^^^^^^
[Warning from Dart2JS]:
web/dart/engine/initialize.dart:11:5:
Cannot resolve 'localStorage'.
if(localStorage["interface"] == null || localStorage["interface"] == "desktop")
^^^^^^^^^^^^
I don't know why I didn't see it before, but what tipped me off was that I saw it was starting the compile with initalize.dart and it occurred to me that that was the file that had main() in it. I then moved the main() method back into main.dart (which has the 'library coUclient' command at the top) and re-ran pub build. This time it succeeded because it started:
Loading source assets...
Building coUclient...
[Info from Dart2JS]:
Compiling coUclient|web/main.dart...
[Dart2JS on coUclient|web/main.dart]:
and all went well.
Moral of the story is, make sure your main() is in the same file as your 'library ' command and the "Directive not allowed here" problem shouldn't happen.