Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/assets/counter_idl_mo/counter_idl.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import A "mo:stdlib/array.mo";
import A "mo:stdlib/array";
import Prim "mo:prim";

type List<T> = ?{head : T; tail : List<T>};
Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/import_mo/main.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Friend "./friend.mo"
import Friend "./friend"

actor Greet {

Expand Down
4 changes: 2 additions & 2 deletions e2e/assets/matrix_multiply_mo/matrix.mo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import A "mo:stdlib/array.mo";
import M "secret_import.mo";
import A "mo:stdlib/array";
import M "secret_import";

type Matrix = [[Int]];

Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/matrix_multiply_mo/transpose.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import A "mo:stdlib/array.mo";
import A "mo:stdlib/array";

type Matrix = [[Int]];

Expand Down
2 changes: 1 addition & 1 deletion e2e/assets/print_mo/print.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Debug "mo:stdlib/debug.mo";
import Debug "mo:stdlib/debug";

actor HelloActor {
public func hello() : async () {
Expand Down
1 change: 0 additions & 1 deletion e2e/assets/stdlib_usage_mo/patch.bash

This file was deleted.

4 changes: 0 additions & 4 deletions e2e/assets/stdlib_usage_mo/stdlib_usage.mo

This file was deleted.

15 changes: 0 additions & 15 deletions e2e/stdlib_usage.bash

This file was deleted.

2 changes: 1 addition & 1 deletion nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"motoko": {
"ref": "master",
"repo": "ssh://git@github.com/dfinity-lab/motoko",
"rev": "4d6afaa511cd9ae62ad2bb6a3c9a0f79c818ab8d",
"rev": "7d223e247be606cb92aced20fa004bf836350fe9",
"type": "git"
},
"napalm": {
Expand Down
93 changes: 63 additions & 30 deletions src/dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ fn motoko_compile(cache: &dyn Cache, params: &MotokoParams<'_>, assets: &AssetMa
#[derive(Debug, PartialEq, Hash, Eq)]
enum MotokoImport {
Canister(String),
Local(PathBuf),
Ic(String),
Lib(String),
Relative(PathBuf),
}

struct MotokoImports(HashSet<MotokoImport>);
Expand All @@ -165,8 +167,60 @@ impl MotokoImports {
}
}

fn parse_moc_deps(line: &str) -> DfxResult<MotokoImport> {
let (url, fullpath) = match line.find(' ') {
Some(index) => {
if index >= line.len() - 1 {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Unknown import {}", line),
)));
}
let (url, fullpath) = line.split_at(index + 1);
(url.trim_end(), Some(fullpath))
}
None => (line, None),
};
let import = match url.find(':') {
Some(index) => {
if index >= line.len() - 1 {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Unknown import {}", url),
)));
}
let (prefix, name) = url.split_at(index + 1);
match prefix {
"canister:" => MotokoImport::Canister(name.to_owned()),
"ic:" => MotokoImport::Ic(name.to_owned()),
"mo:" => MotokoImport::Lib(name.to_owned()),
_ => {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Unknown import {}", url),
)))
}
}
}
None => match fullpath {
Some(fullpath) => {
let path = PathBuf::from(fullpath);
if !path.is_file() {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Cannot find import file {}", path.display()),
)));
};
MotokoImport::Relative(path)
}
None => {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Cannot resolve relative import {}", url),
)))
}
},
};
Ok(import)
}

fn find_deps(cache: &dyn Cache, input_path: &Path, deps: &mut MotokoImports) -> DfxResult {
let import = MotokoImport::Local(input_path.to_path_buf());
let import = MotokoImport::Relative(input_path.to_path_buf());
if deps.0.contains(&import) {
return Ok(());
}
Expand All @@ -178,35 +232,14 @@ fn find_deps(cache: &dyn Cache, input_path: &Path, deps: &mut MotokoImports) ->

let output = String::from_utf8_lossy(&output.stdout);
for dep in output.lines() {
let prefix: Vec<_> = dep.split(':').collect();
match prefix[0] {
"canister" => {
if prefix.len() != 2 {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Illegal canister import {}", dep),
)));
}
deps.0.insert(MotokoImport::Canister(prefix[1].to_string()));
}
// TODO trace canister id once dfx supports downloading IDL from remote canisters
"ic" => (),
// TODO trace mo package once dfx supports packages
"mo" => (),
file => {
let path = input_path
.parent()
.expect("Cannot use root.")
.join(file)
.canonicalize()
.expect("Cannot canonicalize local import file");
if path.is_file() {
find_deps(cache, &path, deps)?;
} else {
return Err(DfxError::BuildError(BuildErrorKind::DependencyError(
format!("Cannot find import file {}", path.display()),
)));
}
let import = parse_moc_deps(dep)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

match import {
MotokoImport::Canister(_) => {
deps.0.insert(import);
}
MotokoImport::Relative(path) => find_deps(cache, &path, deps)?,
MotokoImport::Lib(_) => (),
MotokoImport::Ic(_) => (),
}
}
Ok(())
Expand Down